/* 核动力机器人 盖莫游戏实验室 www.gaimo.net ccsdu2004@yahoo.com.cn 22.11.2008 成都 */ // 功能:操作ini文件 #ifndef G_UTIL_CFG_H #define G_UTIL_CFG_H #include <map> #include <string> #include <fstream> #include <algorithm> #include <boost/lexical_cast.hpp> #include <boost/utility.hpp> //define error status return value. #define ERR_NUM 0xfff #define ERR_STR "" using namespace std; using namespace boost; namespace g { namespace util { class inifile { public: inifile(); ~inifile(); inifile(const std::string); public: typedef map<std::string, map<std::string,std::string> > ::iterator MapItr; typedef map<std::string, std::string>::iterator ValueItr; public: //get string of ini file. string get_data(); //open ini file bool open(const string&); //check file is open or not. bool is_open()const; public: //get pointed int in ini file. int read_int(const string&, const string&); //get pointed float in ini file float read_float(const string&, const string&); //get string in ini file. string read_text(string,string); private: fstream cfgFile; string FileName; map<string, map<string,string> > FileDataMap; string FileData; }; } } #endif /* 核动力机器人 盖莫游戏实验室 www.gaimo.net ccsdu2004@yahoo.com.cn 22.11.2008 成都 */ #include "cfg.hpp" typedef map<string, map<string,string> > ::const_iterator ConMapItr; typedef map<string, map<string,string> > ::iterator MapItr; typedef std::map<string,string> ::iterator SubMapItr; #define GET_VALUE (*(*FileDataMap.find(_index)).second.find(_name)).second #define CHECK_INDEX(_index) FileDataMap.find(_index)==FileDataMap.end() #define CHECK_NAME(_index, _name) / (*FileDataMap.find(_index)).second.find(_name) ==(*FileDataMap.find(_index)).second.end() namespace g { namespace util { inifile::inifile() { } inifile::inifile(const string _fileName) { cfgFile.open(_fileName.c_str()); } inifile::~inifile() { cfgFile.close(); } string inifile::get_data()//const { FileData="/n"; string curLine("/n"); string curBlock("/n"); string curName("/n"); string curValue("/n"); //current block name string curIndexName("/n"); while(!cfgFile.eof()) { getline(cfgFile,curLine); //avoid remar if(curLine.find("--") ==0 || curLine.find("//") ==0) continue; FileData+=curLine+"/n"; //get block name if(curLine.find('[')==0) { curIndexName=curLine; curIndexName.erase(0,1); curIndexName.erase(curLine.size()-2,1); map<string,string> tempMap; tempMap.clear(); FileDataMap.insert(make_pair(curIndexName,tempMap)); } //get data else { curName.assign(curLine,0,curLine.find("=")); curValue.assign(curLine,curLine.find("=")+1,curLine.find("/n")); ((*(FileDataMap.find(curIndexName))).second).insert(make_pair(curName,curValue)); } if(cfgFile.eof()) break; } return FileData; } bool inifile::open(const string& _fileName) { cfgFile.open(_fileName.c_str()); return is_open(); } string inifile::read_text(string _index,string _name) { if(CHECK_INDEX(_index)) return ERR_STR; if(CHECK_NAME(_index, _name)) return ERR_STR; return (*(*FileDataMap.find(_index)).second.find(_name)).second; } int inifile::read_int(const string& _index, const string& _name) { if(CHECK_INDEX(_index)) return ERR_NUM; if(CHECK_NAME(_index,_name)) return ERR_NUM; try { return boost::lexical_cast<int>(GET_VALUE); } catch(...) { return ERR_NUM; } } float inifile::read_float(const string& _index, const string& _name) { if(CHECK_INDEX(_index)) return ERR_NUM; if(CHECK_NAME(_index,_name)) return ERR_NUM; try { return boost::lexical_cast<float>(GET_VALUE); } catch(...) { return ERR_NUM; } } bool inifile::is_open()const { return (cfgFile.fail()==false); } } } 本来还有写功能 但是有点问题 直接去掉了