Helper.cpp 4.9 KB

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