3 /// Class to deal with pre-saved data files
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
35 unsigned char *m_memBlock
; //< pointer to full memory block
36 //< can be null if external
37 size_t m_blockSize
; //< size of m_memBlock buffer allocated
39 unsigned char *m_dataStart
; //< pointer to start of internal data
40 //< can be null if external, and can
41 //< start somewhere in the middle of
42 //< m_memBlock if internal with a
45 size_t m_dataSize
; //< number of bytes of actual data
47 // copy on write feature
48 const unsigned char *m_externalData
;
54 // output format flags
55 static bool bPrintAscii
;
58 void MakeSpace(size_t desiredsize
, size_t desiredprepend
= 0);
59 size_t AvailablePrependSpace() const;
63 explicit Data(int endpoint
, size_t startsize
= 0x4000, size_t prependsize
= 0x100);
64 Data(const void *ValidData
, size_t size
);
65 Data(const Data
&other
);
68 void InputHexLine(std::istream
&is
);
69 void DumpHexLine(std::ostream
&os
, size_t index
, size_t size
) const;
70 void DumpHex(std::ostream
&os
) const;
72 int GetEndpoint() const { return m_endpoint
; }
74 const unsigned char * GetData() const { return m_external
? m_externalData
: m_dataStart
; }
75 size_t GetSize() const { return m_dataSize
; }
77 unsigned char * GetBuffer(size_t requiredsize
= 0);
78 /// Returns size of buffer returned by GetBuffer()
79 size_t GetBufSize() const;
80 void ReleaseBuffer(int datasize
= -1);
82 void AppendHexString(const char *str
);
84 /// set buffer to 0 size, but don't bother overwriting memory with 0
85 void QuickZap() { m_dataSize
= 0; }
86 void Zap(); // does a memset too
88 Data
& operator=(const Data
&other
);
94 // Writing data... basically does a memcpy(dst,src,sizeof(src))
95 // for each type. Does no endian conversions.
96 // dst is calculated as buffer + offset.
97 // The buffer is expanded automatically if needed.
98 // The offset is advanced by the size of the data.
99 // GetSize() will increase automatically if the result of
100 // the MemCpy() goes beyond the existing data size.
102 void MemCpy(size_t &offset
, const void *src
, size_t size
);
103 void Append(const void *buf
, size_t size
);
104 void Prepend(const void *buf
, size_t size
);
105 void Prechop(size_t size
);
106 template <class ValueT
>
107 void SetValue(size_t &offset
, ValueT value
)
109 this->MemCpy(offset
, &value
, sizeof(value
));
114 static void PrintAscii(bool setting
) { bPrintAscii
= setting
; }
115 static bool PrintAscii() { return bPrintAscii
; }
118 BXEXPORT
std::istream
& operator>> (std::istream
&is
, Data
&data
);
119 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const Data
&data
);
124 const Data
&m_old
, &m_new
;
126 BXLOCAL
void Compare(std::ostream
&os
, size_t index
, size_t size
) const;
129 Diff(const Data
&old
, const Data
&new_
);
131 void Dump(std::ostream
&os
) const;
134 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const Diff
&diff
);
138 BXEXPORT
bool LoadDataArray(const std::string
&filename
, std::vector
<Data
> &array
);
139 BXEXPORT
bool ReadDataArray(std::istream
&is
, std::vector
<Data
> &array
);
145 /// Database record data class. The purpose of this class is to contain
146 /// the raw data that flows between low level activity such as device
147 /// read/writes, backup read/writes, and record parsing.
149 /// This class contains the low level record data block, unparsed, as well
150 /// as the surrounding meta data, such as the database name it belongs
151 /// to, the Unique ID, the Rec Type, and format version/type based on what
152 /// commands were used to extract the data from the device. (When using
153 /// newer commands, the format of the records, potentially including the
154 /// individual field type codes, are different.)
156 /// Possible bi-directional data flow in all of Barry:
157 /// Note that this class, DBData, represents the data+meta stage.
159 /// data+meta <-> device
160 /// data+meta <-> backup file
161 /// data+meta <-> record object
162 /// record object <-> boost serialization
163 /// contact record object <-> ldif
165 /// Possible uni-directional data flow in all of Barry:
167 /// record object -> text dump
169 class BXEXPORT DBData
172 enum RecordFormatVersion
180 RecordFormatVersion m_version
;
181 std::string m_dbName
;
186 // the raw data block, internal
189 // the data block to use... could be external or internal,
190 // and does not change for the life of the object
194 /// Default constructor, constructs an empty local Data object
197 /// Copy constructor - always creates an internal Data object, and
198 /// uses Data object's copy constructor to make it.
199 /// Copies all meta data as well.
200 /// If you want to optimize the copy, use one of the constructors
202 DBData(const DBData
&other
);
204 /// Constructs a local Data object that points to external memory
205 DBData(const void *ValidData
, size_t size
);
206 DBData(RecordFormatVersion ver
, const std::string
&dbName
,
207 uint8_t recType
, uint32_t uniqueId
, size_t offset
,
208 const void *ValidData
, size_t size
);
210 /// If copy == false, constructs an external Data object, no local.
211 /// If copy == true, constructs an internal Data object copy
212 /// For speed, set copy to false.
213 /// If you want Copy On Write behaviour, similar to Data(buf,size),
214 /// then use the above (buf, size) constructor, not this one,
215 /// since this constructor uses Data's copy constructor.
216 DBData(Data
&externalData
, bool copy
);
217 DBData(RecordFormatVersion ver
, const std::string
&dbName
,
218 uint8_t recType
, uint32_t uniqueId
, size_t offset
,
219 Data
&externalData
, bool copy
);
224 RecordFormatVersion
GetVersion() const { return m_version
; }
225 const std::string
& GetDBName() const { return m_dbName
; }
226 uint8_t GetRecType() const { return m_recType
; }
227 uint32_t GetUniqueId() const { return m_uniqueId
; }
228 size_t GetOffset() const { return m_offset
; }
230 const Data
& GetData() const { return m_data
; }
234 void SetVersion(RecordFormatVersion ver
)
239 void SetDBName(const std::string
&dbName
)
244 void SetIds(uint8_t recType
, uint32_t uniqueId
)
247 m_uniqueId
= uniqueId
;
250 void SetOffset(size_t offset
)
255 void CopyMeta(const DBData
&src
)
257 m_version
= src
.m_version
;
258 m_dbName
= src
.m_dbName
;
259 m_recType
= src
.m_recType
;
260 m_uniqueId
= src
.m_uniqueId
;
261 m_offset
= src
.m_offset
;
264 DBData
& operator=(const DBData
&other
);