Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624136
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-07-31 11:45:00

配置文件解析函数(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
#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  

 

阅读(4743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~