fork download
  1. #ifndef STREAMING_H
  2. #define STREAMING_H
  3.  
  4. // TODO: support big endian
  5. // TODO: wchar_t currently no longer supported. If it's added again take care that wchar_t has different
  6. // sizes on different systems.
  7.  
  8. #include <string>
  9. #include <stdio.h>
  10. #include <stdint.h>
  11.  
  12. class IArchive
  13. {
  14. public:
  15. virtual ~IArchive() {}
  16.  
  17. virtual void Clear() = 0;
  18. virtual uint64_t GetSize() const = 0;
  19. virtual uint64_t GetReadPos() const = 0;
  20. virtual void SetReadPos(uint64_t pos_) = 0;
  21.  
  22. virtual void Write(bool val_) = 0;
  23. virtual void Write(char val_) = 0;
  24. virtual void Write(unsigned char val_) = 0;
  25. virtual void Write(int16_t val_) = 0;
  26. virtual void Write(uint16_t val_) = 0;
  27. virtual void Write(int32_t val_) = 0;
  28. virtual void Write(uint32_t val_) = 0;
  29. virtual void Write(int64_t val_) = 0;
  30. virtual void Write(uint64_t val_) = 0;
  31. virtual void Write(float val_) = 0;
  32. virtual void Write(double val_) = 0;
  33. virtual void WriteString(const char * str_) = 0;
  34. virtual void WriteData(uint64_t size_, void * data_) = 0;
  35.  
  36. virtual void Read(bool &val_) = 0;
  37. virtual void Read(char &val_) = 0;
  38. virtual void Read(unsigned char &val_) = 0;
  39. virtual void Read(int16_t &val_) = 0;
  40. virtual void Read(uint16_t &val_) = 0;
  41. virtual void Read(int32_t &val_) = 0;
  42. virtual void Read(uint32_t &val_) = 0;
  43. virtual void Read(int64_t &val_) = 0;
  44. virtual void Read(uint64_t &val_) = 0;
  45. virtual void Read(float &val_) = 0;
  46. virtual void Read(double &val_) = 0;
  47. virtual int64_t ReadString(char ** str_) = 0; // return 0 for no string, 1 for empty string and strlen+1 otherwise
  48. virtual int64_t ReadData(void ** data_) = 0; // return size of data
  49.  
  50. // don't append size information but just write the data_
  51. virtual void WriteRaw(uint64_t size_, void * data_) = 0;
  52. virtual void ReadRaw(uint64_t size_, void ** data_) = 0;
  53.  
  54. // comfort functions
  55. virtual int16_t Read16s() { int16_t result; Read(result); return result; }
  56. virtual uint16_t Read16u() { uint16_t result; Read(result); return result; }
  57. virtual int32_t Read32s() { int32_t result; Read(result); return result; }
  58. virtual uint32_t Read32u() { uint32_t result; Read(result); return result; }
  59. virtual int64_t Read64s() { int64_t result; Read(result); return result; }
  60. virtual uint64_t Read64u() { uint64_t result; Read(result); return result; }
  61. };
  62.  
  63. class MemArchive : public IArchive
  64. {
  65. public:
  66. MemArchive(uint64_t startCapacity_ = 1); // capacity will always be min. 1
  67. MemArchive(const MemArchive &archive_);
  68. virtual ~MemArchive();
  69.  
  70. void operator=(const MemArchive &archive_);
  71.  
  72. virtual void Clear();
  73. virtual uint64_t GetSize() const { return mSize; }
  74. virtual uint64_t GetReadPos() const { return mReadPos; }
  75. virtual void SetReadPos(uint64_t pos_) { mReadPos = pos_; }
  76.  
  77. // Write function will stop working when archive size would get larger than size_.
  78. // It will _not_ remove data from archives which are already larger.
  79. virtual void SetMaxWriteSize(uint64_t size_) { mMaxWriteSize = size_; }
  80.  
  81. virtual void Write(bool val_);
  82. virtual void Write(char val_);
  83. virtual void Write(unsigned char val_);
  84. virtual void Write(int16_t val_);
  85. virtual void Write(uint16_t val_);
  86. virtual void Write(int32_t val_);
  87. virtual void Write(uint32_t val_);
  88. virtual void Write(int64_t val_);
  89. virtual void Write(uint64_t val_);
  90. virtual void Write(float val_);
  91. virtual void Write(double val_);
  92. virtual void WriteString(const char * str_);
  93. virtual void WriteData(uint64_t size_, void * data_);
  94.  
  95. virtual void Read(bool &val_);
  96. virtual void Read(char &val_);
  97. virtual void Read(unsigned char &val_);
  98. virtual void Read(int16_t &val_);
  99. virtual void Read(uint16_t &val_);
  100. virtual void Read(int32_t &val_);
  101. virtual void Read(uint32_t &val_);
  102. virtual void Read(int64_t &val_);
  103. virtual void Read(uint64_t &val_);
  104. virtual void Read(float &val_);
  105. virtual void Read(double &val_);
  106. virtual int64_t ReadString(char ** str_); // return 0 for no string, 1 for empty string and strlen+1 otherwise
  107. virtual int64_t ReadData(void ** data_); // return size of data
  108.  
  109. // don't append size information but just write the data_
  110. virtual void WriteRaw(uint64_t size_, void * data_);
  111. virtual void ReadRaw(uint64_t size_, void ** data_);
  112.  
  113. private:
  114. bool CapacityCheck(uint64_t additionalSizeNeeded_);
  115.  
  116. uint64_t mSize;
  117. uint64_t mCapacity;
  118. uint64_t mReadPos;
  119. uint64_t mMaxWriteSize;
  120. char * mData;
  121. };
  122.  
  123. class FileArchive : public IArchive
  124. {
  125. public:
  126. FileArchive();
  127. FileArchive(const std::string &filename_, const std::string &mode_);
  128. virtual ~FileArchive();
  129.  
  130. // mode_: all fopen flags, typicly used here: "wb" writing, "rb" reading
  131. bool Open(const std::string &filename_, const std::string &mode_);
  132. void Close();
  133.  
  134. virtual void Clear();
  135. virtual uint64_t GetSize() const;
  136. virtual uint64_t GetReadPos() const;
  137. virtual void SetReadPos(uint64_t pos_);
  138.  
  139. virtual void Write(bool val_);
  140. virtual void Write(char val_);
  141. virtual void Write(unsigned char val_);
  142. virtual void Write(int16_t val_);
  143. virtual void Write(uint16_t val_);
  144. virtual void Write(int32_t val_);
  145. virtual void Write(uint32_t val_);
  146. virtual void Write(int64_t val_);
  147. virtual void Write(uint64_t val_);
  148. virtual void Write(float val_);
  149. virtual void Write(double val_);
  150. virtual void WriteString(const char * str_);
  151. virtual void WriteData(uint64_t size_, void * data_);
  152.  
  153. virtual void Read(bool &val_);
  154. virtual void Read(char &val_);
  155. virtual void Read(unsigned char &val_);
  156. virtual void Read(int16_t &val_);
  157. virtual void Read(uint16_t &val_);
  158. virtual void Read(int32_t &val_);
  159. virtual void Read(uint32_t &val_);
  160. virtual void Read(int64_t &val_);
  161. virtual void Read(uint64_t &val_);
  162. virtual void Read(float &val_);
  163. virtual void Read(double &val_);
  164. virtual int64_t ReadString(char ** str_); // return 0 for no string, 1 for empty string and strlen+1 otherwise
  165. virtual int64_t ReadData(void ** data_); // return size of data
  166.  
  167. // don't append size information but just write the data_
  168. virtual void WriteRaw(uint64_t size_, void * data_);
  169. virtual void ReadRaw(uint64_t size_, void ** data_);
  170.  
  171. private:
  172. FILE * mFile;
  173. std::string mFilename;
  174. };
  175.  
  176.  
  177. #endif // STREAMING_H
  178.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/lib/gcc/i486-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty