menu: added new Keywords tag to .desktop files
[barry.git] / src / data.h
blob86f3c86bf26a324eb36e95ca8ecd1a29deed1b41
1 ///
2 /// \file data.h
3 /// Class to deal with pre-saved data files
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 #define BARRY_DATA_DEFAULT_SIZE 0x4000
32 #define BARRY_DATA_DEFAULT_PREPEND_SIZE 0x100
34 namespace Barry {
36 class BXEXPORT Data
38 unsigned char *m_memBlock; //< pointer to full memory block
39 //< can be null if external
40 size_t m_blockSize; //< size of m_memBlock buffer allocated
42 unsigned char *m_dataStart; //< pointer to start of internal data
43 //< can be null if external, and can
44 //< start somewhere in the middle of
45 //< m_memBlock if internal with a
46 //< prepend buffer
48 size_t m_dataSize; //< number of bytes of actual data
50 // copy on write feature
51 const unsigned char *m_externalData;
52 bool m_external;
54 // meta data
55 int m_endpoint;
57 // output format flags
58 static bool bPrintAscii;
60 protected:
61 void MakeSpace(size_t desiredsize, size_t desiredprepend = 0);
62 size_t AvailablePrependSpace() const;
64 public:
65 Data();
66 explicit Data(int endpoint, size_t startsize = BARRY_DATA_DEFAULT_SIZE, size_t prependsize = BARRY_DATA_DEFAULT_PREPEND_SIZE);
67 Data(const void *ValidData, size_t size);
68 Data(const Data &other);
69 ~Data();
71 void InputHexLine(std::istream &is);
72 void DumpHexLine(std::ostream &os, size_t index, size_t size) const;
73 void DumpHex(std::ostream &os) const;
75 int GetEndpoint() const { return m_endpoint; }
77 const unsigned char * GetData() const { return m_external ? m_externalData : m_dataStart; }
78 size_t GetSize() const { return m_dataSize; }
80 unsigned char * GetBuffer(size_t requiredsize = 0);
81 /// Returns size of buffer returned by GetBuffer()
82 size_t GetBufSize() const;
83 void ReleaseBuffer(int datasize = -1);
85 void AppendHexString(const char *str);
87 /// set buffer to 0 size, but don't bother overwriting memory with 0
88 void QuickZap() { m_dataSize = 0; }
89 void Zap(); // does a memset too
91 Data& operator=(const Data &other);
95 // Utility functions
97 // Writing data... basically does a memcpy(dst,src,sizeof(src))
98 // for each type. Does no endian conversions.
99 // dst is calculated as buffer + offset.
100 // The buffer is expanded automatically if needed.
101 // The offset is advanced by the size of the data.
102 // GetSize() will increase automatically if the result of
103 // the MemCpy() goes beyond the existing data size.
105 void MemCpy(size_t &offset, const void *src, size_t size);
106 void Append(const void *buf, size_t size);
107 void Prepend(const void *buf, size_t size);
108 void Prechop(size_t size);
109 template <class ValueT>
110 void SetValue(size_t &offset, ValueT value)
112 this->MemCpy(offset, &value, sizeof(value));
116 // static functions
117 static void PrintAscii(bool setting) { bPrintAscii = setting; }
118 static bool PrintAscii() { return bPrintAscii; }
121 BXEXPORT std::istream& operator>> (std::istream &is, Data &data);
122 BXEXPORT std::ostream& operator<< (std::ostream &os, const Data &data);
125 class BXEXPORT Diff
127 const Data &m_old, &m_new;
129 BXLOCAL void Compare(std::ostream &os, size_t index, size_t size) const;
131 public:
132 Diff(const Data &old, const Data &new_);
134 void Dump(std::ostream &os) const;
137 BXEXPORT std::ostream& operator<< (std::ostream &os, const Diff &diff);
140 // utility functions
141 BXEXPORT bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
142 BXEXPORT bool ReadDataArray(std::istream &is, std::vector<Data> &array);
146 // DBData
148 /// Database record data class. The purpose of this class is to contain
149 /// the raw data that flows between low level activity such as device
150 /// read/writes, backup read/writes, and record parsing.
152 /// This class contains the low level record data block, unparsed, as well
153 /// as the surrounding meta data, such as the database name it belongs
154 /// to, the Unique ID, the Rec Type, and format version/type based on what
155 /// commands were used to extract the data from the device. (When using
156 /// newer commands, the format of the records, potentially including the
157 /// individual field type codes, are different.)
159 /// Possible bi-directional data flow in all of Barry:
160 /// Note that this class, DBData, represents the data+meta stage.
162 /// data+meta <-> device
163 /// data+meta <-> backup file
164 /// data+meta <-> record object
165 /// record object <-> boost serialization
166 /// contact record object <-> ldif
168 /// Possible uni-directional data flow in all of Barry:
170 /// record object -> text dump
172 class BXEXPORT DBData
174 public:
175 enum RecordFormatVersion
177 REC_VERSION_1,
178 REC_VERSION_2
181 private:
182 // record meta data
183 RecordFormatVersion m_version;
184 std::string m_dbName;
185 uint8_t m_recType;
186 uint32_t m_uniqueId;
187 size_t m_offset;
189 // the raw data block, internal
190 Data *m_localData;
192 // the data block to use... could be external or internal,
193 // and does not change for the life of the object
194 Data &m_data;
196 public:
197 /// Default constructor, constructs an empty local Data object
198 DBData();
200 /// Copy constructor - always creates an internal Data object, and
201 /// uses Data object's copy constructor to make it.
202 /// Copies all meta data as well.
203 /// If you want to optimize the copy, use one of the constructors
204 /// below.
205 DBData(const DBData &other);
207 /// Constructs a local Data object that points to external memory
208 DBData(const void *ValidData, size_t size);
209 DBData(RecordFormatVersion ver, const std::string &dbName,
210 uint8_t recType, uint32_t uniqueId, size_t offset,
211 const void *ValidData, size_t size);
213 /// If copy == false, constructs an external Data object, no local.
214 /// If copy == true, constructs an internal Data object copy
215 /// For speed, set copy to false.
216 /// If you want Copy On Write behaviour, similar to Data(buf,size),
217 /// then use the above (buf, size) constructor, not this one,
218 /// since this constructor uses Data's copy constructor.
219 DBData(Data &externalData, bool copy);
220 DBData(RecordFormatVersion ver, const std::string &dbName,
221 uint8_t recType, uint32_t uniqueId, size_t offset,
222 Data &externalData, bool copy);
224 ~DBData();
226 // access meta data
227 RecordFormatVersion GetVersion() const { return m_version; }
228 const std::string& GetDBName() const { return m_dbName; }
229 uint8_t GetRecType() const { return m_recType; }
230 uint32_t GetUniqueId() const { return m_uniqueId; }
231 size_t GetOffset() const { return m_offset; }
233 const Data& GetData() const { return m_data; }
234 Data& UseData();
236 // operations
237 void SetVersion(RecordFormatVersion ver)
239 m_version = ver;
242 void SetDBName(const std::string &dbName)
244 m_dbName = dbName;
247 void SetIds(uint8_t recType, uint32_t uniqueId)
249 m_recType = recType;
250 m_uniqueId = uniqueId;
253 void SetOffset(size_t offset)
255 m_offset = offset;
258 void CopyMeta(const DBData &src)
260 m_version = src.m_version;
261 m_dbName = src.m_dbName;
262 m_recType = src.m_recType;
263 m_uniqueId = src.m_uniqueId;
264 m_offset = src.m_offset;
267 DBData& operator=(const DBData &other);
270 } // namespace Barry
272 #endif