hugeice 发表于 2010-5-14 14:53:43

Linux下解析配置文件

由于公司要开发Linux下服务器软件,需要解析配置文件,网上google了一下,没有找到方便的方法,于是自己动手写了一个,附上源码。(不要说我火星,大家是怎么解析配置文件的?)

parseconf-1.0.zipourdev_554086.zip(文件大小:75K) (原文件名:parseconf-1.0.zip)

执行结果:
$ ./parseconf
'net.ipv4.ip_forward' = '0'
'net.ipv4.conf.default.rp_filter' = '1'
'net.ipv4.conf.default.accept_source_route' = '0'
'kernel.sysrq' = '0'
'kernel.core_uses_pid' = '1'
'net.bridge.bridge-nf-call-ip6tables' = '0'
'net.bridge.bridge-nf-call-iptables' = '0'
'net.bridge.bridge-nf-call-arptables' = '0'
'var1' = 'value1'
'var2_var2var2' = 'value2 value2_ value2_value2'
'name' = 'hugeice'
'group' = 'r&d'
'id' = '501'
'name.jts' = 'Jia Tieshuan'
'group.jts' = 'r&d'
'id' = '502'
'name.hxw' = 'Huang Xunwang'
'group.hxw' = 'r&d'
'id' = '503'
'end' = 'true;'
done.

用来测试的配置文件test.conf:
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 0

# Controls source route verification
net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

                                  # this test be added by hugeice
var1=value1

var2_var2\
var2 = \
value2 value2_ \
value2_value2

name = hugeice
group = r&d
id = 501

name.jts = Jia Tieshuan
group.jts = r&d
id = 502

name.hxw = Huang Xunwang
group.hxw = r&d
id = 503

end = true;


-------------------------------------------
论坛是不是考虑支持一下上传tar打包的文件?

hugeice 发表于 2010-5-14 15:02:36

源码,也可以在windows下用,不过Win下又读写ini文件的API就用不着这个了,不停的调parseconf_get_next_variable就可以返回所有变量的变量名和变量值(都是字符串格式)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PARSECONF_MAX                (1024 * 64)
#define PARSECONF_LINE_MAX        (1024 * 4)


/* open a config file and return file pointer*/
FILE* parseconf_open_file(const char *path)
{
        return fopen(path, "r");
}

/* close config file */
int parseconf_close_file(FILE *fp)
{
        return fclose(fp);
}

/* get next variable in config file and move file position to next line */
/* return: value length or -1 for file end or call failed */
int parseconf_get_next_variable(FILE *fp, char *name_buf,
                                int name_buf_size, int *name_len,
                                char *value_buf, int value_buf_size)
{
        char line, subline;
        char *pline, *plineend, *psub, *psubend, *pequal;
        char *pnameend, *pvalue;
        int continue_flag, var_name_len, var_value_len;
        /* get next variable */
        pline = plineend = line;
        while ( fgets(subline, sizeof(subline), fp) != NULL ) {
                psub = subline;
                psubend = psub + strlen(psub);
                /* skip space */
                for ( ;
                      psub < psubend && (' ' == *psub || '\t' == *psub);
                      psub++ );
                /* skip comment */
                if ( '#' == *psub )
                        continue;
                /* skip empty line */
                if ( psub == psubend )
                        continue;
                /* truncation trailing space and "\r" and "\n" */
                for ( ;
                      psubend > psub
                              && (' ' == psubend[-1]
                                  || '\t' == psubend[-1]
                                  || '\r' == psubend[-1]
                                  || '\n' == psubend[-1]);
                      psubend-- );
                *psubend = '\0';
                /* skip empty line again */
                if ( psub == psubend )
                        continue;
                /* continue line ? */
                continue_flag = ('\\' == psubend[-1]);
                if ( continue_flag )
                        *--psubend = '\0';
                /* link sub line to line */
                memcpy(plineend, psub, psubend - psub);
                plineend += psubend - psub;
                /* line end */
                if ( !continue_flag )
                        break;
        }
        /* file end ? */
        if ( pline == plineend && feof(fp) )
                return -1;
        /* terminate line */
        *plineend = '\0';
        /* find equal */
        pequal = strchr(pline, '=');
        if ( NULL == pequal )
                pequal = plineend;
        /* find variable's name */
        for ( pnameend = pequal;
              pnameend > pline
                      && (' ' == pnameend[-1] || '\t' == pnameend[-1]);
              pnameend-- );
        var_name_len = pnameend - pline;
        if ( (var_name_len + 1) > name_buf_size )
                return -2;
        memcpy(name_buf, pline, var_name_len);
        name_buf = '\0';
        if ( NULL != name_len )
                *name_len = var_name_len;
        /* find variable's value */
        if ( pequal == plineend ) {
                pvalue = plineend;
        } else {
                for ( pvalue = pequal + 1;
                      pvalue < plineend && (' ' == *pvalue || '\t' == *pvalue );
                      pvalue++ );
        }
        var_value_len = plineend - pvalue;
        if ( (var_value_len + 1) > value_buf_size )
                return -2;
        memcpy(value_buf, pvalue, var_value_len);
        value_buf = '\0';
        return var_value_len;
}

minux 发表于 2010-5-14 16:53:35

为什么大家都喜欢重复造轮子……
Linux下读取、分析配置文件的库还不够多么……

lz是如何google的?随意Google一下“Linux parse configuration lib”就能给出一堆结果……

hugeice 发表于 2010-5-15 09:55:07

回复【2楼】minux啊啊?
-----------------------------------------------------------------------

看来是没认真搜,呵呵,确实有好多,下了一个libconfig试试,谢谢批评!

sd_yang 发表于 2010-12-20 20:35:32

回复【3楼】hugeice 戬
-----------------------------------------------------------------------
hugeice : 怎么联系你?我有事情找你?
页: [1]
查看完整版本: Linux下解析配置文件