Hikyuu
|
支持简单的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 §ion) const |
判断指定的section是否存在 更多... | |
bool | hasOption (const std::string §ion, const std::string &option) const |
判断指定option是否存在 更多... | |
StringListPtr | getSectionList () const |
获取所有的secton 更多... | |
StringListPtr | getOptionList (const std::string §ion) const |
获取指定Section下的所有option 更多... | |
std::string | get (const std::string §ion, const std::string &option, const std::string &default_str=std::string()) const |
获取指定的option值 更多... | |
int | getInt (const std::string §ion, const std::string &option, const std::string &default_str=std::string()) const |
获取指定的option值,并将其转换为int类型 更多... | |
float | getFloat (const std::string §ion, const std::string &option, const std::string &default_str=std::string()) const |
获取指定的option值,并将其转换为float类型 更多... | |
double | getDouble (const std::string §ion, const std::string &option, const std::string &default_str=std::string()) const |
获取指定的option值,并将其转换为double类型 更多... | |
bool | getBool (const std::string §ion, 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
typedef std::list<std::string> hku::IniParser::StringList |
typedef boost::shared_ptr<std::list<std::string> > hku::IniParser::StringListPtr |
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 |
IniParser::StringListPtr hku::IniParser::getSectionList | ( | ) | const |
获取所有的secton
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 | 指定文件名 |