3 /// Class to deal with pre-saved data files
7 Copyright (C) 2005-2010, 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_data
;
36 size_t m_bufsize
; //< size of m_data buffer allocated
37 size_t m_datasize
; //< number of bytes of actual data
40 // copy on write feature
41 const unsigned char *m_externalData
;
44 // output format flags
45 static bool bPrintAscii
;
48 void MakeSpace(size_t desiredsize
);
49 void CopyOnWrite(size_t desiredsize
);
53 explicit Data(int endpoint
, size_t startsize
= 0x4000);
54 Data(const void *ValidData
, size_t size
);
55 Data(const Data
&other
);
58 void InputHexLine(std::istream
&is
);
59 void DumpHexLine(std::ostream
&os
, size_t index
, size_t size
) const;
60 void DumpHex(std::ostream
&os
) const;
62 int GetEndpoint() const { return m_endpoint
; }
64 const unsigned char * GetData() const { return m_external
? m_externalData
: m_data
; }
65 size_t GetSize() const { return m_datasize
; }
67 unsigned char * GetBuffer(size_t requiredsize
= 0);
68 size_t GetBufSize() const { return m_bufsize
; }
69 void ReleaseBuffer(int datasize
= -1);
71 void AppendHexString(const char *str
);
73 /// set buffer to 0 size, but don't bother overwriting memory with 0
74 void QuickZap() { m_datasize
= 0; }
75 void Zap(); // does a memset too
77 Data
& operator=(const Data
&other
);
83 // Writing data... basically does a memcpy(dst,src,sizeof(src))
84 // for each type. Does no endian conversions.
85 // dst is calculated as buffer + offset.
86 // The buffer is expanded automatically if needed.
87 // The offset is advanced by the size of the data.
89 void MemCpy(size_t &offset
, const void *src
, size_t size
);
90 template <class ValueT
>
91 void SetValue(size_t &offset
, ValueT value
)
93 this->MemCpy(offset
, &value
, sizeof(value
));
98 static void PrintAscii(bool setting
) { bPrintAscii
= setting
; }
99 static bool PrintAscii() { return bPrintAscii
; }
102 BXEXPORT
std::istream
& operator>> (std::istream
&is
, Data
&data
);
103 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const Data
&data
);
108 const Data
&m_old
, &m_new
;
110 BXLOCAL
void Compare(std::ostream
&os
, size_t index
, size_t size
) const;
113 Diff(const Data
&old
, const Data
&new_
);
115 void Dump(std::ostream
&os
) const;
118 BXEXPORT
std::ostream
& operator<< (std::ostream
&os
, const Diff
&diff
);
122 BXEXPORT
bool LoadDataArray(const std::string
&filename
, std::vector
<Data
> &array
);
123 BXEXPORT
bool ReadDataArray(std::istream
&is
, std::vector
<Data
> &array
);
129 /// Database record data class. The purpose of this class is to contain
130 /// the raw data that flows between low level activity such as device
131 /// read/writes, backup read/writes, and record parsing.
133 /// This class contains the low level record data block, unparsed, as well
134 /// as the surrounding meta data, such as the database name it belongs
135 /// to, the Unique ID, the Rec Type, and format version/type based on what
136 /// commands were used to extract the data from the device. (When using
137 /// newer commands, the format of the records, potentially including the
138 /// individual field type codes, are different.)
140 /// Possible bi-directional data flow in all of Barry:
141 /// Note that this class, DBData, represents the data+meta stage.
143 /// data+meta <-> device
144 /// data+meta <-> backup file
145 /// data+meta <-> record object
146 /// record object <-> boost serialization
147 /// contact record object <-> ldif
149 /// Possible uni-directional data flow in all of Barry:
151 /// record object -> text dump
153 class BXEXPORT DBData
156 enum RecordFormatVersion
164 RecordFormatVersion m_version
;
165 std::string m_dbName
;
170 // the raw data block, internal
173 // the data block to use... could be external or internal,
174 // and does not change for the life of the object
178 /// Default constructor, constructs an empty local Data object
181 /// Constructs a local Data object that points to external memory
182 DBData(const void *ValidData
, size_t size
);
183 DBData(RecordFormatVersion ver
, const std::string
&dbName
,
184 uint8_t recType
, uint32_t uniqueId
, size_t offset
,
185 const void *ValidData
, size_t size
);
187 /// If copy == false, constructs an external Data object, no local.
188 /// If copy == true, constructs an internal Data object copy
189 /// For speed, set copy to false.
190 /// If you want Copy On Write behaviour, similar to Data(buf,size),
191 /// then use the above (buf, size) constructor, not this one,
192 /// since this constructor uses Data's copy constructor.
193 DBData(Data
&externalData
, bool copy
);
194 DBData(RecordFormatVersion ver
, const std::string
&dbName
,
195 uint8_t recType
, uint32_t uniqueId
, size_t offset
,
196 Data
&externalData
, bool copy
);
201 RecordFormatVersion
GetVersion() const { return m_version
; }
202 const std::string
& GetDBName() const { return m_dbName
; }
203 uint8_t GetRecType() const { return m_recType
; }
204 uint32_t GetUniqueId() const { return m_uniqueId
; }
205 size_t GetOffset() const { return m_offset
; }
207 const Data
& GetData() const { return m_data
; }
211 void SetVersion(RecordFormatVersion ver
)
216 void SetDBName(const std::string
&dbName
)
221 void SetIds(uint8_t recType
, uint32_t uniqueId
)
224 m_uniqueId
= uniqueId
;
227 void SetOffset(size_t offset
)