#include "StdAfx.h"
#include "Config.h"
Config::Config(CString path)
:m_fd(NULL)
{
memset(m_path, 0, 100);
ConvertCStringToChar(path, m_path);
}
Config::~Config() {
}
CString Config::GetAllSectionName() {
char bufLine[256] = {0};
char AllSection[50];
memset(AllSection, 0, 50);
OpenConfigFile();
while(feof(m_fd) == 0) {
memset(bufLine, 0x00, sizeof(bufLine));
fgets(bufLine, sizeof(bufLine), m_fd); // 获取段名
// 判断是否是注释行(以;开头的行就是注释行)或以其他特殊字符开头的行
if (bufLine[0] == '#'
|| bufLine[0] == '\r'
|| bufLine[0] == '\n'
|| bufLine[0] == '\0')
{
continue;
}
// 匹配段名
if (_strnicmp("[", bufLine, 1) == 0)
{
strncat(AllSection, bufLine + 1, strlen(bufLine) - 3);
sprintf(AllSection, "%s;", AllSection);
}
}
int length = strlen(AllSection);
AllSection[length - 1] = '\0';
CloseConfigFile();
return ConvertCharToCString(AllSection, length);
}
CString Config::GetAllKeyNameBySectionName(CString sec_name) {
CString s;
char name[100];
char bufLine[256] = {0};
char AllKey[500];
memset(AllKey, 0, 500);
memset(name, 0, 100);
ConvertCStringToChar(sec_name, name);
OpenConfigFile();
int ret = GetSectionName(name);
if (PSD_CONFIG_OK != ret) {
handle_config_error("GetString");
return s;
}
while (feof(m_fd) == 0){
memset(bufLine, 0x00, sizeof(bufLine));
fgets(bufLine, sizeof(bufLine), m_fd); // 获取字段值
// 判断是否是注释行(以;开头的行就是注释行)
if (bufLine[0] == '#'
|| bufLine[0] == ' '
|| bufLine[0] == '\n')
{
continue;
}
// 匹配配置项名
if (bufLine[0] != '[')
{
if('\n' == bufLine[strlen(bufLine) - 1] ) {
bufLine[strlen(bufLine) - 1] = '\0';
}
char *des = NULL;
des = (char*)malloc(strlen(bufLine));
ClearBlanksOfStr(bufLine, des);
UINT pos;
for (pos = 0; pos < strlen(des); pos++) {
if (des[pos] == '=') {
break;
}
}
strncat(AllKey, des, pos);
sprintf(AllKey, "%s;", AllKey);
continue;
}
else
{
break;
}
}
int length = strlen(AllKey);
AllKey[length - 1] = '\0';
CloseConfigFile();
return ConvertCharToCString(AllKey, length);
}
void Config::ConvertCStringToChar(CString s, char* path) {
int length = s.GetLength();
int i = 0;
while(i < length) {
path[i] = s.GetAt(i);
i++;
}
path[i] = '\0';
}
CString Config::ConvertCharToCString(char *ch, int length) {
CString s;
for(int i = 0; i< length; i++) {
s.Insert(i, ch[i]);
}
return s;
}
void Config::OpenConfigFile() {
m_fd = fopen(m_path,"r");
if(NULL == m_fd ){
handle_config_error("fopen");
}
}
void Config::CloseConfigFile() {
if (m_fd != NULL) {
fclose(m_fd);
}
}
void Config::ClearBlanksOfStr(char* str, char* des) {
int i = 0;
for (UINT pos = 0; pos < strlen(str); pos ++) {
if (str[pos] == ' ')
{
continue;
} else {
des[i] = str[pos];
i++;
continue;
}
}
des[i] = '\0';
}
int Config::GetSectionName(const char* sec) {
char bufLine[256] = {0};
char FullSection[20];
memset(FullSection, 0, 20);
sprintf(FullSection, "[%s]", sec);
while(feof(m_fd) == 0) {
memset(bufLine, 0x00, sizeof(bufLine));
fgets(bufLine, sizeof(bufLine), m_fd); // 获取段名
#ifdef DEBUG_BUF
printf("readline %s", bufLine);
#endif
// 判断是否是注释行(以;开头的行就是注释行)或以其他特殊字符开头的行
if (bufLine[0] == '#'
|| bufLine[0] == '\r'
|| bufLine[0] == '\n'
|| bufLine[0] == '\0')
{
continue;
}
// 匹配段名
if (_strnicmp(FullSection, bufLine, strlen(FullSection)) == 0)
{
#ifdef DEBUG_BUF
printf("\nSuccess find Section %s\n", FullSection);
#endif
return PSD_CONFIG_OK;
}
}
#ifdef DEBUG_BUF
printf("\nCan't find Section %s\n", FullSection);
#endif
return PSD_CONFIG_ERROR;
}
int Config::GetValueBySecion(const char* key, char* pvalue) {
char bufLine[256] = {0};
while (feof(m_fd) == 0){
memset(bufLine, 0x00, sizeof(bufLine));
fgets(bufLine, sizeof(bufLine), m_fd); // 获取字段值
#ifdef DEBUG_BUF
printf("readline %s", bufLine);
#endif
// 判断是否是注释行(以;开头的行就是注释行)
if (bufLine[0] == '#'
|| bufLine[0] == ' ')
{
continue;
}
// 匹配配置项名
if (_strnicmp(key, bufLine, strlen(key)) == 0)
{
if('\n' == bufLine[strlen(bufLine) - 1] ) {
bufLine[strlen(bufLine) - 1] = '\0';
}
char *des = NULL;
des = (char*)malloc(strlen(bufLine));
ClearBlanksOfStr(bufLine, des);
#ifdef DEBUG_BUF
printf("des=[%s]\n", des);
#endif
UINT pos;
for (pos = 0; pos < strlen(des); pos++) {
if (des[pos] == '=') {
break;
}
}
UINT length = strlen(des) - pos;
#ifdef DEBUG_BUF
printf("length=[%u]\n", length);
#endif
strncpy(pvalue, des + pos + 1, length);
pvalue[length - 1] = '\0';
break;
}
else if (bufLine[0] == '[')
{
return PSD_CONFIG_ERROR;
}
}
return PSD_CONFIG_OK;
}
int Config::GetInt(const char* section, const char* key, int *value) {
if ((NULL == section) || (NULL == key)) {
return PSD_CONFIG_ERROR;
}
OpenConfigFile();
char rvalue[256];
memset(rvalue, 0, 256);
int ret = GetSectionName(section);
if (PSD_CONFIG_OK == ret) {
ret = GetValueBySecion(key, rvalue);
} else {
handle_config_error("GetString");
return PSD_CONFIG_ERROR;
}
if (rvalue == NULL) {
handle_config_error("GetString");
return PSD_CONFIG_ERROR;
}
*value = ::atoi(rvalue);
#ifdef DEBUG_BUF
printf("value=[%d]\n", *value);
#endif
CloseConfigFile();
return PSD_CONFIG_OK;
}
int Config::GetBoolean(const char* section, const char* key, bool *value) {
if ((NULL == section) || (NULL == key)) {
return PSD_CONFIG_ERROR;
}
OpenConfigFile();
char rvalue[256];
memset(rvalue, 0, 256);
int ret = GetSectionName(section);
if (PSD_CONFIG_OK == ret) {
ret = GetValueBySecion(key, rvalue);
} else {
handle_config_error("GetBoolean");
return PSD_CONFIG_ERROR;
}
if (rvalue == NULL) {
handle_config_error("GetBoolean");
return PSD_CONFIG_ERROR;
}
#ifdef DEBUG_BUF
printf("rvalue=[%s]\n", rvalue);
#endif
if (0 == strncmp(rvalue, "true", 4)){
*value = true;
} else if (0 == strncmp(rvalue, "false", 5)) {
*value = false;
}
CloseConfigFile();
return PSD_CONFIG_OK;
}
int Config::GetString(const char* section, const char* key, char *value) {
if ((NULL == section) || (NULL == key)) {
return PSD_CONFIG_ERROR;
}
OpenConfigFile();
int ret = GetSectionName(section);
if (PSD_CONFIG_OK == ret) {
ret = GetValueBySecion(key, value);
} else {
return PSD_CONFIG_ERROR;
}
if (value == NULL) {
handle_config_error("GetString");
return PSD_CONFIG_ERROR;
}
#ifdef DEBUG_BUF
printf("value=[%s]\n", value);
#endif
CloseConfigFile();
return PSD_CONFIG_OK;
}
VS2010获取配置文件内容
需积分: 35 180 浏览量
2017-11-17
16:42:00
上传
评论
收藏 23KB RAR 举报
达天下
- 粉丝: 0
- 资源: 6
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈