BsDataStream.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "BsDataStream.h"
  2. #include "BsDebug.h"
  3. #include "BsException.h"
  4. #include <codecvt>
  5. namespace BansheeEngine
  6. {
  7. const UINT32 DataStream::StreamTempSize = 128;
  8. /**
  9. * @brief Checks does the provided buffer has an UTF32 byte order mark
  10. * in little endian order.
  11. */
  12. bool isUTF32LE(const UINT8* buffer)
  13. {
  14. return buffer[0] == 0xFF && buffer[1] == 0xFE && buffer[2] == 0x00 && buffer[3] == 0x00;
  15. }
  16. /**
  17. * @brief Checks does the provided buffer has an UTF32 byte order mark
  18. * in big endian order.
  19. */
  20. bool isUTF32BE(const UINT8* buffer)
  21. {
  22. return buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF;
  23. }
  24. /**
  25. * @brief Checks does the provided buffer has an UTF16 byte order mark
  26. * in little endian order.
  27. */
  28. bool isUTF16LE(const UINT8* buffer)
  29. {
  30. return buffer[0] == 0xFF && buffer[1] == 0xFE;
  31. }
  32. /**
  33. * @brief Checks does the provided buffer has an UTF16 byte order mark
  34. * in big endian order.
  35. */
  36. bool isUTF16BE(const UINT8* buffer)
  37. {
  38. return buffer[0] == 0xFE && buffer[1] == 0xFF;
  39. }
  40. /**
  41. * @brief Checks does the provided buffer has an UTF8 byte order mark.
  42. */
  43. bool isUTF8(const UINT8* buffer)
  44. {
  45. return (buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF);
  46. }
  47. template <typename T> DataStream& DataStream::operator>> (T& val)
  48. {
  49. read(static_cast<void*>(&val), sizeof(T));
  50. return *this;
  51. }
  52. String DataStream::getAsString()
  53. {
  54. // Read the entire buffer - ideally in one read, but if the size of
  55. // the buffer is unknown, do multiple fixed size reads.
  56. size_t bufSize = (mSize > 0 ? mSize : 4096);
  57. std::stringstream::char_type* tempBuffer = (std::stringstream::char_type*)bs_alloc((UINT32)bufSize);
  58. // Ensure read from begin of stream
  59. seek(0);
  60. std::stringstream result;
  61. while (!eof())
  62. {
  63. size_t numReadBytes = read(tempBuffer, bufSize);
  64. result.write(tempBuffer, numReadBytes);
  65. }
  66. free(tempBuffer);
  67. std::string string = result.str();
  68. UINT32 readBytes = (UINT32)string.size();
  69. if (readBytes >= 4)
  70. {
  71. if (isUTF32LE((UINT8*)string.data()))
  72. {
  73. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header | std::little_endian);
  74. typedef std::codecvt_utf8<char32_t, 1114111, convMode> utf8utf32;
  75. std::wstring_convert<utf8utf32, char32_t> conversion("?");
  76. char32_t* start = (char32_t*)string.data();
  77. char32_t* end = (start + (string.size() - 1) / 4);
  78. return conversion.to_bytes(start, end).c_str();
  79. }
  80. else if (isUTF32BE((UINT8*)string.data()))
  81. {
  82. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header);
  83. typedef std::codecvt_utf8<char32_t, 1114111, convMode> utf8utf32;
  84. std::wstring_convert<utf8utf32, char32_t> conversion("?");
  85. char32_t* start = (char32_t*)string.data();
  86. char32_t* end = (start + (string.size() - 1) / 4);
  87. return conversion.to_bytes(start, end).c_str();
  88. }
  89. }
  90. if (readBytes >= 3)
  91. {
  92. if (isUTF8((UINT8*)string.data()))
  93. {
  94. return string.c_str() + 3;
  95. }
  96. }
  97. if (readBytes >= 2)
  98. {
  99. if (isUTF16LE((UINT8*)string.data()))
  100. {
  101. const std::codecvt_mode convMode = (std::codecvt_mode)(std::little_endian);
  102. typedef std::codecvt_utf8<char16_t, 1114111, convMode> utf8utf16;
  103. std::wstring_convert<utf8utf16, char16_t> conversion("?");
  104. char16_t* start = (char16_t*)(string.data() + 2); // Bug?: std::consume_header seems to be ignored so I manually remove the header
  105. char16_t* end = (start + (string.size() - 1) / 2);
  106. return conversion.to_bytes(start, end).c_str();
  107. }
  108. else if (isUTF16BE((UINT8*)string.data()))
  109. {
  110. const std::codecvt_mode convMode = (std::codecvt_mode)(0);
  111. typedef std::codecvt_utf8<char16_t, 1114111, convMode> utf8utf16;
  112. // Bug?: Regardless me of not providing the std::little_endian flag it seems that is how the data is read
  113. // so I manually flip it
  114. UINT32 numChars = (UINT32)(string.size() - 2) / 2;
  115. for (UINT32 i = 0; i < numChars; i++)
  116. std::swap(string[i * 2 + 0], string[i * 2 + 1]);
  117. std::wstring_convert<utf8utf16, char16_t> conversion("?");
  118. char16_t* start = (char16_t*)(string.data() + 2); // Bug?: std::consume_header seems to be ignored so I manually remove the header
  119. char16_t* end = (start + (string.size() - 1) / 2);
  120. return conversion.to_bytes(start, end).c_str();
  121. }
  122. }
  123. return string.c_str();
  124. }
  125. WString DataStream::getAsWString()
  126. {
  127. // Read the entire buffer - ideally in one read, but if the size of
  128. // the buffer is unknown, do multiple fixed size reads.
  129. size_t bufSize = (mSize > 0 ? mSize : 4096);
  130. std::stringstream::char_type* tempBuffer = (std::stringstream::char_type*)bs_alloc((UINT32)bufSize);
  131. // Ensure read from begin of stream
  132. seek(0);
  133. std::stringstream result;
  134. while (!eof())
  135. {
  136. size_t numReadBytes = read(tempBuffer, bufSize);
  137. result.write(tempBuffer, numReadBytes);
  138. }
  139. free(tempBuffer);
  140. std::string string = result.str();
  141. UINT32 readBytes = (UINT32)string.size();
  142. if (readBytes >= 4)
  143. {
  144. if (isUTF32LE((UINT8*)string.data()))
  145. {
  146. // Not supported
  147. }
  148. else if (isUTF32BE((UINT8*)string.data()))
  149. {
  150. // Not supported
  151. }
  152. }
  153. if (readBytes >= 3)
  154. {
  155. if (isUTF8((UINT8*)string.data()))
  156. {
  157. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header);
  158. typedef std::codecvt_utf8<wchar_t, 1114111, convMode> wcharutf8;
  159. std::wstring_convert<wcharutf8> conversion("?");
  160. return conversion.from_bytes(string).c_str();
  161. }
  162. }
  163. if (readBytes >= 2)
  164. {
  165. if (isUTF16LE((UINT8*)string.data()))
  166. {
  167. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header | std::little_endian);
  168. typedef std::codecvt_utf16 <wchar_t, 1114111, convMode> wcharutf16;
  169. std::wstring_convert<wcharutf16> conversion("?");
  170. return conversion.from_bytes(string).c_str();
  171. }
  172. else if (isUTF16BE((UINT8*)string.data()))
  173. {
  174. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header);
  175. typedef std::codecvt_utf16<wchar_t, 1114111, convMode> wcharutf16;
  176. std::wstring_convert<wcharutf16> conversion("?");
  177. return conversion.from_bytes(string).c_str();
  178. }
  179. }
  180. {
  181. const std::codecvt_mode convMode = (std::codecvt_mode)(std::consume_header);
  182. typedef std::codecvt_utf8<wchar_t, 1114111, convMode> wcharutf8;
  183. std::wstring_convert<wcharutf8> conversion("?");
  184. return conversion.from_bytes(string).c_str();
  185. }
  186. }
  187. MemoryDataStream::MemoryDataStream(void* memory, size_t inSize)
  188. : DataStream(READ | WRITE), mData(nullptr)
  189. {
  190. mData = mPos = static_cast<UINT8*>(memory);
  191. mSize = inSize;
  192. mEnd = mData + mSize;
  193. assert(mEnd >= mPos);
  194. }
  195. MemoryDataStream::MemoryDataStream(DataStream& sourceStream)
  196. : DataStream(READ | WRITE), mData(nullptr)
  197. {
  198. // Copy data from incoming stream
  199. mSize = sourceStream.size();
  200. mData = (UINT8*)bs_alloc((UINT32)mSize);
  201. mPos = mData;
  202. mEnd = mData + sourceStream.read(mData, mSize);
  203. assert(mEnd >= mPos);
  204. }
  205. MemoryDataStream::MemoryDataStream(const DataStreamPtr& sourceStream)
  206. :DataStream(READ | WRITE), mData(nullptr)
  207. {
  208. // Copy data from incoming stream
  209. mSize = sourceStream->size();
  210. mData = (UINT8*)bs_alloc((UINT32)mSize);
  211. mPos = mData;
  212. mEnd = mData + sourceStream->read(mData, mSize);
  213. assert(mEnd >= mPos);
  214. }
  215. MemoryDataStream::~MemoryDataStream()
  216. {
  217. close();
  218. }
  219. size_t MemoryDataStream::read(void* buf, size_t count)
  220. {
  221. size_t cnt = count;
  222. if (mPos + cnt > mEnd)
  223. cnt = mEnd - mPos;
  224. if (cnt == 0)
  225. return 0;
  226. assert (cnt <= count);
  227. memcpy(buf, mPos, cnt);
  228. mPos += cnt;
  229. return cnt;
  230. }
  231. size_t MemoryDataStream::write(const void* buf, size_t count)
  232. {
  233. size_t written = 0;
  234. if (isWriteable())
  235. {
  236. written = count;
  237. if (mPos + written > mEnd)
  238. written = mEnd - mPos;
  239. if (written == 0)
  240. return 0;
  241. memcpy(mPos, buf, written);
  242. mPos += written;
  243. }
  244. return written;
  245. }
  246. void MemoryDataStream::skip(size_t count)
  247. {
  248. size_t newpos = (size_t)( (mPos - mData) + count );
  249. assert(mData + newpos <= mEnd);
  250. mPos = mData + newpos;
  251. }
  252. void MemoryDataStream::seek(size_t pos)
  253. {
  254. assert(mData + pos <= mEnd);
  255. mPos = mData + pos;
  256. }
  257. size_t MemoryDataStream::tell() const
  258. {
  259. return mPos - mData;
  260. }
  261. bool MemoryDataStream::eof() const
  262. {
  263. return mPos >= mEnd;
  264. }
  265. void MemoryDataStream::close()
  266. {
  267. if (mData != nullptr)
  268. {
  269. bs_free(mData);
  270. mData = nullptr;
  271. }
  272. }
  273. FileDataStream::FileDataStream(std::shared_ptr<std::ifstream> s, bool freeOnClose)
  274. : DataStream(READ), mpInStream(s), mpFStreamRO(s), mpFStream(0), mFreeOnClose(freeOnClose)
  275. {
  276. mpInStream->seekg(0, std::ios_base::end);
  277. mSize = (size_t)mpInStream->tellg();
  278. mpInStream->seekg(0, std::ios_base::beg);
  279. determineAccess();
  280. }
  281. FileDataStream::FileDataStream(std::shared_ptr<std::ifstream> s, size_t inSize, bool freeOnClose)
  282. : DataStream(READ), mpInStream(s), mpFStreamRO(s), mpFStream(0), mFreeOnClose(freeOnClose)
  283. {
  284. mSize = inSize;
  285. determineAccess();
  286. }
  287. FileDataStream::FileDataStream(std::shared_ptr<std::fstream> s, bool freeOnClose)
  288. : DataStream(READ | WRITE), mpInStream(s), mpFStreamRO(0), mpFStream(s), mFreeOnClose(freeOnClose)
  289. {
  290. mpInStream->seekg(0, std::ios_base::end);
  291. mSize = (size_t)mpInStream->tellg();
  292. mpInStream->seekg(0, std::ios_base::beg);
  293. determineAccess();
  294. }
  295. FileDataStream::FileDataStream(std::shared_ptr<std::fstream> s, size_t inSize, bool freeOnClose)
  296. : DataStream(READ | WRITE), mpInStream(s), mpFStreamRO(0), mpFStream(s), mFreeOnClose(freeOnClose)
  297. {
  298. mSize = inSize;
  299. determineAccess();
  300. }
  301. void FileDataStream::determineAccess()
  302. {
  303. mAccess = 0;
  304. if (mpInStream)
  305. mAccess |= READ;
  306. if (mpFStream)
  307. mAccess |= WRITE;
  308. }
  309. FileDataStream::~FileDataStream()
  310. {
  311. close();
  312. }
  313. size_t FileDataStream::read(void* buf, size_t count)
  314. {
  315. mpInStream->read(static_cast<char*>(buf), static_cast<std::streamsize>(count));
  316. return (size_t)mpInStream->gcount();
  317. }
  318. size_t FileDataStream::write(const void* buf, size_t count)
  319. {
  320. size_t written = 0;
  321. if (isWriteable() && mpFStream)
  322. {
  323. mpFStream->write(static_cast<const char*>(buf), static_cast<std::streamsize>(count));
  324. written = count;
  325. }
  326. return written;
  327. }
  328. void FileDataStream::skip(size_t count)
  329. {
  330. mpInStream->clear(); // Clear fail status in case eof was set
  331. mpInStream->seekg(static_cast<std::ifstream::pos_type>(count), std::ios::cur);
  332. }
  333. void FileDataStream::seek(size_t pos)
  334. {
  335. mpInStream->clear(); // Clear fail status in case eof was set
  336. mpInStream->seekg(static_cast<std::streamoff>(pos), std::ios::beg);
  337. }
  338. size_t FileDataStream::tell() const
  339. {
  340. mpInStream->clear(); // Clear fail status in case eof was set
  341. return (size_t)mpInStream->tellg();
  342. }
  343. bool FileDataStream::eof() const
  344. {
  345. return mpInStream->eof();
  346. }
  347. void FileDataStream::close()
  348. {
  349. if (mpInStream)
  350. {
  351. if (mpFStreamRO)
  352. mpFStreamRO->close();
  353. if (mpFStream)
  354. {
  355. mpFStream->flush();
  356. mpFStream->close();
  357. }
  358. if (mFreeOnClose)
  359. {
  360. mpInStream = nullptr;
  361. mpFStreamRO = nullptr;
  362. mpFStream = nullptr;
  363. }
  364. }
  365. }
  366. }