NIREP
|
00001 /* +-----------+ 00002 | Libraries | 00003 +-----------+ */ 00004 #include "VariableList.h" 00005 #include <algorithm> 00006 00007 00008 /* +----------------+ 00009 | Public Methods | 00010 +----------------+ */ 00011 00012 VariableList::VariableList(const std::string& fileName) 00013 { 00014 this->Read(fileName); 00015 } 00016 /* 00017 function: Read 00018 purpose: To read in the evaluator list 00019 00020 input: fileName std::string path to the evaluator list description 00021 00022 output: updated task 00023 */ 00024 int VariableList::Read(const std::string& fileName) 00025 { 00026 this->vList.clear(); 00027 std::ifstream file (fileName.c_str()); 00028 int list = 0; 00029 00030 if (file.is_open()) 00031 { 00032 std::string line; 00033 00034 while (! file.eof() ) // loop through the file 00035 { 00036 getline (file,line); 00037 00038 // Convert line to all lower case for comparison 00039 std::transform(line.begin(), line.end(), line.begin(), tolower); 00040 00041 if(list == 0) // check to see if the evaluator list has started 00042 { 00043 // if we find the start designator, update the flag 00044 if(!line.compare("begin variablelist")) 00045 { 00046 list = VARIABLELIST; 00047 } 00048 } 00049 // we're in the evaluator list 00050 else if(list == VARIABLELIST) 00051 { 00052 // check to see if it's over 00053 if(!line.compare("end variablelist")) 00054 { 00055 list=0; 00056 } 00057 else // we're still in the evaluator list \o/ 00058 { 00059 // extract the task name 00060 size_t found = line.find_first_of("="); 00061 std::string id(line, 0,found); 00062 wxString wxID(id.c_str(), wxConvUTF8); // convert to cstring 00063 wxID.Trim(); // trim right side 00064 wxID.Trim(false); // trim left side 00065 wxID = wxID.MakeLower(); 00066 id = wxID.ToAscii(); 00067 00068 00069 // extract the expression 00070 std::string function(line, found+1,line.length()); 00071 wxString wxFunction(function.c_str(), wxConvUTF8); 00072 wxFunction.Trim(); 00073 wxFunction.Trim(false); 00074 wxFunction = wxFunction.MakeLower(); 00075 function = wxFunction.ToAscii(); 00076 00077 // add to evaluator list 00078 this->SetVariable(id, function); 00079 } 00080 } 00081 00082 } 00083 00084 file.close(); 00085 return 0; 00086 } 00087 else 00088 return -1; 00089 } 00090 00099 std::string VariableList::UpdateString(const std::string& source) 00100 { 00101 std::string temp = source; 00102 00103 // first, let's find the first ${ character. 00104 size_t find = temp.find_first_of("${"); 00105 00106 // if found, extract what comes before, and what comes after. 00107 // then find the first } character. extract what comes before, and what comes after. 00108 if ((int)find != std::string::npos) { 00109 00110 //size_t find; 00111 //while ( (int) (find = temp.find_first_of("${")) != std::string::npos ){ 00112 std::string before = temp.substr(0, find); 00113 temp = temp.substr(find+1); 00114 00115 size_t found = temp.find_first_of("}"); 00116 std::string var = temp.substr(1, found-1); 00117 std::string after = temp.substr(found+1); 00118 00119 // reassemble 00120 temp = before + this->GetVariable(var) + after; 00121 00122 // check to see if there are more 00123 temp = this->UpdateString(temp); 00124 } 00125 return temp; 00126 } 00127 00128 /* +-----------------+ 00129 | Private Methods | 00130 +-----------------+ */ 00131 00132 /* 00133 function: inMap 00134 purpose: To determine if a key is in a map 00135 00136 input: 00137 map std::map<S, T> map to be searched 00138 key S key to be searched 00139 00140 output: 00141 true: if found 00142 false: if not found 00143 */ 00144 00145 template <typename S, typename T> 00146 bool VariableList::inMap(std::map<S, T> map, S key) 00147 { 00148 bool flag = 0; 00149 00150 typename std::map<S,T>::iterator keyfound = map.find(key); 00151 if (keyfound != map.end()) flag = 1; 00152 00153 return flag; 00154 } 00155 00156 template bool VariableList::inMap(std::map<std::string, std::string> map, std::string key);