hikyuu2/hikyuu_cpp/hikyuu/utilities/IniParser.h

100 lines
2.9 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>
2019-12-31 23:18:30 +08:00
#include <memory>
2015-01-07 01:26:14 +08:00
#if defined(_MSC_VER)
2019-11-10 19:45:57 +08:00
#pragma warning(disable : 4251)
#pragma warning(disable : 4275)
#pragma warning(disable : 4290)
2015-01-07 01:26:14 +08:00
#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
*/
2019-11-10 19:45:57 +08:00
2019-12-31 23:18:30 +08:00
class HKU_API IniParser {
2015-01-07 01:26:14 +08:00
public:
typedef std::list<std::string> StringList;
2019-12-31 23:18:30 +08:00
typedef std::shared_ptr<std::list<std::string> > StringListPtr;
IniParser(const IniParser&) = delete;
IniParser& operator=(const IniParser&) = delete;
2015-01-07 01:26:14 +08:00
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,
2019-11-10 19:45:57 +08:00
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,
2019-11-10 19:45:57 +08:00
const std::string& default_str = std::string()) const;
2015-01-07 01:26:14 +08:00
float getFloat(const std::string& section, const std::string& option,
2019-11-10 19:45:57 +08:00
const std::string& default_str = std::string()) const;
2015-01-07 01:26:14 +08:00
double getDouble(const std::string& section, const std::string& option,
2019-11-10 19:45:57 +08:00
const std::string& default_str = std::string()) const;
2015-01-07 01:26:14 +08:00
bool getBool(const std::string& section, const std::string& option,
2019-11-10 19:45:57 +08:00
const std::string& default_str = std::string()) const;
2015-01-07 01:26:14 +08:00
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;
};
2019-11-10 19:45:57 +08:00
} // namespace hku
2015-01-07 01:26:14 +08:00
#endif /* INIFILE_H_ */