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-2010, 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         //
00079         // Utility functions
00080         //
00081         // Writing data... basically does a memcpy(dst,src,sizeof(src))
00082         // for each type.  Does no endian conversions.
00083         // dst is calculated as buffer + offset.
00084         // The buffer is expanded automatically if needed.
00085         // The offset is advanced by the size of the data.
00086         //
00087         void MemCpy(size_t &offset, const void *src, size_t size);
00088         template <class ValueT>
00089         void SetValue(size_t &offset, ValueT value)
00090         {
00091                 this->MemCpy(offset, &value, sizeof(value));
00092         }
00093 
00094 
00095         // static functions
00096         static void PrintAscii(bool setting) { bPrintAscii = setting; }
00097         static bool PrintAscii() { return bPrintAscii; }
00098 };
00099 
00100 BXEXPORT std::istream& operator>> (std::istream &is, Data &data);
00101 BXEXPORT std::ostream& operator<< (std::ostream &os, const Data &data);
00102 
00103 
00104 class BXEXPORT Diff
00105 {
00106         const Data &m_old, &m_new;
00107 
00108         BXLOCAL void Compare(std::ostream &os, size_t index, size_t size) const;
00109 
00110 public:
00111         Diff(const Data &old, const Data &new_);
00112 
00113         void Dump(std::ostream &os) const;
00114 };
00115 
00116 BXEXPORT std::ostream& operator<< (std::ostream &os, const Diff &diff);
00117 
00118 
00119 // utility functions
00120 BXEXPORT bool LoadDataArray(const std::string &filename, std::vector<Data> &array);
00121 BXEXPORT bool ReadDataArray(std::istream &is, std::vector<Data> &array);
00122 
00123 } // namespace Barry
00124 
00125 #endif
00126 
Generated by  doxygen 1.6.2-20100208