Common file read and write operations (XML, INI, TXT)

Parsing XML

// 解析XML
void GetXmlContents(string xmlPath, _Config& conf)
{
    
    
	tinyxml2::XMLDocument  docXml;
	XMLError errXml = docXml.LoadFile(xmlPath.data());		//加载USBConfig.xml
	if (XML_SUCCESS == errXml)
	{
    
    
		XMLElement* elmtRoot = docXml.RootElement();		//获取根元素

		// Get Config
		XMLElement* elmtDevList = elmtRoot->FirstChildElement("Device");
			//遍历 Device子节点
		for (XMLElement* elmtDev = elmtDevList->FirstChildElement(); elmtDev; elmtDev = elmtDev->NextSiblingElement())
		{
    
    
			string name = elmtDev->Name();
			string usb2Path = elmtDev->Attribute("USB2Addr");		// Attribute选定属性
			string usb3Path = elmtDev->Attribute("USB3Addr");

			if (usb2Path != "")   // 定位(选定)所有usb2Path
			{
    
    
				conf.usb2Path[name] = usb2Path;
			}

			if (usb3Path != "")
			{
    
    
				conf.usb3Path[name] = usb3Path;
			}
		}

		// Get Test Info
		XMLElement* elmtTest = elmtRoot->FirstChildElement("TEST");	//获取Test子节点

		// Get Log Path
		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; //把xml中获取的路径中的单杠转为双杠
		string path1 = xmlPath.substr(ipos, xmlPath.length() - ipos);		// substr获取子串
		string path2 = path1.substr(0, path1.rfind("."));
		conf.logPath = elmtLog->Attribute("Path") + path2 + ".log";

		conf.head.location = elmtLog->Attribute("LOCATION");	// Attribute选定属性
		conf.head.caption = elmtLog->Attribute("CAPTION");

		vector<string> testList = {
    
     "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2" };
		for (auto it : testList)		// 遍历容器testList
		{
    
    
			XMLElement* elmtItem = elmtTest->FirstChildElement(it.data());	// 获取 Test下面的 "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2"
			_ItemConfig item;
			string errorcode = elmtItem->Attribute("ERRORCODE");	//选定属性ERRORCODE
			item.errorCode = errorcode;
			for (XMLElement* elmtTmp = elmtItem->FirstChildElement(); elmtTmp; elmtTmp = elmtTmp->NextSiblingElement())	// 遍历了 <USB_LAN2_1/>、  <USB_LAN2_2/>、  <USB_2.0_2/> 。。。。
			{
    
    
				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;
		}
	}
}

read 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;
	//printf("LOG:%s\n", conf.logPath.c_str());

	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;
	}
}

Read TXT (read operation at specified location)


	char ch[150];
	char* i;
	int line = 0, line1 = 0char str1[150] = "0-2-4-0";//SATA0
	
	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选定位置;strsrt()获取子串;
			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"); }
			}
}

Guess you like

Origin blog.csdn.net/m0_73344394/article/details/130289719