C實現解析配置檔案

2021-06-05 03:42:50 字數 2280 閱讀 3306

// readconfigfile.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

typedef struct _configvalue

configvalue;

typedef struct _session

session;

typedef struct

configfile;

bool loadconfigfile(const char* pfilename, configfile* pconfigfile);

char* getconfigvalue(configfile* pfile, const char* psessionname, const char* pvaluename);

void freeconfigfile(configfile* pfile);

int _tmain(int argc, _tchar* argv)

return 0;

}bool loadconfigfile(const char* pfilename, configfile* pconfigfile)

fseek(pfile, 0, seek_end);

int len = ftell(pfile);

char* pbuff = (char*)malloc(len + 1);

if (!pbuff)

fseek(pfile, 0, seek_set);

fread(pbuff, 1, len, pfile);

fclose(pfile);

pbuff[len] = 0;

session* pcurrentsession = null;

pconfigfile->m_psesstions = null;

char* p = pbuff;

while(*p)

if (!*p)

if (*p == '<')

if (!pcurrentsession)

char* pdot = strchr(p, ':');

char* pend = pdot - 1;

while(*pend == ' ' || *pend == '\t')

pend[1] = 0;

configvalue* pvalue = (configvalue*)malloc(sizeof(configvalue));

pvalue->m_pnext = null;

pvalue->m_pname = strdup(p);

p = pdot + 1;

while(*p == ' ' || *p == '\t')

pend = strchr(p, '\n');

if (pend)

*pend = 0;

}pvalue->m_pvalue = strdup(p);

pvalue->m_pnext = pcurrentsession->m_pvalues;

pcurrentsession->m_pvalues = pvalue;

if (pend)

else

}return true;

}char* getconfigvalue(configfile* pfile, const char* psessionname, const char* pvaluename)

psession = psession->m_pnext;

}if (!psession)

configvalue* pvalue = psession->m_pvalues;

while(pvalue)

pvalue = pvalue->m_pnext;

}return null;

}void freeconfigfile(configfile* pfile)

free(ptemp->m_pname);

}pfile->m_psesstions = null;

}// stdafx.h : 標準系統包含檔案的包含檔案,

// 或是經常使用但不常更改的

// 特定於專案的包含檔案

//#pragma once

#include "targetver.h"

#include

#include

#include

// todo: 在此處引用程式需要的其他標頭檔案

value1 : 1234

value2 : 3456

valuea : defg

valueb : abcd

shell實現配置檔案解析

需求 解析乙個特定格式的檔案 這個檔案可以成為乙個配置檔案 逐行讀取,並列印出來。方案 while實現讀取,然後用awk進行讀取相應的值。第一步 用while讀取檔案。while read filedo done config.txt 這裡file是乙個變數,代表讀的那一行,shell是弱資料型別語...

配置檔案解析函式

config.h this file is usred for parsing configure file.e mail yhniejun 163.com 2007.01.25 mr.nie the struct of config file.struct conf info typedef st...

tinyxml解析配置檔案

一 tinyxml類簡單介紹 tinyxml實現的時dom訪問模型,因此提供了一系列的類對應xml檔案中的各個節點。主要類間的關係如下圖所示 tixmlbase 其他類的基類,是個抽象類 tixmldocument 表示整個xml文件,不對應其中某個特定的節點。tixmlelement 表示元素節點...