// stdafx.cpp : 只包括标准包含文件的源文件 // Demo.pch 将作为预编译头 // stdafx.obj 将包含预编译类型信息 #include "stdafx.h" // TODO: 在 STDAFX.H 中 // 引用任何所需的附加头文件,而不是在此文件中引用 bool parse_key(std::string &RetValue, std::string strLine, TCHAR *lpszText) { TCHAR szText[MAX_PATH] = {0}; TCHAR szValue[MAX_PATH] = {0}; // 去除空格; #if _MSC_VER > 1900 strLine.erase(std::remove_if(strLine.begin(), strLine.end(), [](unsigned char x) {return std::isspace(x); }), strLine.end()); //C++17 #else for (std::string::iterator it = strLine.begin(); it != strLine.end();) { !isspace(*it) ? it++ : it = it = strLine.erase(it); } #endif #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0 if ( 2 == sscanf(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) ) #else if ( 2 == _stscanf_s(strLine.c_str(), _T("%[^=]=%s"), szText, MAX_PATH, szValue, MAX_PATH) ) #endif { if ( _tcsstr(szText, lpszText) ) { RetValue = szValue; return true; } } return false; } // 计算时间; // 如: 100ms 100m 100s int pares_time_value(std::string strTime) { int nTimes = 0; if ( _tcsstr(strTime.c_str(), _T("ms")) || _tcsstr(strTime.c_str(), _T("MS"))) { nTimes = _tstol(strTime.c_str()); } else if (_tcsstr(strTime.c_str(), _T("s")) || _tcsstr(strTime.c_str(), _T("S"))) { nTimes = _tstol(strTime.c_str()) * 1000; } else if (_tcsstr(strTime.c_str(), _T("m")) || _tcsstr(strTime.c_str(), _T("M"))) { nTimes = _tstol(strTime.c_str()) * 6000; } else { // 不带单位或其他的,默认ms; nTimes = _tstol(strTime.c_str()); } return nTimes; } int parse_cmd_param_from_file(TCHAR *file_name, std::vector &vtCommandParams) { TCHAR buf[MAX_PATH] = {0}; TCHAR name[MAX_PATH] = {0}; TCHAR head[MAX_PATH] = {0}; TCHAR code[MAX_PATH] = {0}; TCHAR param[MAX_PATH] = {0}; TCHAR multicode[MAX_PATH] = {0}; TCHAR cmd_wait_time[MAX_PATH] = {0}; TCHAR read_wait_time[MAX_PATH] = {0}; int ret = -1; FILE *fp = NULL; if(!file_name || file_name[0] == '\0' ) return ret; fp = fopen(file_name, "r"); if(!fp) goto EXIT; while((fgets((char*)buf, MAX_PATH, fp) != NULL)) { int tmp_len = 0; unsigned short keycode = 0; unsigned int protocol = 0; unsigned int scancode = 0; tmp_len = _tcslen(buf); if(tmp_len >= 1) { if (buf[tmp_len - 1] == '\r' || buf[tmp_len - 1] == '\n') buf[tmp_len - 1] = 0; if(tmp_len >= 2) { if(buf[tmp_len - 2] == '\r' || buf[tmp_len - 2] == '\n') buf[tmp_len - 2] = 0; } } #if _MSC_VER >= 1500 //if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7) // 等价下面; if ( _stscanf_s(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%s", name, MAX_PATH, head, MAX_PATH, code, MAX_PATH, param, MAX_PATH, multicode, MAX_PATH, read_wait_time, MAX_PATH, cmd_wait_time, MAX_PATH) == 7) #endif #if _MSC_VER >= 1200 && _MSC_VER < 1500 // VC6.0~8.0 if ( sscanf(buf, "%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];%[^;]", name, head, code, param, multicode, read_wait_time, cmd_wait_time) == 7) #endif { CommandParam cp; parse_key(cp.name, name, _T("Name")); parse_key(cp.head, head, _T("HeadCode")); parse_key(cp.code, code, _T("Command")); parse_key(cp.param, param, _T("CMDParam")); std::string value; parse_key(value, multicode, _T("MultiParams")); cp.bMulticode = !_tcsicmp(value.c_str(), _T("true")) ? true : false; parse_key(value, read_wait_time, _T("ReadWaitTime")); cp.read_wait_time = pares_time_value(value.c_str()); parse_key(value, cmd_wait_time, _T("CMDWaitTime")); cp.cmd_wait_time = pares_time_value(value.c_str()); vtCommandParams.push_back(cp); } } ret = 0; EXIT: if(fp) fclose(fp); return ret; }