zoukankan      html  css  js  c++  java
  • ini 配置文件总结

    记录一下ini文件格式这一经典的配置文件格式,简单好用才是王道。

    目录:

    一、介绍

    二、原理

    三、应用

    一、介绍

    ini文件全称"initialization",文件后缀名为.ini,
    ini 文件格式用于保存相关软件的配置信息。

    ini配置文件常用于windows操作系统下的系统配置项或者软件的特性,包括类似
    字体信息,启动参数等。

    二、原理

    主要结构为包括以下的文本信息:
    1.sections(段)
    2.keys(properties键)
    3.values(值)

    1.sections
    格式如下:
    [section]
    a=a
    b=b

    说明:section用于表示一类的键值对,并且使用"["和"]"符合包围。
    section没有显示的介绍标志。
    一个ini文件中可以存在多个section。
    section之间不可以嵌套。

    2.keys(properties键)
    3.values(值)
    格式如下:
    name=value

    说明:键值对就是ini文件中最基本的元素。

    4.其他说明
    4.1大小写敏感问题:
    ini文件对大小写不敏感(具体取决于使用的解析库)

    4.2注释
    ";"表示注释的开头,分号之后的一行内容都为注释内容

    4.3空格
    需要注意解析库对空格的说明,有些库对空格是不支持的。

    三、应用

    一个简单的解析库参考源码如下
    https://github.com/OSSystems/inih

    test.ini文件
    1 [protocol]                  ; Protocol configuration
    2 version=6                   ; IPv6
    3 
    4 [user]
    5 name = Bob Smith            ; Spaces aroud '=' are strpped
    6 email = bob@smith.com       ; And comments (like this) ignored
    7 active = true               ; Test a boolean
    8 pi = 3.14159                ; Test a floating point number
    实例代码如下:
     1 typedef struct
     2 {
     3     int version;
     4     const char* name;
     5     const char* email;
     6 } configuration;
     7 
     8 static int handler(void* user, const char* section, const char* name,
     9                    const char* value)
    10 {
    11     configuration* pconfig = (configuration*)user;
    12 
    13     #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
    14     if (MATCH("protocol", "version")) {
    15         pconfig->version = atoi(value);
    16     } else if (MATCH("user", "name")) {
    17         pconfig->name = strdup(value);
    18     } else if (MATCH("user", "email")) {
    19         pconfig->email = strdup(value);
    20     } else {
    21         return 0;  /* unknown section/name, error */
    22     }
    23     return 1;
    24 }
    25 
    26 int main(int argc, char* argv[])
    27 {
    28     configuration config;
    29 
    30     if (ini_parse("test.ini", handler, &config) < 0) {
    31         printf("Can't load 'test.ini'
    ");
    32         return 1;
    33     }
    34     printf("Config loaded from 'test.ini': version=%d, name=%s, email=%s
    ",
    35         config.version, config.name, config.email);
    36     return 0;
    37 }
    -------------------------------------------------------------------------------
    编译后运行结果如下:
    Config loaded from 'test.ini': version=6, name=Bob Smith, email=bob@smith.com

    以上

    参考文档:
    http://en.wikipedia.org/wiki/INI_file
    https://github.com/OSSystems/inih
    http://msdn.microsoft.com/en-us/library/aa369282%28v=vs.85%29.aspx
  • 相关阅读:
    Java: Regular Expressions
    Java: Checked & Unchecked Exceptions
    二叉树的构建和层级打印
    [leetcode] 1032: Stream of Characters: Tries&AC自动机
    [leetcode] 1503: Previous Permutation With One Swap
    robot moving on the surface of a square
    killing rabbits
    Find the longest route with the smallest starting point
    [leetcode] Minimum Number of K Consecutive Bit Flips
    检测设备横屏 || 竖屏的状态
  • 原文地址:https://www.cnblogs.com/pansj/p/iniFile_Summary.html
Copyright © 2011-2022 走看看