Stream.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Stream.h"
  23. #include "Types.h"
  24. #include "Compressor.h"
  25. #include "MallocAllocator.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. bool Stream::compress_to(Stream* stream, size_t size, size_t& zipped_size, Compressor* compressor)
  30. {
  31. assert(stream != NULL);
  32. assert(compressor != NULL);
  33. MallocAllocator allocator;
  34. void* in_buffer = (void*)allocator.allocate(size);
  35. read(in_buffer, size);
  36. void* compressed_buffer = compressor->compress(in_buffer, size, zipped_size);
  37. stream->write(compressed_buffer, zipped_size);
  38. return true;
  39. }
  40. //-----------------------------------------------------------------------------
  41. bool Stream::uncompress_to(Stream* stream, size_t& unzipped_size, Compressor* compressor)
  42. {
  43. assert(stream != NULL);
  44. assert(compressor != NULL);
  45. MallocAllocator allocator;
  46. size_t stream_size = size();
  47. void* in_buffer = (void*)allocator.allocate(stream_size);
  48. read(in_buffer, stream_size);
  49. void* uncompressed_buffer = compressor->uncompress(in_buffer, stream_size, unzipped_size);
  50. stream->write(uncompressed_buffer, unzipped_size);
  51. return true;
  52. }
  53. //-----------------------------------------------------------------------------
  54. BinaryReader::BinaryReader(Stream* s)
  55. {
  56. //BinaryReader takes the ownership of the stream.
  57. m_stream = s;
  58. }
  59. //-----------------------------------------------------------------------------
  60. BinaryReader::~BinaryReader()
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. int8_t BinaryReader::read_byte()
  65. {
  66. int8_t buffer;
  67. m_stream->read(&buffer, sizeof(int8_t));
  68. return buffer;
  69. }
  70. //-----------------------------------------------------------------------------
  71. int16_t BinaryReader::read_int16()
  72. {
  73. int16_t buffer;
  74. m_stream->read(&buffer, sizeof(int16_t));
  75. return buffer;
  76. }
  77. //-----------------------------------------------------------------------------
  78. uint16_t BinaryReader::read_uint16()
  79. {
  80. uint16_t buffer;
  81. m_stream->read(&buffer, sizeof(uint16_t));
  82. return buffer;
  83. }
  84. //-----------------------------------------------------------------------------
  85. int32_t BinaryReader::read_int32()
  86. {
  87. int32_t buffer;
  88. m_stream->read(&buffer, sizeof(int32_t));
  89. return buffer;
  90. }
  91. //-----------------------------------------------------------------------------
  92. uint32_t BinaryReader::read_uint32()
  93. {
  94. uint32_t buffer;
  95. m_stream->read(&buffer, sizeof(uint32_t));
  96. return buffer;
  97. }
  98. //-----------------------------------------------------------------------------
  99. int64_t BinaryReader::read_int64()
  100. {
  101. int64_t buffer;
  102. m_stream->read(&buffer, sizeof(int64_t));
  103. return buffer;
  104. }
  105. //-----------------------------------------------------------------------------
  106. double BinaryReader::read_double()
  107. {
  108. double buffer;
  109. m_stream->read(&buffer, sizeof(double));
  110. return buffer;
  111. }
  112. float BinaryReader::read_float()
  113. {
  114. float buffer;
  115. m_stream->read(&buffer, sizeof(float));
  116. return buffer;
  117. }
  118. //-----------------------------------------------------------------------------
  119. BinaryWriter::BinaryWriter(Stream* s)
  120. {
  121. //BinaryWriter takes the ownership of the stream.
  122. m_stream = s;
  123. }
  124. //-----------------------------------------------------------------------------
  125. BinaryWriter::~BinaryWriter()
  126. {
  127. }
  128. //-----------------------------------------------------------------------------
  129. void BinaryWriter::write_byte(int8_t buffer)
  130. {
  131. m_stream->write(&buffer, sizeof(int8_t));
  132. }
  133. //-----------------------------------------------------------------------------
  134. void BinaryWriter::write_int16(int16_t buffer)
  135. {
  136. m_stream->write(&buffer, sizeof(int16_t));
  137. }
  138. //-----------------------------------------------------------------------------
  139. void BinaryWriter::write_uint16(uint16_t buffer)
  140. {
  141. m_stream->write(&buffer, sizeof(uint16_t));
  142. }
  143. //-----------------------------------------------------------------------------
  144. void BinaryWriter::write_int32(int32_t buffer)
  145. {
  146. m_stream->write(&buffer, sizeof(int32_t));
  147. }
  148. //-----------------------------------------------------------------------------
  149. void BinaryWriter::write_uint32(uint32_t buffer)
  150. {
  151. m_stream->write(&buffer, sizeof(uint32_t));
  152. }
  153. //-----------------------------------------------------------------------------
  154. void BinaryWriter::write_int64(int64_t buffer)
  155. {
  156. m_stream->write(&buffer, sizeof(int64_t));
  157. }
  158. //-----------------------------------------------------------------------------
  159. void BinaryWriter::write_double(double buffer)
  160. {
  161. m_stream->write(&buffer, sizeof(double));
  162. }
  163. //-----------------------------------------------------------------------------
  164. void BinaryWriter::write_float(float buffer)
  165. {
  166. m_stream->write(&buffer, sizeof(float));
  167. }
  168. //-----------------------------------------------------------------------------
  169. void BinaryWriter::insert_byte(int8_t val, size_t offset)
  170. {
  171. size_t tmpSize = m_stream->size() - offset;
  172. int8_t* tmp = new int8_t[tmpSize];
  173. m_stream->seek(offset);
  174. m_stream->read(tmp, tmpSize);
  175. m_stream->seek(offset);
  176. m_stream->write_byte(val);
  177. m_stream->write(tmp, tmpSize);
  178. delete[] tmp;
  179. }
  180. //-----------------------------------------------------------------------------
  181. TextReader::TextReader(Stream* s)
  182. {
  183. m_stream = s;
  184. }
  185. //-----------------------------------------------------------------------------
  186. TextReader::~TextReader()
  187. {
  188. }
  189. //-----------------------------------------------------------------------------
  190. char TextReader::read_char()
  191. {
  192. return m_stream->read_byte();
  193. }
  194. //-----------------------------------------------------------------------------
  195. char* TextReader::read_string(char* string, uint32_t count)
  196. {
  197. char currentChar;
  198. int32_t i = 0;
  199. while(!m_stream->end_of_stream() && i < count - 1)
  200. {
  201. currentChar = m_stream->read_byte();
  202. string[i] = currentChar;
  203. i++;
  204. if (currentChar == '\n')
  205. {
  206. break;
  207. }
  208. }
  209. if (i == 0)
  210. {
  211. return NULL;
  212. }
  213. string[i] = '\0';
  214. return string;
  215. }
  216. //-----------------------------------------------------------------------------
  217. TextWriter::TextWriter(Stream* s)
  218. {
  219. m_stream = s;
  220. }
  221. //-----------------------------------------------------------------------------
  222. TextWriter::~TextWriter()
  223. {
  224. }
  225. void TextWriter::write_char(char c)
  226. {
  227. m_stream->write_byte(c);
  228. }
  229. //-----------------------------------------------------------------------------
  230. void TextWriter::write_string(const char* string)
  231. {
  232. size_t count = 0;
  233. while(string[count] != '\0')
  234. {
  235. m_stream->write_byte(string[count]);
  236. count++;
  237. }
  238. }
  239. } // namespace crown