解析文件,出错

#include <iostream>
using namespace std;

typedef struct tagS_ConfieValue
{
 tagS_ConfieValue *pNext;
 char *pName;
 char *pValue;
}S_ConfieValue;

typedef struct tagS_Session
{
 tagS_Session *pNext;
 S_ConfieValue *p_data;
 char *pSesionName;
}S_Session;

typedef struct tagS_ConfigFile
{
 S_Session *pSession;
}S_ConfigFile;

int LoadConfigFile(const char *pCfgFilePath, S_ConfigFile *pConfigFile );
int FreeConfigFile(S_ConfigFile *pConfigFile);
int GetConfigValue(const char* pCfgFilePath, const char *pSessionName, const char *pConfigName, char *pReValue);
int main(int argc, char* argv[])
{
 int nRet = -1;
 S_ConfigFile stConfigObj;

 nRet = LoadConfigFile("config.txt", &stConfigObj);
 if(-1 == nRet)
 {
  return -1;
 }
 nRet = FreeConfigFile(&stConfigObj);
 if(-1 == nRet)
 {
  return -1;
 }
 return 0;
}

/*输入配置文件的路径,获取配置文件所有信息的链表*/
int LoadConfigFile(const char *pCfgFilePath, S_ConfigFile *pConfigFile )
{

 if (NULL==pConfigFile || NULL==pCfgFilePath)
 {
  return -1;
 }

 /* 读取文件中的内容*/
 int nContentSize = 0;

 FILE *pFile = fopen(pCfgFilePath, "rb+");
 if (NULL == pFile)
 {
  return -1;
 }

 fseek(pFile, 0, SEEK_END);
 nContentSize = ftell(pFile);
 fseek(pFile, 0, SEEK_SET);
 char *pContent = new char(nContentSize+1);
 fread(pContent, 1, nContentSize, pFile); 
 pContent[nContentSize] = '\0';
 cout<<pContent<<endl;

 /*遍历内容的每个字符*/
 char *pLoop = pContent;
 
 S_Session *pCurSession = NULL;
 pConfigFile->pSession = NULL;
 while ( *pLoop)
 {
  //while ('\r'==*pLoop || '\n'==*pLoop || '\t'==*pLoop || ' '==*pLoop) //回车换行跳过
  //{
  // ++pLoop;
  //}
  if ('\0'==*pLoop) //判断指针是否指向尾部,结束符号'\0'=0  ,(!*pLoop)
  {
   break;
  }

  if ('<'==*pLoop)
  {
   char *pEnd =NULL;
   pEnd = strchr(pLoop, '>');
   if(NULL == pEnd) //没找到返回NULL
   {
    break;
   }
   *pEnd = '\0';
   /*读取Session Name*/
   
   //pCurSession = new S_Session[1];
   //pCurSession->pNext = NULL;
   //pCurSession->p_data = NULL; // 初始化 
   //pCurSession->pSesionName =  strdup(pLoop+1);
   pLoop = pEnd+1;
   //pCurSession->pNext = pConfigFile->pSession; //第一次进入时,pConfigFile->pSession为NULL
   //pConfigFile->pSession = pCurSession;
   
   /*while ('\r'==*pLoop || ' '==*pLoop || '\n' == *pLoop || '\t'==*pLoop)
   {
    pLoop++;
   }
   */
   continue;
  }
  if (!pCurSession)
  {
   pLoop = strchr(pLoop, '\n');
   continue;
  }
  char *pDot = strchr(pLoop, '=');
  *pDot = 0;
  S_ConfieValue *pCurCfgValue = new S_ConfieValue;
  pCurCfgValue->pNext = NULL;
  pCurCfgValue->pValue = NULL;
  pCurCfgValue->pName = strdup(pLoop);
  pLoop = pDot +1;

  char *pEnd = strchr(pLoop, '\n');
  *pEnd = '\0';
  
  pCurCfgValue->pValue = strdup(pLoop);

  pCurCfgValue->pNext = pCurSession->p_data;
  pCurSession->p_data = pCurCfgValue;
  pLoop = pEnd +1;
 }
//free(pContent);


 return 0;
}
/*释放文件所有信息*/
int FreeConfigFile(S_ConfigFile *pConfigFile)
{
 S_Session* pSession = pConfigFile->pSession;
 while(pSession)
 {
  S_Session* pTemp = pSession;
  pSession = pSession->pNext;

  S_ConfieValue* pValue = pTemp->p_data;
  while(pValue)
  {
   S_ConfieValue* pTmp = pValue;
   pValue = pValue->pNext;
   free(pTmp->pName);
   free(pTmp->pValue);
   free(pTmp);
  }
  free(pTemp->pSesionName);
 }
 pConfigFile->pSession = NULL;
 return 0;
}

/*通过文件路径,Session, ConfigName获取配置数据的值*/
int GetConfigValue(const char* pCfgFilePath, const char *pSessionName, const char *pConfigName, char *pReValue)
{
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值