免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 15817 | 回复: 30
打印 上一主题 下一主题

读取ini文件的例子(原创) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-09-23 11:16 |只看该作者 |倒序浏览
test.ini

dbsybase
ip=192.168.1.4
dbname=sa
dbpassword=test

--------------------------------
testini.c

#include <string.h>;
#include <stdio.h>;
#include <stdlib.h>;
int main(int argv,char **argc)
{
FILE *ini;
char *name,*value,line[100];
if(argv!=2)
{
printf("Usage:%s *.ini\n",argc[0]);
exit(1);
}
ini=fopen(argc[1],"r";
while(!feof(ini))
{
fgets(line,1024,ini);
name=strtok(line,"=";
value=strtok(0,"=";
if(name&&value)
printf("name=%s value=%s",name,value);
else
{
printf("read %s error\n",argc[0]);
}
}
printf("\n";
return 0;
}

论坛徽章:
0
2 [报告]
发表于 2004-09-23 11:40 |只看该作者

读取ini文件的例子(原创)

有没有改写init的程序?楼主写得不错啊

论坛徽章:
0
3 [报告]
发表于 2004-09-23 18:07 |只看该作者

读取ini文件的例子(原创)

这个程序还有很多缺陷,我写了一些代码,大家指正。

/*rwcfg.h*/
#ifndef RW_CFG_H
#define RW_CFG_H

#define MAX_LINE_LENGTH  1024

#define COMMENT_FLAG            0x23   /* .config/.ini file comment symbol # */

#define SECTION_START_FLAG      0x5B
#define SECTION_END_FLAG        0x5D   /* .config/.ini file section symbol []*/

#define KEY_FLAG                0x3D   /* .config/.ini file key symbol =     */


struct cfgline{
        int  nType;
        char *pcName;
        char *pcValue;
        char *pcComment;

        struct cfgline *next;
};
typedef struct cfgline linenode;
typedef linenode *linelink;

linelink fInitCfg(char *filename);
void fFormatCfg(char *filename);
void fCloseCfg(linelink head);
void fWriteCfg(char *filename,linelink head);

char *getKey(linelink head,char *secName,char *keyName);
int addKey(const linelink head,const char *secName,const char *keyName,const char *keyValue);
int modifyKey(const linelink head,const char *secName,const char *keyName,const char *newValue);

论坛徽章:
0
4 [报告]
发表于 2004-09-23 18:09 |只看该作者

读取ini文件的例子(原创)

/*rwcfg.c*/
#include "analyse.h"
#include "rwcfg.h"

/* ------------------------------------------------------------------------- */
/*
>;0        OK
-1        EOF
-2        ERROR
*/
int  fReadLine(FILE *fp, char *buffer)
{
        int  i=0;

          if(fgets(buffer, MAX_LINE_LENGTH, fp) == NULL)
          {
                    i = strlen(buffer);
                    if(feof(fp) != 0)
                    {
                              if(i == 0) return -1;           /* EOF */
                    }
                    else return -2;                         /* error */
          }

          i = strlen(buffer);
         
        if(i >; 0 && buffer[i-1] == '\n')
            buffer[--i] = '\0';

        return i;  
}

/* ------------------------------------------------------------------------- */
linelink fInitCfg(char *pcFileName)
{
        FILE *fp;
        linelink head,ptr;

        head=ptr=NULL;

        assert(pcFileName!=NULL);       

        fp = fopen(pcFileName, "r";
        assert(fp!=NULL);

        char buffer[MAX_LINE_LENGTH+1];
        memset(buffer,0x00,MAX_LINE_LENGTH+1);

        while((fReadLine(fp,buffer)>;=0)&&(!feof(fp)))
        {
                char bufName[MAX_LINE_LENGTH+1];
                char bufValue[MAX_LINE_LENGTH+1];
                char bufComment[MAX_LINE_LENGTH+1];

                memset(bufName,0x00,MAX_LINE_LENGTH+1);
                memset(bufValue,0x00,MAX_LINE_LENGTH+1);
                memset(bufComment,0x00,MAX_LINE_LENGTH+1);

                int nType=strAnalyse(bufName,bufValue,bufComment,buffer,MAX_LINE_LENGTH);

                if(nType>;0)
                {
                        char *pcName=NULL;
                        char *pcValue=NULL;
                        char *pcComment=NULL;

                        linelink t=(linelink)malloc(sizeof(linenode));
                        assert(t!=NULL);

                        t->;nType=nType;
                        t->;pcName=NULL;
                        t->;pcValue=NULL;
                        t->;pcComment=NULL;
                        t->;next=NULL;

                        if(((nType==1)||(nType==2))&&(bufName[0]!='\0'))
                        {
                                pcName=(char *)malloc(sizeof(char)*(strlen(bufName)+1));
                                assert(pcName!=NULL);

                                strcpy(pcName,bufName);
                                pcName[strlen(bufName)+1]='\0';

                                t->;pcName=pcName;
                        }
                       
                        if((nType==2)&&(bufValue[0]!='\0'))
                        {
                                pcValue=(char *)malloc(sizeof(char)*(strlen(bufValue)+1));
                                assert(pcValue!=NULL);

                                strcpy(pcValue,bufValue);
                                pcValue[strlen(bufValue)+1]='\0';

                                t->;pcValue=pcValue;
                        }
                       
                        if(bufComment[0]!='\0')
                        {
                                pcComment=(char *)malloc(sizeof(char)*(strlen(bufComment)+1));
                                assert(pcComment!=NULL);

                                strcpy(pcComment,bufComment);
                                pcComment[strlen(bufComment)+1]='\0';

                                t->;pcComment=pcComment;
                        }
               
                        if(head==NULL)
                        {
                                head=t;
                                ptr=t;
                        }
                        else
                        {
                                ptr->;next=t;
                                ptr=t;
                         }
                }                   
        }

        fclose(fp);
       
        return head;
}

/* ------------------------------------------------------------------------- */
void fCloseCfg(linelink head)
{
        linelink ptr=head;

        while(ptr!=NULL)
        {
                linelink temp=ptr;
                ptr=ptr->;next;       

                if(temp->;pcName!=NULL) free(temp->;pcName);
                if(temp->;pcValue!=NULL) free(temp->;pcValue);
                if(temp->;pcComment!=NULL) free(temp->;pcComment);

                free(temp);
        }

        head=NULL;
}

/* ------------------------------------------------------------------------- */
void fWriteCfg(char *filename,linelink head)
{
        assert(head!=NULL);

        FILE *fp;
        linelink ptr=head;

        fp = fopen(filename, "w";
        assert(fp!=NULL);
               
        while(ptr!=NULL)
        {
                char buf[MAX_LINE_LENGTH+1];
                memset(buf,0x00,MAX_LINE_LENGTH+1);

                int i=0;
                int j=0;
                if(ptr->;nType==1)
                {
                        buf[i++]=SECTION_START_FLAG;
                        for(j=0;j<strlen(ptr->;pcName);j++)
                                buf[i++]=ptr->;pcName[j];
                        buf[i++]=SECTION_END_FLAG;
                       
                        if(ptr->;pcComment!=NULL)
                        {
                                buf[i++]='\t';
                                buf[i++]=COMMENT_FLAG;
                                for(j=0;j<strlen(ptr->;pcComment);j++)
                                        buf[i++]=ptr->;pcComment[j];
                        }

                        buf='\0';                       
                }
                else if(ptr->;nType==2)
                {
                        buf[i++]='\t';
                       
                        for(j=0;j<strlen(ptr->;pcName);j++)
                                buf[i++]=ptr->;pcName[j];
                        buf[i++]=' ';
                        buf[i++]=KEY_FLAG;
                        buf[i++]=' ';

                        for(j=0;j<strlen(ptr->;pcValue);j++)
                                buf[i++]=ptr->;pcValue[j];

                        if(ptr->;pcComment!=NULL)
                        {
                                buf[i++]='\t';
                                buf[i++]=COMMENT_FLAG;
                                for(j=0;j<strlen(ptr->;pcComment);j++)
                                        buf[i++]=ptr->;pcComment[j];
                        }

                        buf='\0';
                }
                else
                {
                        buf[i++]=COMMENT_FLAG;
                        for(j=0;j<strlen(ptr->;pcComment);j++)
                                buf[i++]=ptr->;pcComment[j];
                        buf='\0';
                }
               
                fputs(buf, fp);
                fputs("\n", fp);
       
                ptr=ptr->;next;

                if((ptr!=NULL)&&(ptr->;nType==1))
                        fputs("\n", fp);
        }

        fclose(fp);
}

/* ------------------------------------------------------------------------- */
void fFormatCfg(char *filename)
{
        assert(filename!=NULL);

        linelink head=fInitCfg(filename);

        fWriteCfg(filename,head);

        fCloseCfg(head);
}

/* ------------------------------------------------------------------------- */
linelink locateSection(const linelink head,const char *secName)
{
        assert((head!=NULL)&&(secName!=NULL));

        linelink ptr=head;

        while(ptr!=NULL)
        {
                if((ptr->;nType==1)&&(strcmp(ptr->;pcName,secName)==0))
                        return ptr;
               
                ptr=ptr->;next;
        }

        return NULL;
}

/* ------------------------------------------------------------------------- */
linelink locateKey(const linelink head,const char *secName,const char *keyName)
{
        assert(((head!=NULL)&&(secName!=NULL))&&(keyName!=NULL));       

        linelink ptr=locateSection(head,secName);

        if(ptr==NULL)
                return NULL;

        ptr=ptr->;next;
        while((ptr!=NULL)&&(ptr->;nType!=1))
        {
                if((ptr->;nType==2)&&(strcmp(ptr->;pcName,keyName)==0))
                        return ptr;

                ptr=ptr->;next;
        }

        return NULL;
}

/* ------------------------------------------------------------------------- */
char *getKey(linelink head,char *secName,char *keyName)
{
        assert(((head!=NULL)&&(secName!=NULL))&&(keyName!=NULL));
       
        linelink ptr=locateKey(head,secName,keyName);

        if(ptr==NULL)
                return NULL;
        else
                return ptr->;pcValue;
}

/* ------------------------------------------------------------------------- */
int modifyKey(const linelink head,const char *secName,const char *keyName,const char *newValue)
{
        assert(((head!=NULL)&&(secName!=NULL))&&((keyName!=NULL)&&(newValue!=NULL)));

        linelink ptr=locateKey(head,secName,keyName);

        if(ptr==NULL)
                return -1;
        else
        {
                free(ptr->;pcValue);
                ptr->;pcValue=(char *)malloc(sizeof(char)*(strlen(newValue)+1));               
                memset(ptr->;pcValue,0x00,sizeof(char)*(strlen(newValue)+1));
                strcpy(ptr->;pcValue,newValue);
               
                return 0;
        }
}

/* ------------------------------------------------------------------------- */
int addKey(const linelink head,const char *secName,const char *keyName,const char *keyValue)
{
        assert(((head!=NULL)&&(secName!=NULL))&&((keyName!=NULL)&&(keyValue!=NULL)));

        linelink ptr=locateSection(head,secName);

        if(ptr==NULL)
                return -1;
        else
        {
                linelink t=(linelink)malloc(sizeof(linenode));
                assert(t!=NULL);

                t->;nType=2;
               
                t->;pcName=(char *)malloc(sizeof(char)*(strlen(keyName)+1));
                memset(t->;pcName,0x00,sizeof(char)*(strlen(keyName)+1));
                strcpy(t->;pcName,keyName);

                t->;pcValue=(char *)malloc(sizeof(char)*(strlen(keyValue)+1));
                memset(t->;pcValue,0x00,sizeof(char)*(strlen(keyValue)+1));
                strcpy(t->;pcValue,keyValue);

                t->;pcComment=NULL;

                t->;next=ptr->;next;
                ptr->;next=t;

                return 0;
        }
}

/* -------------------------------------------------------------------------*/
int deleteKey(const linelink head,const char *secName,const char *keyName)
{
        assert(((head!=NULL)&&(secName!=NULL))&&(keyName!=NULL));

        linelink ptr=locateSection(head,secName);
        if(ptr==NULL)
                return -1;
        else
        {
                linelink t=ptr->;next;
                while((t!=NULL)&&(t->;nType!=1))
                {
                        if((t->;nType==2)&&(strcmp(t->;pcName,keyName)==0))
                        {
                                ptr->;next=t->;next;
                               
                                if(t->;pcName!=NULL) free(t->;pcName);
                                if(t->;pcValue!=NULL) free(t->;pcValue);
                                if(t->;pcComment!=NULL) free(t->;pcComment);
                                free(t);

                                return 0;
                        }
                        else
                        {
                                ptr=t;
                                t=ptr->;next;
                        }
                }       

                return -1;
        }
}

论坛徽章:
0
5 [报告]
发表于 2004-09-23 18:11 |只看该作者

读取ini文件的例子(原创)

/*
*Copyright (C) 2004.9,        Jason.zhao
*All rights reserved.
*
*Name:                analyse.h
*Version:        1.1
*Date:                2004/9/9       
*/

#ifndef STR_ANALYSE_H
#define STR_ANALYSE_H

#include <stdio.h>;
#include <string.h>;
#include <assert.h>;

#define COMMENT_FLAG                  0x23   /* .config/.ini file comment symbol # */

#define SECTION_START_FLAG          0x5B
#define SECTION_END_FLAG          0x5D   /* .config/.ini file section symbol []*/

#define KEY_FLAG                 0x3D   /* .config/.ini file key symbol =     */

int strAnalyse(char *pcName,char *pcValue,char *pcComment,const char *pcSource,int maxlength);

#endif

论坛徽章:
0
6 [报告]
发表于 2004-09-23 18:11 |只看该作者

读取ini文件的例子(原创)

/*analyse.c*/
#include "analyse.h"

/* ------------------------------------------------------------------------- */
int  strTrimSpace(char *pcResult,const char *pcSource,int maxlength)
{
        assert(pcResult!=NULL);
        assert((pcSource!=NULL)&&(strlen(pcSource)<=maxlength));
       
        /*delete the head space&TAB */
        int i=0;
        while(((32==pcSource)||(9==pcSource))&&(i<strlen(pcSource)))       
                i++;
               
        /*delete the tail space&TAB */
        int j=strlen(pcSource)-1;
        while(((32==pcSource[j])||(9==pcSource[j]))&&(j>;=0))       
                j--;
               
        /*save result*/       
        int k=0;
        for(k=0;k<=j-i;k++)
                pcResult[k]=pcSource[i+k];
        pcResult[k]='\0';       
       
        return 0;
}

/* ------------------------------------------------------------------------- */
int strSplitComment(char *pcNComment,char *pcComment,const char *pcSource,int maxlength)
{
        assert((pcNComment!=NULL)&&(pcComment!=NULL));
        assert((pcSource!=NULL)&&(strlen(pcSource)<=maxlength));

        /*locate the index of COMMENT_FLAG*/
        int i=0;
        while((COMMENT_FLAG!=pcSource)&&(i<strlen(pcSource)))       
                i++;
       
        /*save result*/
        int j=0;
        for(j=0;j<i;j++)
                pcNComment[j]=pcSource[j];
        pcNComment[j]='\0';
       
        for(j=i+1;j<strlen(pcSource);j++)
                pcComment[j-i-1]=pcSource[j];
        pcComment[j-i-1]='\0';

        char temp[maxlength+1];
        memset(temp,0x00,maxlength+1);

        strcpy(temp,pcNComment);
        strTrimSpace(pcNComment,temp,maxlength);

        strcpy(temp,pcComment);
        strTrimSpace(pcComment,temp,maxlength);
       
        return 0;
}

/* -------------------------------------------------------------------------
*return value:
*0        success,pcResult ->; section name
*-1        not section       
*/
int strGetSection(char *pcResult,const char *pcSource,int maxlength)
{
        assert(pcResult!=NULL);
        assert((pcSource!=NULL)&&(strlen(pcSource)<=maxlength));

        if(strlen(pcSource)>;2)
        {
                if((SECTION_START_FLAG==pcSource[0])&&(SECTION_END_FLAG==pcSource[strlen(pcSource)-1]))
                 {
                        int i;
                        for(i=0;i<strlen(pcSource)-2;i++)
                                pcResult=pcSource[i+1];
                        pcResult='\0';

                        char temp[maxlength+1];
                        memset(temp, 0x00, maxlength+1);
                       
                        strcpy(temp,pcResult);
                        strTrimSpace(pcResult,temp,maxlength);
                       
                        if(pcResult[0]!='\0')
                                return 0;
                }
        }
       
        return -1;
}

/* ------------------------------------------------------------------------- */
int strGetKeyAndValue(char *pcKey,char *pcValue,const char *pcSource,int maxlength)
{
        assert(pcValue!=NULL);
        assert(pcKey!=NULL);
        assert((pcSource!=NULL)&&(strlen(pcSource)<=maxlength));

        char temp[maxlength+1];
        memset(temp, 0x00, maxlength+1);

        char keyTemp[maxlength+1];
        char valueTemp[maxlength+1];

        memset(keyTemp, 0x00, maxlength+1);
        memset(valueTemp, 0x00, maxlength+1);

        /*locate the index of KEY_FLAG*/
        int i=0;
        while((KEY_FLAG!=pcSource)&&(i<strlen(pcSource)))       
                i++;
       
        /*get key name*/
        int j=0;
        for(j=0;j<i;j++)
                keyTemp[j]=pcSource[j];
        keyTemp[j]='\0';
               
        strcpy(temp,keyTemp);
        strTrimSpace(keyTemp,temp,maxlength);

        if(keyTemp[0]=='\0')
                return -1;

        /*get key value*/
        for(j=i+1;j<strlen(pcSource);j++)
                valueTemp[j-i-1]=pcSource[j];
        valueTemp[j-i-1]='\0';

        strcpy(temp,valueTemp);
        strTrimSpace(valueTemp,temp,maxlength);
                       
        if(valueTemp[0]=='\0')
                return -1;

        /*save result*/
        strcpy(pcKey,keyTemp);
        strcpy(pcValue,valueTemp);

        return 0;
}

/* -------------------------------------------------------------------------
*return value:
*1   section        ;pcName ->; section name,pcComment ->; comment
*2   key        ;pcName ->; key name ,pcValue ->; key value,pcComment ->; comment
*3   comment        ;pcComment ->; comment
*-1  other       
*/
int strAnalyse(char *pcName,char *pcValue,char *pcComment,const char *pcSource,int maxlength)
{
        assert(((pcName!=NULL)&&(pcValue!=NULL))&&(pcComment!=NULL));
        assert((pcSource!=NULL)&&(strlen(pcSource)<=maxlength));

        char temp[maxlength+1];
        memset(temp,0x00,maxlength+1);

        /* COMMENT is prior to SECTION,KEY*/
        strSplitComment(temp,pcComment,pcSource,maxlength);
       
        if(temp[0]=='\0')
        {
                if(pcComment[0]!='\0') return 3;
                else return -1;
        }
        /*SECTION is prior to KEY*/
        else if(strGetSection(pcName,temp,maxlength)==0)
                return 1;
        /*KEY is prior to others*/
        else if((strGetKeyAndValue(pcName,pcValue,temp,maxlength))==0)
                return 2;
       
        return -1;
}

论坛徽章:
0
7 [报告]
发表于 2004-09-23 18:15 |只看该作者

读取ini文件的例子(原创)

一个相对完整的INI文件读写程序
1 有section key 及注释
2 格式化成标准INI文件
3 读,写,修改条目

论坛徽章:
0
8 [报告]
发表于 2004-09-24 11:24 |只看该作者

读取ini文件的例子(原创)

比较简陋!

论坛徽章:
0
9 [报告]
发表于 2004-09-24 15:07 |只看该作者

读取ini文件的例子(原创)

请楼上的提出改进意见,大家相互学习。

论坛徽章:
0
10 [报告]
发表于 2004-09-24 15:35 |只看该作者

读取ini文件的例子(原创)


  1. #ifndef tini_h
  2. #define tini_h

  3. #ifndef tmemlist_h
  4. #include <memlist.h>;
  5. #endif

  6. typedef struct
  7. {
  8.   char * Name;
  9.   char * Value;
  10.   char * Comment;
  11. }IniItemStru;


  12. class TIniSet
  13. {
  14. private:
  15. public:
  16.   TMemList * mList;
  17.   char * mName;
  18.   char * mComment;
  19. private:
  20.   void FreeItem(IniItemStru * obj);
  21. public:
  22.   TIniSet();
  23.   ~TIniSet();

  24.   void SetInfo(char * setname,char * comment);

  25.   int GoHead();
  26.   int HaveNext();
  27.   int Next();
  28.   IniItemStru * GetItem();
  29.   IniItemStru * GetItem(char * item);
  30.   char * GetKey(char * item);
  31.   int AddItem(char * item,char * key,char * comment);
  32.   int SetKey(char * item,char * key);
  33.   void RemoveKey(char * item,char * key);
  34.   void RemoveItem(char * item);
  35. };

  36. class TIni
  37. {
  38. private:
  39.   char mComment_Flag;
  40. public:
  41.   TMemList * mList;
  42. private:
  43.   int IniSeperate(char * line,char * name,char * value,char * comment);
  44. public:
  45.   TIni();
  46.   ~TIni();
  47.   void SetComment(char );
  48.   TIniSet * GetSet(char * grpname);
  49.   TIniSet * AddSet(char * grpname,char * comment);
  50.   int RemoveSet(char * grpname);
  51.   int AddItem(char * grpname,char * item,char * key,char * comment);
  52.   int SetKey(char * grpname,char * item,char * key);
  53.   void RemoveKey(char * grpname,char * item,char * key);
  54.   void RemoveItem(char * grpname,char * item);
  55.   IniItemStru * GetItem(char * grpname,char * item);
  56.   char * GetKey(char * grpname,char * item);
  57.   int GetItemKey(char * grpname,char * item,char * pkeyval);
  58.   char * GetKey(char * ininame,char * grpname,char * item);
  59.   int GetItemKey(char * ininame,char * grpname,char * item,char * pkeyval);
  60.   int SaveToFile(char * filename);
  61.   int LoadFile(char * filename);
  62. };

  63. #endif


  64. /***************************************************************
  65. * 模块名称:cini.cpp
  66. * 功能描述:配置文件操作类
  67. * 关键算法:
  68. * 可移植性: unix / window / c++
  69. * 外部引用:cini.h
  70. * 作    者: mengwg
  71. * 完工日期: 2001-02-25
  72. * 最后修改日期: 2001-02-25
  73. * 修改记录:
  74. *          修改者:
  75. *          修改描述:
  76. *          修改日期:
  77. *
  78. *          修改者:
  79. *          修改描述:
  80. *          修改日期:
  81. ***************************************************************/

  82. #include <system.h>;
  83. #include <stdio.h>;
  84. #include <stdlib.h>;
  85. #include <string.h>;
  86. #include <ctype.h>;
  87. #include <cini.h>;
  88. #include <tbase.h>;

  89. /*******************************************************************
  90. *                    下面是TIniSet类的成员函数
  91. *******************************************************************/
  92. /*
  93. 函数功能:释放一个节点
  94. 传入参数:
  95.   obj: 要释放的节点
  96. 返回参数:
  97. */
  98. void TIniSet::FreeItem(IniItemStru * obj)
  99. {
  100.   if(obj==NULL) return;
  101.   if(obj->;Name!=NULL) IMem.Free(obj->;Name);
  102.   if(obj->;Value!=NULL) IMem.Free(obj->;Value);
  103.   if(obj->;Comment!=NULL) IMem.Free(obj->;Comment);
  104.   IMem.Free(obj);
  105. }

  106. /*
  107. 函数功能:初始化
  108. 传入参数:
  109. 返回参数:
  110. */
  111. TIniSet::TIniSet()
  112. {
  113.   mName = NULL;
  114.   mComment = NULL;
  115.   mList = new TMemList();
  116. }

  117. /*
  118. 函数功能:释放内存
  119. 传入参数:
  120. 返回参数:
  121. */
  122. TIniSet::~TIniSet()
  123. {
  124. IniItemStru * obj;

  125.   if(mName != NULL) IMem.Free(mName);
  126.   if(mComment != NULL) IMem.Free(mComment);
  127.   while(mList->;GoHead())
  128.   {
  129.     obj = (IniItemStru * )mList->;GetPtr();
  130.     mList->;Remove(obj);
  131.     FreeItem(obj);
  132.   };
  133.   delete mList;
  134. }

  135. /*
  136. 函数功能:设置集合的名称和注释
  137. 传入参数:
  138.   setname: 集合名称
  139.   comment: 集合注释
  140. 返回参数:
  141. */
  142. void TIniSet::SetInfo(char * setname,char * comment)
  143. {
  144.   if(mName != NULL)
  145.   {
  146.     IMem.Free(mName);
  147.     mName=NULL;
  148.   };
  149.   if(mComment != NULL)
  150.   {
  151.     IMem.Free(mComment);
  152.     mComment=NULL;
  153.   };
  154.   if(setname!=NULL)
  155.   {
  156.     mName = (char *)IMem.Malloc((int)strlen(setname)+1);
  157.     strcpy(mName,setname);
  158.   };
  159.   if(comment!=NULL)
  160.   {
  161.     mComment = (char *)IMem.Malloc((int)strlen(comment)+1);
  162.     strcpy(mComment,comment);
  163.   };
  164. }

  165. /*
  166. 函数功能:到集合头
  167. 传入参数:
  168. 返回参数:
  169.   成功 1
  170.   失败 0
  171. */
  172. int TIniSet::GoHead()
  173. {
  174.   return mList->;GoHead();
  175. }

  176. /*
  177. 函数功能:判断集合是否有下一条目
  178. 传入参数:
  179. 返回参数:
  180.   有 1
  181.   无 0
  182. */
  183. int TIniSet::HaveNext()
  184. {
  185.   return mList->;HaveNext();
  186. }

  187. /*
  188. 函数功能:定位到集合的下一条目
  189. 传入参数:
  190. 返回参数:
  191.   有 1
  192.   无 0
  193. */
  194. int TIniSet::Next()
  195. {
  196.   return mList->;Next();
  197. }

  198. /*
  199. 函数功能:取集合的当前条目
  200. 传入参数:
  201. 返回参数:
  202.   成功 返回条目
  203.   失败 返回NULL
  204. */
  205. IniItemStru * TIniSet::GetItem()
  206. {
  207.   return (IniItemStru * )mList->;GetPtr();
  208. }

  209. /*
  210. 函数功能:根据名称取一个条目
  211. 传入参数:
  212.   item: 条目名称
  213. 返回参数:
  214.   成功:节点指针
  215.   失败:NULL
  216. */
  217. IniItemStru * TIniSet::GetItem(char * item)
  218. {
  219. IniItemStru * obj;

  220.   if(item==NULL) return NULL;
  221.   mList->;GoHead();
  222.   while(1)
  223.   {
  224.     obj = (IniItemStru * )mList->;GetPtr();
  225.     if(obj==NULL) break;
  226.     if(obj->;Name==NULL)
  227.     {
  228.       if(!mList->;Next()) break;
  229.       continue;
  230.     };
  231.     if(!strcmp(obj->;Name,item))
  232.       return obj;
  233.     if(!mList->;Next()) break;
  234.   };
  235.   return NULL;
  236. }

  237. /*
  238. 函数功能:根据名称取一个条目的值
  239. 传入参数:
  240.   item: 条目名称
  241. 返回参数:
  242.   成功:条目的值
  243.   失败:NULL
  244. */
  245. char * TIniSet::GetKey(char * item)
  246. {
  247. IniItemStru * obj;

  248.   if(item==NULL) return NULL;
  249.   mList->;GoHead();
  250.   while(1)
  251.   {
  252.     obj = (IniItemStru * )mList->;GetPtr();
  253.     if(obj==NULL) break;
  254.     if(obj->;Name==NULL)
  255.     {
  256.       if(!mList->;Next()) break;
  257.       continue;
  258.     };
  259.     //printf("objname %s item %s\n",obj->;Name,item);
  260.     if(!strcmp(obj->;Name,item))
  261.       return obj->;Value;
  262.     if(!mList->;Next()) break;
  263.   };
  264.   return NULL;
  265. }

  266. /*
  267. 函数功能:在集合里添加一个条目
  268. 传入参数:
  269.   item: 条目名称
  270.   key: 条目的值
  271.   comment: 条目的注释
  272. 返回参数:
  273.   成功 1
  274.   失败 0
  275. */
  276. int TIniSet::AddItem(char * item,char * key,char * comment)
  277. {
  278. IniItemStru * obj;

  279.   obj=(IniItemStru * )IMem.Malloc(sizeof(IniItemStru));
  280.   if(obj==NULL) goto malloc_err;
  281.   obj->;Name=obj->;Value=obj->;Comment=NULL;

  282.   if(item!=NULL)
  283.   {
  284.     obj->;Name=(char *)IMem.Malloc((int)strlen(item)+1);
  285.     if(obj->;Name==NULL) goto malloc_err;
  286.     strcpy(obj->;Name,item);
  287.   };
  288.   if(key!=NULL)
  289.   {
  290.     obj->;Value=(char *)IMem.Malloc((int)strlen(key)+1);
  291.     if(obj->;Value==NULL) goto malloc_err;
  292.           strcpy(obj->;Value,key);
  293.   };
  294.   if(comment!=NULL)
  295.   {
  296.     obj->;Comment=(char *)IMem.Malloc((int)strlen(comment)+1);
  297.     if(obj->;Comment==NULL) goto malloc_err;
  298.     strcpy(obj->;Comment,comment);
  299.   };
  300.   if(mList->;AddTail(obj))
  301.     return 1;
  302. malloc_err:
  303.   FreeItem(obj);
  304.   return 0;
  305. }

  306. /*
  307. 函数功能:设置一个条目的值
  308. 传入参数:
  309.   item: 条目名称
  310.   key: 条目的值
  311. 返回参数:
  312.   成功 1
  313.   失败 0
  314. */
  315. int TIniSet::SetKey(char * item,char * key)
  316. {
  317. IniItemStru * obj;

  318.   if(item==NULL) return 0;
  319.   mList->;GoHead();
  320.   while(1)
  321.   {
  322.     obj = (IniItemStru * )mList->;GetPtr();
  323.     if(obj==NULL) break;
  324.     if(obj->;Name==NULL)
  325.     {
  326.       if(!mList->;Next()) break;
  327.       continue;
  328.     };
  329.     if(strcmp(obj->;Name,item))
  330.     {
  331.       if(!mList->;Next()) break;
  332.       continue;
  333.     };
  334.     if(obj->;Value==NULL)
  335.     {
  336.       IMem.Free(obj->;Value);
  337.       obj->;Value=NULL;
  338.     };
  339.     if(key!=NULL)
  340.     {
  341.       obj->;Value=(char *)IMem.Malloc((int)strlen(key)+1);
  342.       strcpy(obj->;Value,key);
  343.       return 1;
  344.     };
  345.     if(!mList->;Next()) break;
  346.   };
  347.   AddItem(item,key,NULL);
  348.   return 1;
  349. }

  350. /*
  351. 函数功能:根据值删除一个条目
  352. 传入参数:
  353.   item: 条目名称
  354.   key: 条目的值
  355. 返回参数:
  356. */
  357. void TIniSet::RemoveKey(char * item,char * key)
  358. {
  359. IniItemStru * obj;

  360.   if(item==NULL||key==NULL) return;
  361.   mList->;GoHead();
  362.   while(1)
  363.   {
  364.     obj = (IniItemStru * )mList->;GetPtr();
  365.     if(obj==NULL) break;
  366.     if(obj->;Name==NULL||obj->;Value==NULL)
  367.     {
  368.       if(!mList->;Next()) break;
  369.       continue;
  370.     };
  371.     if(!strcmp(obj->;Name,item)&&!strcmp(obj->;Value,key))
  372.     {
  373.       mList->;Remove(obj);
  374.       FreeItem(obj);
  375.       continue;
  376.     };
  377.     if(!mList->;Next()) break;
  378.   };
  379. }

  380. /*
  381. 函数功能:根据名称删除一个条目
  382. 传入参数:
  383.   item: 条目名称
  384. 返回参数:
  385. */
  386. void TIniSet::RemoveItem(char * item)
  387. {
  388. IniItemStru * obj;

  389.   if(item==NULL) return;
  390.   mList->;GoHead();
  391.   while(1)
  392.   {
  393.     obj = (IniItemStru * )mList->;GetPtr();
  394.     if(obj==NULL) break;
  395.     if(obj->;Name==NULL)
  396.     {
  397.       if(!mList->;Next()) break;
  398.       continue;
  399.     };
  400.     if(!strcmp(obj->;Name,item))
  401.     {
  402.       mList->;Remove(obj);
  403.       FreeItem(obj);
  404.       continue;
  405.     };
  406.     if(!mList->;Next()) break;
  407.   };
  408. }


  409. /*******************************************************************
  410. *                    下面是TIni类的成员函数
  411. *******************************************************************/

  412. /*
  413. 函数功能:初始化
  414. 传入参数:
  415. 返回参数:
  416. */
  417. TIni::TIni()
  418. {
  419.   mComment_Flag = ';';
  420.   mList = new TMemList();
  421. }

  422. /*
  423. 函数功能:释放内存
  424. 传入参数:
  425. 返回参数:
  426. */
  427. TIni::~TIni()
  428. {
  429. TIniSet * obj;

  430.   while(mList->;GoHead())
  431.   {
  432.     obj = (TIniSet * )mList->;GetPtr();
  433.     mList->;Remove(obj);
  434.     delete obj;
  435.   };
  436.   delete mList;
  437. }

  438. /*
  439. 函数功能:设置标识注释的字符
  440. 传入参数:
  441.   flag: 标识注释的字符
  442. 返回参数:
  443. */
  444. void TIni::SetComment(char flag)
  445. {
  446.   mComment_Flag = flag;
  447. }

  448. /*
  449. 函数功能:根据名称取一个集合指针
  450. 传入参数:
  451.   grpname: 集合名称
  452. 返回参数:
  453.   成功:集合指针
  454.   失败:NULL
  455. */
  456. TIniSet * TIni::GetSet(char * grpname)
  457. {
  458. TIniSet * obj;

  459.   if(grpname==NULL) return NULL;
  460.   mList->;GoHead();
  461.   while(1)
  462.   {
  463.     obj = (TIniSet * )mList->;GetPtr();
  464.     if(obj==NULL) break;
  465.     if(obj->;mName==NULL)
  466.     {
  467.       if(!mList->;Next()) break;
  468.       continue;
  469.     };
  470.     if(!strcmp(obj->;mName,grpname))
  471.       return obj;
  472.     if(!mList->;Next()) break;
  473.   };
  474.   //printf("unable to get set %s\n",grpname);
  475.   return NULL;
  476. }

  477. /*
  478. 函数功能:添加一个集合
  479. 传入参数:
  480.   grpname: 集合名称
  481.   comment: 集合注释
  482. 返回参数:
  483.   成功:集合指针
  484.   失败:NULL
  485. */
  486. TIniSet * TIni::AddSet(char * grpname,char * comment)
  487. {
  488. TIniSet * obj;

  489.   obj=GetSet(grpname);
  490.   if(obj!=NULL)
  491.   {
  492.     obj->;SetInfo(grpname,comment);
  493.     return obj;
  494.   };
  495.   obj=new TIniSet();
  496.   obj->;SetInfo(grpname,comment);
  497.   if(!mList->;AddTail(obj))
  498.   {
  499.     delete obj;
  500.     return NULL;
  501.   };
  502.   return obj;
  503. }

  504. /*
  505. 函数功能:根据集合名称删除一个集合
  506. 传入参数:
  507.   grpname: 集合名称
  508. 返回参数:
  509.   成功:1
  510.   失败:0
  511. */
  512. int TIni::RemoveSet(char * grpname)
  513. {
  514. TIniSet * obj;

  515.   obj=GetSet(grpname);
  516.   if(obj==NULL) return 0;
  517.   mList->;Remove(obj);
  518.   delete obj;
  519.   return 1;
  520. }

  521. /*
  522. 函数功能:在集合里添加一个条目
  523. 传入参数:
  524.   grpname: 集合名称
  525.   item: 条目名称
  526.   key: 条目的值
  527.   comment: 条目的注释
  528. 返回参数:
  529.   成功 1
  530.   失败 0
  531. */
  532. int TIni::AddItem(char * grpname,char * item,char * key,char * comment)
  533. {
  534. TIniSet * obj;

  535.   obj=GetSet(grpname);
  536.   if(obj==NULL) return 0;
  537.   return obj->;AddItem(item,key,comment);
  538. }

  539. /*
  540. 函数功能:设置一个条目的值
  541. 传入参数:
  542.   grpname: 集合名称
  543.   item: 条目名称
  544.   key: 条目的值
  545. 返回参数:
  546.   成功 1
  547.   失败 0
  548. */
  549. int TIni::SetKey(char * grpname,char * item,char * key)
  550. {
  551. TIniSet * obj;

  552.   obj=GetSet(grpname);
  553.   if(obj==NULL) return 0;
  554.   return obj->;SetKey(item,key);
  555. }

  556. /*
  557. 函数功能:根据值删除一个条目
  558. 传入参数:
  559.   grpname: 集合名称
  560.   item: 条目名称
  561.   key: 条目的值
  562. 返回参数:
  563. */
  564. void TIni::RemoveKey(char * grpname,char * item,char * key)
  565. {
  566. TIniSet * obj;

  567.   obj=GetSet(grpname);
  568.   if(obj==NULL) return;
  569.   obj->;RemoveKey(item,key);
  570. }

  571. /*
  572. 函数功能:根据名称删除一个条目
  573. 传入参数:
  574.   grpname: 集合名称
  575.   item: 条目名称
  576. 返回参数:
  577. */
  578. void TIni::RemoveItem(char * grpname,char * item)
  579. {
  580. TIniSet * obj;

  581.   obj=GetSet(grpname);
  582.   if(obj==NULL) return;
  583.   obj->;RemoveItem(item);
  584. }

  585. /*
  586. 函数功能:根据名称取一个条目
  587. 传入参数:
  588.   grpname: 集合名称
  589.   item: 条目名称
  590. 返回参数:
  591.   成功:节点指针
  592.   失败:NULL
  593. */
  594. IniItemStru * TIni::GetItem(char * grpname,char * item)
  595. {
  596. TIniSet * obj;

  597.   obj=GetSet(grpname);
  598.   if(obj==NULL) return NULL;
  599.   return obj->;GetItem(item);
  600. }

  601. /*
  602. 函数功能:根据名称取一个条目的值
  603. 传入参数:
  604.   grpname: 集合名称
  605.   item: 条目名称
  606. 返回参数:
  607.   成功:条目的值
  608.   失败:NULL
  609. */
  610. char * TIni::GetKey(char * grpname,char * item)
  611. {
  612. TIniSet * obj;

  613.   obj=GetSet(grpname);
  614.   if(obj==NULL)
  615.   {
  616.     printf("unable to get set %s\n",grpname);
  617.     return NULL;
  618.   };
  619.   return obj->;GetKey(item);
  620. }

  621. /*
  622. 函数功能:根据名称取一个条目的值
  623. 传入参数:
  624.   grpname: 集合名称
  625.   item: 条目名称
  626.   pkeyval: 用于存放条目值的内存指针
  627. 返回参数:
  628.   成功:1,同时把条目的值存放在pkeyval的内存中
  629.   失败:0
  630. */
  631. int TIni::GetItemKey(char * grpname,char * item,char * pkeyval)
  632. {
  633. char * p;
  634.   p=GetKey(grpname,item);
  635.   if(p==NULL) return 0;
  636.   strcpy(pkeyval,p);
  637.   return 1;
  638. };

  639. /*
  640. 函数功能:根据名称取一个条目的值
  641. 传入参数:
  642.   ininame: ini文件名称
  643.   grpname: 集合名称
  644.   item: 条目名称
  645. 返回参数:
  646.   成功:条目的值
  647.   失败:NULL
  648. */
  649. char * TIni::GetKey(char * ininame,char * grpname,char * item)
  650. {
  651. TIni ini;
  652. static char itemvalue[500];
  653. char * pos;

  654.   itemvalue[0]=0;
  655.   if(!ini.LoadFile(ininame))
  656.     return itemvalue;
  657.   pos = ini.GetKey(grpname,item);
  658.   if(pos==NULL) return itemvalue;
  659.   strcpy(itemvalue,pos);
  660.   return itemvalue;
  661. }

  662. /*
  663. 函数功能:根据名称取一个条目的值
  664. 传入参数:
  665.   ininame: ini文件名称
  666.   grpname: 集合名称
  667.   item: 条目名称
  668. 返回参数:
  669.   成功:1,同时把条目的值存放在pkeyval的内存中
  670.   失败:0
  671. */
  672. int TIni::GetItemKey(char * ininame,char * grpname,char * item,char * pkeyval)
  673. {
  674. char * p;
  675.   p=GetKey(ininame,grpname,item);
  676.   if(p==NULL) return 0;
  677.   strcpy(pkeyval,p);
  678.   return 1;
  679. };

  680. /*
  681. 函数功能:解析一行配置信息
  682. 传入参数:
  683.   line: 原始信息
  684.   name: 名称
  685.   value:值
  686.   comment:注释
  687. 返回参数:
  688.   定义的类型
  689.   如果是集合定义就返回 1
  690.   否则返回 0
  691. */
  692. int TIni::IniSeperate(char * line,char * name,char * value,char * comment)
  693. {
  694. char * pos;
  695. int isgrp=0;

  696.   *name=*value=*comment=0;
  697.   pos=line;
  698.   while(*pos==' '||*pos==9) pos++;
  699.   if(*pos=='[')
  700.   {
  701.     pos++;
  702.     while(*pos==' '||*pos==9) pos++;
  703.     while(*pos!=' '&&*pos!=9&&*pos!=']'&&*pos!=0)
  704.       *name++=(char)tolower(*pos++);
  705.     *name=0;
  706.     while(*pos==' '||*pos==9||*pos==']') pos++;
  707.     isgrp = 1;
  708.   }
  709.   else
  710.   {
  711.     while(*pos!='='&&*pos!=mComment_Flag&&*pos!=0&&*pos!=' '&&*pos!=9)
  712.       *name++=(char)tolower(*pos++);
  713.     *name=0;
  714.   };
  715.   while(*pos==' '||*pos==9||*pos=='=') pos++;
  716.   while(*pos!=mComment_Flag&&*pos!=0)
  717.     *value++=*pos++;
  718.   *value=0;
  719.   while(*pos==mComment_Flag) pos++;
  720.   while(*pos!=0)
  721.     *comment++=*pos++;
  722.   *comment=0;
  723.   return isgrp;
  724. }


  725. /*
  726. 函数功能:更新配置文件
  727. 传入参数:
  728.   filename: 要保存的文件名
  729. 返回参数:
  730.   0 保存失败
  731.   1 保存成功
  732. */
  733. int TIni::SaveToFile(char * filename)
  734. {
  735. FILE * fp;
  736. TIniSet * grp;
  737. IniItemStru * item;

  738.   fp=fopen(filename,"wt");
  739.         if(fp==(FILE *) NULL)
  740.     return 0;
  741.   mList->;GoHead();
  742.   while(1)
  743.   {
  744.     grp = (TIniSet * )mList->;GetPtr();
  745.     if(grp==NULL) break;
  746.     if(grp->;mName!=NULL)
  747.       if(grp->;mName[0]!=0)
  748.                fprintf(fp,"[%s]",grp->;mName);
  749.     if(grp->;mComment!=NULL)
  750.     {
  751.       if(grp->;mComment[0]!=0)
  752.                fprintf(fp,"     ;%s",grp->;mComment);
  753.     };
  754.     if(grp->;mName!=NULL||grp->;mComment!=NULL)
  755.              fprintf(fp,"\n");
  756.     grp->;GoHead();
  757.     while(1)
  758.     {
  759.       item=grp->;GetItem();
  760.       if(item==NULL)
  761.         break;
  762.       if(item->;Name!=NULL)
  763.         if(item->;Name[0]!=0)
  764.                  fprintf(fp,"%s ",item->;Name);
  765.       if(item->;Value!=NULL)
  766.         if(item->;Value[0]!=0)
  767.                  fprintf(fp,"= %s",item->;Value);
  768.         else
  769.                  fprintf(fp,"= ");
  770.       if(item->;Comment!=NULL)
  771.         if(item->;Comment[0]!=0)
  772.                  fprintf(fp,"     ;%s",item->;Comment);
  773.              fprintf(fp,"\n");
  774.       if(!grp->;Next()) break;
  775.     };
  776.     fprintf(fp,"\n");
  777.     if(!mList->;Next()) break;
  778.   };
  779.    fclose(fp);
  780.    return 1;
  781. }

  782. /*
  783. 函数功能:打开配置文件
  784. 传入参数:
  785.   filename: 要打开的文件名
  786. 返回参数:
  787.   成功: 1
  788.   失败: 0
  789. */
  790. int TIni::LoadFile(char * filename)
  791. {
  792. FILE    * fp;
  793. TIniSet * grp;
  794. int  isgrp,len;
  795. char line[1024]={0,0,0},name[1024],value[1024],comment[1024];

  796. //在提取之前应该先清除一下,mengwg 2001-08-20 modi
  797. TIniSet * obj;

  798.   while(mList->;GoHead())
  799.   {
  800.     obj = (TIniSet * )mList->;GetPtr();
  801.     mList->;Remove(obj);
  802.     delete obj;
  803.   };
  804.         fp=fopen(filename,"rt");
  805.         if(fp==(FILE *) NULL)
  806.                 return 0;

  807.         while(fgets(line,1024,fp)!=NULL)
  808.         {
  809.     IString.Allt(line);
  810.     len=(int)strlen(line);
  811.     while(len>;0)
  812.       if(line[len-1]==0x0a||line[len-1]==0x0d)
  813.         len--;
  814.       else
  815.         break;
  816.     line[len]=0;
  817.     if(len==0)
  818.       continue;
  819.     isgrp=IniSeperate(line,name,value,comment);
  820.     len=(int)strlen(value);
  821.     while(len>;0)
  822.       if(value[len-1]==' ')
  823.         len--;
  824.       else
  825.         break;
  826.     value[len]=0;
  827.     if(isgrp)
  828.     {
  829.       grp = AddSet(name,comment);
  830.       if(grp==NULL)
  831.         goto load_err;
  832.       continue;
  833.     };
  834.     if(grp==NULL)
  835.     {
  836.       grp = AddSet(NULL,NULL);
  837.       if(grp==NULL)
  838.         goto load_err;
  839.     };
  840.     grp->;AddItem(name,value,comment);
  841.         }
  842.         fclose(fp);
  843.         return 1;
  844. load_err:
  845.   fclose(fp);
  846.   return 0;
  847. }

  848. #ifdef TIni_Test
  849. #include <cini.h>;
  850. int main()
  851. {
  852. TIni ini;

  853.   ini.LoadFile("test.ini");
  854.   ini.RemoveSet("general");
  855.   printf("key of mintermid is '%s'\n",ini.GetKey("general","mintermid"));
  856.   printf("key of serialno is '%s'\n",ini.GetKey("general","serialno"));
  857.   //ini.RemoveItem("general","serialno");
  858.   //ini.RemoveSet("general");
  859.   ini.SaveToFile("save.ini");
  860.   getchar();
  861.   return 1;
  862. }
  863. #endif
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP