Hikyuu
Public 类型 | Public 成员函数 | 所有成员列表

支持简单的ini格式文件的读取
更多...

#include <IniParser.h>

继承自 noncopyable .

Public 类型

typedef std::list< std::string > StringList
 
typedef boost::shared_ptr< std::list< std::string > > StringListPtr
 

Public 成员函数

 IniParser ()
 
virtual ~IniParser ()
 
void read (const std::string &filename)
 读取并分析指定的ini文件 更多...
 
void clear ()
 清除现有已读入的信息 更多...
 
bool hasSection (const std::string &section) const
 判断指定的section是否存在 更多...
 
bool hasOption (const std::string &section, const std::string &option) const
 判断指定option是否存在 更多...
 
StringListPtr getSectionList () const
 获取所有的secton 更多...
 
StringListPtr getOptionList (const std::string &section) const
 获取指定Section下的所有option 更多...
 
std::string get (const std::string &section, const std::string &option, const std::string &default_str=std::string()) const
 获取指定的option值 更多...
 
int getInt (const std::string &section, const std::string &option, const std::string &default_str=std::string()) const
 获取指定的option值,并将其转换为int类型 更多...
 
float getFloat (const std::string &section, const std::string &option, const std::string &default_str=std::string()) const
 获取指定的option值,并将其转换为float类型 更多...
 
double getDouble (const std::string &section, const std::string &option, const std::string &default_str=std::string()) const
 获取指定的option值,并将其转换为double类型 更多...
 
bool getBool (const std::string &section, const std::string &option, const std::string &default_str=std::string()) const
 获取指定的option值,并将其转换为bool类型 更多...
 

详细描述

支持简单的ini格式文件的读取

典型的ini文件格式如下, 其中";"为行注释符号:
[section1]
;第一段
key1 = value1
key2 = value2

[section2]
;第二段
key1 = value1 ;注释1
key2 = value2 ;注释2

注解
同一个section可以在不同的位置定义,但如果不同位置的section中包含同名的option(key), 则该option(key)的值为最后读入的值。这可能造成潜在的错误。建议不要将同一个section 在不同的位置定义。
对于配置信息分散在多个文件中的情况,可以通过多次调用read成员方法全部读入后,再统一处理。
该类目前不支持复制操作,暂时没有此需求
作者
fasiondog
日期
20100519

成员类型定义说明

typedef std::list<std::string> hku::IniParser::StringList
typedef boost::shared_ptr<std::list<std::string> > hku::IniParser::StringListPtr

构造及析构函数说明

hku::IniParser::IniParser ( )
hku::IniParser::~IniParser ( )
virtual

成员函数说明

void hku::IniParser::clear ( )

清除现有已读入的信息

std::string hku::IniParser::get ( const std::string &  section,
const std::string &  option,
const std::string &  default_str = std::string() 
) const

获取指定的option值

异常
std::invalid_argument当指定option不存在对应值,并且没有指定缺省值时,抛出该异常
参数
section指定的section
option指定的option
default_str缺省值,在不存在对应的option值时,返回该值。当该值为空时,表示没有缺省值,此时如果不存在对应option值, 将抛出std::invalid_argument异常。默认为空,没有指定缺省值。
bool hku::IniParser::getBool ( const std::string &  section,
const std::string &  option,
const std::string &  default_str = std::string() 
) const

获取指定的option值,并将其转换为bool类型

option表示为true时,可接受的值为:1|true|yes|on (不区分大小写)
option表示为false时,可接受的值为:0|false|no|off (不区分大小写)

异常
std::invalid_argument当指定option不存在对应值,并且没有指定缺省值时,抛出该异常; 或者当指定的缺省值(非空)无法转换为bool类型时,无论指定的option值是否存在都将抛出该异常。
std::domain_error当指定的option值,无法转换为bool类型时,抛出该异常
参数
section指定的section
option指定的option
default_str缺省值。在不存在对应的option值时,返回该值。当该值为空时,表示没有缺省值。 默认为空,没有指定缺省值。
double hku::IniParser::getDouble ( const std::string &  section,
const std::string &  option,
const std::string &  default_str = std::string() 
) const

获取指定的option值,并将其转换为double类型

异常
std::invalid_argument当指定option不存在对应值,并且没有指定缺省值时,抛出该异常; 或者当指定的缺省值(非空)无法转换为double类型时,无论指定的option值是否存在都将抛出该异常。
std::domain_error当指定的option值,无法转换为double类型时,抛出该异常
参数
section指定的section
option指定的option
default_str缺省值。在不存在对应的option值时,返回该值。当该值为空时,表示没有缺省值。 默认为空,没有指定缺省值。
float hku::IniParser::getFloat ( const std::string &  section,
const std::string &  option,
const std::string &  default_str = std::string() 
) const

获取指定的option值,并将其转换为float类型

异常
std::invalid_argument当指定option不存在对应值,并且没有指定缺省值时,抛出该异常; 或者当指定的缺省值(非空)无法转换为float类型时,无论指定的option值是否存在都将抛出该异常。
std::domain_error当指定的option值,无法转换为float类型时,抛出该异常
参数
section指定的section
option指定的option
default_str缺省值。在不存在对应的option值时,返回该值。当该值为空时,表示没有缺省值。 默认为空,没有指定缺省值。
int hku::IniParser::getInt ( const std::string &  section,
const std::string &  option,
const std::string &  default_str = std::string() 
) const

获取指定的option值,并将其转换为int类型

异常
std::invalid_argument当指定option不存在对应值,并且没有指定缺省值时,抛出该异常; 或者当指定的缺省值(非空)无法转换为int类型时,无论指定的option值是否存在都将抛出该异常。
std::domain_error当指定的option值,无法转换为int类型时,抛出该异常
参数
section指定的section
option指定的option
default_str缺省值。在不存在对应的option值时,返回该值。当该值为空时,表示没有缺省值。 默认为空,没有指定缺省值。
IniParser::StringListPtr hku::IniParser::getOptionList ( const std::string &  section) const

获取指定Section下的所有option

异常
若指定的section不存在,将抛出std::invalid_argument异常
参数
section指定的section
返回
指定的section下所有option列表
IniParser::StringListPtr hku::IniParser::getSectionList ( ) const

获取所有的secton

返回
所有的section列表
bool hku::IniParser::hasOption ( const std::string &  section,
const std::string &  option 
) const

判断指定option是否存在

bool hku::IniParser::hasSection ( const std::string &  section) const

判断指定的section是否存在

void hku::IniParser::read ( const std::string &  filename)

读取并分析指定的ini文件

可多次读取不同的ini文件后,再统一处理信息

异常
std::invalid_argument当指定的文件无法打开时,抛出该异常
std::logic_error当文件格式错误时,抛出该异常
参数
filename指定文件名

该类的文档由以下文件生成: