依赖项:
依赖于 ProcessString 类,可从该篇博客获取「字符串处理类 ProcessString (包含常用字符串处理函数)」
ParseConfig.h
//Linux & C++11
#pragma once
//包含系统头文件
#include
#include
#include
#include
//宏定义:函数参数流向
#define IN
#define OUT
#define INOUT
//使用命名
using std::string;
using std::exception;
using std::shared_ptr;
using std::make_shared;
using std::map;
namespace sinicheveen
{
class ParseConfig
{
public:
//group 1:解析、获取配置
//解析配置文件到 map 中
static void parseConfFileIntoMap();
//输出 m_mConfs 中的配置项
static void printConfInMap();
//从 m_mConfs 中获取某个 section 下的某项配置
static string getSpecConfValFromMap(IN const string& strSectionName, IN const string& strConfName);
//从 m_mConfs 中获取某项配置,如果在多个 section 下有相同的配置名,则获取第一个 seciton 下的配置值
static string getSpecConfValFromMap(IN const string& strConfName);
//从配置文件中获取某个 section 下的某项配置
static string getSpecConfValFromFile(IN const string& strSectionName, IN const string& strConfName);
//从配置文件中获取某项配置,如果在多个 section 下有相同的配置名,则获取第一个 seciton 下的配置值
static string getSpecConfValFromFile(IN const string& strConfName);
//guoup 2:格式校验
//校验 section name 格式,格式必须是 [XXXX]
static void checkSectionNameFromat(INOUT const string& strConf, IN const uint32_t lineNum);
//校验配置项格式
static void checkConfItemFormat(INOUT const string& strConf, IN const uint32_t lineNum);
//group 3:静态成员初始化
//初始化静态成员 m_mConfs
static map> initStaticMapConfs();
//group 4:静态变量
private:
static string m_strConfFilePath;
static map > m_mConfs;
}; //ParseConfig
} //sinicheveen
ParseConfig.cpp
未完 ......
点击访问原文(进入后根据右侧标签,快速定位到本文)
本文介绍了一个用C++实现的配置文件解析器类ParseConfig,包括解析配置文件、获取配置值及格式校验等功能。提供了从配置文件或内部映射中获取特定配置的方法。
1920

被折叠的 条评论
为什么被折叠?



