lib: added Backup parser and Restore builder classes
[barry.git] / src / data.h
blob106b6bd86d69220ec53685c92ad24c22a3a59f37
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>
29 namespace Barry {
31 class BXEXPORT Data
33 unsigned char *m_data;
34 size_t m_bufsize; //< size of m_data buffer allocated
35 size_t m_datasize; //< number of bytes of actual data
36 int m_endpoint;
38 // copy on write feature
39 const unsigned char *m_externalData;
40 bool m_external;
42 // output format flags
43 static bool bPrintAscii;
45 protected:
46 void MakeSpace(size_t desiredsize);
47 void CopyOnWrite(size_t desiredsize);
49 public:
50 Data();
51 explicit Data(int endpoint, size_t startsize = 0x4000);
52 Data(const void *ValidData, size_t size);
53 Data(const Data &other);
54 ~Data();
56 void InputHexLine(std::istream &is);
57 void DumpHexLine(std::ostream &os, size_t index, size_t size) const;
58 void DumpHex(std::ostream &os) const;
60 int GetEndpoint() const { return m_endpoint; }
62 const unsigned char * GetData() const { return m_external ? m_externalData : m_data; }
63 size_t GetSize() const { return m_datasize; }
65 unsigned char * GetBuffer(size_t requiredsize = 0);
66 size_t GetBufSize() const { return m_bufsize; }
67 void ReleaseBuffer(int datasize = -1);
69 void AppendHexString(const char *str);
71 /// set buffer to 0 size, but don't bother overwriting memory with 0
72 void QuickZap() { m_datasize = 0; }
73 void Zap(); // does a memset too
75 Data& operator=(const Data &other);
79 // Utility functions
81 // Writing data... basically does a memcpy(dst,src,sizeof(src))
82 // for each type. Does no endian conversions.
83 // dst is calculated as buffer + offset.
84 // The buffer is expanded automatically if needed.
85 // The offset is advanced by the size of the data.
87 void MemCpy(size_t &offset, const void *src, size_t size);
88 template <class ValueT>
89 void SetValue(size_t &offset, ValueT value)
91 this->MemCpy(offset, &value, sizeof(value));
95 // static functions
96 static void PrintAscii(bool setting) { bPrintAscii = setting; }
97 static bool PrintAscii() { return bPrintAscii; }
100 BXEXPORT std::istream& operator>> (std::istream &is, Data &data);
101 BXEXPORT std::ostream& operator<< (std::ostream &os, const Data &data);
104 class BXEXPORT Diff
106 const Data &m_old, &m_new;
108 BXLOCAL void Compare(std::ostream &os, size_t index, size_t size) const;
110 public:
111 Diff(const Data &old, const Data &new_);
113 void Dump(std::ostream &os) const;
116 BXEXPORT std::ostream& operator<< (std::ostream &os, const Diff &diff);
119 // utility functions
120 BXEXPORT bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
121 BXEXPORT bool ReadDataArray(std::istream &is, std::vector<Data> &array);
123 } // namespace Barry
125 #endif