doc: added link to Nicolas Vivien's development blog
[barry.git] / src / data.h
blob721c8edf88310d20e9c932483db221b662d492c5
1 ///
2 /// \file data.h
3 /// Class to deal with pre-saved data files
4 ///
6 /*
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.
22 #ifndef __SB_DATA_H__
23 #define __SB_DATA_H__
25 #include "dll.h"
26 #include <iosfwd>
27 #include <vector>
28 #include <string>
29 #include <stdint.h>
31 namespace Barry {
33 class BXEXPORT Data
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
38 int m_endpoint;
40 // copy on write feature
41 const unsigned char *m_externalData;
42 bool m_external;
44 // output format flags
45 static bool bPrintAscii;
47 protected:
48 void MakeSpace(size_t desiredsize);
49 void CopyOnWrite(size_t desiredsize);
51 public:
52 Data();
53 explicit Data(int endpoint, size_t startsize = 0x4000);
54 Data(const void *ValidData, size_t size);
55 Data(const Data &other);
56 ~Data();
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);
81 // Utility functions
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 void Append(const void *buf, size_t size);
91 template <class ValueT>
92 void SetValue(size_t &offset, ValueT value)
94 this->MemCpy(offset, &value, sizeof(value));
98 // static functions
99 static void PrintAscii(bool setting) { bPrintAscii = setting; }
100 static bool PrintAscii() { return bPrintAscii; }
103 BXEXPORT std::istream& operator>> (std::istream &is, Data &data);
104 BXEXPORT std::ostream& operator<< (std::ostream &os, const Data &data);
107 class BXEXPORT Diff
109 const Data &m_old, &m_new;
111 BXLOCAL void Compare(std::ostream &os, size_t index, size_t size) const;
113 public:
114 Diff(const Data &old, const Data &new_);
116 void Dump(std::ostream &os) const;
119 BXEXPORT std::ostream& operator<< (std::ostream &os, const Diff &diff);
122 // utility functions
123 BXEXPORT bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
124 BXEXPORT bool ReadDataArray(std::istream &is, std::vector<Data> &array);
128 // DBData
130 /// Database record data class. The purpose of this class is to contain
131 /// the raw data that flows between low level activity such as device
132 /// read/writes, backup read/writes, and record parsing.
134 /// This class contains the low level record data block, unparsed, as well
135 /// as the surrounding meta data, such as the database name it belongs
136 /// to, the Unique ID, the Rec Type, and format version/type based on what
137 /// commands were used to extract the data from the device. (When using
138 /// newer commands, the format of the records, potentially including the
139 /// individual field type codes, are different.)
141 /// Possible bi-directional data flow in all of Barry:
142 /// Note that this class, DBData, represents the data+meta stage.
144 /// data+meta <-> device
145 /// data+meta <-> backup file
146 /// data+meta <-> record object
147 /// record object <-> boost serialization
148 /// contact record object <-> ldif
150 /// Possible uni-directional data flow in all of Barry:
152 /// record object -> text dump
154 class BXEXPORT DBData
156 public:
157 enum RecordFormatVersion
159 REC_VERSION_1,
160 REC_VERSION_2
163 private:
164 // record meta data
165 RecordFormatVersion m_version;
166 std::string m_dbName;
167 uint8_t m_recType;
168 uint32_t m_uniqueId;
169 size_t m_offset;
171 // the raw data block, internal
172 Data *m_localData;
174 // the data block to use... could be external or internal,
175 // and does not change for the life of the object
176 Data &m_data;
178 public:
179 /// Default constructor, constructs an empty local Data object
180 DBData();
182 /// Constructs a local Data object that points to external memory
183 DBData(const void *ValidData, size_t size);
184 DBData(RecordFormatVersion ver, const std::string &dbName,
185 uint8_t recType, uint32_t uniqueId, size_t offset,
186 const void *ValidData, size_t size);
188 /// If copy == false, constructs an external Data object, no local.
189 /// If copy == true, constructs an internal Data object copy
190 /// For speed, set copy to false.
191 /// If you want Copy On Write behaviour, similar to Data(buf,size),
192 /// then use the above (buf, size) constructor, not this one,
193 /// since this constructor uses Data's copy constructor.
194 DBData(Data &externalData, bool copy);
195 DBData(RecordFormatVersion ver, const std::string &dbName,
196 uint8_t recType, uint32_t uniqueId, size_t offset,
197 Data &externalData, bool copy);
199 ~DBData();
201 // access meta data
202 RecordFormatVersion GetVersion() const { return m_version; }
203 const std::string& GetDBName() const { return m_dbName; }
204 uint8_t GetRecType() const { return m_recType; }
205 uint32_t GetUniqueId() const { return m_uniqueId; }
206 size_t GetOffset() const { return m_offset; }
208 const Data& GetData() const { return m_data; }
209 Data& UseData();
211 // operations
212 void SetVersion(RecordFormatVersion ver)
214 m_version = ver;
217 void SetDBName(const std::string &dbName)
219 m_dbName = dbName;
222 void SetIds(uint8_t recType, uint32_t uniqueId)
224 m_recType = recType;
225 m_uniqueId = uniqueId;
228 void SetOffset(size_t offset)
230 m_offset = offset;
233 void CopyMeta(const DBData &src)
235 m_version = src.m_version;
236 m_dbName = src.m_dbName;
237 m_recType = src.m_recType;
238 m_uniqueId = src.m_uniqueId;
239 m_offset = src.m_offset;
242 DBData& operator=(const DBData &other);
245 } // namespace Barry
247 #endif