找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4206|回复: 0

C语言实现的配置文件读取功能

[复制链接]
发表于 2011-12-23 09:24:50 | 显示全部楼层 |阅读模式
1. 首先声明, 代码主要是从Linux udev程序中提取出来的.
2.  支持的配置文件格式如下:
  1. #comment
  2. var_name1 = "XXX"                  #comment
  3. var_name2 = "QQQ"                #comment
  4. 3. config_file.h
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include
  8. <syslog.h>
  9. #define    MAX_PATH_LEN         (512)
  10. #define    MAX_FILE_NAME_LEN   
  11. (128)
复制代码


/* 将配置文件中的配置项解析后存放到全局数组中 */
int parse_config_file(char *path_to_config_file);   

/* 从全局数组中读取一个个配置项。例如  get_config_var("var_name1");  */
char * get_config_var(char *var_name);  
4.  config_file.c
  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include
  4. <sys/socket.h>
  5. #include <unistd.h>
  6. #include
  7. <stdio.h>
  8. #include <stdlib.h>
  9. #include
  10. <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #include
  13. <errno.h>
  14. #include <ctype.h>
  15. #include <syslog.h>
  16. #include <fcntl.h>
  17. #include
  18. <sys/resource.h>
  19. #include <signal.h>
  20. #include "config_file.h"
  21. #define    MAX_VAR_NUM          (16)
  22. #define    MAX_VAR_NAME_LEN     
  23. (128)
  24. #define    MAX_VAR_VALUE_LEN    (MAX_PATH_LEN)
  25. #define COMMENT_CHARACTER   '#'
  26. #define LINE_SIZE    (768)
  27. char ga_variables[MAX_VAR_NUM][MAX_VAR_NAME_LEN + 1];
  28. char
  29. ga_values[MAX_VAR_NUM][MAX_VAR_VALUE_LEN + 1];
  30. int  g_var_num = 0;
  31. void get_dir_path_of_file(char *file, char *dir_path)
  32. {
  33. char
  34. *temp;
  35.     strncpy(dir_path, file, MAX_PATH_LEN);
  36.     temp =
  37. strrchr(dir_path, '/');
  38.     temp[0] = '\0';
  39. }
  40. void remove_trailing_chars(char *path, char c)
  41. {
  42. size_t len;
  43. len = strlen(path);
  44. while (len > 0 && path[len-1] ==
  45. c)
  46.   path[--len] = '\0';
  47. }
  48. int get_key(char **line, char **key, char **value)
  49. {
  50. char
  51. *linepos;
  52. char *temp;
  53. linepos = *line;
  54. if (!linepos)
  55.     {   
  56.   return -1;
  57.     }
  58. /* skip whitespace */
  59. while (isspace(linepos[0]))
  60.   linepos++;
  61. if (linepos[0] == '\0')
  62.     {   
  63.   return -1;
  64.     }
  65. /* get the key */
  66. *key = linepos;
  67. while (1)
  68. {
  69.   linepos++;
  70.   if (linepos[0] == '\0')
  71.         {
  72.    return
  73. -1;
  74.         }
  75.   if (isspace(linepos[0]))
  76.    break;
  77.   if (linepos[0]
  78. == '=')
  79.    break;
  80. }
  81. /* terminate key */
  82. linepos[0] = '\0';
  83. while (1) {
  84.   linepos++;
  85.   if (linepos[0] == '\0')
  86.         
  87. {
  88.    return -1;
  89.         }
  90.   if
  91. (isspace(linepos[0]))
  92.    continue;
  93.   if (linepos[0] ==
  94. '=')
  95.    continue;
  96.         break;
  97. }
  98. /* get the value*/
  99. if (linepos[0] == '"')
  100.     {   
  101.   linepos++;
  102.     }
  103. else
  104.     {
  105.   return -1;
  106.     }
  107. *value
  108. = linepos;
  109. temp = strchr(linepos, '"');
  110. if (!temp)
  111.     {
  112.   return -1;
  113.    
  114. }
  115.    
  116. temp[0] = '\0';
  117. return 0;
  118. }
  119. int parse_config_file(char *path_to_config_file)
  120. {
  121. char
  122. line[LINE_SIZE + 2];
  123. char *bufline;
  124. char *linepos;
  125. char
  126. *variable;
  127. char *value;
  128. char *buf;
  129. size_t bufsize;
  130. size_t
  131. cur;
  132. size_t count;
  133. int lineno;
  134. int retval = 0;
  135.     FILE *cfg_file = fopen(path_to_config_file, "r");
  136. if (NULL ==
  137. cfg_file)
  138.     {
  139.   ErrSysLog("can't open '%s' as config file: %s",
  140. path_to_config_file, strerror(errno));
  141.   goto EXIT;
  142. }
  143. /* loop through the whole file */
  144. lineno = 0;
  145. cur = 0;
  146. while
  147. (NULL != fgets(line, sizeof(line), cfg_file))
  148.    
  149. {
  150.   lineno++;
  151.   bufline = line;
  152.   count = strlen(line);
  153.   if (count > LINE_SIZE)
  154.         {
  155.    ErrSysLog("line too long,
  156. conf line skipped %s, line %d", path_to_config_file,
  157. lineno);
  158.    continue;
  159.   }
  160.   /* eat the whitespace */
  161.   while ((count > 0) &&
  162. isspace(bufline[0]))
  163.         
  164. {
  165.    bufline++;
  166.    count--;
  167.   }
  168.   if (count ==
  169. 0)
  170.    continue;
  171.   /* see if this is a comment */
  172.   if (bufline[0] ==
  173. COMMENT_CHARACTER)
  174.    continue;
  175.   memcpy(line, bufline, count);
  176.   line[count] = '\0';
  177.   linepos = line;
  178.   retval = get_key(&linepos, &variable,
  179. &value);
  180.   if (retval != 0)
  181.         {
  182.    ErrSysLog("error parsing
  183. %s, line %d:%d", path_to_config_file, lineno,
  184. (int)(linepos-line));
  185.    continue;
  186.   }
  187.         if (g_var_num >= MAX_VAR_NUM)
  188.         {
  189.    ErrSysLog("too
  190. many vars in  %s, line %d:%d", path_to_config_file, lineno,
  191. (int)(linepos-line));
  192.    continue;
  193.   }
  194.         if (strlen(variable) > MAX_VAR_NAME_LEN)
  195.         
  196. {
  197.    ErrSysLog("var name to long %s, line %d:%d", path_to_config_file,
  198. lineno, (int)(linepos-line));
  199.    continue;
  200.   }
  201.         if (strlen(value) > MAX_VAR_VALUE_LEN)
  202.         
  203. {
  204.    ErrSysLog("value to long %s, line %d:%d", path_to_config_file, lineno,
  205. (int)(linepos-line));
  206.    continue;
  207.   }
  208.   strncpy(ga_variables[g_var_num], variable,
  209. sizeof(ga_variables[g_var_num]));
  210.         remove_trailing_chars(value,
  211. '/');
  212.   strncpy(ga_values[g_var_num], value,
  213. sizeof(ga_values[g_var_num]));
  214.         g_var_num++;
  215.         continue;
  216. }
  217. EXIT:
  218. fclose(cfg_file);
  219. return g_var_num;
  220. }
  221. char * get_config_var(char *var_name)
  222. {
  223.     int i;
  224. for(i = 0; i
  225. < g_var_num; i++)
  226.     {
  227.   if (strcasecmp(ga_variables[i], var_name) ==
  228. 0)
  229.         {
  230.    return ga_values[i];
  231.   }
  232.     }
  233.     ErrSysLog("get %s failed", var_name);
  234. return NULL;
  235.    
  236. }
  237. void print_all_vars()
  238. {
  239.     int i;
  240.     ErrPrintf("g_var_num == %d",
  241. g_var_num);
  242. for(i = 0; i < g_var_num; i++)
  243.     {
  244.         printf("%s
  245. = %s\n", ga_variables[i], ga_values[i]);
  246.     }
  247.    
  248.    
  249. }
复制代码



作者:crazycoder8848 发表于2011-12-23 0:32:27 原文链接
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-26 05:44 , Processed in 0.014332 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表