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 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if NCNN_STRING
  21. int DataReader::scan(const char* /*format*/, void* /*p*/) const
  22. {
  23. return 0;
  24. }
  25. #endif // NCNN_STRING
  26. size_t DataReader::read(void* /*buf*/, size_t /*size*/) const
  27. {
  28. return 0;
  29. }
  30. #if NCNN_STDIO
  31. DataReaderFromStdio::DataReaderFromStdio(FILE* _fp) : fp(_fp)
  32. {
  33. }
  34. #if NCNN_STRING
  35. int DataReaderFromStdio::scan(const char* format, void* p) const
  36. {
  37. return fscanf(fp, format, p);
  38. }
  39. #endif // NCNN_STRING
  40. size_t DataReaderFromStdio::read(void* buf, size_t size) const
  41. {
  42. return fread(buf, 1, size, fp);
  43. }
  44. #endif // NCNN_STDIO
  45. DataReaderFromMemory::DataReaderFromMemory(const unsigned char*& _mem) : mem(_mem)
  46. {
  47. }
  48. #if NCNN_STRING
  49. int DataReaderFromMemory::scan(const char* format, void* p) const
  50. {
  51. size_t fmtlen = strlen(format);
  52. char* format_with_n = new char[fmtlen + 3];
  53. sprintf(format_with_n, "%s%%n", format);
  54. int nconsumed = 0;
  55. int nscan = sscanf((const char*)mem, format_with_n, p, &nconsumed);
  56. mem += nconsumed;
  57. delete[] format_with_n;
  58. return nconsumed > 0 ? nscan : 0;
  59. }
  60. #endif // NCNN_STRING
  61. size_t DataReaderFromMemory::read(void* buf, size_t size) const
  62. {
  63. memcpy(buf, mem, size);
  64. mem += size;
  65. return size;
  66. }
  67. #if __ANDROID_API__ >= 9
  68. DataReaderFromAndroidAsset::DataReaderFromAndroidAsset(AAsset* _asset) : asset(_asset), mem(0)
  69. {
  70. }
  71. #if NCNN_STRING
  72. int DataReaderFromAndroidAsset::scan(const char* format, void* p) const
  73. {
  74. if (!mem)
  75. {
  76. off_t pos = AAsset_seek(asset, 0, SEEK_CUR);
  77. mem = (const unsigned char*)AAsset_getBuffer(asset);
  78. mem += pos;
  79. }
  80. int fmtlen = strlen(format);
  81. char* format_with_n = new char[fmtlen + 3];
  82. sprintf(format_with_n, "%s%%n", format);
  83. int nconsumed = 0;
  84. int nscan = sscanf((const char*)mem, format_with_n, p, &nconsumed);
  85. mem += nconsumed;
  86. delete[] format_with_n;
  87. if (nconsumed == 0)
  88. return 0;
  89. AAsset_seek(asset, nconsumed, SEEK_CUR);
  90. return nscan;
  91. }
  92. #endif // NCNN_STRING
  93. size_t DataReaderFromAndroidAsset::read(void* buf, size_t size) const
  94. {
  95. int nread = AAsset_read(asset, buf, size);
  96. if (nread < 0)
  97. return 0;
  98. if (mem)
  99. {
  100. mem += nread;
  101. }
  102. return nread;
  103. }
  104. #endif // __ANDROID_API__ >= 9
  105. } // namespace ncnn