Helper.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //#pragma warning(disable : 4996)
  2. #include "Helper.h"
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <esp_log.h>
  8. //#define ISWINDOWS_TRUE
  9. #define PATH_MAX_STRING_SIZE 256
  10. using namespace std;
  11. std::string FormatFileName(std::string input)
  12. {
  13. #ifdef ISWINDOWS_TRUE
  14. input.erase(0, 1);
  15. std::string os = "/";
  16. std::string ns = "\\";
  17. FindReplace(input, os, ns);
  18. #endif
  19. return input;
  20. }
  21. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  22. const size_t oldSize = oldString.length();
  23. // do nothing if line is shorter than the string to find
  24. if (oldSize > line.length()) return;
  25. const size_t newSize = newString.length();
  26. for (size_t pos = 0; ; pos += newSize) {
  27. // Locate the substring to replace
  28. pos = line.find(oldString, pos);
  29. if (pos == std::string::npos) return;
  30. if (oldSize == newSize) {
  31. // if they're same size, use std::string::replace
  32. line.replace(pos, oldSize, newString);
  33. }
  34. else {
  35. // if not same size, replace by erasing and inserting
  36. line.erase(pos, oldSize);
  37. line.insert(pos, newString);
  38. }
  39. }
  40. }
  41. bool ctype_space(const char c, string adddelimiter)
  42. {
  43. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  44. {
  45. return true;
  46. }
  47. if (adddelimiter.find(c) != string::npos)
  48. return true;
  49. return false;
  50. }
  51. string trim(string istring, string adddelimiter)
  52. {
  53. bool trimmed = false;
  54. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  55. {
  56. istring.erase(istring.length() - 1);
  57. trimmed = true;
  58. }
  59. if (ctype_space(istring[0], adddelimiter))
  60. {
  61. istring.erase(0, 1);
  62. trimmed = true;
  63. }
  64. if ((trimmed == false) || (istring.size() == 0))
  65. {
  66. return istring;
  67. }
  68. else
  69. {
  70. return trim(istring, adddelimiter);
  71. }
  72. }
  73. size_t findDelimiterPos(string input, string delimiter)
  74. {
  75. size_t pos = std::string::npos;
  76. size_t zw;
  77. string akt_del;
  78. for (int anz = 0; anz < delimiter.length(); ++anz)
  79. {
  80. akt_del = delimiter[anz];
  81. if ((zw = input.find(akt_del)) != std::string::npos)
  82. {
  83. if (pos != std::string::npos)
  84. {
  85. if (zw < pos)
  86. pos = zw;
  87. }
  88. else
  89. pos = zw;
  90. }
  91. }
  92. return pos;
  93. }
  94. void CopyFile(string input, string output)
  95. {
  96. input = FormatFileName(input);
  97. output = FormatFileName(output);
  98. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  99. {
  100. printf("wlan.ini kann nicht kopiert werden!\n");
  101. return;
  102. }
  103. char cTemp;
  104. FILE* fpSourceFile = fopen(input.c_str(), "rb");
  105. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  106. {
  107. printf("File %s existiert nicht!\n", input.c_str());
  108. return;
  109. }
  110. FILE* fpTargetFile = fopen(output.c_str(), "wb");
  111. // Code Section
  112. // Read From The Source File - "Copy"
  113. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  114. {
  115. // Write To The Target File - "Paste"
  116. fwrite(&cTemp, 1, 1, fpTargetFile);
  117. }
  118. // Close The Files
  119. fclose(fpSourceFile);
  120. fclose(fpTargetFile);
  121. }
  122. string getFileType(string filename)
  123. {
  124. int lastpos = filename.find(".", 0);
  125. int neu_pos;
  126. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  127. {
  128. lastpos = neu_pos;
  129. }
  130. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  131. zw = toUpper(zw);
  132. return zw;
  133. }
  134. /* recursive mkdir */
  135. int mkdir_r(const char *dir, const mode_t mode) {
  136. char tmp[PATH_MAX_STRING_SIZE];
  137. char *p = NULL;
  138. struct stat sb;
  139. size_t len;
  140. /* copy path */
  141. len = strnlen (dir, PATH_MAX_STRING_SIZE);
  142. if (len == 0 || len == PATH_MAX_STRING_SIZE) {
  143. return -1;
  144. }
  145. memcpy (tmp, dir, len);
  146. tmp[len] = '\0';
  147. /* remove trailing slash */
  148. if(tmp[len - 1] == '/') {
  149. tmp[len - 1] = '\0';
  150. }
  151. /* check if path exists and is a directory */
  152. if (stat (tmp, &sb) == 0) {
  153. if (S_ISDIR (sb.st_mode)) {
  154. return 0;
  155. }
  156. }
  157. /* recursive mkdir */
  158. for(p = tmp + 1; *p; p++) {
  159. if(*p == '/') {
  160. *p = 0;
  161. /* test path */
  162. if (stat(tmp, &sb) != 0) {
  163. /* path does not exist - create directory */
  164. if (mkdir(tmp, mode) < 0) {
  165. return -1;
  166. }
  167. } else if (!S_ISDIR(sb.st_mode)) {
  168. /* not a directory */
  169. return -1;
  170. }
  171. *p = '/';
  172. }
  173. }
  174. /* test path */
  175. if (stat(tmp, &sb) != 0) {
  176. /* path does not exist - create directory */
  177. if (mkdir(tmp, mode) < 0) {
  178. return -1;
  179. }
  180. } else if (!S_ISDIR(sb.st_mode)) {
  181. /* not a directory */
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. string toUpper(string in)
  187. {
  188. for (int i = 0; i < in.length(); ++i)
  189. in[i] = toupper(in[i]);
  190. return in;
  191. }
  192. // CPU Temp
  193. extern "C" uint8_t temprature_sens_read();
  194. float temperatureRead()
  195. {
  196. return (temprature_sens_read() - 32) / 1.8;
  197. }
  198. time_t addDays(time_t startTime, int days) {
  199. struct tm* tm = localtime(&startTime);
  200. tm->tm_mday += days;
  201. return mktime(tm);
  202. }
  203. int removeFolder(const char* folderPath, const char* logTag) {
  204. ESP_LOGI(logTag, "Delete folder %s", folderPath);
  205. DIR *dir = opendir(folderPath);
  206. if (!dir) {
  207. ESP_LOGI(logTag, "Failed to stat dir : %s", folderPath);
  208. return -1;
  209. }
  210. struct dirent *entry;
  211. int deleted = 0;
  212. while ((entry = readdir(dir)) != NULL) {
  213. std::string path = string(folderPath) + "/" + entry->d_name;
  214. if (entry->d_type == DT_REG) {
  215. if (unlink(path.c_str()) == 0) {
  216. deleted ++;
  217. } else {
  218. ESP_LOGE(logTag, "can't delete file : %s", path.c_str());
  219. }
  220. } else if (entry->d_type == DT_DIR) {
  221. deleted += removeFolder(path.c_str(), logTag);
  222. }
  223. }
  224. closedir(dir);
  225. if (rmdir(folderPath) != 0) {
  226. ESP_LOGE(logTag, "can't delete file : %s", folderPath);
  227. }
  228. ESP_LOGI(logTag, "%d older log files in folder %s deleted.", deleted, folderPath);
  229. return deleted;
  230. }