现在的位置: 首页 > 综合 > 正文

Initialize.cpp

2013年04月09日 ⁄ 综合 ⁄ 共 2333字 ⁄ 字号 评论关闭
#include "Initialize.h"
#include "tinyxml/tinyxml.h"
#include <stdlib.h>


struct ConfigValue configvalues;

bool GetNodePointerByName(TiXmlElement* pRootEle, const char *strNodeName,TiXmlElement* &destNode)  
{  
	// 假如等于根节点名,就退出  
	if (!(strcmp(strNodeName, pRootEle->Value())))  
	{  
		destNode = pRootEle;  
		printf("目标节点是根节点\r\n");
		return 0;  
	}    
	TiXmlElement *pEle = pRootEle;    
	for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())    
	{    
		if (0 != strcmp(pEle->Value(), strNodeName))  
		{  
			GetNodePointerByName(pEle,strNodeName,destNode);  
		}  
		else  
		{  
			destNode = pEle;  
			//printf("目标节点名: %s\n", pEle->Value());  
			return 0;  
		}   
	}    
	return 1;  
}   

char *InquireNodeText(const char *XmlFile,const char *strNodeName)  
{  
	TiXmlElement *pNode = NULL; 

	TiXmlDocument doc(XmlFile);
	bool loadOkay = doc.LoadFile();
	if (loadOkay == 1)
	{
		printf("filename: %s\n", XmlFile);
	}
	else
	{
		printf("Failed to load file \n");
		return NULL;
	}
	TiXmlElement *pRootEle = doc.RootElement();  
	if (NULL==pRootEle)  
	{  
		printf("Have No RootNode, SomeThing Wrong!\r\n");
		return NULL;  
	}
	GetNodePointerByName(pRootEle,strNodeName,pNode);  
	if (NULL != pNode)  
	{  
		if(NULL != pNode->GetText())
		{
			char *strText = (char *)malloc(strlen(pNode->GetText())+1);
			if(NULL == strText)
			{
				printf("Malloc strText Error!!\r\n");
				return NULL;
			}
			strcpy(strText, pNode->GetText());
			return strText; 
		}
		else
		{
			printf("The Node Have No Text!!\r\n");
			return NULL;
		}
	}  
	else  
	{  
		return NULL;  
	}
}



///读取配置文件
bool ReadInitConfig(const char *path)
{
	//int ret = 0;
	//FILE * fp = NULL;
	//char *buff = NULL;
	
	//configvalues stconfigvalue;
	//memset((void *)configvalues, 0, sizeof(configvalues));

	configvalues.DefaultUrl = InquireNodeText(path,"server_ip");
	printf("DefaultUrl = %s\r\n",configvalues.DefaultUrl);

	configvalues.Defaultport = atoi(InquireNodeText(path,"interval"));
	printf("Interval = %d\r\n",configvalues.Interval);

	configvalues.Defaultport = atoi(InquireNodeText(path,"server_port"));
	printf("Defaultport = %d\r\n",configvalues.Defaultport);

	configvalues.MaxThread = atoi(InquireNodeText(path,"send_count"));
	printf("MaxThread = %d\r\n",configvalues.MaxThread);

	configvalues.HopeData = InquireNodeText(path,"hope_data");
	printf("HopeData = %s\r\n",configvalues.HopeData);

/////////////////////////////////////////////////////
#if 0
	fp = fopen("./URLconf/apache.txt","rb");
	if(NULL == fp)
	{
		printf("ERROR\r\n");
		return 1;
	}

	buff = (char *)malloc(512);
	fgets(buff, 512, fp);
	printf("11111111111111111111111%s",buff);
	if(!strcmp(buff, configvalues.HopeData))
	{
		printf("3333333333\r\n");
	}
#endif
////////////////////////////////////////////////////	
	return 0; 
}

 

抱歉!评论已关闭.