Helper.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //#pragma warning(disable : 4996)
  2. #include "Helper.h"
  3. //#define ISWINDOWS_TRUE
  4. using namespace std;
  5. std::string FormatFileName(std::string input)
  6. {
  7. #ifdef ISWINDOWS_TRUE
  8. input.erase(0, 1);
  9. std::string os = "/";
  10. std::string ns = "\\";
  11. FindReplace(input, os, ns);
  12. #endif
  13. return input;
  14. }
  15. void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
  16. const size_t oldSize = oldString.length();
  17. // do nothing if line is shorter than the string to find
  18. if (oldSize > line.length()) return;
  19. const size_t newSize = newString.length();
  20. for (size_t pos = 0; ; pos += newSize) {
  21. // Locate the substring to replace
  22. pos = line.find(oldString, pos);
  23. if (pos == std::string::npos) return;
  24. if (oldSize == newSize) {
  25. // if they're same size, use std::string::replace
  26. line.replace(pos, oldSize, newString);
  27. }
  28. else {
  29. // if not same size, replace by erasing and inserting
  30. line.erase(pos, oldSize);
  31. line.insert(pos, newString);
  32. }
  33. }
  34. }
  35. bool ctype_space(const char c, string adddelimiter)
  36. {
  37. if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
  38. {
  39. return true;
  40. }
  41. if (adddelimiter.find(c) != string::npos)
  42. return true;
  43. return false;
  44. }
  45. string trim(string istring, string adddelimiter)
  46. {
  47. bool trimmed = false;
  48. if (ctype_space(istring[istring.length() - 1], adddelimiter))
  49. {
  50. istring.erase(istring.length() - 1);
  51. trimmed = true;
  52. }
  53. if (ctype_space(istring[0], adddelimiter))
  54. {
  55. istring.erase(0, 1);
  56. trimmed = true;
  57. }
  58. if ((trimmed == false) || (istring.size() == 0))
  59. {
  60. return istring;
  61. }
  62. else
  63. {
  64. return trim(istring, adddelimiter);
  65. }
  66. }
  67. size_t findDelimiterPos(string input, string delimiter)
  68. {
  69. size_t pos = std::string::npos;
  70. size_t zw;
  71. string akt_del;
  72. for (int anz = 0; anz < delimiter.length(); ++anz)
  73. {
  74. akt_del = delimiter[anz];
  75. if ((zw = input.find(akt_del)) != std::string::npos)
  76. {
  77. if (pos != std::string::npos)
  78. {
  79. if (zw < pos)
  80. pos = zw;
  81. }
  82. else
  83. pos = zw;
  84. }
  85. }
  86. return pos;
  87. }
  88. void CopyFile(string input, string output)
  89. {
  90. input = FormatFileName(input);
  91. output = FormatFileName(output);
  92. if (toUpper(input).compare("/SDCARD/WLAN.INI") == 0)
  93. {
  94. printf("wlan.ini kann nicht kopiert werden!\n");
  95. return;
  96. }
  97. char cTemp;
  98. FILE* fpSourceFile = fopen(input.c_str(), "rb");
  99. if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
  100. {
  101. printf("File %s existiert nicht!\n", input.c_str());
  102. return;
  103. }
  104. FILE* fpTargetFile = fopen(output.c_str(), "wb");
  105. // Code Section
  106. // Read From The Source File - "Copy"
  107. while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
  108. {
  109. // Write To The Target File - "Paste"
  110. fwrite(&cTemp, 1, 1, fpTargetFile);
  111. }
  112. // Close The Files
  113. fclose(fpSourceFile);
  114. fclose(fpTargetFile);
  115. }
  116. string getFileType(string filename)
  117. {
  118. int lastpos = filename.find(".", 0);
  119. int neu_pos;
  120. while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
  121. {
  122. lastpos = neu_pos;
  123. }
  124. string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
  125. zw = toUpper(zw);
  126. return zw;
  127. }
  128. string toUpper(string in)
  129. {
  130. for (int i = 0; i < in.length(); ++i)
  131. in[i] = toupper(in[i]);
  132. return in;
  133. }
  134. // CPU Temp
  135. extern "C" uint8_t temprature_sens_read();
  136. float temperatureRead()
  137. {
  138. return (temprature_sens_read() - 32) / 1.8;
  139. }