io_utils.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifdef linux
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #endif
  5. #ifdef WIN32
  6. #include <windows.h>
  7. #include <io.h>
  8. #endif
  9. #include <iostream>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include "io_utils.h"
  14. #include "model_code.h"
  15. #include "log.h"
  16. namespace ioutil {
  17. int read_file(const char* file_path, unsigned char** dataptr, size_t* sizeptr) {
  18. FILE* fp = NULL;
  19. fp = fopen(file_path, "rb");
  20. if (fp == NULL) {
  21. LOGD("[M]open file(%s) failed", file_path);
  22. return CODE_OPEN_FAILED;
  23. }
  24. fseek(fp, 0, SEEK_END);
  25. *sizeptr = ftell(fp);
  26. *dataptr = (unsigned char*) malloc(sizeof(unsigned char) * (*sizeptr));
  27. fseek(fp, 0, SEEK_SET);
  28. fread(*dataptr, 1, *sizeptr, fp);
  29. fclose(fp);
  30. return CODE_OK;
  31. }
  32. int read_with_pos_and_length(const char* file_path, unsigned char* dataptr, size_t pos, size_t length) {
  33. if (dataptr == NULL) {
  34. LOGD("Read file pos dataptr = NULL");
  35. return CODE_READ_FILE_PTR_IS_NULL;
  36. }
  37. FILE* fp = NULL;
  38. if ((fp = fopen(file_path, "rb")) == NULL) {
  39. LOGD("[M]open file(%s) failed", file_path);
  40. return CODE_OPEN_FAILED;
  41. }
  42. fseek(fp, pos, SEEK_SET);
  43. fread(dataptr, 1, length, fp);
  44. fclose(fp);
  45. return CODE_OK;
  46. }
  47. int read_with_pos(const char* file_path, size_t pos, unsigned char** dataptr, size_t* sizeptr) {
  48. FILE* fp = NULL;
  49. if ((fp = fopen(file_path, "rb")) == NULL) {
  50. LOGD("[M]open file(%s) failed when read_with_pos", file_path);
  51. return CODE_OPEN_FAILED;
  52. }
  53. fseek(fp, 0, SEEK_END);
  54. size_t filesize = ftell(fp);
  55. *sizeptr = filesize - pos;
  56. *dataptr = (unsigned char*) malloc(sizeof(unsigned char) * (filesize - pos));
  57. fseek(fp, pos, SEEK_SET);
  58. fread(*dataptr, 1, filesize - pos, fp);
  59. fclose(fp);
  60. return CODE_OK;
  61. }
  62. int write_file(const char* file_path, const unsigned char* dataptr, size_t sizeptr) {
  63. FILE* fp = NULL;
  64. if ((fp = fopen(file_path, "wb")) == NULL) {
  65. LOGD("[M]open file(%s) failed", file_path);
  66. return CODE_OPEN_FAILED;
  67. }
  68. fwrite(dataptr, 1, sizeptr, fp);
  69. fclose(fp);
  70. return CODE_OK;
  71. }
  72. int append_file(const char* file_path, const unsigned char* data, size_t len) {
  73. FILE* fp = fopen(file_path, "ab+");
  74. if (fp == NULL) {
  75. LOGD("[M]open file(%s) failed when append_file", file_path);
  76. return CODE_OPEN_FAILED;
  77. }
  78. fwrite(data, sizeof(char), len, fp);
  79. fclose(fp);
  80. return CODE_OK;
  81. }
  82. size_t read_file_size(const char* file_path) {
  83. FILE* fp = NULL;
  84. fp = fopen(file_path, "rb");
  85. if (fp == NULL) {
  86. LOGD("[M]open file(%s) failed when read_file_size", file_path);
  87. return 0;
  88. }
  89. fseek(fp, 0, SEEK_END);
  90. size_t filesize = ftell(fp);
  91. fclose(fp);
  92. return filesize;
  93. }
  94. int read_file_to_file(const char* src_path, const char* dst_path) {
  95. FILE* infp = NULL;
  96. if ((infp = fopen(src_path, "rb")) == NULL) {
  97. LOGD("[M]read src file failed when read_file_to_file");
  98. return CODE_OPEN_FAILED;
  99. }
  100. fseek(infp, 0, SEEK_END);
  101. size_t insize = ftell(infp);
  102. char* content = (char*) malloc(sizeof(char) * insize);
  103. fseek(infp, 0, SEEK_SET);
  104. fread(content, 1, insize, infp);
  105. fclose(infp);
  106. FILE* outfp = NULL;
  107. if ((outfp = fopen(dst_path, "wb")) == NULL) {
  108. LOGD("[M]open dst file failed when read_file_to_file");
  109. return CODE_OPEN_FAILED;
  110. }
  111. fwrite(content, 1, insize, outfp);
  112. fclose(outfp);
  113. free(content);
  114. return CODE_OK;
  115. }
  116. int read_dir_files(const char* dir_path, std::vector<std::string>& files) {
  117. #ifdef linux
  118. struct dirent* ptr;
  119. DIR* dir = NULL;
  120. dir = opendir(dir_path);
  121. if (dir == NULL) {
  122. return -1; // CODE_NOT_EXIST_DIR
  123. }
  124. while ((ptr = readdir(dir)) != NULL) {
  125. if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
  126. files.push_back(ptr->d_name);
  127. }
  128. }
  129. closedir(dir);
  130. #endif
  131. #ifdef WIN32
  132. intptr_t handle;
  133. struct _finddata_t fileinfo;
  134. std::string tmp_dir(dir_path);
  135. std::string::size_type idx = tmp_dir.rfind("\\*");
  136. if (idx == std::string::npos || idx != tmp_dir.length() - 1)
  137. {
  138. tmp_dir.append("\\*");
  139. }
  140. handle = _findfirst(tmp_dir.c_str(), &fileinfo);
  141. if (handle == -1) {
  142. return -1;
  143. }
  144. do {
  145. std::cout << "File name = " << fileinfo.name << std::endl;
  146. if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
  147. files.push_back(fileinfo.name);
  148. }
  149. } while (!_findnext(handle, &fileinfo));
  150. std::cout << files.size() << std::endl;
  151. for (size_t i = 0; i < files.size(); i++)
  152. {
  153. std::cout << files[i] << std::endl;
  154. }
  155. _findclose(handle);
  156. #endif
  157. return files.size();
  158. }
  159. int dir_exist_or_mkdir(const char* dir) {
  160. #ifdef WIN32
  161. if (CreateDirectory(dir, NULL)) {
  162. // return CODE_OK;
  163. } else {
  164. return CODE_MKDIR_FAILED;
  165. }
  166. #endif
  167. #ifdef linux
  168. if (access(dir, 0) != 0) {
  169. mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
  170. }
  171. #endif
  172. return CODE_OK;
  173. }
  174. }