data.h

Go to the documentation of this file.
00001 ///
00002 /// \file       data.h
00003 ///             Class to deal with pre-saved data files
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __SB_DATA_H__
00023 #define __SB_DATA_H__
00024 
00025 #include "dll.h"
00026 #include <iosfwd>
00027 #include <vector>
00028 
00029 namespace Barry {
00030 
00031 class BXEXPORT Data
00032 {
00033         unsigned char *m_data;
00034         size_t m_bufsize;               //< size of m_data buffer allocated
00035         size_t m_datasize;              //< number of bytes of actual data
00036         int m_endpoint;
00037 
00038         // copy on write feature
00039         const unsigned char *m_externalData;
00040         bool m_external;
00041 
00042         // output format flags
00043         static bool bPrintAscii;
00044 
00045 protected:
00046         void MakeSpace(size_t desiredsize);
00047         void CopyOnWrite(size_t desiredsize);
00048 
00049 public:
00050         Data();
00051         explicit Data(int endpoint, size_t startsize = 0x4000);
00052         Data(const void *ValidData, size_t size);
00053         Data(const Data &other);
00054         ~Data();
00055 
00056         void InputHexLine(std::istream &is);
00057         void DumpHexLine(std::ostream &os, size_t index, size_t size) const;
00058         void DumpHex(std::ostream &os) const;
00059 
00060         int GetEndpoint() const { return m_endpoint; }
00061 
00062         const unsigned char * GetData() const { return m_external ? m_externalData : m_data; }
00063         size_t GetSize() const { return m_datasize; }
00064 
00065         unsigned char * GetBuffer(size_t requiredsize = 0);
00066         size_t GetBufSize() const { return m_bufsize; }
00067         void ReleaseBuffer(int datasize = -1);
00068 
00069         void AppendHexString(const char *str);
00070 
00071         /// set buffer to 0 size, but don't bother overwriting memory with 0
00072         void QuickZap() { m_datasize = 0; }
00073         void Zap();     // does a memset too
00074 
00075         Data& operator=(const Data &other);
00076 
00077 
00078         // static functions
00079         static void PrintAscii(bool setting) { bPrintAscii = setting; }
00080         static bool PrintAscii() { return bPrintAscii; }
00081 };
00082 
00083 BXEXPORT std::istream& operator>> (std::istream &is, Data &data);
00084 BXEXPORT std::ostream& operator<< (std::ostream &os, const Data &data);
00085 
00086 
00087 class BXEXPORT Diff
00088 {
00089         const Data &m_old, &m_new;
00090 
00091         BXLOCAL void Compare(std::ostream &os, size_t index, size_t size) const;
00092 
00093 public:
00094         Diff(const Data &old, const Data &new_);
00095 
00096         void Dump(std::ostream &os) const;
00097 };
00098 
00099 BXEXPORT std::ostream& operator<< (std::ostream &os, const Diff &diff);
00100 
00101 
00102 // utility functions
00103 BXEXPORT bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
00104 BXEXPORT bool ReadDataArray(std::istream &is, std::vector<Data> &array);
00105 
00106 } // namespace Barry
00107 
00108 #endif
00109 

Generated on Tue Jun 30 16:08:13 2009 for Barry by  doxygen 1.5.8