代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
配置文件读取
作者:
善培
/ 发布于
2015/6/25
/
657
/***************************** writed by zhangliang email: liangzhws@foxmail.com date:2016/6/19 *****************************/ /************************* data model CFG---------- \----------- | cg_count | CFGNODE----->| cn_count | | next |--- | node_name | \ | \0 | \ | key | | | \0 | | | value | | | key | | | \0 | | | value | | . / . / . _ / CFGNODE----->| cn_count |< | next |----> NULL | node_name | | \0 | . . . *************************/ #ifndef _CFG_H_ #define _CFG_H_ 1 typedef struct ConfigNode { int cn_count; struct ConfigNode * next; char cn_buf[]; }ConfigNode_t,* CFGNODE; typedef struct Config { int cg_count; char cg_buf[]; }Config_t, * CFG; /** @name: readConfig @function: init CFGfrom config file @param1: #const char * fileptr# file path @return: #CFG# return null is failure ,other is success */ CFG readConfig(const char * fileptr); /** @name: closeConfig @function: destory CFG @param1: #CFG cgptr# CFGobject @return: #void# */ void closeConfig(CFG cgptr); /** @name: getConfigNode @function: get the node from config_t object @param1: #const CFG* cgptr# CFGobject; @param2: #char * nameptr# node's name @return: #CFGNODE*# return null is the node not exist in config, other return the config_node object */ CFGNODE getConfigNode(const CFG cgptr, const char * nameptr); /** @name: getConfigField @function: get the value of key from config_node object @param1: #CFGNODE cnptr# config_node object @param2: #const char * keyptr# key's name */ char * getConfigField(CFGNODE cnptr, const char * keyptr); /** @name: getConfigNodeField @function: get the value of key from config object @param1: #CFG cgptr# CFGobject @param2: #const char * nodeptr# node's name @param3: #const char * keyptr# key's name @return: #char * # return NULL is means the node not exist or the value is empty, other is value */ char * getConfigNodeField(const CFG cgptr, const char * nodeptr, const char * keyptr); /** */ #endif /* _CFG_H_ */ #include "cfg.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXLINE 1024 #define CONFIGNODE 0 #define CONFIGFIELD 1 #define CONFIGNONE 2 static char * trim(char * sptr) { char * pend = sptr + strlen(sptr)-1; char * pstart = sptr; int n = 0; for (;pstart <= pend && *pstart == ' '; ++pstart); for (;pend > pstart && *pend == ' '; --pend); *++pend = '\0'; n = pend - pstart + 1; memmove(sptr, pstart, n); return sptr; } static int parseConfig(char * lineptr,int * type, char * nodeptr, char * keyptr, char * valueptr) { int n = 0; int flag = 0; char * pnode = nodeptr; char * pkey = keyptr; char * pval = valueptr; char * line = strdup(lineptr); char * p = trim(line); if ('[' == *p) { ++p; while (*p != ']') { if (*p != ' ' && *p != '\t') { if (nodeptr != NULL) *pnode++ = *p; ++n; } if (*++p == '\0') { n = 0; /* not good format, '[' not exist ']' to matching */ if (type != NULL) *type = CONFIGNONE; if (nodeptr != NULL) *nodeptr = '\0'; break; } } n += 1; /* for store '\0' */ if (type != NULL) *type = CONFIGNODE; if (nodeptr != NULL) *pnode = '\0'; } else if ('#' == *p || '=' == *p) { n = 0; if (type != NULL) *type = CONFIGNONE; } else { while (*p != '#' && *p != '\0' && *p != '\n') { if ('=' == *p) { flag = 1; } else if (*p != ' ') { if (0 == flag && keyptr != NULL) { *pkey++ = *p; } else if(1 == flag && valueptr != NULL){ *pval++ = *p; } ++n; } ++p; } n = n + (n != 0 && 0 == flag ? 1 : 0); if (0 == n) { if (type != NULL) *type = CONFIGNONE; } else { n += 2; /* for store '\0' */ if (type != NULL) *type = CONFIGFIELD; if (0 == flag && valueptr != NULL) { *pval++ = '1'; } if (keyptr != NULL) { *pkey = '\0'; } if (valueptr != NULL) { *pval = '\0'; } } } free(line); return n; } CFGNODE getConfigNode(const CFG cgptr, const char * nameptr) { int i = 0; ConfigNode_t * cnptr; char * nptr; cnptr = (ConfigNode_t *)cgptr->cg_buf; for (i = 0; i < cgptr->cg_count; ++i) { nptr = (char *)cnptr + sizeof(ConfigNode_t); if (strcmp(nameptr, nptr) == 0) { return cnptr; } cnptr = cnptr->next; } return NULL; } char * getConfigField(CFGNODE cnptr, const char * keyptr) { int i = 0; char * nptr; char * kptr; char * vptr; nptr = (char *)cnptr + sizeof(ConfigNode_t); kptr = nptr + strlen(nptr) + 1; for (i = 0; i < cnptr->cn_count; ++i) { vptr = kptr + strlen(kptr) + 1; if (strcmp(kptr, keyptr) == 0) { return vptr; } kptr = vptr + strlen(vptr) + 1; } return NULL; } char * getConfigNodeField(const CFG cgptr, const char * nameptr, const char * keyptr) { ConfigNode_t * cnptr; if (NULL == (cnptr = getConfigNode(cgptr, nameptr))){ return NULL; } return getConfigField(cnptr, keyptr); } CFG readConfig(const char * fileptr) { char line[MAXLINE] = ""; char node[MAXLINE] = ""; char key[MAXLINE] = ""; char value[MAXLINE] = ""; int type; int nodecount = 0; Config_t * cgptr = NULL; ConfigNode_t * cnptr = NULL; ConfigNode_t * cnprevptr = NULL; char * punUsed = NULL; int n = 0; FILE * fp; fp = fopen(fileptr, "r"); if (NULL == fp) { return NULL; } while (fgets(line, sizeof(line), fp)) { n += parseConfig(line, &type, NULL, NULL, NULL); if (CONFIGNODE == type) ++nodecount; } n = n + sizeof(Config_t) + nodecount * sizeof(ConfigNode_t); cgptr = (Config_t *)malloc(n); cgptr->cg_count = nodecount; punUsed = cgptr->cg_buf; fseek(fp, 0, SEEK_SET); while (fgets(line, sizeof(line), fp)) { parseConfig(line, &type, node, key, value); switch (type) { case CONFIGNODE: cnptr = (ConfigNode_t *) punUsed; cnptr->cn_count = 0; cnptr->next = NULL; punUsed += sizeof(ConfigNode_t); strcpy(punUsed, node); punUsed += strlen(punUsed) + 1; if (cnprevptr != NULL) { cnprevptr->next = cnptr; } cnprevptr = cnptr; break; case CONFIGFIELD: ++cnptr->cn_count; strcpy(punUsed, key); punUsed += strlen(punUsed) + 1; strcpy(punUsed, value); punUsed += strlen(punUsed) + 1; break; } } fclose(fp); return (CFG)cgptr; } void closeConfig(CFG cgptr) { free((char *) cgptr); } #include "../src/cfg.h" #include <stdio.h> int main(int argc, char * argv[]) { CFG cfg; cfg = readConfig(argv[1]); printf("%s %s=%s\n",argv[2],argv[3], getConfigNodeField(cfg, argv[2], argv[3])); closeConfig(cfg); return 0; }
试试其它关键字
配置文件
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
善培
贡献的其它代码
(
14
)
.
删除文件夹,指定文件夹,删除文件夹和所有文件,删除
.
查询字符串内有多少个指定字符
.
Node.js 连接池
.
如何检查cookie是否启用
.
取汉字拼音首字母含多音字及不常见的字
.
二分图最大匹配(hungary邻接表形式,邻接阵接口)
.
重命名文件
.
计算权重
.
配置文件读取
.
微信信息servlet端的处理
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3