正文

ini解析2010-02-20 20:24:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/19880128/50686.html

分享到:

#include <iostream>#include <stdio.h>#include <fstream>#include <tchar.h>#include <conio.h>#include <io.h>#include <Windows.h>#include <string.h>using namespace std;#define NOPRO 0#define INSECTION 1#define INKEY 2#define INVAL 3struct KEY{    string name;    string val;    KEY* next;};struct SECTION{    string name;    KEY *Key;    SECTION *next;};class INIReader{private:    int state;    SECTION *SECTION_head;    int CreateSection(SECTION *nowpos);    int CreateKey(KEY *nowpos);    int ReadINI(FILE *File_In);public:    INIReader()    {        state=NOPRO;        SECTION_head=new SECTION;    }    int LoadFile(char* FilePath,char* ReadMode);    int PrintFile();};int INIReader::LoadFile(char *FilePath,char *ReadMode){    FILE *File_In=NULL;    File_In=fopen(FilePath,ReadMode);    if (File_In)    {        ReadINI(File_In);    }    else    {        return 0;    }    fclose(File_In);    return 1;}int INIReader::CreateKey(KEY *nowpos){    KEY *tmp=new KEY;    tmp->next=NULL;    nowpos->next=tmp;    return 1;}int INIReader::CreateSection(SECTION *nowpos){    SECTION *tmp=new SECTION;    tmp->Key=NULL;    tmp->next=NULL;    nowpos->next=tmp;    return 1;}int INIReader::ReadINI(FILE *File_In){    SECTION *nowsec=SECTION_head;    KEY *nowkeyhead=NULL;    KEY *nowkey=NULL,*prekey=NULL;    string data="";    char ch=fgetc(File_In);    while (ch!=EOF)    {        if (ch=='[')        {//  删除上一字段产生的多余的Key            if (prekey)            {                delete prekey->next;                prekey->next=NULL;            }            state=INSECTION;            CreateSection(nowsec);            nowsec=nowsec->next;            ch=fgetc(File_In);            continue;        }        if (ch==']')        {            state=INKEY;            nowsec->name=data;            data.clear();            nowkey=new KEY;            nowsec->Key=nowkey;            ch=fgetc(File_In);            continue;        }        if (ch=='='&&state==INKEY)        {            state=INVAL;            nowkey->name=data;            data.clear();            ch=fgetc(File_In);            continue;        }        if (state==INVAL&&ch=='\n')        {            state=INKEY;            nowkey->val=data;            CreateKey(nowkey);            prekey=nowkey;            nowkey=nowkey->next;            data.clear();            ch=fgetc(File_In);            continue;        }        data+=ch;        ch=fgetc(File_In);    }/// 删除程序产生的多余Key    if(nowkey)                     {        delete nowkey;        prekey->next=NULL;    }    return 1;}int INIReader::PrintFile(){    SECTION *tmpsec=SECTION_head;    while(tmpsec->next!=NULL)    {        tmpsec=tmpsec->next;        cout<<tmpsec->name.data()<<endl;        KEY *tmpkey=tmpsec->Key;        while(tmpkey->next!=NULL)        {            tmpkey=tmpkey->next;            cout<<tmpkey->name.data()<<"="<<tmpkey->val.data()<<endl;        }    }    return 1;}

阅读(853) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册