You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

datareader.cpp 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "datareader.h"
  15. #include <string.h>
  16. namespace ncnn {
  17. DataReader::DataReader()
  18. {
  19. }
  20. DataReader::~DataReader()
  21. {
  22. }
  23. #if NCNN_STRING
  24. int DataReader::scan(const char* /*format*/, void* /*p*/) const
  25. {
  26. return 0;
  27. }
  28. #endif // NCNN_STRING
  29. size_t DataReader::read(void* /*buf*/, size_t /*size*/) const
  30. {
  31. return 0;
  32. }
  33. size_t DataReader::reference(size_t /*size*/, const void** /*buf*/) const
  34. {
  35. return 0;
  36. }
  37. #if NCNN_STDIO
  38. class DataReaderFromStdioPrivate
  39. {
  40. public:
  41. DataReaderFromStdioPrivate(FILE* _fp)
  42. : fp(_fp)
  43. {
  44. }
  45. FILE* fp;
  46. };
  47. DataReaderFromStdio::DataReaderFromStdio(FILE* _fp)
  48. : DataReader(), d(new DataReaderFromStdioPrivate(_fp))
  49. {
  50. }
  51. DataReaderFromStdio::~DataReaderFromStdio()
  52. {
  53. delete d;
  54. }
  55. DataReaderFromStdio::DataReaderFromStdio(const DataReaderFromStdio&)
  56. : d(0)
  57. {
  58. }
  59. DataReaderFromStdio& DataReaderFromStdio::operator=(const DataReaderFromStdio&)
  60. {
  61. return *this;
  62. }
  63. #if NCNN_STRING
  64. int DataReaderFromStdio::scan(const char* format, void* p) const
  65. {
  66. return fscanf(d->fp, format, p);
  67. }
  68. #endif // NCNN_STRING
  69. size_t DataReaderFromStdio::read(void* buf, size_t size) const
  70. {
  71. return fread(buf, 1, size, d->fp);
  72. }
  73. #endif // NCNN_STDIO
  74. class DataReaderFromMemoryPrivate
  75. {
  76. public:
  77. DataReaderFromMemoryPrivate(const unsigned char*& _mem)
  78. : mem(_mem)
  79. {
  80. }
  81. const unsigned char*& mem;
  82. };
  83. DataReaderFromMemory::DataReaderFromMemory(const unsigned char*& _mem)
  84. : DataReader(), d(new DataReaderFromMemoryPrivate(_mem))
  85. {
  86. }
  87. DataReaderFromMemory::~DataReaderFromMemory()
  88. {
  89. delete d;
  90. }
  91. DataReaderFromMemory::DataReaderFromMemory(const DataReaderFromMemory&)
  92. : d(0)
  93. {
  94. }
  95. DataReaderFromMemory& DataReaderFromMemory::operator=(const DataReaderFromMemory&)
  96. {
  97. return *this;
  98. }
  99. #if NCNN_STRING
  100. int DataReaderFromMemory::scan(const char* format, void* p) const
  101. {
  102. size_t fmtlen = strlen(format);
  103. char* format_with_n = new char[fmtlen + 4];
  104. sprintf(format_with_n, "%s%%n", format);
  105. int nconsumed = 0;
  106. int nscan = sscanf((const char*)d->mem, format_with_n, p, &nconsumed);
  107. d->mem += nconsumed;
  108. delete[] format_with_n;
  109. return nconsumed > 0 ? nscan : 0;
  110. }
  111. #endif // NCNN_STRING
  112. size_t DataReaderFromMemory::read(void* buf, size_t size) const
  113. {
  114. memcpy(buf, d->mem, size);
  115. d->mem += size;
  116. return size;
  117. }
  118. size_t DataReaderFromMemory::reference(size_t size, const void** buf) const
  119. {
  120. *buf = d->mem;
  121. d->mem += size;
  122. return size;
  123. }
  124. #if NCNN_PLATFORM_API
  125. #if __ANDROID_API__ >= 9
  126. class DataReaderFromAndroidAssetPrivate
  127. {
  128. public:
  129. DataReaderFromAndroidAssetPrivate(AAsset* _asset)
  130. : asset(_asset), mem(0)
  131. {
  132. }
  133. AAsset* asset;
  134. mutable const unsigned char* mem;
  135. };
  136. DataReaderFromAndroidAsset::DataReaderFromAndroidAsset(AAsset* _asset)
  137. : DataReader(), d(new DataReaderFromAndroidAssetPrivate(_asset))
  138. {
  139. }
  140. DataReaderFromAndroidAsset::~DataReaderFromAndroidAsset()
  141. {
  142. delete d;
  143. }
  144. DataReaderFromAndroidAsset::DataReaderFromAndroidAsset(const DataReaderFromAndroidAsset&)
  145. : d(0)
  146. {
  147. }
  148. DataReaderFromAndroidAsset& DataReaderFromAndroidAsset::operator=(const DataReaderFromAndroidAsset&)
  149. {
  150. return *this;
  151. }
  152. #if NCNN_STRING
  153. int DataReaderFromAndroidAsset::scan(const char* format, void* p) const
  154. {
  155. if (!d->mem)
  156. {
  157. off_t pos = AAsset_seek(d->asset, 0, SEEK_CUR);
  158. d->mem = (const unsigned char*)AAsset_getBuffer(d->asset);
  159. d->mem += pos;
  160. }
  161. int fmtlen = strlen(format);
  162. char* format_with_n = new char[fmtlen + 3];
  163. sprintf(format_with_n, "%s%%n", format);
  164. int nconsumed = 0;
  165. int nscan = sscanf((const char*)d->mem, format_with_n, p, &nconsumed);
  166. d->mem += nconsumed;
  167. delete[] format_with_n;
  168. if (nconsumed == 0)
  169. return 0;
  170. AAsset_seek(d->asset, nconsumed, SEEK_CUR);
  171. return nscan;
  172. }
  173. #endif // NCNN_STRING
  174. size_t DataReaderFromAndroidAsset::read(void* buf, size_t size) const
  175. {
  176. int nread = AAsset_read(d->asset, buf, size);
  177. if (nread < 0)
  178. return 0;
  179. if (d->mem)
  180. {
  181. d->mem += nread;
  182. }
  183. return nread;
  184. }
  185. #endif // __ANDROID_API__ >= 9
  186. #endif // NCNN_PLATFORM_API
  187. } // namespace ncnn