//用于定义实现一些模拟器通用的函数功能 #include #include #include #include #include #include #ifdef RVC_OS_WIN #include #else #include //ntohl #include // Linux系统中 #include #include #include #include #include #include #include #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 k_v; std::map >::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 k_v; std::map::iterator it_item; std::map >::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 k_v; std::map::iterator it_item; std::map >::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 k_v; std::map::iterator it_item; std::map >::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 settings_; std::map >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; }