📄 config.h
字号:
/*************************************************************************** DSemu - The Next Generation ** Configuration file parser [config.h] ** Copyright Imran Nazar, 2005; released under the BSD public licence. ***************************************************************************/#ifndef __CONFIG_H_#define __CONFIG_H_#include "defs.h"#include <iostream>#include <fstream>#include <string>#include <map>// ACK: Kniht/EFnet#C++ (The original idea, and most all the code too)using namespace std;struct Config{ typedef map< string, string > map_t; typedef map_t::iterator iterator; typedef map_t::const_iterator const_iterator; //---Make this look just like a map, by wrapping map's functions iterator begin() { return map_.begin(); } const_iterator begin() const { return map_.begin(); } iterator end() { return map_.end(); } const_iterator end() const { return map_.end(); } void clear() { map_.clear(); } bool empty() const { return map_.empty(); } bool exists(string const &k) { return map_.count(k); } string& operator[](string const &k) { return map_[k]; } iterator find(string const &k) { return map_.find(k); } const_iterator find(string const &k) const { return map_.find(k); } void swap(Config &other) { map_.swap(other.map_); } //---Stream overloads to allow =-delimited read/write friend basic_ostream<string::value_type> &operator<<(basic_ostream<string::value_type>& out, Config const& val) { for(Config::const_iterator i=val.begin(), end=val.end(); i!=end; ++i) out << i->first << "=" << i->second << '\n'; out << '\n'; return out; } friend basic_istream<string::value_type> &operator>>(basic_istream<string::value_type>& in, Config& val) { Config input; for(string line; getline(in, line); ) { if((line[0] != '#') && line.length()) { // Windows will drop the second newline char, Unix won't. if((line[line.length()-1] == '\r') || (line[line.length()-1] == '\n')) line.erase(line.length()-1); string::size_type split = line.find(std::string("="),0); if(split != string::npos) { string key = line.substr(0,split); string val = line.substr(split+1); input[key]=val; } } } if(!input.empty()) input.swap(val); else in.setstate(ios::failbit); return in; } //---Userfriendly functions to parse an .ini bool write(char const *fname) { basic_ofstream<string::value_type> out(fname); out << *this; return out.good(); } bool read(char const *fname) { basic_ifstream<string::value_type> in(fname); in >> *this; return in.good(); } private: map_t map_;};#endif//__CONFIG_H_/*** EOF: config.h *******************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -