read_wlanini.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "read_wlanini.h"
  2. #include "Helper.h"
  3. #include "connect_wlan.h"
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8. #include <iostream>
  9. #include <string.h>
  10. std::vector<string> ZerlegeZeileWLAN(std::string input, std::string _delimiter = "")
  11. {
  12. std::vector<string> Output;
  13. std::string delimiter = " =,";
  14. if (_delimiter.length() > 0){
  15. delimiter = _delimiter;
  16. }
  17. input = trim(input, delimiter);
  18. size_t pos = findDelimiterPos(input, delimiter);
  19. std::string token;
  20. if (pos != std::string::npos) // Zerlegt nur bis ersten Gleichheitszeichen !!! Sonderfall für WLAN.ini
  21. {
  22. token = input.substr(0, pos);
  23. token = trim(token, delimiter);
  24. Output.push_back(token);
  25. input.erase(0, pos + 1);
  26. input = trim(input, delimiter);
  27. }
  28. Output.push_back(input);
  29. return Output;
  30. }
  31. void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns)
  32. {
  33. std::string ssid = "";
  34. std::string passphrase = "";
  35. std::string ipaddress = "";
  36. std::string gw = "";
  37. std::string netmask = "";
  38. std::string dns = "";
  39. std::string line = "";
  40. std::vector<string> zerlegt;
  41. hostname = std_hostname;
  42. FILE* pFile;
  43. fn = FormatFileName(fn);
  44. pFile = OpenFileAndWait(fn.c_str(), "r");
  45. printf("file loaded\n");
  46. if (pFile == NULL)
  47. return;
  48. char zw[1024];
  49. fgets(zw, 1024, pFile);
  50. line = std::string(zw);
  51. while ((line.size() > 0) || !(feof(pFile)))
  52. {
  53. // printf("%s", line.c_str());
  54. zerlegt = ZerlegeZeileWLAN(line, "=");
  55. zerlegt[0] = trim(zerlegt[0], " ");
  56. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  57. hostname = trim(zerlegt[1]);
  58. if ((hostname[0] == '"') && (hostname[hostname.length()-1] == '"')){
  59. hostname = hostname.substr(1, hostname.length()-2);
  60. }
  61. }
  62. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID")){
  63. ssid = trim(zerlegt[1]);
  64. if ((ssid[0] == '"') && (ssid[ssid.length()-1] == '"')){
  65. ssid = ssid.substr(1, ssid.length()-2);
  66. }
  67. }
  68. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD")){
  69. passphrase = zerlegt[1];
  70. if ((passphrase[0] == '"') && (passphrase[passphrase.length()-1] == '"')){
  71. passphrase = passphrase.substr(1, passphrase.length()-2);
  72. }
  73. }
  74. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "IP")){
  75. ipaddress = zerlegt[1];
  76. if ((ipaddress[0] == '"') && (ipaddress[ipaddress.length()-1] == '"')){
  77. ipaddress = ipaddress.substr(1, ipaddress.length()-2);
  78. }
  79. }
  80. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "GATEWAY")){
  81. gw = zerlegt[1];
  82. if ((gw[0] == '"') && (gw[gw.length()-1] == '"')){
  83. gw = gw.substr(1, gw.length()-2);
  84. }
  85. }
  86. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "NETMASK")){
  87. netmask = zerlegt[1];
  88. if ((netmask[0] == '"') && (netmask[netmask.length()-1] == '"')){
  89. netmask = netmask.substr(1, netmask.length()-2);
  90. }
  91. }
  92. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "DNS")){
  93. dns = zerlegt[1];
  94. if ((dns[0] == '"') && (dns[dns.length()-1] == '"')){
  95. dns = dns.substr(1, dns.length()-2);
  96. }
  97. }
  98. if (fgets(zw, 1024, pFile) == NULL)
  99. {
  100. line = "";
  101. }
  102. else
  103. {
  104. line = std::string(zw);
  105. }
  106. }
  107. fclose(pFile);
  108. // Check if Hostname was empty in .ini if yes set to std_hostname
  109. if(hostname.length() == 0){
  110. hostname = std_hostname;
  111. }
  112. _hostname = new char[hostname.length() + 1];
  113. strcpy(_hostname, hostname.c_str());
  114. _ssid = new char[ssid.length() + 1];
  115. strcpy(_ssid, ssid.c_str());
  116. _password = new char[passphrase.length() + 1];
  117. strcpy(_password, passphrase.c_str());
  118. if (ipaddress.length() > 0)
  119. {
  120. _ipadr = new char[ipaddress.length() + 1];
  121. strcpy(_ipadr, ipaddress.c_str());
  122. }
  123. else
  124. _ipadr = NULL;
  125. if (gw.length() > 0)
  126. {
  127. _gw = new char[gw.length() + 1];
  128. strcpy(_gw, gw.c_str());
  129. }
  130. else
  131. _gw = NULL;
  132. if (netmask.length() > 0)
  133. {
  134. _netmask = new char[netmask.length() + 1];
  135. strcpy(_netmask, netmask.c_str());
  136. }
  137. else
  138. _netmask = NULL;
  139. if (dns.length() > 0)
  140. {
  141. _dns = new char[dns.length() + 1];
  142. strcpy(_dns, dns.c_str());
  143. }
  144. else
  145. _dns = NULL;
  146. }
  147. bool ChangeHostName(std::string fn, std::string _newhostname)
  148. {
  149. if (_newhostname == hostname)
  150. return false;
  151. string line = "";
  152. std::vector<string> zerlegt;
  153. bool found = false;
  154. std::vector<string> neuesfile;
  155. FILE* pFile;
  156. fn = FormatFileName(fn);
  157. pFile = OpenFileAndWait(fn.c_str(), "r");
  158. printf("file loaded\n");
  159. if (pFile == NULL)
  160. return false;
  161. char zw[1024];
  162. fgets(zw, 1024, pFile);
  163. line = std::string(zw);
  164. while ((line.size() > 0) || !(feof(pFile)))
  165. {
  166. printf("%s", line.c_str());
  167. zerlegt = ZerlegeZeileWLAN(line, "=");
  168. zerlegt[0] = trim(zerlegt[0], " ");
  169. if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "HOSTNAME")){
  170. line = "hostname = \"" + _newhostname + "\"\n";
  171. found = true;
  172. }
  173. neuesfile.push_back(line);
  174. if (fgets(zw, 1024, pFile) == NULL)
  175. {
  176. line = "";
  177. }
  178. else
  179. {
  180. line = std::string(zw);
  181. }
  182. }
  183. if (!found)
  184. {
  185. line = "\nhostname = \"" + _newhostname + "\"\n";
  186. neuesfile.push_back(line);
  187. }
  188. fclose(pFile);
  189. pFile = OpenFileAndWait(fn.c_str(), "w+");
  190. for (int i = 0; i < neuesfile.size(); ++i)
  191. {
  192. printf(neuesfile[i].c_str());
  193. fputs(neuesfile[i].c_str(), pFile);
  194. }
  195. fclose(pFile);
  196. printf("*** Hostname update done ***\n");
  197. return true;
  198. }