CCBundleReader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "3d/CCBundleReader.h"
  22. #include "platform/CCFileUtils.h"
  23. NS_CC_BEGIN
  24. BundleReader::BundleReader()
  25. {
  26. _buffer = nullptr;
  27. _position = 0;
  28. _length = 0;
  29. };
  30. BundleReader::~BundleReader()
  31. {
  32. };
  33. void BundleReader::init(char* buffer, ssize_t length)
  34. {
  35. _position = 0;
  36. _buffer = buffer;
  37. _length = length;
  38. }
  39. ssize_t BundleReader::read(void* ptr, ssize_t size, ssize_t count)
  40. {
  41. if (!_buffer || eof())
  42. {
  43. CCLOG("warning: bundle reader out of range");
  44. return 0;
  45. }
  46. ssize_t validCount;
  47. ssize_t validLength = _length - _position;
  48. ssize_t needLength = size*count;
  49. char* ptr1 = (char*)ptr;
  50. if(validLength < needLength)
  51. {
  52. validCount = validLength/size;
  53. ssize_t readLength = size*validCount;
  54. memcpy(ptr1,(char*)_buffer+_position,readLength);
  55. ptr1 += readLength;
  56. _position += readLength;
  57. readLength = validLength - readLength;
  58. if(readLength>0)
  59. {
  60. memcpy(ptr1,(char*)_buffer+_position,readLength);
  61. _position += readLength;
  62. validCount+=1;
  63. }
  64. CCLOG("warning: bundle reader out of range");
  65. }
  66. else
  67. {
  68. memcpy(ptr1,(char*)_buffer+_position,needLength);
  69. _position += needLength;
  70. validCount = count;
  71. }
  72. return validCount;
  73. }
  74. char* BundleReader::readLine(int num,char* line)
  75. {
  76. if (!_buffer)
  77. return nullptr;
  78. char* buffer = (char*)_buffer+_position;
  79. char* p = line;
  80. char c;
  81. ssize_t readNum = 0;
  82. while((c=*buffer) != 10 && readNum < (ssize_t)num && _position < _length)
  83. {
  84. *p = c;
  85. p++;
  86. buffer++;
  87. _position++;
  88. readNum++;
  89. }
  90. *p = '\0';
  91. return line;
  92. }
  93. bool BundleReader::eof()
  94. {
  95. if (!_buffer)
  96. return true;
  97. return ((ssize_t)tell()) >= length();
  98. }
  99. ssize_t BundleReader::length()
  100. {
  101. return _length;
  102. }
  103. ssize_t BundleReader::tell()
  104. {
  105. if (!_buffer)
  106. return -1;
  107. return _position;
  108. }
  109. bool BundleReader::seek(long int offset, int origin)
  110. {
  111. if (!_buffer)
  112. return false;
  113. if(origin == SEEK_CUR)
  114. {
  115. _position += offset;
  116. }
  117. else if(origin == SEEK_SET)
  118. {
  119. _position = offset;
  120. }
  121. else if(origin == SEEK_END)
  122. {
  123. _position = _length+offset;
  124. }
  125. else
  126. return false;
  127. return true;
  128. }
  129. bool BundleReader::rewind()
  130. {
  131. if (_buffer != nullptr)
  132. {
  133. _position = 0;
  134. return true;
  135. }
  136. return false;
  137. }
  138. std::string BundleReader::readString()
  139. {
  140. unsigned int length;
  141. if(read(&length, 4, 1) != 1)
  142. {
  143. return std::string();
  144. }
  145. std::string str;
  146. ssize_t validLength = _length - _position;
  147. if (length > 0 && static_cast<ssize_t>(length) <= validLength)
  148. {
  149. str.resize(length);
  150. if (read(&str[0], 1, length) != length)
  151. {
  152. return std::string();
  153. }
  154. }
  155. return str;
  156. }
  157. bool BundleReader::readMatrix(float* m)
  158. {
  159. return (read(m, sizeof(float), 16) == 16);
  160. }
  161. NS_CC_END