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

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