解析XML
void GetXmlContents(string xmlPath, _Config& conf)
{
tinyxml2::XMLDocument docXml;
XMLError errXml = docXml.LoadFile(xmlPath.data());
if (XML_SUCCESS == errXml)
{
XMLElement* elmtRoot = docXml.RootElement();
XMLElement* elmtDevList = elmtRoot->FirstChildElement("Device");
for (XMLElement* elmtDev = elmtDevList->FirstChildElement(); elmtDev; elmtDev = elmtDev->NextSiblingElement())
{
string name = elmtDev->Name();
string usb2Path = elmtDev->Attribute("USB2Addr");
string usb3Path = elmtDev->Attribute("USB3Addr");
if (usb2Path != "")
{
conf.usb2Path[name] = usb2Path;
}
if (usb3Path != "")
{
conf.usb3Path[name] = usb3Path;
}
}
XMLElement* elmtTest = elmtRoot->FirstChildElement("TEST");
XMLElement* elmtLog = elmtTest->FirstChildElement("LOG");
string::size_type ipos = (xmlPath.find_last_of("\\") + 1) == 0 ? xmlPath.find_last_of("/") + 1 : xmlPath.find_last_of("\\") + 1;
string path1 = xmlPath.substr(ipos, xmlPath.length() - ipos);
string path2 = path1.substr(0, path1.rfind("."));
conf.logPath = elmtLog->Attribute("Path") + path2 + ".log";
conf.head.location = elmtLog->Attribute("LOCATION");
conf.head.caption = elmtLog->Attribute("CAPTION");
vector<string> testList = { "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2" };
for (auto it : testList)
{
XMLElement* elmtItem = elmtTest->FirstChildElement(it.data());
_ItemConfig item;
string errorcode = elmtItem->Attribute("ERRORCODE");
item.errorCode = errorcode;
for (XMLElement* elmtTmp = elmtItem->FirstChildElement(); elmtTmp; elmtTmp = elmtTmp->NextSiblingElement())
{
string name = elmtTmp->Name();
item.usbPort[name] = "-";
if ((it == "USB1.0" || it == "USB2.0") && conf.usb2Path.find(name) != conf.usb2Path.cend())
{
item.usbPort[name] = conf.usb2Path[name];
}
if ((it == "USB3.0" || it == "LOOPBACK" || it == "USB3_GEN2") && conf.usb3Path.find(name) != conf.usb3Path.cend())
{
item.usbPort[name] = conf.usb3Path[name];
}
}
conf.item[it] = item;
}
}
}
读取INI
void GetINIContents(string iniPath, _Config& conf)
{
char logPath[MAX_PATH] = { 0 }, location[MAX_PATH] = { 0 }, caption[MAX_PATH] = { 0 };
GetPrivateProfileString("LOG_PATH", "PATH", "D:\\Bigdata\\INI\\", logPath, MAX_PATH, iniPath.c_str());
GetPrivateProfileString("LOG_PATH", "LOCATION", "USB Port Test", location, MAX_PATH, iniPath.c_str());
GetPrivateProfileString("LOG_PATH", "CAPTION", "USB Port", caption, MAX_PATH, iniPath.c_str());
string::size_type ipos = (iniPath.find_last_of("\\") + 1) == 0 ? iniPath.find_last_of("/") + 1 : iniPath.find_last_of("\\") + 1;
string path1 = iniPath.substr(ipos, iniPath.length() - ipos);
string path2 = path1.substr(0, path1.rfind("."));
conf.logPath = logPath + path2 + ".log";
conf.head.location = location;
conf.head.caption = caption;
vector<string> testList = { "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2" };
for (auto it : testList)
{
_ItemConfig itemConf;
char allUSB[MAX_PATH] = { 0 };
int len = GetPrivateProfileString(it.c_str(), NULL, "", allUSB, MAX_PATH, iniPath.c_str());
for (int i = 0; i < len; i += strlen(allUSB + i) + 1)
{
string usbName = allUSB + i;
char path[MAX_PATH] = { 0 };
GetPrivateProfileString(it.c_str(), usbName.c_str(), "NULL", path, MAX_PATH, iniPath.c_str());
if (!_stricmp(usbName.c_str(), "ERRORCODE"))
{
itemConf.errorCode = path;
}
else if (_stricmp(path, "NULL"))
{
itemConf.usbPort[usbName] = path;
}
}
conf.item[it] = itemConf;
}
}
读取TXT(指定位置读取操作)
char ch[150];
char* i;
int line = 0, line1 = 0;
char str1[150] = "0-2-4-0";
FILE* fp = NULL;
fp = fopen("./SATA.txt", "r");
if (fp == NULL) {
printf("can not find the SATA.txt");
exit(-1);
}
while (!feof(fp))
{
if (fgets(ch, sizeof(ch), fp) != NULL);
{
line++;
i = strstr(ch, str1);
if (i) {
line1 = line;
strcpy(sataact[0].ID ,"3");
}
if ((line == line1 + 3) & (line != 3)) {
i = strstr(ch, "1.5");
if (i) { strcpy(sataact[0].Speed, "1.5"); }
i = strstr(ch, "3");
if (i) { strcpy(sataact[0].Speed, "3.0"); }
i = strstr(ch, "6");
if (i) { strcpy(sataact[0].Speed, "6.0"); }
}
}