- added initial parsing of ServiceBook field 0x09, and calling it
[barry.git] / src / data.h
blob0d32ad45cc813d26fd936bf9a2b82d01f85f953c
1 ///
2 /// \file data.h
3 /// Class to deal with pre-saved data files
4 ///
6 /*
7 Copyright (C) 2005-2006, 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 class Data
30 unsigned char *m_data;
31 size_t m_bufsize; //< size of m_data buffer allocated
32 size_t m_datasize; //< number of bytes of actual data
33 int m_endpoint;
35 // copy on write feature
36 const unsigned char *m_externalData;
37 bool m_external;
39 // output format flags
40 static bool bPrintAscii;
42 protected:
43 void MakeSpace(size_t desiredsize);
44 void CopyOnWrite(size_t desiredsize);
46 public:
47 Data();
48 Data(int endpoint, size_t startsize = 0x4000);
49 Data(const void *ValidData, size_t size);
50 Data(const Data &other);
51 ~Data();
53 void InputHexLine(std::istream &is);
54 void DumpHexLine(std::ostream &os, size_t index, size_t size) const;
55 void DumpHex(std::ostream &os) const;
57 int GetEndpoint() const { return m_endpoint; }
59 const unsigned char * GetData() const { return m_external ? m_externalData : m_data; }
60 size_t GetSize() const { return m_datasize; }
62 unsigned char * GetBuffer(size_t requiredsize = 0);
63 size_t GetBufSize() const { return m_bufsize; }
64 void ReleaseBuffer(int datasize = -1);
66 void Zap();
68 Data& operator=(const Data &other);
71 // static functions
72 static void PrintAscii(bool setting) { bPrintAscii = setting; }
73 static bool PrintAscii() { return bPrintAscii; }
76 std::istream& operator>> (std::istream &is, Data &data);
77 std::ostream& operator<< (std::ostream &os, const Data &data);
80 class Diff
82 const Data &m_old, &m_new;
84 void Compare(std::ostream &os, size_t index, size_t size) const;
86 public:
87 Diff(const Data &old, const Data &new_);
89 void Dump(std::ostream &os) const;
92 std::ostream& operator<< (std::ostream &os, const Diff &diff);
95 // utility functions
96 bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
98 #endif