DeviceSimulator.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //用于定义实现一些模拟器通用的函数功能
  2. #include <string>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fstream>
  6. #include <string>
  7. #include <map>
  8. #ifdef RVC_OS_WIN
  9. #include <WinSock2.h>
  10. #else
  11. #include <arpa/inet.h> //ntohl
  12. #include <unistd.h> // Linux系统中
  13. #include <netdb.h>
  14. #include <net/if.h>
  15. #include <arpa/inet.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #endif
  21. //读取配置文件功能类
  22. class iniReader
  23. {
  24. public:
  25. iniReader()
  26. {
  27. }
  28. ~iniReader()
  29. {
  30. }
  31. bool ReadConfig(const std::string& filename)
  32. {
  33. settings_.clear();
  34. std::ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
  35. //std::ifstream infile;
  36. //infile.open(filename.c_str());
  37. //bool ret = infile.is_open()
  38. if (!infile) {
  39. return false;
  40. }
  41. std::string line, key, value, section;
  42. std::map<std::string, std::string> k_v;
  43. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  44. while (getline(infile, line))
  45. {
  46. if (AnalyseLine(line, section, key, value))
  47. {
  48. it = settings_.find(section);
  49. if (it != settings_.end())
  50. {
  51. k_v[key] = value;
  52. it->second = k_v;
  53. }
  54. else
  55. {
  56. k_v.clear();
  57. settings_.insert(std::make_pair(section, k_v));
  58. }
  59. }
  60. key.clear();
  61. value.clear();
  62. }
  63. infile.close();
  64. return true;
  65. }
  66. std::string ReadString(const char* section, const char* item, const char* default_value)
  67. {
  68. std::string tmp_s(section);
  69. std::string tmp_i(item);
  70. std::string def(default_value);
  71. std::map<std::string, std::string> k_v;
  72. std::map<std::string, std::string>::iterator it_item;
  73. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  74. it = settings_.find(tmp_s);
  75. if (it == settings_.end())
  76. {
  77. //printf("111");
  78. return def;
  79. }
  80. k_v = it->second;
  81. it_item = k_v.find(tmp_i);
  82. if (it_item == k_v.end())
  83. {
  84. //printf("222");
  85. return def;
  86. }
  87. return it_item->second;
  88. }
  89. int ReadInt(const char* section, const char* item, const int& default_value)
  90. {
  91. std::string tmp_s(section);
  92. std::string tmp_i(item);
  93. std::map<std::string, std::string> k_v;
  94. std::map<std::string, std::string>::iterator it_item;
  95. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  96. it = settings_.find(tmp_s);
  97. if (it == settings_.end())
  98. {
  99. return default_value;
  100. }
  101. k_v = it->second;
  102. it_item = k_v.find(tmp_i);
  103. if (it_item == k_v.end())
  104. {
  105. return default_value;
  106. }
  107. return atoi(it_item->second.c_str());
  108. }
  109. float ReadFloat(const char* section, const char* item, const float& default_value)
  110. {
  111. std::string tmp_s(section);
  112. std::string tmp_i(item);
  113. std::map<std::string, std::string> k_v;
  114. std::map<std::string, std::string>::iterator it_item;
  115. std::map<std::string, std::map<std::string, std::string> >::iterator it;
  116. it = settings_.find(tmp_s);
  117. if (it == settings_.end())
  118. {
  119. return default_value;
  120. }
  121. k_v = it->second;
  122. it_item = k_v.find(tmp_i);
  123. if (it_item == k_v.end())
  124. {
  125. return default_value;
  126. }
  127. return atof(it_item->second.c_str());
  128. }
  129. private:
  130. bool IsSpace(char c)
  131. {
  132. if (' ' == c || '\t' == c)
  133. return true;
  134. return false;
  135. }
  136. bool IsCommentChar(char c)
  137. {
  138. switch (c)
  139. {
  140. case '#':
  141. return true;
  142. default:
  143. return false;
  144. }
  145. }
  146. void Trim(std::string& str)
  147. {
  148. if (str.empty())
  149. {
  150. return;
  151. }
  152. int i, start_pos, end_pos;
  153. for (i = 0; i < str.size(); ++i) {
  154. if (!IsSpace(str[i])) {
  155. break;
  156. }
  157. }
  158. if (i == str.size())
  159. {
  160. str = "";
  161. return;
  162. }
  163. start_pos = i;
  164. for (i = str.size() - 1; i >= 0; --i) {
  165. if (!IsSpace(str[i])) {
  166. break;
  167. }
  168. }
  169. end_pos = i;
  170. str = str.substr(start_pos, end_pos - start_pos + 1);
  171. }
  172. bool AnalyseLine(const std::string& line, std::string& section, std::string& key, std::string& value)
  173. {
  174. if (line.empty())
  175. return false;
  176. int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
  177. if ((pos = line.find("#")) != -1)
  178. {
  179. if (0 == pos)
  180. {
  181. return false;
  182. }
  183. end_pos = pos - 1;
  184. }
  185. if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
  186. {
  187. section = line.substr(s_startpos + 1, s_endpos - 1);
  188. return true;
  189. }
  190. std::string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
  191. if ((pos = new_line.find('=')) == -1)
  192. return false;
  193. key = new_line.substr(0, pos);
  194. value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
  195. Trim(key);
  196. if (key.empty()) {
  197. return false;
  198. }
  199. Trim(value);
  200. if ((pos = value.find("\r")) > 0)
  201. {
  202. value.replace(pos, 1, "");
  203. }
  204. if ((pos = value.find("\n")) > 0)
  205. {
  206. value.replace(pos, 1, "");
  207. }
  208. return true;
  209. }
  210. private:
  211. //std::map<std::string, std::string> settings_;
  212. std::map<std::string, std::map<std::string, std::string> >settings_;
  213. };
  214. //读取本地IP功能函数
  215. std::string GetLocalIP()
  216. {
  217. #ifdef RVC_OS_WIN
  218. hostent* ent = gethostbyname(NULL);
  219. if (ent && ent->h_addr_list[0] != NULL)
  220. {
  221. int i = 0;
  222. for (; ent->h_addr_list[i] != NULL; ++i)
  223. {
  224. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  225. if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
  226. break;
  227. }
  228. if (ent->h_addr_list[i] == NULL)
  229. i = 0;
  230. auto in = (struct in_addr*)ent->h_addr_list[i];
  231. char xIP[64] = {};
  232. sprintf(xIP, "%d.%d.%d.%d", in->S_un.S_un_b.s_b1, in->S_un.S_un_b.s_b2, in->S_un.S_un_b.s_b3, in->S_un.S_un_b.s_b4);
  233. //LOG4VTM(INFO, ("ip:%s", xIP));
  234. return std::string(xIP);
  235. }
  236. #else
  237. std::string re = "";
  238. int sfd, intr;
  239. struct ifreq buf[16];
  240. struct ifconf ifc;
  241. sfd = socket(AF_INET, SOCK_DGRAM, 0);
  242. if (sfd < 0)
  243. return "wrong 1: get ip socket failed";
  244. ifc.ifc_len = sizeof(buf);
  245. ifc.ifc_buf = (caddr_t)buf;
  246. if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
  247. return "wrong 2: get ip ioctl failed";
  248. intr = ifc.ifc_len / sizeof(struct ifreq);
  249. while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
  250. close(sfd);
  251. unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
  252. char* str = new char[1024];
  253. sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
  254. re = str;
  255. return re;
  256. #endif
  257. }
  258. std::string GetValueFromIni(const std::string& filePath, const std::string& section, const std::string& key) {
  259. std::ifstream file(filePath);
  260. std::string line;
  261. std::string value;
  262. bool sectionFound = false;
  263. while (std::getline(file, line)) {
  264. if (!sectionFound && line == "[" + section + "]") {
  265. sectionFound = true;
  266. }
  267. else if (sectionFound) {
  268. size_t found = line.find(key);
  269. if (found != std::string::npos) {
  270. size_t equalsPos = line.find("=");
  271. value = line.substr(equalsPos + 1);
  272. break;
  273. }
  274. }
  275. }
  276. return value;
  277. }
  278. int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
  279. {
  280. int len = strlen(strBuf);
  281. if (len == 0 || len % 2 != 0)
  282. return 0;
  283. BYTE* buf = new BYTE[len / 2];
  284. if (buf == NULL)
  285. return 0;
  286. int j = 0;
  287. for (int i = 0; i < len;)
  288. {
  289. int tmpVal;
  290. sscanf(strBuf + i, "%2X", &tmpVal);
  291. buf[j] = tmpVal;
  292. //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
  293. i += 2;
  294. j++;
  295. }
  296. //memcpy(buf,strBuf,len);
  297. *hexBuf = buf;
  298. return j;
  299. }
  300. int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  301. {
  302. char* tmpStr = *strBuf;
  303. int count = 0;
  304. for (int i = 0; i < len; ++i)
  305. {
  306. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  307. count += 2;
  308. }
  309. return 0;
  310. }