hikyuu2/hikyuu_cpp/hikyuu/utilities/IniParser.h

97 lines
2.8 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* IniFile.h
*
* Created on: 2010-5-19
* Author: fasiondog
*/
#pragma once
2015-01-07 01:26:14 +08:00
#ifndef INIPARSER_H_
#define INIPARSER_H_
#include <stdexcept>
#include <string>
#include <list>
#include <map>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#if defined(_MSC_VER)
2015-01-07 01:26:14 +08:00
#pragma warning(disable: 4251)
#pragma warning(disable: 4275)
#pragma warning(disable: 4290)
#endif
#ifndef HKU_API
#define HKU_API
#endif
namespace hku {
/**
* ini格式文件的读取 \n
* @details
* ini文件格式如下, ";"\n
* [section1] \n
* ; \n
* key1 = value1 \n
* key2 = value2 \n
* \n
* [section2] \n
* ; \n
* key1 = value1 ;1 \n
* key2 = value2 ;2 \n
*
* @note section可以在不同的位置定义section中包含同名的optionkey
* optionkeysection
* \n
* read成员方法全部读入后\n
* \n
*
* @author fasiondog
* @date 20100519
* @ingroup Utilities
2015-01-07 01:26:14 +08:00
*/
class HKU_API IniParser : boost::noncopyable {
public:
typedef std::list<std::string> StringList;
typedef boost::shared_ptr<std::list<std::string> > StringListPtr;
IniParser();
virtual ~IniParser();
void read(const std::string& filename);
2015-01-07 01:26:14 +08:00
void clear();
bool hasSection(const std::string& section) const;
bool hasOption(const std::string& section, const std::string& option) const;
StringListPtr getSectionList() const;
StringListPtr getOptionList(const std::string& section) const;
std::string get(const std::string& section, const std::string& option,
const std::string& default_str = std::string()) const;
2015-01-07 01:26:14 +08:00
//以下默认值类型使用string的原因是因为int/float/double/bool类型没有空对象
int getInt(const std::string& section, const std::string& option,
const std::string& default_str = std::string()) const;
float getFloat(const std::string& section, const std::string& option,
const std::string& default_str = std::string()) const;
double getDouble(const std::string& section, const std::string& option,
const std::string& default_str = std::string()) const;
bool getBool(const std::string& section, const std::string& option,
const std::string& default_str = std::string()) const;
private:
typedef std::map<std::string, std::string> item_map_type;
typedef std::map<std::string, item_map_type> section_map_type;
section_map_type m_sections;
};
} /* namespace */
#endif /* INIFILE_H_ */