00001 #include "ConfigSection.h"
00002 #include "Error.h"
00003
00004 ConfigSection::~ConfigSection()
00005 {
00006 m_items.clear();
00007 }
00008
00009 void ConfigSection::unsetItem(std::string name)
00010 {
00011 m_items.erase(name);
00012 }
00013
00014 ConfigItem &ConfigSection::getItem(std::string name)
00015 {
00016 ItemMap::iterator it = m_items.find(name);
00017 if (it == m_items.end()) {
00018 std::string errorMsg = "Config Item '";
00019 errorMsg += name;
00020 errorMsg += "' does not exists!";
00021 throw Error(errorMsg.c_str());
00022 }
00023 return it->second;
00024 }
00025
00026 void ConfigSection::setItem(std::string name, std::string value)
00027 {
00028 ItemMap::iterator it = m_items.find(name);
00029 if (it != m_items.end()) {
00030 it->second.set(value);
00031 } else {
00032 m_items.insert(std::make_pair(name, ConfigItem(value)));
00033 }
00034 }
00035
00036 void ConfigSection::save(std::ofstream &file)
00037 {
00038 for (ItemMap::iterator it = m_items.begin(); it != m_items.end(); ++it) {
00039 file << "\t" << it->first << " = " << it->second.get() << std::endl;
00040 }
00041 }
00042
00043 void ConfigSection::load(std::string &content)
00044 {
00045 std::string line, name, var;
00046 size_t posEndLine, posEqual;
00047 while (std::string::npos != (posEndLine = content.find("\n"))) {
00048 line = content.substr(0, posEndLine);
00049 posEqual = line.find('=');
00050 if (posEqual != std::string::npos) {
00051 name = line.substr(0, posEqual-1);
00052 var = line.substr(posEqual+1);
00053 chomp(name);
00054 chomp(var);
00055 setItem(name, var);
00056 }
00057 content = content.substr(posEndLine+1);
00058 }
00059 }
00060
00061 Vector3d ConfigSection::getVector3d(std::string name)
00062 {
00063 Vector3d temp;
00064 std::string serialized = getItem(name).get();
00065 size_t sep;
00066 for (int i = 0; i < 3; ++i) {
00067 sep = serialized.find(',');
00068 if (sep == std::string::npos) {
00069 std::string msg = "ConfigSection::getVector3d(" + name + "): string is malformated!";
00070 throw Error(msg.c_str());
00071 }
00072 temp[i] = convertTo<std::string, double>(serialized.substr(0, sep));
00073 serialized = serialized.substr(sep+1);
00074 }
00075 return temp;
00076 }
00077
00078 void ConfigSection::setVector3d(std::string name, Vector3d &value)
00079 {
00080 std::string temp;
00081 for (int i = 0; i < 3; ++i) {
00082 temp += convertTo<double, std::string>(value.get(i)) + ",";
00083 }
00084 setItem(name, temp);
00085 }
00086
00087 Matrix<double> ConfigSection::getMatrixDouble(std::string name)
00088 {
00089 std::string serialized = getItem(name).get();
00090 size_t n, m, sep;
00091 sep = serialized.find(',');
00092 if (sep == std::string::npos) {
00093 std::string msg = "ConfigSection::getMatrixDouble(" + name + "): (while getting n) string is malformated!";
00094 throw Error(msg.c_str());
00095 }
00096 n = convertTo<std::string, size_t>(serialized.substr(0, sep));
00097 serialized = serialized.substr(sep+1);
00098 sep = serialized.find(',');
00099 if (sep == std::string::npos) {
00100 std::string msg = "ConfigSection::getMatrixDouble(" + name + "): (while getting m) string is malformated!";
00101 throw Error(msg.c_str());
00102 }
00103 m = convertTo<std::string, size_t>(serialized.substr(0, sep));
00104 serialized = serialized.substr(sep+1);
00105
00106 Matrix<double> temp(n,m);
00107
00108 for (size_t i = 0; i < n; ++i) {
00109 for (size_t j = 0; j < m; ++ j) {
00110 sep = serialized.find(',');
00111 if (sep == std::string::npos) {
00112 std::string msg = "ConfigSection::getMatrixDouble(" + name + "): (while getting values) string is malformated!";
00113 throw Error(msg.c_str());
00114 }
00115 temp[i][j] = convertTo<std::string, double>(serialized.substr(0, sep));
00116 serialized = serialized.substr(sep+1);
00117 }
00118 }
00119 return temp;
00120 }
00121
00122 void ConfigSection::setMatrixDouble(std::string name, Matrix<double> &matrix)
00123 {
00124 std::string temp = convertTo<size_t, std::string>(matrix.getN()) + ",";
00125 temp += convertTo<size_t, std::string>(matrix.getM()) + ",";
00126 for (size_t i = 0; i < matrix.getN(); ++i) {
00127 for (size_t j = 0; j < matrix.getM(); ++j) {
00128 temp += convertTo<double, std::string>(matrix.get(i, j)) + ",";
00129 }
00130 }
00131 setItem(name, temp);
00132 }