メインページ | ネームスペース一覧 | クラス階層 | 構成 | Directories | ファイル一覧 | ネームスペースメンバ | 構成メンバ | ファイルメンバ | 関連ページ

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ファイルから設定を読み込む。
00013 class WBIniFile
00014 {
00015 public:
00016     //これを最初に呼んでファイル名をセット。
00017     static void setFileName(const std::string& fn);
00018     //セクションとキーを指定して、値を読み込む。
00019     static std::string getString(const std::string& section, const std::string& key);
00020     //セクションとキーを指定して、値を読み込む。
00021     //存在しない時はdefValueを返す。
00022     static std::string getString(const std::string& section, const std::string& key, const std::string& defValue);
00023     //セクションとキーを指定して、文字列を書き込む。
00024     static void setString(const std::string& section, const std::string& key, const std::string& value);
00025 
00026     //セクションとキーを指定して、値を読み込み、指定の型に変換。
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が読めない…。
00031         std::istringstream ss(getString(section, key));
00032         T value;
00033         if (ss >> value){
00034             return value;
00035         }else{
00036             onError("INIファイルの値が変です。\n"+fileName+"\nセクション:"+section+" キー:"+key);
00037             return T();
00038         }
00039     }
00040     //セクションとキーを指定して、値を読み込み、指定の型に変換。
00041     //存在しない時はdefValueを返す。
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が読めない…。
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ファイルの値が変です。\n"+fileName+"\nセクション:"+section+" キー:"+key);
00054             return T();
00055         }
00056     }
00057 
00058     //セクションとキーを指定して、値を書き込む。
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     //エラーの時の処理。
00070     static void onError(const std::string& message);
00071 
00072 };
00073 
00074 #endif
00075 

Springheadに対してSun Apr 16 01:57:58 2006に生成されました。  doxygen 1.4.1