CCBundleReader.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifndef __CC_BUNDLE_READER_H__
  22. #define __CC_BUNDLE_READER_H__
  23. #include <string>
  24. #include <vector>
  25. #include "base/CCRef.h"
  26. #include "platform/CCPlatformMacros.h"
  27. #include "base/CCConsole.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup _3d
  31. * @{
  32. */
  33. /**
  34. * @brief BundleReader is an interface for reading sequence of bytes.
  35. * @js NA
  36. * @lua NA
  37. */
  38. class BundleReader: public cocos2d::Ref
  39. {
  40. public:
  41. /**
  42. * Constructor
  43. */
  44. BundleReader();
  45. /**
  46. * Destructor
  47. */
  48. ~BundleReader();
  49. /**
  50. * initialise
  51. * @param buffer The data buffer pointer
  52. * @param length The data buffer size
  53. */
  54. void init(char* buffer, ssize_t length);
  55. /**
  56. * Reads an array of elements.
  57. *
  58. * @param ptr The pointer to the memory to copy into.
  59. * The available size should be at least bytes.
  60. * @param size The size of each element to be read, in bytes.
  61. * @param count The number of elements to read.
  62. *
  63. * @return The number of elements read.
  64. */
  65. ssize_t read(void* ptr, ssize_t size, ssize_t count);
  66. /**
  67. * Reads a line from the buffer.
  68. */
  69. char* readLine(int num, char* line);
  70. /**
  71. * Returns true if the end of the buffer has been reached.
  72. */
  73. bool eof();
  74. /**
  75. * Returns the length of the buffer in bytes.
  76. */
  77. ssize_t length();
  78. /**
  79. * Returns the position of the file pointer.
  80. */
  81. ssize_t tell();
  82. /**
  83. * Sets the position of the file pointer.
  84. */
  85. bool seek(long int offset, int origin);
  86. /**
  87. * Sets the file pointer at the start of the file.
  88. */
  89. bool rewind();
  90. /**
  91. * read binary typed value.
  92. */
  93. template<typename T> bool read(T* ptr);
  94. template<typename T> bool readArray(unsigned int* length, std::vector<T>* values);
  95. /**
  96. * first read length, then read string text
  97. */
  98. std::string readString();
  99. /**
  100. * Read the matrix.
  101. * @note the matrix type must be the 4*4 float matrix
  102. */
  103. bool readMatrix(float* m);
  104. private:
  105. ssize_t _position;
  106. ssize_t _length;
  107. char* _buffer;
  108. };
  109. /// @cond
  110. /**
  111. * template read routines
  112. */
  113. template<typename T>
  114. inline bool BundleReader::read(T *ptr)
  115. {
  116. return (read(ptr, sizeof(T), 1) == 1);
  117. }
  118. /**
  119. * template function to read array of value.
  120. */
  121. template<typename T>
  122. inline bool BundleReader::readArray(unsigned int *length, std::vector<T> *values)
  123. {
  124. if (!read(length))
  125. {
  126. return false;
  127. }
  128. if (*length > 0 && values)
  129. {
  130. values->resize(*length);
  131. if (read(&(*values)[0], sizeof(T), *length) != *length)
  132. {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. /**
  139. * specialization for char
  140. */
  141. template<>
  142. inline bool BundleReader::read<char>(char *ptr)
  143. {
  144. if (read(ptr, sizeof(char), 1) == 1)
  145. {
  146. return true;
  147. }
  148. else
  149. {
  150. *ptr = -1;
  151. return false;
  152. }
  153. }
  154. /**
  155. * specialization for std::string
  156. */
  157. template<>
  158. inline bool BundleReader::read<std::string>(std::string* /*ptr*/)
  159. {
  160. CCLOG("can not read std::string, use readString() instead");
  161. return false;
  162. }
  163. /**
  164. * template function to read array of value.
  165. */
  166. template<>
  167. inline bool BundleReader::readArray<std::string>(unsigned int *length, std::vector<std::string> *values)
  168. {
  169. if (!read(length))
  170. {
  171. return false;
  172. }
  173. values->clear();
  174. if (*length > 0 && values)
  175. {
  176. for (int i = 0; i < (int)*length; ++i)
  177. {
  178. values->push_back(readString());
  179. }
  180. }
  181. return true;
  182. }
  183. /// @endcond
  184. // end of 3d group
  185. /// @}
  186. NS_CC_END
  187. #endif