前段时间看udhcpd的源代码,后来因为没直接应用,也就放弃了。网上有文章udhcpd详解(搜索一下有很多),其中在介绍udhcpd的配置相关代码时,有如下一句话,学习了这段代码后,以后用到需要读配置文件的地方可以直接使用。恰巧项目需要,就认真学习了下这段代码。不得不说,高人就是高人,写得代码简直是艺术品。我再做任何改动都毁了这件艺术品。我参看的源代码是udhcp-0.9.8。主要文件看的文件就是file.c,相关头文件就是file.h和dhcp.h。下面我贴我应用的代码,建议直接看源代码,我做一点注释。
下面是.c文件。
1 struct general_config_t g_general_config; 2 3 static struct config_keyword keywords[] = { 4 /* keyword[14] handler variable address default[30] */ 5 {"server_ip", read_str, &(g_general_config.server_ip), "192.168.1.50"}, 6 {"my_id", read_str, &(g_general_config.my_id), "lt1000"}, 7 {"position",read_str,&(g_general_config.my_positon),"not set position"}, 8 {"", NULL, NULL, ""}, 9 }; 10 11 /*********************************************************************************************** 12 *函数名 :read_str 13 *函数功能描述:关键字对应的参数为字符串 14 *函数参数 : 输入参数字符串,存储输入参数的地址 15 *函数返回值 : 1 成功返回1 16 *作者 : nelson 17 *函数创建日期 : 2015.12.09 18 *函数修改日期 : 尚未修改 19 *修改人 :尚未修改 20 *修改原因 : 尚未修改 21 *版本 : 1.0 22 *历史版本 : 无 23 ***********************************************************************************************/ 24 int read_str(char *line, void *arg) 25 { 26 char *dest = (char *)arg; //arg是指向数组首地址的地址 27 28 strcpy(dest,line); 29 30 return 1; 31 } 32 33 /*********************************************************************************************** 34 *函数名 :read_config 35 *函数功能描述:读配置文件 36 *函数参数 :file 配置文件文件名 相对路径或者绝对路径都可以 37 *函数返回值 :成功返回1 失败返回0 38 *作者 : nelson 39 *函数创建日期 : 2015.12.09 40 *函数修改日期 : 尚未修改 41 *修改人 :尚未修改 42 *修改原因 : 尚未修改 43 *版本 : 1.0 44 *历史版本 : 无 45 ***********************************************************************************************/ 46 int read_config(char *file) 47 { 48 FILE *in; 49 char buffer[80], *token, *line; 50 int i; 51 52 for (i = 0; strlen(keywords[i].keyword); i++) 53 { 54 if (strlen(keywords[i].def)) 55 keywords[i].handler(keywords[i].def, keywords[i].var); 56 } 57 58 if (!(in = fopen(file, "r"))) 59 { 60 Important_Printf("config file not found,using the default value!"); 61 return 0; 62 } 63 64 while (fgets(buffer, 80, in)) 65 { 66 if (strchr(buffer, ' ')) 67 *(strchr(buffer, ' ')) = '