搜索
bottom↓
回复: 4

Linux下解析配置文件

[复制链接]

出0入0汤圆

发表于 2010-5-14 14:53:43 | 显示全部楼层 |阅读模式
由于公司要开发Linux下服务器软件,需要解析配置文件,网上google了一下,没有找到方便的方法,于是自己动手写了一个,附上源码。(不要说我火星,大家是怎么解析配置文件的?)

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

执行结果:
$ ./parseconf
'net.ipv4.ip_forward'[19] = '0'[1]
'net.ipv4.conf.default.rp_filter'[31] = '1'[1]
'net.ipv4.conf.default.accept_source_route'[41] = '0'[1]
'kernel.sysrq'[12] = '0'[1]
'kernel.core_uses_pid'[20] = '1'[1]
'net.bridge.bridge-nf-call-ip6tables'[35] = '0'[1]
'net.bridge.bridge-nf-call-iptables'[34] = '0'[1]
'net.bridge.bridge-nf-call-arptables'[35] = '0'[1]
'var1'[4] = 'value1'[6]
'var2_var2var2'[13] = 'value2 value2_ value2_value2'[28]
'name'[4] = 'hugeice'[7]
'group'[5] = 'r&d'[3]
'id'[2] = '501'[3]
'name.jts'[8] = 'Jia Tieshuan'[12]
'group.jts'[9] = 'r&d'[3]
'id'[2] = '502'[3]
'name.hxw'[8] = 'Huang Xunwang'[13]
'group.hxw'[9] = 'r&d'[3]
'id'[2] = '503'[3]
'end'[3] = 'true;'[5]
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打包的文件?

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

 楼主| 发表于 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[PARSECONF_MAX], subline[PARSECONF_LINE_MAX];
        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[var_name_len] = '\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[var_value_len] = '\0';
        return var_value_len;
}

出0入0汤圆

发表于 2010-5-14 16:53:35 | 显示全部楼层
为什么大家都喜欢重复造轮子……
Linux下读取、分析配置文件的库还不够多么……

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

出0入0汤圆

 楼主| 发表于 2010-5-15 09:55:07 | 显示全部楼层
回复【2楼】minux  啊啊?
-----------------------------------------------------------------------

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

出0入0汤圆

发表于 2010-12-20 20:35:32 | 显示全部楼层
回复【3楼】hugeice 戬
-----------------------------------------------------------------------
hugeice : 怎么联系你?我有事情找你?
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 03:42

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表