有时开发项目,需要对数据库等配置放到程序对外面作为配置文件,配置文件对读取
ConfigManager.h
/*
* ConfigManager.h
*
* Created on: 2018年7月28日
* Author: oftenlin
*/ #ifndef CONFIGMANAGER_H_
#define CONFIGMANAGER_H_
#define SIZE_FILENAME 50 class ConfigManager{
public:
ConfigManager(const char *filename){ConfigFileLoad(filename);};
~ConfigManager(){ConfigFileFree();};
//加载ini文件至内存 char gFilename[SIZE_FILENAME];
char *gBuffer;
int gBuflen;
int ConfigFileLoad(const char *filename);
//释放ini文件所占资源
void ConfigFileFree();
int FindSection(const char *section, char **sect1, char **sect2, char **cont1, char **cont2, char **nextsect);
//获取字符串,不带引号
int GetString(const char *section, const char *key, char *value, int size, const char *defvalue);
//获取整数值
int GetInt(const char *section, const char *key, int defvalue);
//获取浮点数
double GetDouble(const char *section, const char *key, double defvalue);
int GetValue(const char *section, const char *key, char *value, int maxlen, const char *defvalue);
//设置字符串:若value为NULL,则删除该key所在行,包括注释
int SetString(const char *section, const char *key, const char *value);
//设置整数值:base取值10、16、8,分别表示10、16、8进制,缺省为10进制
int SetInt(const char *section, const char *key, int value, int base=);
// int iniGetIP(const char *section, const char *key, BasicHashTable *hashtable, int size, const char *defvalue);
}; #endif /* CONFIGMANAGER_H_ */
ConfigManager.cpp
/*
* ConfigManager.cpp
* Created on: 2018年7月28日
* Author: oftenlin
*/ #include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ConfigManager.h" #define SIZE_LINE 100 #define fmin(x, y) (x <= y) ? x : y typedef enum _ELineType_ {
LINE_IDLE, //未处理行
LINE_ERROR, //错误行
LINE_EMPTY, //空白行或注释行
LINE_SECTION, //节定义行
LINE_VALUE //值定义行
} ELineType ; //去除串首尾空格,原串被改写
char *StrStrip(char *s)
{
size_t size;
char *p1, *p2; size = strlen(s);
if (!size)
return s; p2 = s + size - ; while ((p2 >= s) && isspace(*p2))
p2 --;
*(p2 + ) = '\0'; p1 = s;
while (*p1 && isspace(*p1))
p1 ++;
if (s != p1)
memmove(s, p1, p2 - p1 + );
return s;
} //不区分大小写比较字符串
int StriCmp(const char *s1, const char *s2)
{
int ch1, ch2;
do
{
ch1 = (unsigned char)*(s1++);
if ((ch1 >= 'A') && (ch1 <= 'Z'))
ch1 += 0x20; ch2 = (unsigned char)*(s2++);
if ((ch2 >= 'A') && (ch2 <= 'Z'))
ch2 += 0x20;
} while ( ch1 && (ch1 == ch2) );
return(ch1 - ch2);
} //取一行
//输入:数据区(指针及长度)
//输出:行类型、有效内容串(去首尾空格)、注释首、注释尾、下一行首(行尾与下一行首间为换行符)
// 有效内容位置为[buf, rem1)
int GetLine(char *buf, int buflen, char *content, char **rem1, char **rem2, char **nextline)
{
char *cont1, *cont2;
int cntblank, cntCR, cntLF; //连续空格、换行符数量
char isQuot1, isQuot2; //引号
int i;
char *p; //首先断行断注释,支持如下换行符:\r、\n、\r\n、\n\r
cntblank = ;
cntCR = cntLF = ;
isQuot1 = isQuot2 = ;
cont1 = *rem1 = ;
content[] = ;
for (i = , p = buf; i < buflen; i ++, p ++)
{
if (*p == ) {
p ++;
break;
}
//2个CR或LF,行结束
if (cntCR == || cntLF == ) {
p --; //回溯1
break;
}
//CR或LF各1个之后任意字符,行结束
if (cntCR + cntLF >= ) {
break;
}
//CR或LF之后出现其它字符,行结束
if ((cntCR || cntLF) && *p != '\r' && *p != '\n')
break; switch (*p) {
case '\r':
cntCR ++;
break;
case '\n':
cntLF ++;
break;
case '\'':
if (!isQuot2)
isQuot1 = - isQuot1;
break;
case '\"':
if (!isQuot1)
isQuot2 = - isQuot2;
break;
case ';':
case '#':
if (isQuot1 || isQuot2)
break;
if (*rem1 == NULL)
*rem1 = p - cntblank;
break;
default:
if (isspace((unsigned char)*p)) {
cntblank ++;
} else {
cntblank = ;
if ((*rem1 == NULL) && (cont1 == NULL))
cont1 = p;
}
break;
}
} *nextline = p;
*rem2 = p - cntCR - cntLF;
if (*rem1 == NULL)
*rem1 = *rem2;
cont2 = *rem1 - cntblank; if (cont1 == NULL) {
cont1 = cont2;
return LINE_EMPTY;
} i = (int)(cont2 - cont1);
if (i >= SIZE_LINE)
return LINE_ERROR; //内容头尾已无空格
memcpy(content, cont1, i);
content[i] = ; if (content[] == '[' && content[i - ] == ']')
return LINE_SECTION;
if (strchr(content, '=') != NULL)
return LINE_VALUE; return LINE_ERROR;
} //取一节section
//输入:节名称
//输出:成功与否、节名称首、节名称尾、节内容首、节内容尾(含换行)、下一节首(节尾与下一节首间为空行或注释行)
int ConfigManager::FindSection(const char *section, char **sect1, char **sect2, char **cont1, char **cont2, char **nextsect)
{
int type;
char content[SIZE_LINE];
char *rem1, *rem2, *nextline; char *p;
char *empty;
int uselen = ;
char found = ; if (gBuffer == NULL) {
return ;
} while (gBuflen - uselen > ) {
p = gBuffer + uselen;
type = GetLine(p, gBuflen - uselen, content, &rem1, &rem2, &nextline);
uselen += (int)(nextline - p); if (LINE_SECTION == type) {
if (found || section == NULL) break; //发现另一section
content[strlen(content) - ] = ; //去尾部]
StrStrip(content + ); //去首尾空格
if (StriCmp(content + , section) == ) {
found = ;
*sect1 = p;
*sect2 = rem1;
*cont1 = nextline;
}
empty = nextline;
} else
if (LINE_VALUE == type) {
if (!found && section == NULL) {
found = ;
*sect1 = p;
*sect2 = p;
*cont1 = p;
}
empty = nextline;
}
} if (!found) return ; *cont2 = empty;
*nextsect = nextline;
return ;
} //从一行取键、值
//输入:内容串(将被改写)
//输出:键串、值串
void GetKeyValue(char *content, char **key, char **value)
{
char *p; p = strchr(content, '=');
*p = ;
StrStrip(content);
StrStrip(p + );
*key = content;
*value = p + ;
} //释放ini文件所占资源
void ConfigManager:: ConfigFileFree()
{
if (gBuffer != NULL) {
free(gBuffer);
gBuffer = ;
gBuflen = ;
}
} //加载ini文件至内存
int ConfigManager::ConfigFileLoad(const char *filename)
{
FILE *file;
int len; //iniFileFree();
if (strlen(filename) >= sizeof(gFilename))
return ;
strcpy(gFilename, filename); file = fopen(gFilename, "ab+");
if (file == NULL)
return ; fseek(file, , SEEK_END);
len = ftell(file);
gBuffer =(char *) malloc(len);
if (gBuffer == NULL) {
fclose(file);
return ;
} fseek(file, , SEEK_SET);
len = fread(gBuffer, , len, file);
fclose(file);
gBuflen = len;
return ;
} //读取值原始串
int ConfigManager::GetValue(const char *section, const char *key, char *value, int maxlen, const char *defvalue)
{
int type;
char content[SIZE_LINE];
char *rem1, *rem2, *nextline;
char *key0, *value0; char *p;
int uselen = ;
char found = ;
int len; if (gBuffer == NULL || key == NULL) {
if (value != NULL)
value[] = ;
return ;
} while (gBuflen - uselen > ) {
p = gBuffer + uselen;
type = GetLine(p, gBuflen - uselen, content, &rem1, &rem2, &nextline);
uselen += (int)(nextline - p); if (LINE_SECTION == type) {
if (found || section == NULL) break; //发现另一section
content[strlen(content) - ] = ; //去尾部]
StrStrip(content + ); //去首尾空格
if (StriCmp(content + , section) == ) {
found = ;
}
} else
if (LINE_VALUE == type) {
if (!found && section == NULL) {
found = ;
}
if (!found)
continue;
GetKeyValue(content, &key0, &value0);
if (StriCmp(key0, key) == ) {
len = strlen(value0);
if (len == ) break; //空值视为无效
if (value != NULL) {
len = fmin(len, maxlen - );
strncpy(value, value0, len);
value[len] = ;
}
// printf("value = %s\n",value);
return ;
}
}
} //未发现键值取缺省
if (value != NULL) {
if (defvalue != NULL) {
len = fmin(strlen(defvalue), maxlen - );
strncpy(value, defvalue, len);
value[len] = ;
SetString(section, key, defvalue);
} else {
value[] = ;
}
}
return ;
} //获取字符串,不带引号
int ConfigManager::GetString(const char *section, const char *key, char *value, int maxlen, const char *defvalue)
{
int ret;
int len; ret = GetValue(section, key, value, maxlen, defvalue);
if (!ret)
return ret; //去首尾空格
len = strlen(value);
if (value[] == '\'' && value[len - ] == '\'') {
value[len - ] = ;
memmove(value, value + , len - );
} else
if (value[] == '\"' && value[len - ] == '\"') {
value[len - ] = ;
memmove(value, value + , len - );
}
return ret;
} //获取整数值
int ConfigManager::GetInt(const char *section, const char *key, int defvalue)
{
char valstr[]; if (GetValue(section, key, valstr, sizeof(valstr), NULL))
return (int)strtol(valstr, NULL, );
SetInt(section, key,defvalue,);
return defvalue;
} //获取浮点数
double ConfigManager::GetDouble(const char *section, const char *key, double defvalue)
{
char valstr[]; if (GetValue(section, key, valstr, sizeof(valstr), NULL))
return (int)atof(valstr);
return defvalue;
} //设置字符串:若value为NULL,则删除该key所在行,包括注释
int ConfigManager::SetString(const char *section, const char *key, const char *value)
{
FILE *file;
char *sect1, *sect2, *cont1, *cont2, *nextsect;
char *p;
int len, type;
char content[SIZE_LINE];
char *key0, *value0;
char *rem1, *rem2, *nextline; if (gBuffer == NULL) {
return ;
} if (FindSection(section, §1, §2, &cont1, &cont2, &nextsect) == )
{
//未找到节
//value无效则返回
if (value == NULL)
return ;
//在文件尾部添加
file = fopen(gFilename, "ab");
if (file == NULL)
return ;
fprintf(file, "\r\n[%s]\r\n%s=%s;\r\n", section, key, value);
fclose(file);
ConfigFileLoad(gFilename);
return ;
} //找到节,则节内查找key
p = cont1;
len = (int)(cont2 - cont1);
while (len > ) {
type = GetLine(p, len, content, &rem1, &rem2, &nextline);
if (LINE_VALUE == type) {
GetKeyValue(content, &key0, &value0);
if (StriCmp(key0, key) == ) {
//找到key
file = fopen(gFilename, "wb");
if (file == NULL)
return ;
len = (int)(p - gBuffer);
fwrite(gBuffer, , len, file); //写入key之前部分
if (value == NULL) {
//value无效,删除
len = (int)(nextline - gBuffer); //整行连同注释一并删除
} else {
//value有效,改写
fprintf(file, "%s=%s", key, value);
len = (int)(rem1 - gBuffer); //保留尾部原注释!
}
fwrite(gBuffer + len, , gBuflen - len, file); //写入key所在行含注释之后部分
fclose(file);
ConfigFileLoad(gFilename);
return ;
}
} len -= (int)(nextline - p);
p = nextline;
} //未找到key //value无效则返回
if (value == NULL)
return ; //在文件尾部添加
file = fopen(gFilename, "wb");
if (file == NULL)
return ;
len = (int)(cont2 - gBuffer);
fwrite(gBuffer, , len, file); //写入key之前部分
fprintf(file, "%s=%s;\r\n", key, value);
fwrite(gBuffer + len, , gBuflen - len, file); //写入key之后部分
fclose(file);
ConfigFileLoad(gFilename);
return ;
} //设置整数值:base取值10、16、8,分别表示10、16、8进制,缺省为10进制
int ConfigManager::SetInt(const char *section, const char *key, int value, int base)
{
char valstr[]; switch (base) {
case :
sprintf(valstr, "0x%x", value);
return SetString(section, key, valstr);
case :
sprintf(valstr, "0%o", value);
return SetString(section, key, valstr);
default: //
sprintf(valstr, "%d", value);
return SetString(section, key, valstr);
}
}
读取方法
#include "utils/ConfigManager.h"
int main() {
ConfigManager *configManger=new
ConfigManager("config.ini");
char *general_sect="general";
int city = configManger->GetInt(general_sect,"cityid" ,);
//整型数字的获取
}
配置文件
[general]
cityid=1;
dbhost=127.0.0.1;
port=3306;
user=root;
passwd=root;