Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3961428
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2012-08-26 19:05:21

INI 配置文件的格式   

       早期的Windows桌面系统,主要使用INI 文件作为系统的配置文件,从Win95 以后开始转向使用注册表,但是还有很多系统的配置是使用INI 文件的。其实,INI 文件就是简单的text 文件,只不过这种txt 文件要遵循一定的INI 文件格式。“.ini” 就是英文 “initialization” 的头三个字母的缩写;当然INI file 的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。


经典格式: INI文件的格式很简单,最基本的三个要素是:parameters,sections 和 comments。

什么是 parameters?

       INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value由等号“=”隔开,name在等号的左边。

如:  name = value


什么是sections ?

       所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的开始就是上一个section的结束。

section 如:   [section]


 什么是 comments ?

       在INI 文件中注释语句是以分号“;”开始的。所有的注释语句不管多长都是独占一行直到结束的。在分号和行结束符之间的所有内容都是被忽略的。

注释如:   ;comments text


以下是Linux c下,对读取conf文件的实现,不支持sections。

inifile.h

点击(此处)折叠或打开

  1. #ifndef __INIFILE_H__
  2. #define __INIFILE_H__

  3. int write_conf_value(const char *key, char *value, const char *file);

  4. int read_conf_value(const char *key, char *value, const char *file);

  5. #endif
inifile.c

点击(此处)折叠或打开

  1. /*
  2.  * @file:inifile.c
  3.  * @brief initialization file read and write API implementation
  4.  * @author WuMin
  5.  * @date 2012-8-26
  6.  * @version 0.1
  7. */

  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>

  11. #define MAX_READ_LINE_NUM         1024
  12. #define MAX_ITEM_COMMENT_NUM    50
  13. #define MAX_ITEMS_NUM            100

  14. typedef struct _item_t_ {
  15.     char **comment;
  16.     short num;
  17.     char *key;
  18.     char *value;
  19. } item_t;

  20. /* 去除字符串右端空格 */
  21. char *strtrimr(char *pstr)
  22. {
  23.     int i;

  24.     i = strlen(pstr) - 1;
  25.     while (isspace(pstr[i]) && (i >= 0))
  26.         pstr[i--] = '\0';

  27.     return pstr;
  28. }

  29. /* 去除字符串左端空格 */
  30. char *strtriml(char *pstr)
  31. {
  32.     int i = 0, j;

  33.     j = strlen(pstr) - 1;
  34.     while (isspace(pstr[i]) && (i <= j))
  35.         i++;
  36.     if (0 < i)
  37.         strcpy(pstr, &pstr[i]);

  38.     return pstr;
  39. }

  40. /* 去除字符串两端空格 */
  41. char *strtrim(char *pstr)
  42. {
  43.     char *p;

  44.     p = strtrimr(pstr);

  45.     return strtriml(p);
  46. }

  47. void free_item_space(item_t * item)
  48. {
  49.     int i;

  50.     if (item == NULL)
  51.         return;

  52.     if (item->comment != NULL) {
  53.         for (i = 0; i < item->num; i++) {
  54.             free(item->comment[i]);
  55.             item->comment[i] = NULL;
  56.         }

  57.         free(item->comment);
  58.         item->comment = NULL;
  59.     }

  60.     item->num = 0;

  61.     if (item->key != NULL) {
  62.         free(item->key);
  63.         item->key = NULL;
  64.     }

  65.     if (item->value != NULL) {
  66.         free(item->value);
  67.         item->value = NULL;
  68.     }

  69.     return;
  70. }

  71. /* 从配置文件读出key或value,数据返回到item_t指针 */
  72. int get_item_from_line(char *line, item_t * item)
  73. {
  74.     char *p = NULL;
  75.     char *p2 = NULL;
  76.     int len = 0;

  77.     p = strtrim(line);
  78.     len = strlen(p);

  79.     if (len <= 0) {
  80.         return -1;
  81.     } else if (p[0] == '#') {
  82.         if (item->num > MAX_ITEM_COMMENT_NUM)
  83.             return 1;

  84.         if (item->comment == NULL) {
  85.             item->comment =
  86.                 (char **)malloc(MAX_ITEM_COMMENT_NUM * sizeof(char *));
  87.             if (item->comment == NULL) {
  88.                 printf("malloc error!\n");
  89.                 return -1;
  90.             }
  91.         }

  92.         item->comment[item->num] = (char *)malloc(len + 1);
  93.         strcpy(item->comment[item->num], p);
  94.         item->num++;
  95.         return 1;
  96.     } else {
  97.         p2 = strchr(p, '=');
  98.         if (p2 == NULL) {
  99.             p2 = strchr(p, ' ');
  100.             if (p2 != NULL) {
  101.                 *p2++ = '\0';
  102.             } else {
  103.                 return -1;
  104.             }
  105.         } else {
  106.             *p2++ = '\0';
  107.         }

  108.         item->key = (char *)malloc(strlen(p) + 1);
  109.         item->value = (char *)malloc(strlen(p2) + 1);
  110.         strcpy(item->key, p);
  111.         strcpy(item->value, p2);
  112.     }

  113.     return 0;
  114. }

  115. int file_to_memory(const char *file, item_t * items, int items_num)
  116. {
  117.     FILE *fp = NULL;
  118.     int i = 0, j = 0, len = 0;
  119.     char *p = NULL;
  120.     char *p2 = NULL;
  121.     char line[MAX_READ_LINE_NUM];

  122.     fp = fopen(file, "r");
  123.     if (fp == NULL) {
  124.         printf("file %s not exit!\n", file);
  125.         return -1;
  126.     }

  127.     memset(items, 0, (items_num * sizeof(item_t)));

  128.     while (fgets(line, MAX_READ_LINE_NUM - 1, fp)) {
  129.         if (i >= MAX_ITEMS_NUM) {
  130.             printf("too much items,only support 100 items!\n");
  131.             return MAX_ITEMS_NUM;
  132.         }

  133.         if (!get_item_from_line(line, &items[i])) {
  134.             i++;
  135.         }
  136.     }

  137.     for (j < 0; j < i + 1; j++) {
  138.         if (items[j].comment == NULL && items[j].key == NULL
  139.             && items[j].value == NULL)
  140.             break;
  141.     }

  142.     fclose(fp);
  143.     fp = NULL;

  144.     return j;
  145. }

  146. int read_conf_value(const char *key, char *value, const char *file)
  147. {
  148.     FILE *fp = NULL;
  149.     item_t item;
  150.     char line[MAX_READ_LINE_NUM];

  151.     if (key == NULL || value == NULL || file == NULL) {
  152.         printf("params error!\n");
  153.         return -1;
  154.     }

  155.     fp = fopen(file, "r");
  156.     if (fp == NULL) {
  157.         printf("file %s not exit!\n", file);
  158.         return -1;
  159.     }

  160.     memset(&item, 0, sizeof(item_t));

  161.     while (fgets(line, MAX_READ_LINE_NUM - 1, fp)) {
  162.         if (!get_item_from_line(line, &item)) {
  163.             if (item.key != NULL && item.value != NULL) {
  164.                 if (!strcmp(item.key, key)) {
  165.                     strcpy(value, item.value);
  166.                     break;
  167.                 }
  168.             }
  169.             free_item_space(&item);
  170.         }
  171.     }

  172.     free_item_space(&item);
  173.     fclose(fp);
  174.     fp = NULL;

  175.     return 0;
  176. }

  177. int write_conf_value(const char *key, char *value, const char *file)
  178. {
  179.     int i = 0, j = 0;
  180.     int num = 0;
  181.     FILE *fp = NULL;
  182.     item_t items[MAX_ITEMS_NUM];
  183.     int flag = 0;

  184.     if (key == NULL || value == NULL || file == NULL) {
  185.         printf("params error!\n");
  186.         return -1;
  187.     }

  188.     num = file_to_memory(file, items, MAX_ITEMS_NUM);
  189.     if (num < 0) {
  190.         return -1;
  191.     }

  192.     for (i = 0; i < num; i++) {
  193.         if (items[i].key != NULL && items[i].value != NULL) {
  194.             if (!strcmp(items[i].key, key)) {
  195.                 strcpy(items[i].value, value);
  196.                 flag = 1;
  197.                 break;
  198.             }
  199.         }
  200.     }

  201.     fp = fopen(file, "w");
  202.     if (fp == NULL) {
  203.         return -1;
  204.     }

  205.     for (i = 0; i < num; i++) {
  206.         if (items[i].comment != NULL) {
  207.             fprintf(fp, "\n");
  208.             for (j = 0; j < items[i].num; j++) {
  209.                 if (items[i].comment[j] != NULL)
  210.                     fprintf(fp, "%s\n", items[i].comment[j]);
  211.             }
  212.             fprintf(fp, "\n");
  213.         }

  214.         if (items[i].key != NULL && items[i].value != NULL)
  215.             fprintf(fp, "%s=%s\n", items[i].key, items[i].value);
  216.     }

  217.     if (flag == 0) {
  218.         fprintf(fp, "%s=%s\n", key, value);
  219.     }

  220.     for (i = 0; i < num; i++) {
  221.         free_item_space(&items[i]);
  222.     }

  223.     fclose(fp);
  224.     fp = NULL;

  225.     return 0;
  226. }
test.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include "inifile.h"

  3. int main(int argc,char **argv)
  4. {
  5.     char value[20];

  6.     write_conf_value("IP_SERVER", "192.168.1.101", "test.conf");

  7.     read_conf_value("IP", value, "test.conf");
  8.     printf("value: %s\n",value);

  9.     return 0;
  10. }
test.conf

点击(此处)折叠或打开

  1. IP=192.168.1.101
  2. DHCP_START=192.168.1.20
  3. DHCP_END=192.168.1.200

  4. ###############

  5. DOMAIN=sam

  6. ###############
  7. #
  8. #ffgfg
  9. #12 hgjgh

  10. IP_SERVER=192.168.1.23

  11. ###############


源码下载地址:

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