00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #ifndef EDOC_BINARYPERSISTENCE_H 00020 #define EDOC_BINARYPERSISTENCE_H 00021 00022 #include "EDoc/stdint.h" 00023 #include "EDoc/PersistenceIFace.h" 00024 00025 #include <string> 00026 #include <fstream> 00027 00028 namespace EDoc 00029 { 00030 //=========================================================================== 00031 /** \brief An implementation of PersistenceIFace for writing to binary files. 00032 * 00033 * This implementation of PersistenceIFace is much more efficient than its 00034 * text counterpart in both size of the data file and speed of 00035 * reading/writing. However it does not lend itself easily to debugging 00036 * problems. 00037 */ 00038 class BinaryPersistence : public PersistenceIFace 00039 { 00040 public: 00041 00042 //------------------------------------------------------------------------ 00043 /** \brief Create an instance that represents a binary file for reading 00044 * OR writing (But not both at once). 00045 * 00046 * Note: This does not open the file for reading/writing but just creates 00047 * an instance of this class that is prepared for reading/writing to the 00048 * file. 00049 * 00050 * \param read If true then the file is considered an input file and can 00051 * only be used for reading. Otherwise it will be considered an output 00052 * file and will only be available for writing to. 00053 * 00054 * \param filename The name of the file to open. 00055 */ 00056 BinaryPersistence(bool read, std::string filename); 00057 00058 //------------------------------------------------------------------------ 00059 /** \brief Closes the file upon destruction. 00060 */ 00061 virtual ~BinaryPersistence(); 00062 00063 //------------------------------------------------------------------------ 00064 /** \brief Opens the file 00065 */ 00066 virtual void Open(); 00067 00068 //------------------------------------------------------------------------ 00069 /** \brief Closes the file. 00070 */ 00071 virtual void Close(); 00072 00073 //------------------------------------------------------------------------ 00074 /** \brief See PersistenceIFace 00075 */ 00076 virtual std::string ReadString(const char* key); 00077 00078 //------------------------------------------------------------------------ 00079 /** \brief See PersistenceIFace 00080 */ 00081 virtual bool ReadBoolean(const char* key); 00082 00083 //------------------------------------------------------------------------ 00084 /** \brief See PersistenceIFace 00085 */ 00086 virtual uint8_t ReadUInt8(const char* key); 00087 00088 //------------------------------------------------------------------------ 00089 /** \brief See PersistenceIFace 00090 */ 00091 virtual int32_t ReadInt32(const char* key); 00092 00093 //------------------------------------------------------------------------ 00094 /** \brief See PersistenceIFace 00095 */ 00096 virtual uint32_t ReadUInt32(const char* key); 00097 00098 //------------------------------------------------------------------------ 00099 /** \brief See PersistenceIFace 00100 */ 00101 virtual uint8_t ReadRecordType(); 00102 00103 00104 //------------------------------------------------------------------------ 00105 /** \brief See PersistenceIFace 00106 */ 00107 virtual void WriteString(const char* key, std::string value); 00108 00109 //------------------------------------------------------------------------ 00110 /** \brief See PersistenceIFace 00111 */ 00112 virtual void WriteBoolean(const char* key, bool value); 00113 00114 //------------------------------------------------------------------------ 00115 /** \brief See PersistenceIFace 00116 */ 00117 virtual void WriteUInt8(const char* key, uint8_t value); 00118 00119 //------------------------------------------------------------------------ 00120 /** \brief See PersistenceIFace 00121 */ 00122 virtual void WriteInt32(const char* key, int32_t value); 00123 00124 //------------------------------------------------------------------------ 00125 /** \brief See PersistenceIFace 00126 */ 00127 virtual void WriteUInt32(const char* key, uint32_t value); 00128 00129 //------------------------------------------------------------------------ 00130 /** \brief See PersistenceIFace 00131 */ 00132 virtual void WriteUInt32Debug(const char* key, uint32_t value, 00133 std::string debug_text); 00134 00135 //------------------------------------------------------------------------ 00136 /** \brief See PersistenceIFace 00137 */ 00138 virtual void WriteInt32Debug(const char* key, int32_t value, 00139 std::string debug_text); 00140 00141 //------------------------------------------------------------------------ 00142 /** \brief See PersistenceIFace 00143 */ 00144 virtual void WriteRecordType(uint8_t value); 00145 00146 //------------------------------------------------------------------------ 00147 00148 private: 00149 00150 //------------------------------------------------------------------------ 00151 /** \brief Used internally to read some data into a buffer. 00152 */ 00153 void ReadData(char* buffer, size_t amount, const char* key); 00154 00155 //------------------------------------------------------------------------ 00156 /** \brief The file stream object that will be manipulated. 00157 */ 00158 std::fstream file; 00159 00160 //------------------------------------------------------------------------ 00161 /** \brief True if reading from the file or false if writing to it. 00162 */ 00163 bool reading; 00164 00165 //------------------------------------------------------------------------ 00166 /** \brief The name of the file being operated on. 00167 */ 00168 std::string filename; 00169 00170 //------------------------------------------------------------------------ 00171 00172 }; 00173 //=========================================================================== 00174 00175 } 00176 00177 00178 #endif // EDOC_BINARYPERSISTENCE_H