fork download
  1. #include "streaming.h"
  2. #include <cassert>
  3. #include <climits>
  4. #include <cstring>
  5.  
  6. // --------------------------------------------------------
  7. MemArchive::MemArchive(uint64_t startCapacity_)
  8. {
  9. mSize = 0;
  10. mCapacity = startCapacity_;
  11. if ( mCapacity < 1 )
  12. mCapacity = 1;
  13. mReadPos = 0;
  14. mMaxWriteSize = ULONG_MAX;
  15. mData = new char[mCapacity];
  16. }
  17.  
  18. MemArchive::MemArchive(const MemArchive &archive_)
  19. : IArchive()
  20. , mSize(0)
  21. , mCapacity(0)
  22. , mReadPos(0)
  23. , mMaxWriteSize(ULONG_MAX)
  24. , mData(NULL)
  25. {
  26. *this = archive_;
  27. }
  28.  
  29. void MemArchive::operator=(const MemArchive &archive_)
  30. {
  31. delete[] mData;
  32. mData = NULL;
  33.  
  34. mSize = archive_.mSize;
  35. mReadPos = archive_.mReadPos;
  36. mCapacity = archive_.mCapacity;
  37. mData = new char[mCapacity];
  38. memcpy(mData, archive_.mData, mCapacity);
  39. }
  40.  
  41. MemArchive::~MemArchive()
  42. {
  43. delete[] mData;
  44. }
  45.  
  46. bool MemArchive::CapacityCheck(uint64_t additionalSizeNeeded_)
  47. {
  48. if ( mSize + additionalSizeNeeded_ > mMaxWriteSize )
  49. return false;
  50. if ( mSize + additionalSizeNeeded_ > mCapacity )
  51. {
  52. char * dummy = mData;
  53. uint64_t newCapacity = mCapacity * 2 + 1;
  54. if ( mSize + additionalSizeNeeded_ > newCapacity )
  55. newCapacity = mSize + additionalSizeNeeded_;
  56. mData = new char[newCapacity];
  57. memcpy(mData, dummy, mCapacity);
  58. delete[] dummy;
  59.  
  60. mCapacity = newCapacity;
  61. }
  62.  
  63. return true;
  64. }
  65.  
  66. void MemArchive::Clear()
  67. {
  68. mSize = 0;
  69. mReadPos = 0;
  70. }
  71.  
  72. void MemArchive::Write(bool val_)
  73. {
  74. if ( CapacityCheck(sizeof(val_)) )
  75. {
  76. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  77. mSize += sizeof(val_);
  78. }
  79. }
  80.  
  81. void MemArchive::Write(char val_)
  82. {
  83. if ( CapacityCheck(sizeof(val_)) )
  84. {
  85. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  86. mSize += sizeof(val_);
  87. }
  88. }
  89.  
  90. void MemArchive::Write(unsigned char val_)
  91. {
  92. if ( CapacityCheck(sizeof(val_)) )
  93. {
  94. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  95. mSize += sizeof(val_);
  96. }
  97. }
  98.  
  99. void MemArchive::Write(int16_t val_)
  100. {
  101. if ( CapacityCheck(sizeof(val_)) )
  102. {
  103. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  104. mSize += sizeof(val_);
  105. }
  106. }
  107.  
  108. void MemArchive::Write(uint16_t val_)
  109. {
  110. if ( CapacityCheck(sizeof(val_)) )
  111. {
  112. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  113. mSize += sizeof(val_);
  114. }
  115. }
  116.  
  117. void MemArchive::Write(int32_t val_)
  118. {
  119. if ( CapacityCheck(sizeof(val_)) )
  120. {
  121. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  122. mSize += sizeof(val_);
  123. }
  124. }
  125.  
  126. void MemArchive::Write(uint32_t val_)
  127. {
  128. if ( CapacityCheck(sizeof(val_)) )
  129. {
  130. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  131. mSize += sizeof(val_);
  132. }
  133. }
  134.  
  135. void MemArchive::Write(int64_t val_)
  136. {
  137. if ( CapacityCheck(sizeof(val_)) )
  138. {
  139. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  140. mSize += sizeof(val_);
  141. }
  142. }
  143.  
  144. void MemArchive::Write(uint64_t val_)
  145. {
  146. if ( CapacityCheck(sizeof(val_)) )
  147. {
  148. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  149. mSize += sizeof(val_);
  150. }
  151. }
  152.  
  153.  
  154. void MemArchive::Write(float val_)
  155. {
  156. if ( CapacityCheck(sizeof(val_)) )
  157. {
  158. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  159. mSize += sizeof(val_);
  160. }
  161. }
  162.  
  163. void MemArchive::Write(double val_)
  164. {
  165. if ( CapacityCheck(sizeof(val_)) )
  166. {
  167. memcpy(&(mData[mSize]), &val_, sizeof(val_));
  168. mSize += sizeof(val_);
  169. }
  170. }
  171.  
  172. void MemArchive::WriteString(const char * str_)
  173. {
  174. if ( !str_ )
  175. {
  176. uint64_t strLen = 0;
  177. if ( CapacityCheck(sizeof(strLen)) )
  178. {
  179. memcpy(&(mData[mSize]), &strLen, sizeof(strLen));
  180. mSize += sizeof(strLen);
  181. }
  182. }
  183. else
  184. {
  185. uint64_t strLen = strlen(str_) + 1;
  186. if ( CapacityCheck(strLen + sizeof(strLen)) )
  187. {
  188. memcpy(&(mData[mSize]), &strLen, sizeof(strLen));
  189. mSize += sizeof(strLen);
  190. memcpy(&(mData[mSize]), str_, strLen);
  191. mSize += strLen;
  192. }
  193. }
  194. }
  195.  
  196. void MemArchive::WriteData(uint64_t size_, void * data_)
  197. {
  198. if ( CapacityCheck(sizeof(size_) + size_) )
  199. {
  200. memcpy(&(mData[mSize]), &size_, sizeof(size_));
  201. mSize += sizeof(size_);
  202. if ( size_ )
  203. {
  204. assert(data_);
  205. memcpy(&(mData[mSize]), data_, size_);
  206. mSize += size_;
  207. }
  208. }
  209. }
  210.  
  211. void MemArchive::WriteRaw(uint64_t size_, void * data_)
  212. {
  213. if ( CapacityCheck(size_) )
  214. {
  215. if ( size_ && data_)
  216. {
  217. memcpy(&(mData[mSize]), data_, size_);
  218. mSize += size_;
  219. }
  220. }
  221. }
  222.  
  223. void MemArchive::ReadRaw(uint64_t size_, void ** data_)
  224. {
  225. if ( !data_ )
  226. {
  227. assert(!"zero data_ is given to MemArchive::ReadRaw");
  228. return;
  229. }
  230.  
  231. if ( !size_ )
  232. {
  233. *data_ = 0;
  234. return;
  235. }
  236.  
  237. if ( mReadPos + size_ > mSize )
  238. {
  239. assert(!"trying to read data from archive behind end in MemArchive::ReadRaw");
  240. return;
  241. }
  242. *data_ = new char[size_];
  243. memcpy(*data_, &(mData[mReadPos]), size_);
  244. mReadPos += size_;
  245. }
  246.  
  247. void MemArchive::Read(bool &val_)
  248. {
  249. if ( mReadPos + sizeof(val_) > mSize )
  250. {
  251. assert(!"trying to read from archive behind end");
  252. return;
  253. }
  254. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  255. mReadPos += sizeof(val_);
  256. }
  257.  
  258. void MemArchive::Read(char &val_)
  259. {
  260. if ( mReadPos + sizeof(val_) > mSize )
  261. {
  262. assert(!"trying to read from archive behind end");
  263. return;
  264. }
  265. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  266. mReadPos += sizeof(val_);
  267. }
  268.  
  269. void MemArchive::Read(unsigned char &val_)
  270. {
  271. if ( mReadPos + sizeof(val_) > mSize )
  272. {
  273. assert(!"trying to read from archive behind end");
  274. return;
  275. }
  276. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  277. mReadPos += sizeof(val_);
  278. }
  279.  
  280. void MemArchive::Read(int16_t &val_)
  281. {
  282. if ( mReadPos + sizeof(val_) > mSize )
  283. {
  284. assert(!"trying to read from archive behind end");
  285. return;
  286. }
  287. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  288. mReadPos += sizeof(val_);
  289. }
  290.  
  291. void MemArchive::Read(uint16_t &val_)
  292. {
  293. if ( mReadPos + sizeof(val_) > mSize )
  294. {
  295. assert(!"trying to read from archive behind end");
  296. return;
  297. }
  298. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  299. mReadPos += sizeof(val_);
  300. }
  301.  
  302. void MemArchive::Read(int32_t &val_)
  303. {
  304. if ( mReadPos + sizeof(val_) > mSize )
  305. {
  306. assert(!"trying to read from archive behind end");
  307. return;
  308. }
  309. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  310. mReadPos += sizeof(val_);
  311. }
  312.  
  313. void MemArchive::Read(uint32_t &val_)
  314. {
  315. if ( mReadPos + sizeof(val_) > mSize )
  316. {
  317. assert(!"trying to read from archive behind end");
  318. return;
  319. }
  320. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  321. mReadPos += sizeof(val_);
  322. }
  323.  
  324. void MemArchive::Read(int64_t &val_)
  325. {
  326. if ( mReadPos + sizeof(val_) > mSize )
  327. {
  328. assert(!"trying to read from archive behind end");
  329. return;
  330. }
  331. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  332. mReadPos += sizeof(val_);
  333. }
  334.  
  335. void MemArchive::Read(uint64_t &val_)
  336. {
  337. if ( mReadPos + sizeof(val_) > mSize )
  338. {
  339. assert(!"trying to read from archive behind end");
  340. return;
  341. }
  342. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  343. mReadPos += sizeof(val_);
  344. }
  345.  
  346. void MemArchive::Read(float &val_)
  347. {
  348. if ( mReadPos + sizeof(val_) > mSize )
  349. {
  350. assert(!"trying to read from archive behind end");
  351. return;
  352. }
  353. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  354. mReadPos += sizeof(val_);
  355. }
  356.  
  357. void MemArchive::Read(double &val_)
  358. {
  359. if ( mReadPos + sizeof(val_) > mSize )
  360. {
  361. assert(!"trying to read from archive behind end");
  362. return;
  363. }
  364. memcpy(&val_, &(mData[mReadPos]), sizeof(val_));
  365. mReadPos += sizeof(val_);
  366. }
  367.  
  368. int64_t MemArchive::ReadString(char ** str_)
  369. {
  370. if ( !str_ )
  371. {
  372. assert(!"zeror str_ is given to MemArchive::ReadString");
  373. return -1;
  374. }
  375.  
  376. int64_t strlen = 0;
  377. if ( mReadPos + sizeof(strlen) > mSize )
  378. {
  379. assert(!"trying to read from archive behind end in MemArchive::ReadString");
  380. return -1;
  381. }
  382. memcpy(&strlen, &(mData[mReadPos]), sizeof(strlen));
  383. mReadPos += sizeof(strlen);
  384.  
  385. if ( !strlen )
  386. {
  387. *str_ = 0;
  388. return 0;
  389. }
  390.  
  391. if ( mReadPos + strlen > mSize )
  392. {
  393. assert(!"trying to read string from archive behind end in MemArchive::ReadString");
  394. return -1;
  395. }
  396. *str_ = new char[strlen]; // not strlen+1, because end (0) is also transported
  397. memcpy(*str_, &(mData[mReadPos]), strlen);
  398. mReadPos += strlen;
  399.  
  400. return strlen;
  401. }
  402.  
  403. int64_t MemArchive::ReadData(void ** data_)
  404. {
  405. if ( !data_ )
  406. {
  407. assert(!"zero data_ is given to MemArchive::ReadData");
  408. return -1;
  409. }
  410. int64_t size = 0;
  411. if ( mReadPos + sizeof(size) > mSize )
  412. {
  413. assert(!"trying to read from archive behind end in MemArchive::ReadData");
  414. return -1;
  415. }
  416. memcpy(&size, &(mData[mReadPos]), sizeof(size));
  417. mReadPos += sizeof(size);
  418.  
  419. if ( !size )
  420. {
  421. *data_ = 0;
  422. return 0;
  423. }
  424.  
  425. if ( mReadPos + size > mSize )
  426. {
  427. assert(!"trying to read data from archive behind end in MemArchive::ReadData");
  428. return -1;
  429. }
  430. *data_ = new char[size];
  431. memcpy(*data_, &(mData[mReadPos]), size);
  432. mReadPos += size;
  433.  
  434. return size;
  435. }
  436.  
  437. // --------------------------------------------------------
  438. FileArchive::FileArchive()
  439. : mFile(0)
  440. {
  441. }
  442.  
  443. FileArchive::FileArchive(const std::string &filename_, const std::string &mode_)
  444. : mFilename(filename_)
  445. {
  446. Open(mFilename, mode_);
  447. }
  448.  
  449. FileArchive::~FileArchive()
  450. {
  451. Close();
  452. }
  453.  
  454. bool FileArchive::Open(const std::string &filename_, const std::string &mode_)
  455. {
  456. Close();
  457. mFile = fopen(filename_.c_str(), mode_.c_str());
  458. if ( !mFile )
  459. return false;
  460. return true;
  461. }
  462.  
  463. void FileArchive::Close()
  464. {
  465. if ( mFile )
  466. {
  467. fclose(mFile);
  468. }
  469. }
  470.  
  471. void FileArchive::Clear()
  472. {
  473. if ( mFile )
  474. {
  475. fclose(mFile);
  476. mFile = fopen(mFilename.c_str(), "w");
  477. }
  478. }
  479.  
  480. uint64_t FileArchive::GetSize() const
  481. {
  482. if ( !mFile)
  483. return 0;
  484.  
  485. uint64_t oldPos = ftell(mFile);
  486. fseek(mFile, 0, SEEK_END);
  487. uint64_t result = ftell(mFile);
  488. fseek(mFile, oldPos, SEEK_SET);
  489. return result;
  490. }
  491.  
  492. uint64_t FileArchive::GetReadPos() const
  493. {
  494. if ( !mFile )
  495. return 0;
  496. return ftell(mFile);
  497. }
  498.  
  499. void FileArchive::SetReadPos(uint64_t pos_)
  500. {
  501. if ( mFile )
  502. {
  503. fseek(mFile, pos_, SEEK_SET);
  504. }
  505. }
  506.  
  507. void FileArchive::Write(bool val_)
  508. {
  509. if ( mFile )
  510. {
  511. fwrite(&val_, sizeof(val_), 1, mFile);
  512. }
  513. }
  514.  
  515. void FileArchive::Write(char val_)
  516. {
  517. if ( mFile )
  518. {
  519. fwrite(&val_, sizeof(val_), 1, mFile);
  520. }
  521. }
  522.  
  523. void FileArchive::Write(unsigned char val_)
  524. {
  525. if ( mFile )
  526. {
  527. fwrite(&val_, sizeof(val_), 1, mFile);
  528. }
  529. }
  530.  
  531. void FileArchive::Write(int16_t val_)
  532. {
  533. if ( mFile )
  534. {
  535. fwrite(&val_, sizeof(val_), 1, mFile);
  536. }
  537. }
  538.  
  539. void FileArchive::Write(uint16_t val_)
  540. {
  541. if ( mFile )
  542. {
  543. fwrite(&val_, sizeof(val_), 1, mFile);
  544. }
  545. }
  546.  
  547. void FileArchive::Write(int32_t val_)
  548. {
  549. if ( mFile )
  550. {
  551. fwrite(&val_, sizeof(val_), 1, mFile);
  552. }
  553. }
  554.  
  555. void FileArchive::Write(uint32_t val_)
  556. {
  557. if ( mFile )
  558. {
  559. fwrite(&val_, sizeof(val_), 1, mFile);
  560. }
  561. }
  562.  
  563. void FileArchive::Write(int64_t val_)
  564. {
  565. if ( mFile )
  566. {
  567. fwrite(&val_, sizeof(val_), 1, mFile);
  568. }
  569. }
  570.  
  571. void FileArchive::Write(uint64_t val_)
  572. {
  573. if ( mFile )
  574. {
  575. fwrite(&val_, sizeof(val_), 1, mFile);
  576. }
  577. }
  578.  
  579. void FileArchive::Write(float val_)
  580. {
  581. if ( mFile )
  582. {
  583. fwrite(&val_, sizeof(val_), 1, mFile);
  584. }
  585. }
  586.  
  587. void FileArchive::Write(double val_)
  588. {
  589. if ( mFile )
  590. {
  591. fwrite(&val_, sizeof(val_), 1, mFile);
  592. }
  593. }
  594.  
  595. void FileArchive::WriteString(const char * str_)
  596. {
  597. if ( !mFile )
  598. return;
  599. if ( !str_ )
  600. {
  601. uint64_t strLen = 0;
  602. fwrite(&strLen, sizeof(strLen), 1, mFile);
  603. }
  604. else
  605. {
  606. uint64_t strLen = strlen(str_) + 1;
  607. fwrite(&strLen, sizeof(strLen), 1, mFile);
  608. fwrite(str_, strLen, 1, mFile);
  609. }
  610. }
  611.  
  612. void FileArchive::WriteData(uint64_t size_, void * data_)
  613. {
  614. if ( !mFile )
  615. return;
  616. fwrite(&size_, sizeof(size_), 1, mFile);
  617. if ( size_ )
  618. {
  619. fwrite(data_, size_, 1, mFile);
  620. }
  621. }
  622.  
  623. void FileArchive::WriteRaw(uint64_t size_, void * data_)
  624. {
  625. if ( mFile && size_ && data_ )
  626. {
  627. fwrite(data_, size_, 1, mFile);
  628. }
  629. }
  630.  
  631. void FileArchive::ReadRaw(uint64_t size_, void ** data_)
  632. {
  633. if ( mFile && size_ && data_ )
  634. {
  635. *data_ = new char[size_];
  636. fread(*data_, size_, 1, mFile);
  637. }
  638. }
  639.  
  640. void FileArchive::Read(bool &val_)
  641. {
  642. if ( mFile )
  643. {
  644. fread(&val_, sizeof(val_), 1, mFile);
  645. }
  646. }
  647.  
  648. void FileArchive::Read(char &val_)
  649. {
  650. if ( mFile )
  651. {
  652. fread(&val_, sizeof(val_), 1, mFile);
  653. }
  654. }
  655.  
  656. void FileArchive::Read(unsigned char &val_)
  657. {
  658. if ( mFile )
  659. {
  660. fread(&val_, sizeof(val_), 1, mFile);
  661. }
  662. }
  663.  
  664. void FileArchive::Read(int16_t &val_)
  665. {
  666. if ( mFile )
  667. {
  668. fread(&val_, sizeof(val_), 1, mFile);
  669. }
  670. }
  671.  
  672. void FileArchive::Read(uint16_t &val_)
  673. {
  674. if ( mFile )
  675. {
  676. fread(&val_, sizeof(val_), 1, mFile);
  677. }
  678. }
  679.  
  680. void FileArchive::Read(int32_t &val_)
  681. {
  682. if ( mFile )
  683. {
  684. fread(&val_, sizeof(val_), 1, mFile);
  685. }
  686. }
  687.  
  688. void FileArchive::Read(uint32_t &val_)
  689. {
  690. if ( mFile )
  691. {
  692. fread(&val_, sizeof(val_), 1, mFile);
  693. }
  694. }
  695.  
  696. void FileArchive::Read(int64_t &val_)
  697. {
  698. if ( mFile )
  699. {
  700. fread(&val_, sizeof(val_), 1, mFile);
  701. }
  702. }
  703.  
  704. void FileArchive::Read(uint64_t &val_)
  705. {
  706. if ( mFile )
  707. {
  708. fread(&val_, sizeof(val_), 1, mFile);
  709. }
  710. }
  711.  
  712. void FileArchive::Read(float &val_)
  713. {
  714. if ( mFile )
  715. {
  716. fread(&val_, sizeof(val_), 1, mFile);
  717. }
  718. }
  719.  
  720. void FileArchive::Read(double &val_)
  721. {
  722. if ( mFile )
  723. {
  724. fread(&val_, sizeof(val_), 1, mFile);
  725. }
  726. }
  727.  
  728. // return 0 for no string, 1 for empty string and strlen+1 otherwise
  729. int64_t FileArchive::ReadString(char ** str_)
  730. {
  731. if ( !mFile )
  732. {
  733. return -1;
  734. }
  735. if ( !str_ )
  736. {
  737. assert(!"zeror str_ is given to FileArchive::ReadString");
  738. return -1;
  739. }
  740.  
  741. int64_t strLen = 0;
  742. fread(&strLen, sizeof(strLen), 1, mFile);
  743.  
  744. if ( !strLen )
  745. {
  746. *str_ = 0;
  747. return 0;
  748. }
  749.  
  750. *str_ = new char[strLen]; // not strlen+1, because end (0) is also transported
  751. fread(*str_, strLen, 1, mFile);
  752.  
  753. return strLen;
  754. }
  755.  
  756. // return size of data
  757. int64_t FileArchive::ReadData(void ** data_)
  758. {
  759. if ( !mFile )
  760. {
  761. return -1;
  762. }
  763. if ( !data_ )
  764. {
  765. assert(!"zero data_ is given to FileArchive::ReadData");
  766. return -1;
  767. }
  768. int64_t size = 0;
  769. fread(&size, sizeof(size), 1, mFile);
  770.  
  771. if ( !size )
  772. {
  773. *data_ = 0;
  774. return 0;
  775. }
  776.  
  777. *data_ = new char[size];
  778. fread(*data_, size, 1, mFile);
  779.  
  780. return size;
  781. }
  782.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:23: fatal error: streaming.h: No such file or directory
 #include "streaming.h"
                       ^
compilation terminated.
stdout
Standard output is empty