免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9491 | 回复: 12

[ZT]用C++ STL快速编写INI文件识别类 [复制链接]

论坛徽章:
0
发表于 2008-01-23 17:46 |显示全部楼层
ini文件是技术人员经常用到的一种系统配置方法,如何读取和快速识别ini文件中的内容实现起来比较繁琐。STL强大的功能在于能快速的实现排序、查找、 识别等功能。本文通过STL中的map,string,vector,ifstream等,来快速实现ini文件的识别类class IniFile?。IniFile可以实现常见查找功能,并提供完整的源码。


1 设计需求:
ini文件的格式一般如下:

[section1]
key1=value1
key2=value2
......

[section2]
key1=value1
key2=value2    #注释
......
实际的例子是:

#ini for path
[path]
dictfile = /home/tmp/dict.dat
inputfile= /home/tmp/input.txt
outputfile= /home/tmp/output.txt

#ini for exe
[exe]
user= winter       //user name
passwd= 1234567    #pass word
database= mydatabase

其中有五种元素:section 名,Key名,value值,注释 #或者//开头,标志字符"[" "]" "="。查找项的对应关系为sectiong-key和value对应。需要得到是value。class IniFile?要实现的是两个函数:读入ini文件,读取sect-key对应的value值。即实现下面的接口:

class IniFile{
public:
    IniFile();
    //打开ini文件
    bool open(const char* pinipath);
    //读取value值
    const char* read(const char* psect, const char*pkey);
  };

2 设计实现:
用ifstream按行读入ini文件的内容

识别每一行的字符串,分析出sectiong,key,value,和注释。

用map >来记录所有的sectiong-key和value。

重新定义class IniFile?

typedef map<string, string, less<string> > strMap;
typedef strMap::iterator strMapIt;

const char*const MIDDLESTRING = "_____***_______";

class IniFile
{
public:
    IniFile( ){};
    ~IniFile( ){};
    bool open(const char* pinipath)
    {
        return do_open(pinipath);
    }
    string read(const char*psect, const char*pkey)
    {
        string mapkey = psect;
        mapkey += MIDDLESTRING;
        mapkey += pkey;
        strMapIt it = c_inimap.find(mapkey);
        if(it == c_inimap.end())
            return "";
        else
            return it->second;
    }
protected:
    bool do_open(const char* pinipath)
    {
        ifstream fin(pinipath);
        if(!fin.is_open())
            return false;
        vector<string> strvect;
        while(!fin.eof())
        {
            string inbuf;
            getline(fin, inbuf,'\n');
            strvect.push_back(inbuf);
        }
        if(strvect.empty())
            return false;
        for_each(strvect.begin(), strvect.end(), analyzeini(c_inimap));
        return !c_inimap.empty();
    }
    strMap c_inimap;
};

其中do_open是用来真正实现初始化ini内容的函数。先用ifstream fin打开一个文件,然后用is_open判断文件是否正常打开。顺序读取文件的时候用eof()判断是否到文件尾。getline是一个字符处理函数:直接从fin中读取一行。然后用while循环过滤一行末尾的空格等字符。最后保存到一个vector中,完成读入文本工作。其中比较值得关注的是以下为体,你知道为什么这么做么?


用ifstream和getline来读入而不是用fopen和fread。
用is_open判断是否打开,而不是直接读取。
用vector的push_pack而不是insert。
用empty判断是否为空,而不是用size()==0。

下一步用for_each函数来完成字符串的内容提取工作。声明一个结构,实现对操作符()的重载。代码如下:

struct analyzeini{
    string strsect;
    strMap *pmap;
    analyzeini(strMap & strmap):pmap(&strmap){}
    void operator()( const string & strini)
    {
        int first =strini.find('[');
        int last = strini.rfind(']');
        if( first != string::npos && last != string::npos && first != last+1)
        {
            strsect = strini.substr(first+1,last-first-1);
            return ;
        }
        if(strsect.empty())
            return ;
        if((first=strini.find('='))== string::npos)
            return ;
        string strtmp1= strini.substr(0,first);
        string strtmp2=strini.substr(first+1, string::npos);
        first= strtmp1.find_first_not_of(" \t");
        last = strtmp1.find_last_not_of(" \t");
        if(first == string::npos || last == string::npos)
            return ;
        string strkey = strtmp1.substr(first, last-first+1);
        first = strtmp2.find_first_not_of(" \t");
        if(((last = strtmp2.find("\t#", first )) != string::npos) ||
            ((last = strtmp2.find(" #", first )) != string::npos) ||
            ((last = strtmp2.find("\t//", first )) != string::npos)||
            ((last = strtmp2.find(" //", first )) != string::npos))
        {
            strtmp2 = strtmp2.substr(0, last-first);
        }
        last = strtmp2.find_last_not_of(" \t");
        if(first == string::npos || last == string::npos)
            return ;
        string value = strtmp2.substr(first, last-first+1);
        string mapkey = strsect + MIDDLESTRING;
        mapkey += strkey;
        (*pmap)[mapkey]=value;
        return ;
    }
};

这里大量使用了字符串的查找和字串功能。string的find_last_of系列和find系列,功能确实十分强大。所有在string中没有找到都会返回一个变量string::npos。
函数先找sectiong,然后分离key值和value值。符合要求的,把section和key值通过中间加上MIDDLESTRING组成一个新的string,插入map中。这里值得注意的是:

* for_each的使用,结构可以传递参数。 * string的查找函数及返回值 * string的链接和合并函数。 * map的下标操作符的使用。


3 具体使用
把所有代码放在一个头文件中,以后别人使用的时候,只需要包含头文件就可以了,点击查看inifile.h文件。在使用的过程中,注意判断返回值。使用代码如下:

#include <iostream>
#include "inifile.h"
using namespace std;
int main()
{
    IniFile ini;
    if(!ini.open("test.ini"))
       return -1;
    string strvalue = ini.read("sect1","key1");
    if(strvalue.empty())
        return -1;
    else
        cout<<"value="<<strvalue<<endl;
    return 0;
}

论坛徽章:
0
发表于 2008-01-23 17:49 |显示全部楼层
做一个保留,免得以后再有人问起.

论坛徽章:
0
发表于 2008-01-24 01:02 |显示全部楼层

回复 #2 converse 的帖子

这个世界上喜欢重复发明轮子的人真多

还是太粗糙了,我用glib里提供的ini读写库,支持根据locale读取不同的value,支持comment

很好很强大。

论坛徽章:
0
发表于 2008-01-24 09:18 |显示全部楼层
原帖由 fallshuang 于 2008-1-24 01:02 发表
这个世界上喜欢重复发明轮子的人真多


关键是还 太粗糙了

论坛徽章:
0
发表于 2008-01-24 09:31 |显示全部楼层
我写的一个功能还算强大的ini解析库,c实现

/* Licensed to the EZ Software Foundation (EZSF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
*/

/**
* @file  es_ini.h
* @brief ES Inifile Routines
*/

#include "es_ini.h"
#include "es_lib.h"

struct es_inifile_t
{
        es_string_t  *fname;
        es_ini_sec_t *top;
        es_string_t  *mark;
        es_int32_t    flag;   /* ES_INI_READ or ES_INI_WRITE */
        es_bool_t     updated;   /* has updated */
};

struct es_ini_sec_t
{
        es_string_t *name;
        es_string_t *mark;
        es_ini_key_t *keys;
        es_ini_sec_t *next;
};

struct es_ini_key_t
{
        es_string_t *name;
        es_string_t *value;
        es_string_t *mark;
        es_ini_key_t *next;
};

static void ini_line_trim(es_string_t *line)
{
        char *p;
        int count = 0;
        int str_len = strlen(line);
       
        p = line + str_len - 1;
        while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') {
                *p = '\0';
                p--;
                count++;
        }
       
        p = line;
        while (*p == ' ' || *p == '\t') {
                p++;
                count++;
        }
        if (p != line) {
                memmove(line, p, str_len-count);
                line[str_len-count] = '\0';
        }
}

static void pfree_key(es_ini_key_t *key)
{
        es_ini_key_t *ref_curr = key;
        es_ini_key_t *ref_next = NULL;
       
        if (!key)
                return;
       
        while (ref_curr) {
                ref_next = ref_curr->next;
               
                es_pfree(&ref_curr->name);
                es_pfree(&ref_curr->value);
                es_pfree(&ref_curr->mark);
               
                es_pfree(&ref_curr);
               
                ref_curr = ref_next;
        }
}

static void pfree_sec(es_ini_sec_t *sec)
{
        es_ini_sec_t *ref_curr = sec;
        es_ini_sec_t *ref_next = NULL;
       
        if (!sec)
                return;
       
        while (ref_curr) {
                ref_next = ref_curr->next;
               
                es_pfree(&ref_curr->name);
                es_pfree(&ref_curr->mark);
               
                if (ref_curr->keys) {
                        pfree_key(ref_curr->keys);
                        ref_curr->keys = NULL;
                }
               
                es_pfree(&ref_curr);
               
                ref_curr = ref_next;
        }
}

ES_DECLARE(es_status_t) es_inifile_open(es_inifile_t **ini,
                                         const char *fname,
                                         es_int32_t flag)
{
        FILE *ini_file = NULL;
        es_status_t rv;
        es_ini_sec_t *curr_sec = NULL, *last_sec = NULL;
        es_ini_key_t *curr_key = NULL, *last_key = NULL;
        char *p = NULL;
        char *end = NULL;
        es_uint16_t buffer_size = 4096;
        es_uint16_t read_pos = 0;
        es_string_t *buffer = NULL;
       
        (*ini) = (es_inifile_t *)es_pmalloc(sizeof(es_inifile_t));
        if (!*ini)
                return ENOMEM;
        (*ini)->top = NULL;
        (*ini)->mark = NULL;
        (*ini)->flag = flag;
        (*ini)->updated = FALSE;
        (*ini)->fname = es_pstrdup(fname);
       
        ini_file = fopen(fname, "r");
        if ((!ini_file) && (flag == ES_INI_RDWR) && (errno == ENOENT)) {
                return ES_SUCCESS;
        }
        else if (!ini_file) {
                rv = errno;
                goto free_all_return;
        }
       
        buffer = es_pmalloc(buffer_size);
        while (!feof(ini_file)) {
                errno = 0;  //!!!;( if not set to 0 else errno =  buffer_size - read_pos;
                p = fgets(buffer + read_pos, buffer_size - read_pos, ini_file);
                if (p == NULL && errno != 0) {
                        rv = errno;
                        goto free_all_return;
                }
               
                /* Check if we got all the line. */
                if (p) {
                        if (!feof(ini_file)) {
                                end = strchr(buffer, '\n');
                                if (!end) {
                                        read_pos = es_pdata_size(buffer) - 1;
                                        buffer = es_prealloc(buffer, es_pdata_size(buffer) * 2);
                               
                                        continue;
                                }
                        }
                }
                read_pos = 0;
               
                /* 去前后无效字符 */
                ini_line_trim(buffer);
               
                /* 空行则忽略 */
                if (buffer[0] == 0)
                        continue;
               
                if (buffer[0] == '#' || buffer[0] == ';') {
                        if (!(*ini)->top) {
                                /* 第一个section前面的说明信息 */
                                es_pstrcat(&(*ini)->mark, buffer);
                                es_pstrcat(&(*ini)->mark, "\n");
                        }
                        else if (curr_key) {
                                es_pstrcat(&curr_key->mark, buffer);
                                es_pstrcat(&curr_key->mark, "\n");
                        }
                        else if (curr_sec) {
                                es_pstrcat(&curr_sec->mark, buffer);
                                es_pstrcat(&curr_sec->mark, "\n");
                        }
                        else {
                                rv = -1;
                                goto free_all_return;
                        }
                }
                else if (buffer[0] == '[') {
                        end = strchr(buffer, ']');
                        if (!end) {
                                rv = -1;
                                goto free_all_return;
                        }
                        curr_sec = (es_ini_sec_t *)es_pcalloc(sizeof(es_ini_sec_t));;
                        curr_sec->keys = NULL;
                        es_pstrncpy(&curr_sec->name, buffer + 1, end - buffer - 1);

                        if (!(*ini)->top) {
                                (*ini)->top = curr_sec;
                        }
                        else {
                                last_sec->next = curr_sec;
                        }
                        last_sec = curr_sec;
                        curr_key = NULL;
                       
                        /* [sec]后面还有'#' or ';' ? */
                        if ((end = strpbrk(end, "#;")) != 0) {
                                es_pstrcat(&curr_sec->mark, end);
                                es_pstrcat(&curr_sec->mark, "\n");
                        }
                }
                else {
                        /* "key = value" or "key" */
                        char *pEqual = NULL;
                       
                        pEqual = strchr(buffer, '=');
                       
                        /* 忽略无效的key */
                        if (!curr_sec || !pEqual)
                                continue;
                       
                        curr_key = (es_ini_key_t *)es_pcalloc(sizeof(es_ini_key_t));
                        es_pstrncpy(&curr_key->name, buffer, pEqual - buffer);
                        ini_line_trim(curr_key->name);
                       
                        if (!curr_sec->keys) {
                                curr_sec->keys = curr_key;
                        }
                        else {
                                last_key->next = curr_key;
                        }
                        last_key = curr_key;
                       
                        /* 搜索'='后第一个有效字符是否为'"' */
                        p = pEqual + 1;
                        while (*p == ' ' || *p == '\t') {
                                p++;
                        }
                        if (*p == '"') {
                                end = strrchr(p + 1, '"');
                                if (end) {
                                        es_pstrncpy(&curr_key->value, p + 1, end - p - 1);
                                       
                                        /* 后面还有'#' or ';' ? */
                                        if ((end = strpbrk(end, "#;")) != 0) {
                                                es_pstrcat(&curr_key->mark, end);
                                                es_pstrcat(&curr_key->mark, "\n");
                                        }
                                }
                                else {
                                        es_pstrcpy(&curr_key->value, p + 1);
                                }
                        }
                        else {
                                /* 后面有'#' or ';' ? */
                                if ((end = strpbrk(p, "#;")) != 0) {
                                        es_pstrcat(&curr_key->mark, end);
                                        es_pstrcat(&curr_key->mark, "\n");
                                }
                               
                                es_pstrncpy(&curr_key->value, p, end - p);
                                ini_line_trim(curr_key->value);
                        }
                }
        } /* end of while() */
       
        es_pfree(&buffer);
        fclose(ini_file);
        return ES_SUCCESS;
       
free_all_return:
       
        if (ini_file) fclose(ini_file);
        es_pfree(&buffer);
        es_pfree(&(*ini)->fname);
        es_pfree(&(*ini)->mark);
        //free top
        pfree_sec((*ini)->top);
        es_pfree(ini);
        return rv;
}

static es_status_t ini_update(FILE *file, es_inifile_t *ini)
{
        es_status_t rv;
        es_ini_sec_t *sec;
        es_ini_key_t *key_;
       
        /* header mark */
        if (ini->mark) {
                if ((rv = fputs(ini->mark, file)) < 0)
                        return rv;
                if ((rv = fputs("\n", file)) < 0)
                        return rv;
        }
       
        /* section */
        for (sec = es_ini_sec_first(ini); sec; sec = es_ini_sec_next(sec)) {
                /* section name */
                if ((rv = fputs("[", file)) < 0)
                        return rv;
                if ((rv = fputs(sec->name, file)) < 0)
                        return rv;
                if ((rv = fputs("]\n", file)) < 0)
                        return rv;
               
                /*section mark*/
                if (sec->mark) {
                        if ((rv = fputs(sec->mark, file)) < 0)
                                return rv;
                }
               
                /*key*/               
                for (key_ = es_ini_key_first(sec); key_; key_ = es_ini_key_next(key_)) {
                        /* key = value */
                        if ((rv = fputs(key_->name, file)) < 0)
                                return rv;
                        if ((rv = fputs(" = \"", file)) < 0)
                                return rv;
                        if ((rv = fputs(key_->value, file)) < 0)
                                return rv;
                        if ((rv = fputs("\"\n", file)) < 0)
                                return rv;
                        /* key mark */
                        if (key_->mark) {
                                if ((rv = fputs(key_->mark, file)) < 0)
                                        return rv;
                        }
                }
                if ((rv = fputs("\n", file)) < 0)
                        return rv;
        }
        return ES_SUCCESS;
}

ES_DECLARE(es_status_t) es_inifile_close(es_inifile_t **ini)
{
        es_inifile_t *theini = *ini;
        FILE *ini_file = NULL;
        es_status_t rv;
       
        if (!theini)
                return ES_SUCCESS;
       
        if ((theini->flag == ES_INI_RDWR)
                && theini->updated) {
                ini_file = fopen(theini->fname, "w+");
                if (!ini_file) {
                        return errno;
                }
               
                if ((rv = ini_update(ini_file, theini)) != ES_SUCCESS) {
                        fclose(ini_file);
                        return rv;
                }
               
                if ((rv = fclose(ini_file)) != ES_SUCCESS)
                        return rv;               
        }
       
        if (theini->top) {
                pfree_sec(theini->top);
                theini->top = NULL;
        }
       
        es_pfree(&theini->fname);
        es_pfree(&theini->mark);
        es_pfree(&theini);
        *ini = NULL;
       
        return ES_SUCCESS;
}

ES_DECLARE(es_ini_sec_t *) es_ini_sec_first(es_inifile_t *ini)
{
    if (!ini)
            return NULL;
    else
                return ini->top;
}

ES_DECLARE(es_ini_sec_t *) es_ini_sec_next(es_ini_sec_t *sec)
{
    if (!sec)
            return NULL;
    else
                return sec->next;
}

ES_DECLARE(es_string_t *) es_ini_sec_get_name(es_ini_sec_t *sec)
{
    if (!sec)
            return NULL;
    else
                return sec->name;
}

ES_DECLARE(es_string_t *) es_ini_sec_get_mark(es_ini_sec_t *sec)
{
    if (!sec)
            return NULL;
    else
                return sec->mark;
}

ES_DECLARE(es_ini_key_t *) es_ini_key_first(es_ini_sec_t *sec)
{
    if (!sec)
            return NULL;
    else
                return sec->keys;
}

ES_DECLARE(es_ini_key_t *) es_ini_key_next(es_ini_key_t *key)
{
    if (!key)
            return NULL;
    else
                return key->next;
}

ES_DECLARE(es_string_t *) es_ini_key_get_name(es_ini_key_t *key)
{
    if (!key)
            return NULL;
    else
                return key->name;
}

ES_DECLARE(es_string_t *) es_ini_key_get_value(es_ini_key_t *key)
{
    if (!key)
            return NULL;
    else
                return key->value;
}

ES_DECLARE(es_string_t *) es_ini_key_get_mark(es_ini_key_t *key)
{
    if (!key)
            return NULL;
    else
                return key->mark;
}

ES_DECLARE(es_ini_sec_t *) es_ini_get_section(es_inifile_t *ini,
                                              const char *section,
                                              es_int32_t idx_sec)
{
        es_ini_sec_t *sec;
        int count = 0;
       
        if (!ini || !section || !*section)
                return NULL;
       
        for (sec = es_ini_sec_first(ini); sec; sec = es_ini_sec_next(sec)) {
                if (strcmp(sec->name, section) == 0) {
                        count++;
                }
                if (idx_sec == count)
                        return sec;
        }
       
        return NULL;
}

ES_DECLARE(es_ini_key_t *) es_ini_get_key(es_ini_sec_t *sec,
                                              const char *key,
                                              es_int32_t idx_key)
{
        es_ini_key_t *key_;
        int count = 0;
       
        if (!sec || !key || !*key)
                return NULL;
       
        for (key_ = es_ini_key_first(sec); key_; key_ = es_ini_key_next(key_)) {
                if (strcmp(key_->name, key) == 0) {
                        count++;
                }
                if (idx_key == count)
                        return key_;
        }
       
        return NULL;
}

ES_DECLARE(es_string_t *) es_ini_read_string(es_inifile_t *ini,
                                      const char *section,
                                      const char *key,
                                      char *def)
{
        return         es_ini_read_string2(ini, section, 1, key, 1, def);
}

ES_DECLARE(es_string_t *) es_ini_read_string2(es_inifile_t *ini,
                                      const char *section,
                                      es_int32_t idx_sec,
                                      const char *key,
                                      es_int32_t idx_key,
                                      char *def)
{
        es_ini_sec_t *sec_;
        es_ini_key_t *key_;
       
        if (!ini || !section || !*section || !key || !*key)
                return NULL;
       
        sec_ = es_ini_get_section(ini, section, idx_sec);
        if (!sec_)
                return def;
       
        key_ = es_ini_get_key(sec_, key, idx_key);
        if (!key_)
                return def;
       
        return es_ini_key_get_value(key_);
}

ES_DECLARE(es_ini_sec_t *) es_ini_add_sec(es_inifile_t *ini,
                                      const char *section,
                                      const char *mark)
{
        es_ini_sec_t *sec;
       
        if (!ini || !section || !*section)
                return NULL;
       
        sec = (es_ini_sec_t *)es_pcalloc(sizeof(es_ini_sec_t));
        if (!sec)
                return NULL;
        sec->name = es_pstrdup(section);
        if (mark)
                sec->mark = es_pstrdup(mark);
        sec->next = ini->top;
        ini->top = sec;
       
        ini->updated = TRUE;
       
        return sec;
}

ES_DECLARE(es_ini_key_t *) es_ini_add_key(es_inifile_t *ini,
                                      const char *section,
                                      es_int32_t idx_sec,
                                      const char *key,
                                      const char *value,
                                      const char *mark)
{
        es_ini_sec_t *sec;
        es_ini_key_t *key_;
       
        if (!ini || !section || !*section || !key || !*key)
                return NULL;
       
        sec = es_ini_get_section(ini, section, idx_sec);
        if (!sec) {
                sec = es_ini_add_sec(ini, section, NULL);
                if (!sec)
                        return NULL;
        }
       
        key_ = (es_ini_key_t *)es_pcalloc(sizeof(es_ini_key_t));
        if (!key_)
                return NULL;
        key_->name = es_pstrdup(key);
        if (value)
                key_->value = es_pstrdup(value);
        if (mark)
                key_->mark = es_pstrdup(mark);
        key_->next = sec->keys;
        sec->keys = key_;
       
        ini->updated = TRUE;
       
        return key_;
}

ES_DECLARE(void) es_ini_del_sec(es_inifile_t *ini,
                                      const char *section)
{
        es_ini_del_sec2(ini, section, 1);
}

ES_DECLARE(void) es_ini_del_sec2(es_inifile_t *ini,
                                      const char *section,
                                      es_int32_t idx_sec)
{
        es_ini_sec_t *sec, *ref;
       
        if (!ini || !section || !*section)
                return;
       
        sec = es_ini_get_section(ini, section, idx_sec);
        if (!sec)
                return;
       
        if (ini->top == sec)
                ini->top = sec->next;
        else
        for (ref = es_ini_sec_first(ini); ref; ref = es_ini_sec_next(ref)) {
                if (ref->next == sec) {
                        ref->next = sec->next;
                        break;
                }
        }
       
        sec->next = NULL;
        pfree_sec(sec);
       
        ini->updated = TRUE;
       
        return;
}

ES_DECLARE(void) es_ini_del_key(es_inifile_t *ini,
                                      const char *section,
                                      const char *key)
{
        es_ini_del_key2(ini, section, 1, key, 1);
}

ES_DECLARE(void) es_ini_del_key2(es_inifile_t *ini,
                                      const char *section,
                                      es_int32_t idx_sec,
                                      const char *key,
                                      es_int32_t idx_key)
{
        es_ini_sec_t *sec;
        es_ini_key_t *key_, *ref;
       
        if (!ini || !section || !*section || !key || !*key)
                return;
       
        sec = es_ini_get_section(ini, section, idx_sec);
        if (!sec)
                return;
       
        key_ = es_ini_get_key(sec, key, idx_key);
        if (!key_)
                return;
       
        if (sec->keys == key_)
                sec->keys = key_->next;
        else
        for (ref = es_ini_key_first(sec); ref; ref = es_ini_key_next(ref)) {
                if (ref->next == key_) {
                        ref->next = key_->next;
                        break;
                }
        }
       
        key_->next = NULL;
        pfree_key(key_);
       
        ini->updated = TRUE;
       
        return;
}

ES_DECLARE(es_bool_t) es_ini_upd_key(es_inifile_t *ini,
                                      const char *section,
                                      const char *key,
                                      const char *value)
{
        return es_ini_upd_key2(ini, section, 1, key, 1, value);
}

ES_DECLARE(es_bool_t) es_ini_upd_key2(es_inifile_t *ini,
                                      const char *section,
                                      es_int32_t idx_sec,
                                      const char *key,
                                      es_int32_t idx_key,
                                      const char *value)
{
        es_ini_sec_t *sec;
        es_ini_key_t *key_;
       
        if (!ini || !section || !*section || !key || !*key)
                return FALSE;
       
        sec = es_ini_get_section(ini, section, idx_sec);
        if (!sec)
                return FALSE;
       
        key_ = es_ini_get_key(sec, key, idx_key);
        if (!key_)
                return FALSE;
       
        if (value) {
                es_pstrcpy(&key_->value, value);
        }
        else
                es_pstrcpy(&key_->value, "");
       
        ini->updated = TRUE;
       
        return TRUE;
}


可以支持解析如下的示例.ini文件
#lllllllllllllllllllll
;llllllllllllllllllllll
#lllllllllllllllllll

[sec_one]
#           222222222222222222222
;22223333333333
key1 = "    " 334455    #  "
#######################################
#3333333333333344444444444444444444444444
#key11111111111111111111111111111111
key1 = "rrrrrrrrrrrrrrrrrrrrrrrr444444444444444444444444444444"
key2 = "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww4"
;key2wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

[sec_two]
#           222222222222222222222
;22223333333333
key1111 = "334455"
#3333333333333344444444444444444444444444
#key11111111111111111111111111111111
key1111 = "rrrrrrrrrrrrrrrrrrrrrrrr44444444444444444"
key22222 = "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww4"
;key2wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
ABC = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAAA"


AA"

[sec_one]
key=kkk
key=111


还支持改写.ini文件。

评分

参与人数 1可用积分 +3 收起 理由
converse + 3 感谢分享!

查看全部评分

论坛徽章:
0
发表于 2008-01-24 10:38 |显示全部楼层
呵呵~~~~

论坛徽章:
26
处女座
日期:2016-04-18 14:00:4515-16赛季CBA联赛之深圳
日期:2020-06-02 10:10:5015-16赛季CBA联赛之广夏
日期:2019-07-23 16:59:452016科比退役纪念章
日期:2019-06-26 16:59:1315-16赛季CBA联赛之天津
日期:2019-05-28 14:25:1915-16赛季CBA联赛之青岛
日期:2019-05-16 10:14:082016科比退役纪念章
日期:2019-01-11 14:44:062016科比退役纪念章
日期:2018-07-18 16:17:4015-16赛季CBA联赛之上海
日期:2017-08-22 18:18:5515-16赛季CBA联赛之江苏
日期:2017-08-04 17:00:4715-16赛季CBA联赛之佛山
日期:2017-02-20 18:21:1315-16赛季CBA联赛之天津
日期:2016-12-12 10:44:23
发表于 2008-01-24 10:48 |显示全部楼层
现在不是流行XML么?

论坛徽章:
0
发表于 2008-01-24 12:36 |显示全部楼层
对啊,老说发明轮子,老说glib或者别的库有,也没有见到开源,跨平台的。
而且xml也有,可是为了读写一个配置文件,用xerces是不是太庞大了。
要是有方便的可用的轮子就好了。

论坛徽章:
0
发表于 2008-01-24 13:21 |显示全部楼层
关于 inifile 的读取,最近刚刚尝试过一个比较简单的做法。
先把 inifile 按行读取到一个 vector 里面,vector 的每个元素就是一行。
这样做了之后,按 section,key 读取 value 的时候就很方便了,少了很多特殊情况需要处理。

http://spnetkit.googlecode.com/svn/trunk/spnetkit/spnkini.cpp

[ 本帖最后由 iunknown 于 2008-1-24 13:24 编辑 ]

论坛徽章:
0
发表于 2008-01-24 18:25 |显示全部楼层
原帖由 evaspring 于 2008-1-24 10:48 发表
现在不是流行XML么?

确实
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP