updated change log to reflect reality
[barry.git] / src / data.h
blob5f867adf3e9ded130613eef0e7327aefcf17bdb3
1 ///
2 /// \file data.h
3 /// Class to deal with pre-saved data files
4 ///
6 /*
7 Copyright (C) 2005-2007, 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 <iosfwd>
26 #include <vector>
28 namespace Barry {
30 class Data
32 unsigned char *m_data;
33 size_t m_bufsize; //< size of m_data buffer allocated
34 size_t m_datasize; //< number of bytes of actual data
35 int m_endpoint;
37 // copy on write feature
38 const unsigned char *m_externalData;
39 bool m_external;
41 // output format flags
42 static bool bPrintAscii;
44 protected:
45 void MakeSpace(size_t desiredsize);
46 void CopyOnWrite(size_t desiredsize);
48 public:
49 Data();
50 explicit Data(int endpoint, size_t startsize = 0x4000);
51 Data(const void *ValidData, size_t size);
52 Data(const Data &other);
53 ~Data();
55 void InputHexLine(std::istream &is);
56 void DumpHexLine(std::ostream &os, size_t index, size_t size) const;
57 void DumpHex(std::ostream &os) const;
59 int GetEndpoint() const { return m_endpoint; }
61 const unsigned char * GetData() const { return m_external ? m_externalData : m_data; }
62 size_t GetSize() const { return m_datasize; }
64 unsigned char * GetBuffer(size_t requiredsize = 0);
65 size_t GetBufSize() const { return m_bufsize; }
66 void ReleaseBuffer(int datasize = -1);
68 void AppendHexString(const char *str);
70 void Zap();
72 Data& operator=(const Data &other);
75 // static functions
76 static void PrintAscii(bool setting) { bPrintAscii = setting; }
77 static bool PrintAscii() { return bPrintAscii; }
80 std::istream& operator>> (std::istream &is, Data &data);
81 std::ostream& operator<< (std::ostream &os, const Data &data);
84 class Diff
86 const Data &m_old, &m_new;
88 void Compare(std::ostream &os, size_t index, size_t size) const;
90 public:
91 Diff(const Data &old, const Data &new_);
93 void Dump(std::ostream &os) const;
96 std::ostream& operator<< (std::ostream &os, const Diff &diff);
99 // utility functions
100 bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
101 bool ReadDataArray(std::istream &is, std::vector<Data> &array);
103 } // namespace Barry
105 #endif