Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

WBIniFile.h

00001 // IniFile Loader
00002 // Contributed from Gimite
00003 
00004 #ifndef WBINIFILE_H
00005 #define WBINIFILE_H
00006 
00007 #include <windows.h>
00008 #include <string>
00009 #include <boost/lexical_cast.hpp>
00010 
00011 
00012 //INIƒtƒ@ƒCƒ‹‚©‚çÝ’è‚ð“ǂݍž‚ށB
00013 class WBIniFile
00014 {
00015 public:
00016     //‚±‚ê‚ðÅ‰‚ɌĂñ‚Ńtƒ@ƒCƒ‹–¼‚ðƒZƒbƒgB
00017     static void setFileName(const std::string& fn);
00018     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA’l‚ð“ǂݍž‚ށB
00019     static std::string getString(const std::string& section, const std::string& key);
00020     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA’l‚ð“ǂݍž‚ށB
00021     //‘¶Ý‚µ‚È‚¢Žž‚ÍdefValue‚ð•Ô‚·B
00022     static std::string getString(const std::string& section, const std::string& key, const std::string& defValue);
00023     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA•¶Žš—ñ‚ð‘‚«ž‚ށB
00024     static void setString(const std::string& section, const std::string& key, const std::string& value);
00025 
00026     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA’l‚ð“ǂݍž‚݁AŽw’è‚ÌŒ^‚ɕϊ·B
00027     //—á: int markers= WBIniFile::getValue<int>("hoge", "markers");
00028     template <class T>
00029     static T getValue(const std::string& section, const std::string& key){
00030         //‰½ŒÌ‚©lexical_cast‚¾‚ÆVec3d‚ª“ǂ߂Ȃ¢cB
00031         std::istringstream ss(getString(section, key));
00032         T value;
00033         if (ss >> value){
00034             return value;
00035         }else{
00036             onError("INIƒtƒ@ƒCƒ‹‚Ì’l‚ª•ςł·B\n"+fileName+"\nƒZƒNƒVƒ‡ƒ“:"+section+" ƒL[:"+key);
00037             return T();
00038         }
00039     }
00040     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA’l‚ð“ǂݍž‚݁AŽw’è‚ÌŒ^‚ɕϊ·B
00041     //‘¶Ý‚µ‚È‚¢Žž‚ÍdefValue‚ð•Ô‚·B
00042     //—á: int markers= WBIniFile::getValue<int>("hoge", "markers", 3);
00043     template <class T>
00044     static T getValue(const std::string& section, const std::string& key, const T& defValue){
00045         //‰½ŒÌ‚©lexical_cast‚¾‚ÆVec3d‚ª“ǂ߂Ȃ¢cB
00046         std::string str= getString(section, key);
00047         if (str=="") return defValue;
00048         std::istringstream ss(str);
00049         T value;
00050         if (ss >> value){
00051             return value;
00052         }else{
00053             onError("INIƒtƒ@ƒCƒ‹‚Ì’l‚ª•ςł·B\n"+fileName+"\nƒZƒNƒVƒ‡ƒ“:"+section+" ƒL[:"+key);
00054             return T();
00055         }
00056     }
00057 
00058     //ƒZƒNƒVƒ‡ƒ“‚ƃL[‚ðŽw’肵‚āA’l‚ð‘‚«ž‚ށB
00059     template <class T>
00060         static void setValue(const std::string& section, const std::string& key, const T& value){
00061             std::ostringstream ss;
00062             ss << value;
00063             setString(section, key, ss.str());
00064         }
00065 
00066 private:
00067     static std::string fileName;
00068 
00069     //ƒGƒ‰[‚ÌŽž‚̏ˆ—B
00070     static void onError(const std::string& message);
00071 
00072 };
00073 
00074 #endif
00075 

Generated on Sun Apr 16 02:07:15 2006 for Springhead by  doxygen 1.4.1