以前写的程序,都弄丢了,从今天开始,放到博客上,积累起来!
类CMetaDataOutputFileStream用来序列化数据到文件;类CMetaDataInputMemoryStream先将文件中的数据加载到内存中,然后用于反序列化。
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 5 #define DAF_RUN_ERROR printf 6 7 ////////////////////////////////////////////////////////////////////////// 8 // CMetaDataOutputFileStream 9 10 class CMetaDataOutputFileStream 11 { 12 typedef CMetaDataOutputFileStream MyType; 13 14 public: 15 CMetaDataOutputFileStream() 16 : m_pFile(NULL) 17 , m_good(true) 18 { 19 20 } 21 22 ~CMetaDataOutputFileStream() 23 { 24 Close(); 25 } 26 27 bool IsOpen() const { return NULL != m_pFile; } 28 bool Good() const { return m_good; } 29 30 void Open() 31 { 32 SetBad(); 33 m_pFile = fopen("C:\\metadata.dat", "wb"); 34 if (NULL != m_pFile) 35 SetGood(); 36 } 37 38 void Close() 39 { 40 if (IsOpen()) 41 { 42 if (fclose(m_pFile)) 43 SetBad(); 44 m_pFile = NULL; 45 } 46 } 47 48 template<typename T> 49 MyType& operator<<(const T& t) 50 { 51 if (Good()) 52 { 53 if (!Write(t)) 54 SetBad(); 55 } 56 return *this; 57 } 58 59 MyType& operator<<(bool b) 60 { 61 return *this << (char)b; 62 } 63 64 MyType& operator<<(const char* p) 65 { 66 return WriteStringWithNull(p, strlen(p)); 67 } 68 69 MyType& operator<<(const std::string& s) 70 { 71 return WriteStringWithNull(s.c_str(), s.size()); 72 } 73 74 MyType& WriteBinary(const void* p, size_t n) 75 { 76 if (Good()) 77 { 78 if (!Write(n) || !Write(p, n)) 79 SetBad(); 80 } 81 return *this; 82 } 83 84 MyType& WriteString(const char* p, size_t n) 85 { 86 if (Good()) 87 { 88 if (!Write(n) || !Write(p, n) || !Write("", 1)) 89 SetBad(); 90 } 91 return *this; 92 } 93 94 MyType& WriteStringWithNull(const char* p, size_t n) 95 { 96 if (Good()) 97 { 98 if (!Write(n) || !Write(p, n + 1)) 99 SetBad(); 100 } 101 return *this; 102 } 103 104 private: 105 bool Write(const void* p, size_t n) 106 { 107 return n == fwrite(p, 1, n, m_pFile); 108 } 109 110 template<typename T> 111 bool Write(const T& t) 112 { 113 return Write(&t, sizeof(t)); 114 } 115 116 void SetGood() { m_good = true; } 117 void SetBad() { m_good = false; } 118 119 private: 120 FILE* m_pFile; 121 bool m_good; 122 }; 123 124 ////////////////////////////////////////////////////////////////////////// 125 // CMetaDataInputMemoryStream 126 127 class CMetaDataInputMemoryStream 128 { 129 typedef CMetaDataInputMemoryStream MyType; 130 131 class CBin 132 { 133 public: 134 const void* p; 135 size_t n; 136 }; 137 138 class CStr 139 { 140 public: 141 const char* p; 142 size_t n; 143 }; 144 145 public: 146 CMetaDataInputMemoryStream() 147 : m_pBegin(NULL) 148 , m_pEnd(NULL) 149 , m_pCur(NULL) 150 , m_good(true) 151 { 152 153 } 154 155 ~CMetaDataInputMemoryStream() 156 { 157 Close(); 158 } 159 160 bool IsOpen() const { return NULL != m_pBegin; } 161 bool Good() const { return m_good; } 162 163 void Open() 164 { 165 Close(); 166 SetBad(); 167 168 FILE* pFile = fopen("C:\\metadata.dat", "rb"); 169 if (NULL == pFile) 170 return; 171 172 char* buf = NULL; 173 do 174 { 175 if (fseek(pFile, 0L, SEEK_END)) 176 break; 177 178 size_t len = (size_t)ftell(pFile); 179 if (0 == len) 180 break; 181 182 if (fseek(pFile, 0L, SEEK_SET)) 183 break; 184 185 buf = new char[len]; 186 187 if (len != fread(buf, 1, len, pFile)) 188 break; 189 190 m_pBegin = buf; 191 m_pCur = buf; 192 m_pEnd = buf + len; 193 SetGood(); 194 return; 195 196 } while (0); 197 198 (void)fclose(pFile); 199 delete[] buf; 200 } 201 202 void Close() 203 { 204 if (IsOpen()) 205 { 206 delete[] m_pBegin; 207 m_pBegin = NULL; 208 m_pCur = NULL; 209 m_pEnd = NULL; 210 } 211 } 212 213 template<typename T> 214 MyType& operator>>(T& t) 215 { 216 if (Good()) 217 { 218 if (!Read(t)) 219 SetBad(); 220 } 221 return *this; 222 } 223 224 MyType& operator>>(bool& b) 225 { 226 if (Good()) 227 { 228 char c; 229 if (!Read(c)) 230 SetBad(); 231 else 232 b = (c != 0); 233 } 234 return *this; 235 } 236 237 MyType& operator>>(const char*& p) 238 { 239 size_t n; 240 return ReadString(p, n); 241 } 242 243 MyType& operator>>(std::string& s) 244 { 245 if (Good()) 246 { 247 const char* p; 248 size_t n; 249 if (!Read(n) || !Read(p, n + 1) || p[n]) 250 SetBad(); 251 else 252 (void)s.assign(p, n); 253 } 254 return *this; 255 } 256 257 MyType& operator>>(CBin& b) 258 { 259 return ReadBinary(b.p, b.n); 260 } 261 262 MyType& operator>>(CStr& s) 263 { 264 return ReadString(s.p, s.n); 265 } 266 267 MyType& ReadBinary(const void*& p, size_t& n) 268 { 269 if (Good()) 270 { 271 if (!Read(n) || !Read(p, n)) 272 SetBad(); 273 } 274 return *this; 275 } 276 277 MyType& ReadString(const char*& p, size_t& n) 278 { 279 if (Good()) 280 { 281 if (!Read(n) || !Read(p, n + 1) || p[n]) 282 SetBad(); 283 } 284 return *this; 285 } 286 287 private: 288 size_t RemainingLength() const { return m_pEnd - m_pCur; } 289 290 template<typename T> 291 bool Read(const T*& p, size_t n) 292 { 293 if (RemainingLength() >= n) 294 { 295 p = (const T*)m_pCur; 296 m_pCur += n; 297 return true; 298 } 299 return false; 300 } 301 302 template<typename T> 303 bool Read(T& t) 304 { 305 if (RemainingLength() >= sizeof(t)) 306 { 307 t = *(const T*)m_pCur; 308 m_pCur += sizeof(t); 309 return true; 310 } 311 return false; 312 } 313 314 void SetGood() { m_good = true; } 315 void SetBad() { m_good = false; } 316 317 private: 318 const char* m_pBegin; 319 const char* m_pEnd; 320 const char* m_pCur; 321 bool m_good; 322 }; 323 324 ////////////////////////////////////////////////////////////////////////// 325 326 int _tmain(int argc, _TCHAR* argv[]) 327 { 328 int a = 'aaaa'; 329 long long b = (((long long)'bbbb') << 32) + 'bbbb'; 330 const char* c = "ccccc"; 331 std::string d("ddddd"); 332 const char e[] = "eeeee"; 333 CMetaDataOutputFileStream ofs; 334 ofs.Open(); 335 ofs << a; 336 ofs << b; 337 ofs << c; 338 ofs << d; 339 ofs.WriteBinary(e, sizeof(e)); 340 ofs.Close(); 341 342 int aa = 0; 343 long long bb = 0; 344 const char* cc = 0; 345 std::string dd; 346 const void* ee = 0; 347 size_t een = 0; 348 CMetaDataInputMemoryStream ims; 349 ims.Open(); 350 ims >> aa; 351 ims >> bb; 352 ims >> cc; 353 ims >> dd; 354 ims.ReadBinary(ee, een); 355 ims.Close(); 356 357 return 0; 358 }