读取到配置文件的C语言的接口实现

转自:http://www.linuxidc.com/Linux/2015-03/115172.htm


为了完成读取系统中的配置文件的某个key键的值,由于使用别人的库总是不爽,而且对于格式有一定的要求,那么就自己来写一个这样的接口以供使用了。实现原理很简单,通过打开配置文件,进行一行一行的读取,对比行中是否存在key串且此key串的下一个字符是否为'=',若是,则得到'='号之后的值。

注意:此实现方法只适用于key=value这样的配置格式,而不是很多配置文件的key="value"的格式。

以下即是此接口的实现及测试代码:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int get_conf_value(char *file_path, char *key_name, char *value)
{
    FILE *fp = NULL;
        char *line = NULL, *substr = NULL;
        size_t len = 0, tlen = 0;
        ssize_t read = 0;
    
    if(file_path == NULL || key_name == NULL || value == NULL)
    {
        printf("paramer is invaild!\n");
        return -1;
    }
        fp = fopen(file_path, "r");
        if (fp == NULL)
    {
        printf("open config file is error!\n");
        return -1;
    }

        while ((read = getline(&line, &len, fp)) != -1) 
    {
        substr = strstr(line, key_name);
        if(substr == NULL)
        {
            continue;
        }
        else
        {
            tlen = strlen(key_name);
            if(line[tlen] == '=')
            {
                strncpy(value, &line[tlen+1], len-tlen+1);
                printf("config file format is invaild tlen is %d len is %d\n", tlen, len);
                tlen = strlen(value);
                printf("get value is %s tlen is %d\n", value, tlen);
                //replace enter key
                *(value+tlen-1) = '\0';
                break;
            }
            else
            {
                printf("config file format is invaild tlen is %d len is %d\n", tlen, len);
                fclose(fp);
                return -2;
            }
        }
        }
    if(substr == NULL)
    {
        printf("key: %s is not in config file!\n", key_name);
        fclose(fp);
        return -1;
    }

        free(line);
    fclose(fp);
    return 0;
}

int main()
{
    char getva[128] = {0};
    char pathname_key[] = "Path";
    char profilename[] = "/home/linuxidc/.mozilla/firefox/profiles.ini";
    int ret = get_conf_value(profilename, pathname_key, getva);
    if(ret == 0)
        printf("get pathname_key's value from profile:%s is %s\n", profilename, getva);
    return ret;
}

其中profilename是firefox的配置文件,获取key:Path的值。运行结果如下:
linuxidc@linuxidc:~/$ ./a.out 
config file format is invaild tlen is 4 len is 120
get value is cojs83dh.default
 tlen is 17
get pathname_key's value from profile:/home/linuxidc/.mozilla/firefox/profiles.ini is cojs83dh.default

配置文件的内容如下:
linuxidc@linuxidc:~$ cat /home/linuxidc/.mozilla/firefox/profiles.ini 
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=cojs83dh.default
Default=1


405


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenWRT是一个基于Linux的嵌入式操作系统,它提供了一套名为UCI(Unified Configuration Interface)的配置接口,用于管理系统的配置文件。UCI提供了C语言接口,使开发者可以通过编程方式读取和修改系统配置。 在使用OpenWRT的UCI C语言接口之前,需要包含相应的头文件和链接相关的库文件。头文件是`uci.h`,库文件是`libuci.so`。 下面是一个简单的例子,演示如何使用UCI C语言接口读取和修改配置: ```c #include <stdio.h> #include <uci.h> int main() { struct uci_context *ctx = uci_alloc_context(); if (!ctx) { fprintf(stderr, "Failed to allocate UCI context\n"); return 1; } struct uci_package *pkg; if (uci_load(ctx, "wireless", &pkg) != UCI_OK) { fprintf(stderr, "Failed to load wireless package\n"); uci_free_context(ctx); return 1; } struct uci_element *elem; uci_foreach_element(&pkg->sections, elem) { struct uci_section *section = uci_to_section(elem); const char *name = section->e.name; const char *option_value = uci_lookup_option_string(ctx, section, "option_name"); printf("Section: %s\n", name); printf("Option value: %s\n", option_value); } uci_unload(ctx, pkg); uci_free_context(ctx); return 0; } ``` 上述代码中,首先通过`uci_alloc_context()`函数分配一个UCI上下文对象。然后使用`uci_load()`函数加载指定的配置包(这里是"wireless")。接着使用`uci_foreach_element()`函数遍历配置包中的所有节(section),并通过`uci_lookup_option_string()`函数获取指定选项(option)的值。最后,使用`uci_unload()`函数卸载配置包,并通过`uci_free_context()`函数释放UCI上下文对象。 以上是一个简单的示例,你可以根据具体的需求进一步扩展和修改代码。希望对你有帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值