00001 #include "GProperties.h"
00002 #include <fstream>
00003 #include <iostream>
00004 #include <vector>
00005 #include "../io_src/GStd_Output.h"
00006 #include "../io_src/GError_Output.h"
00007
00008 using namespace GMathLib::Util;
00009 using namespace GMathLib::IO;
00010
00011 GProperties::GProperties()
00012 : GObject(), load_flag(false)
00013 {
00014 Class_Name("GProperties");
00015 }
00016
00017 void GProperties::Load(const std::string filename)
00018 {
00019
00020 std::ifstream ifs(filename.c_str());
00021
00022
00023 if(ifs.fail()){
00024 std::string errorinfo = "Can't open the file '" + filename + "'.";
00025 GError_Output::Puts(Class_Name() + "::Load", errorinfo);
00026 load_flag = false;
00027 return;
00028 }
00029
00030
00031 m_map.clear();
00032 char tmp[256];
00033
00034
00035
00036 ifs.getline(tmp, 256);
00037 while(!ifs.eof())
00038 {
00039 std::string line(tmp);
00040
00041 if(line.substr(0, 1).compare("#") != 0){
00042 std::string::size_type nEq = line.find("=");
00043 if(nEq != std::string::npos){
00044 std::string key = line.substr(0, nEq);
00045 std::string val = line.substr(nEq+1);
00046 m_map.insert(std::pair<std::string, std::string>(key, val));
00047
00048 }
00049 }
00050 ifs.getline(tmp, 256);
00051
00052 }
00053
00054 load_flag = true;
00055 ifs.close();
00056 }
00057
00058
00059 std::string GProperties::GetProperty(const std::string key)
00060 {
00061
00062
00063 if(!load_flag){
00064 GError_Output::Puts(Class_Name()+"::GetProperty", "Failed to load file.");
00065 return std::string("");
00066 }
00067
00068
00069 if(m_map.count(key) == 0){
00070 const std::string errinfo = "Key '" + key + "' can not be found in Key datas of properties.";
00071 GError_Output::Puts(Class_Name()+"::GetProperty", errinfo);
00072 return std::string("");
00073 }else{
00074 return m_map.find(key)->second;
00075 }
00076 }
00077
00078 void GProperties::Print_All_Properties()
00079 {
00080 PropertyMap::iterator itr = m_map.begin();
00081 std::vector<std::string> property_list;
00082
00083
00084 while( itr != m_map.end()){
00085 property_list.push_back((*itr).first + " : " + (*itr).second);
00086 itr++;
00087 }
00088
00089
00090 GStd_Output::Write(Class_Name()+"::Print_All_Properties", property_list);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118