讀取到配置文件的C語言的接口實現


/*********************************************************************
 * Author  : Samson
 * Date    : 03/13/2015
 * Test platform:
 *              3.13.0-24-generic
 *              GNU bash, 4.3.11(1)-release 
 * *******************************************************************/

為了完畢讀取系統中的配置文件的某個key鍵的值。因為使用別人的庫總是不爽,並且對於格式有一定的要求,那么就自己來寫一個這種接口以供使用了。實現原理非常easy,通過打開配置文件,進行一行一行的讀取,對照行中是否存在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/ufo/.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的值。執行結果例如以下:
ufo@ufo:~/$ ./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/ufo/.mozilla/firefox/profiles.ini is cojs83dh.default

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

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

405


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM