/*********************************************************************
 * 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

最新文章

  1. 希尔伯特矩阵(Hilbert matrix)
  2. unity, 调节图片导入尺寸
  3. C语言可变参数函数实现原理
  4. plsql快速选中一行的快捷键
  5. IOS开发-cell的动态高度
  6. poj 3981 字符串替换
  7. web页面缓存技术之Local Storage
  8. ie9 css文件大小限制
  9. js开发打印证书功能(二)
  10. (数学 尾0的个数) 51nod1003 阶乘后面0的数量
  11. ros自定义消息的时候报错ImportError: No module named em
  12. learning svn change commit log
  13. months_between
  14. Linux-压缩与归档
  15. 70部MAYA灯光材质渲染教程合集
  16. jQuery的介绍和选择器详解
  17. Logstash写入MongoDB数据库
  18. form 表单添加 enctype =&quot;multipart/form-data&quot; 属性后后台接收中文乱码
  19. html表格内容自动换行
  20. MVC模式 与 Model2模型 介绍

热门文章

  1. DataFrame入门案例(集团公司对人事信息处理场景)
  2. mybatis or
  3. MySQL数据库的三大范式定义,作用—------你所期待的最佳答案
  4. 深入浅出的 SQL Server 查询优化
  5. JS——AJAX
  6. Android ExpandableListView的使用详解
  7. RTL Compiler之Example
  8. 如何在Centos里面,把.net core程序设为开机自启动
  9. python学习笔记--关于函数的那点事1
  10. sysbench测试阿里云ECS云磁盘的IOPS,吞吐量