123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- //用于定义实现一些模拟器通用的函数功能
- #include <string>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <string>
- #include <map>
- #ifdef RVC_OS_WIN
- #include <WinSock2.h>
- #else
- #include <arpa/inet.h> //ntohl
- #include <unistd.h> // Linux系统中
- #include <netdb.h>
- #include <net/if.h>
- #include <arpa/inet.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #endif
- //读取配置文件功能类
- class iniReader
- {
- public:
- iniReader()
- {
- }
- ~iniReader()
- {
- }
- bool ReadConfig(const std::string& filename)
- {
- settings_.clear();
- std::ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
- //std::ifstream infile;
- //infile.open(filename.c_str());
- //bool ret = infile.is_open()
- if (!infile) {
- return false;
- }
- std::string line, key, value, section;
- std::map<std::string, std::string> k_v;
- std::map<std::string, std::map<std::string, std::string> >::iterator it;
- while (getline(infile, line))
- {
- if (AnalyseLine(line, section, key, value))
- {
- it = settings_.find(section);
- if (it != settings_.end())
- {
- k_v[key] = value;
- it->second = k_v;
- }
- else
- {
- k_v.clear();
- settings_.insert(std::make_pair(section, k_v));
- }
- }
- key.clear();
- value.clear();
- }
- infile.close();
- return true;
- }
- std::string ReadString(const char* section, const char* item, const char* default_value)
- {
- std::string tmp_s(section);
- std::string tmp_i(item);
- std::string def(default_value);
- std::map<std::string, std::string> k_v;
- std::map<std::string, std::string>::iterator it_item;
- std::map<std::string, std::map<std::string, std::string> >::iterator it;
- it = settings_.find(tmp_s);
- if (it == settings_.end())
- {
- //printf("111");
- return def;
- }
- k_v = it->second;
- it_item = k_v.find(tmp_i);
- if (it_item == k_v.end())
- {
- //printf("222");
- return def;
- }
- return it_item->second;
- }
- int ReadInt(const char* section, const char* item, const int& default_value)
- {
- std::string tmp_s(section);
- std::string tmp_i(item);
- std::map<std::string, std::string> k_v;
- std::map<std::string, std::string>::iterator it_item;
- std::map<std::string, std::map<std::string, std::string> >::iterator it;
- it = settings_.find(tmp_s);
- if (it == settings_.end())
- {
- return default_value;
- }
- k_v = it->second;
- it_item = k_v.find(tmp_i);
- if (it_item == k_v.end())
- {
- return default_value;
- }
- return atoi(it_item->second.c_str());
- }
- float ReadFloat(const char* section, const char* item, const float& default_value)
- {
- std::string tmp_s(section);
- std::string tmp_i(item);
- std::map<std::string, std::string> k_v;
- std::map<std::string, std::string>::iterator it_item;
- std::map<std::string, std::map<std::string, std::string> >::iterator it;
- it = settings_.find(tmp_s);
- if (it == settings_.end())
- {
- return default_value;
- }
- k_v = it->second;
- it_item = k_v.find(tmp_i);
- if (it_item == k_v.end())
- {
- return default_value;
- }
- return atof(it_item->second.c_str());
- }
- private:
- bool IsSpace(char c)
- {
- if (' ' == c || '\t' == c)
- return true;
- return false;
- }
- bool IsCommentChar(char c)
- {
- switch (c)
- {
- case '#':
- return true;
- default:
- return false;
- }
- }
- void Trim(std::string& str)
- {
- if (str.empty())
- {
- return;
- }
- int i, start_pos, end_pos;
- for (i = 0; i < str.size(); ++i) {
- if (!IsSpace(str[i])) {
- break;
- }
- }
- if (i == str.size())
- {
- str = "";
- return;
- }
- start_pos = i;
- for (i = str.size() - 1; i >= 0; --i) {
- if (!IsSpace(str[i])) {
- break;
- }
- }
- end_pos = i;
- str = str.substr(start_pos, end_pos - start_pos + 1);
- }
- bool AnalyseLine(const std::string& line, std::string& section, std::string& key, std::string& value)
- {
- if (line.empty())
- return false;
- int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
- if ((pos = line.find("#")) != -1)
- {
- if (0 == pos)
- {
- return false;
- }
- end_pos = pos - 1;
- }
- if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
- {
- section = line.substr(s_startpos + 1, s_endpos - 1);
- return true;
- }
- std::string new_line = line.substr(start_pos, start_pos + 1 - end_pos);
- if ((pos = new_line.find('=')) == -1)
- return false;
- key = new_line.substr(0, pos);
- value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
- Trim(key);
- if (key.empty()) {
- return false;
- }
- Trim(value);
- if ((pos = value.find("\r")) > 0)
- {
- value.replace(pos, 1, "");
- }
- if ((pos = value.find("\n")) > 0)
- {
- value.replace(pos, 1, "");
- }
- return true;
- }
- private:
- //std::map<std::string, std::string> settings_;
- std::map<std::string, std::map<std::string, std::string> >settings_;
- };
- //读取本地IP功能函数
- std::string GetLocalIP()
- {
- #ifdef RVC_OS_WIN
- hostent* ent = gethostbyname(NULL);
- if (ent && ent->h_addr_list[0] != NULL)
- {
- int i = 0;
- for (; ent->h_addr_list[i] != NULL; ++i)
- {
- struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
- if (in->S_un.S_un_b.s_b1 == 99 || in->S_un.S_un_b.s_b1 == 10)
- break;
- }
- if (ent->h_addr_list[i] == NULL)
- i = 0;
- auto in = (struct in_addr*)ent->h_addr_list[i];
- char xIP[64] = {};
- 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);
- //LOG4VTM(INFO, ("ip:%s", xIP));
- return std::string(xIP);
- }
- #else
- std::string re = "";
- int sfd, intr;
- struct ifreq buf[16];
- struct ifconf ifc;
- sfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sfd < 0)
- return "wrong 1: get ip socket failed";
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = (caddr_t)buf;
- if (ioctl(sfd, SIOCGIFCONF, (char*)&ifc))
- return "wrong 2: get ip ioctl failed";
- intr = ifc.ifc_len / sizeof(struct ifreq);
- while (intr-- > 0 && ioctl(sfd, SIOCGIFADDR, (char*)&buf[intr]));
- close(sfd);
- unsigned long ip = ntohl(((struct sockaddr_in*)(&buf[intr].ifr_addr))->sin_addr.s_addr);
- char* str = new char[1024];
- sprintf(str, "%u.%u.%u.%u", ip >> 24 & 0xFF, ip >> 16 & 0xFF, ip >> 8 & 0xFF, ip >> 0 & 0xFF);
- re = str;
- return re;
- #endif
- }
- std::string GetValueFromIni(const std::string& filePath, const std::string& section, const std::string& key) {
- std::ifstream file(filePath);
- std::string line;
- std::string value;
- bool sectionFound = false;
- while (std::getline(file, line)) {
- if (!sectionFound && line == "[" + section + "]") {
- sectionFound = true;
- }
- else if (sectionFound) {
- size_t found = line.find(key);
- if (found != std::string::npos) {
- size_t equalsPos = line.find("=");
- value = line.substr(equalsPos + 1);
- break;
- }
- }
- }
- return value;
- }
- int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
- {
- int len = strlen(strBuf);
- if (len == 0 || len % 2 != 0)
- return 0;
- BYTE* buf = new BYTE[len / 2];
- if (buf == NULL)
- return 0;
- int j = 0;
- for (int i = 0; i < len;)
- {
- int tmpVal;
- sscanf(strBuf + i, "%2X", &tmpVal);
- buf[j] = tmpVal;
- //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
- i += 2;
- j++;
- }
- //memcpy(buf,strBuf,len);
- *hexBuf = buf;
- return j;
- }
- int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
- {
- char* tmpStr = *strBuf;
- int count = 0;
- for (int i = 0; i < len; ++i)
- {
- sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
- count += 2;
- }
- return 0;
- }
|