io.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* auto generate by HHB_VERSION "1.8.x" */
  20. #include "io.h"
  21. /******************************************************************************
  22. * *
  23. * Utils for process data *
  24. * *
  25. * ***************************************************************************/
  26. /*!
  27. * \brief Get the type of file (JPEG PNG or txt/tensor by suffix
  28. *
  29. * \param filename Image file name.
  30. * \return FILE_PNG FILE_JPEG or FILE_TENSOR of enum file_type
  31. *
  32. */
  33. enum file_type get_file_type(const char* filename) {
  34. enum file_type type = 0;
  35. const char* ptr;
  36. char sep = '.';
  37. uint32_t pos, n;
  38. char buff[32] = {0};
  39. ptr = strrchr(filename, sep);
  40. pos = ptr - filename;
  41. n = strlen(filename) - (pos + 1);
  42. strncpy(buff, filename + (pos + 1), n);
  43. if (strcmp(buff, "jpg") == 0 || strcmp(buff, "jpeg") == 0 || strcmp(buff, "JPG") == 0 ||
  44. strcmp(buff, "JPEG") == 0) {
  45. type = FILE_JPEG;
  46. } else if (strcmp(buff, "png") == 0 || strcmp(buff, "PNG") == 0) {
  47. type = FILE_PNG;
  48. } else if (strcmp(buff, "tensor") == 0) {
  49. type = FILE_TENSOR;
  50. } else if (strcmp(buff, "txt") == 0) {
  51. type = FILE_TXT;
  52. } else if (strcmp(buff, "bin") == 0) {
  53. type = FILE_BIN;
  54. } else if (strcmp(buff, "rgb") == 0) {
  55. type = 0;
  56. } else {
  57. printf("Unsupport for .%s file\n", buff);
  58. exit(1);
  59. }
  60. return type;
  61. }
  62. /*!
  63. * \brief Save float data into file.
  64. *
  65. * \param filename The file that you will put the data into.
  66. * \param data The float data that you will put into file.
  67. * \param size The size of data.
  68. */
  69. void save_data_to_file(const char* filename, float* data, uint32_t size) {
  70. int i = 0;
  71. FILE* fp = fopen(filename, "w+");
  72. for (i = 0; i < size; i++) {
  73. if (i == size - 1) {
  74. fprintf(fp, "%f", data[i]);
  75. } else {
  76. fprintf(fp, "%f\n", data[i]);
  77. }
  78. }
  79. fclose(fp);
  80. }
  81. void save_uint8_to_file(const char* filename, uint8_t* data, uint32_t size) {
  82. int i = 0;
  83. FILE* fp = fopen(filename, "w+");
  84. for (i = 0; i < size; i++) {
  85. if (i == size - 1) {
  86. fprintf(fp, "%d", data[i]);
  87. } else {
  88. fprintf(fp, "%d\n", data[i]);
  89. }
  90. }
  91. fclose(fp);
  92. }
  93. void save_uint8_to_binary(const char* filename, uint8_t* data, uint32_t size) {
  94. int i = 0;
  95. FILE* fp = fopen(filename, "w+");
  96. fwrite(data, 1, size, fp);
  97. fclose(fp);
  98. }
  99. /*!
  100. * \brief Get the binary params char from model.params
  101. *
  102. * \param filename It is generally model.params for anole
  103. * \return The char data of params
  104. *
  105. */
  106. int fill_buffer_from_file(const char* filename, char* buffer) {
  107. int file_size;
  108. int ret;
  109. FILE* fp = fopen(filename, "rb");
  110. if (fp == NULL) {
  111. printf("Invalid input file: %s\n", filename);
  112. return -1;
  113. }
  114. fseek(fp, 0, SEEK_END);
  115. file_size = ftell(fp);
  116. rewind(fp);
  117. ret = fread(buffer, 1, file_size, fp);
  118. if (ret != file_size) {
  119. printf("Read input file error\n");
  120. return -1;
  121. }
  122. fclose(fp);
  123. return 0;
  124. }
  125. char* get_binary_from_file(const char* filename) {
  126. char* buffer = NULL;
  127. int file_size;
  128. int ret;
  129. FILE* fp = fopen(filename, "rb");
  130. if (fp == NULL) {
  131. printf("Invalid input file: %s\n", filename);
  132. return NULL;
  133. }
  134. fseek(fp, 0, SEEK_END);
  135. file_size = ftell(fp);
  136. rewind(fp);
  137. buffer = (char*)malloc(file_size); // NOLINT
  138. if (buffer == NULL) {
  139. printf("Malloc fail\n");
  140. return NULL;
  141. }
  142. ret = fread(buffer, 1, file_size, fp);
  143. if (ret != file_size) {
  144. printf("Read input file error\n");
  145. return NULL;
  146. }
  147. fclose(fp);
  148. return buffer;
  149. }
  150. char** read_string_from_file(const char* filename, int* len) {
  151. char buff[MAX_FILENAME_LEN];
  152. char** result = (char**)malloc(sizeof(char*) * (MAX_FILE_LINE * MAX_INPUT_NUMBER)); // NOLINT
  153. char *find, *sep, *inter;
  154. FILE* fp = fopen(filename, "r+");
  155. if (fp == NULL) {
  156. return NULL;
  157. }
  158. int cnt = 0;
  159. while (fgets(buff, sizeof(buff), fp)) {
  160. find = strchr(buff, '\n');
  161. if (find) {
  162. *find = '\0';
  163. }
  164. sep = strtok(buff, " "); // NOLINT
  165. inter = (char*)malloc((strlen(sep) + 2) * sizeof(char)); // NOLINT
  166. memcpy(inter, sep, strlen(sep) + 1);
  167. result[cnt++] = inter;
  168. while (sep != NULL) {
  169. sep = strtok(NULL, " "); // NOLINT
  170. if (sep) {
  171. inter = (char*)malloc((strlen(sep) + 2) * sizeof(char)); // NOLINT
  172. memcpy(inter, sep, strlen(sep) + 1);
  173. result[cnt++] = inter;
  174. }
  175. }
  176. }
  177. *len = cnt;
  178. fclose(fp);
  179. return result;
  180. }
  181. uint32_t shape2string(uint32_t* shape, uint32_t dim_num, char* buf, uint32_t buf_sz) {
  182. uint32_t s;
  183. uint32_t count;
  184. if (NULL == shape || NULL == buf || dim_num == 0 || buf_sz == 0) {
  185. return 0;
  186. }
  187. count = 0;
  188. for (s = 0; s < dim_num; s++) {
  189. if (count >= buf_sz) {
  190. break;
  191. }
  192. count += snprintf(&buf[count], buf_sz - count, "%d_", shape[s]);
  193. }
  194. buf[count - 1] = 0;
  195. return count;
  196. }