配置文件解析函数(C语言)

配置文件解析函数(C语言)

// config.h

struct conf_info
{
    const char *name;
    void *object;
};

typedef struct conf_info Cconf_info;
   
/*
* the function of removing the free space.
*/

extern void trim(char*);

extern struct conf_info *lookup_keyword(char*);
   
extern void apply_command(Cconf_info*, char *);

extern void parse(FILE*);

以下是调用例子:

// config.c

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

#include <ctype.h>
#include "config.h"

char *server_root;
char *db_addr;
char *db_user;
char *db_passwd;

struct conf_info clist[]= {
        {"serverName",&server_root},
        {"dbAddr",&db_addr},
        {"dbUser",&db_user},
        {"dbPasswd",&db_passwd},
};

void trim(char*s)
{
    char *c = s + strlen(s)- 1;
    while (isspace(*c)&& c > s) {
        *c = '\0';
        --c;
    }

    if(c == s)     //此处

        *c = '\0';
}

struct conf_info *lookup_keyword(char*c)
{
    struct conf_info *p;
       
    for (p = clist; p < clist+ (sizeof(clist) / sizeof (struct conf_info)); p++)
    {
        if (strcasecmp(c, p->name)== 0)
            return p;
    }
    return NULL;
}

static void apply_command(Cconf_info* p, char *args)
{
    if (p->object){
            if (*(char**) p->object!= NULL)
                free(*(char**) p->object);
            *(char**) p->object= strdup(args);
        }
}
/* parse configure file */
void parse(FILE* fp)
{
    Cconf_info *p;
    char buf[1024],*c;
    int line = 0;
    while (fgets(buf, 1024, fp)!= NULL)
    {
        ++line;
        if (buf[0]== '\0' || buf[0]== '#' || buf[0]== '\n')

        {

           memset(buf, 0, 1024);
            continue;
        }

        trim(buf);
        if (buf[0]== '\0')
            continue;
        c = buf;
        while (!isspace(*c))
            ++c;
        if (*c== '\0') {
           c = NULL;
        }
        else {
            *c ='\0';
            ++c;
        }
        p = lookup_keyword(buf);

        if(p != NULL)
             apply_command(p, c);

        memset(buf, 0, 1024);
    }
}

//main.c

#include<stdio.h>
#include <stdlib.h>
#include "config.h"
extern char *server_root;
extern char *db_addr;
extern char *db_user;
extern char *db_passwd;
int main(void)
{
    FILE *fp;
    if ((fp= fopen("test.conf","r"))== NULL)
    {
        fprintf(stderr,"Can't open conf file %s .\n","test.conf");
        exit(1);
    }
    parse(fp);
    printf("ServerRoot is : %s\n", server_root);
    printf("DataBase address is : %s\n", db_addr);
    printf("DataBase user is : %s\n", db_user);
    printf("DataBase user password is : %s\n", db_passwd);
    return 0;
}

此是Makefie文件:

LIBS= -lm
CFLAGS= -Wall -O2 -g

OBJS= main.o config.o

config: $(OBJS)
       gcc -o $@ $(OBJS) $(LIBS)

clean:
       rm -f config $(OBJS)

下面是test.conf配置文件:

#######配置文件test.conf######
#

serverName 10.10.206.30
dbAddr 10.10.206.32
dbUser root
dbPasswd 123456
####
##

值得注意的是:

1、windows下的回车/换行,\r\n 两个分开的,在Linux下,\n作用可以等同于\r\n,

2、windows下Enter键 是\r\n  而Linux下Enter键 是\n 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值