|
| 1 | +#ifndef PARAMETERSCONSTRUCTOR_HXX |
| 2 | +#define PARAMETERSCONSTRUCTOR_HXX |
| 3 | + |
| 4 | +#include<map> |
| 5 | +#include<string> |
| 6 | +#include<iostream> |
| 7 | +#include<vector> |
| 8 | +#include<fstream> |
| 9 | +#include<string> |
| 10 | +#include"disjoint-paths/disjointPathsMethods.hxx" |
| 11 | + |
| 12 | +namespace disjointPaths { |
| 13 | + |
| 14 | +//class ParametersParser{ |
| 15 | +// public: |
| 16 | +// ParametersParser(){} |
| 17 | + |
| 18 | +// void initFromFile(std::string& fileName){ |
| 19 | +// std::ifstream data; |
| 20 | +// //data.exceptions( std::ifstream::failbit | std::ifstream::badbit ); |
| 21 | +// try{ |
| 22 | +// data.open(fileName); |
| 23 | +// if (!data){ |
| 24 | +// throw std::system_error(errno, std::system_category(), "failed to open parameter file "+fileName); |
| 25 | +// } |
| 26 | +// bool solverPart=false; |
| 27 | +// std::string line; |
| 28 | +// while (!solverPart&&std::getline(data, line) ) { |
| 29 | +// size_t commentPos=line.find_first_of('#'); |
| 30 | +// if(commentPos<line.length()){ |
| 31 | +// line.erase(commentPos); |
| 32 | +// } |
| 33 | +// if(!line.empty()&&line.find("[SOLVER]")!=line.npos){ |
| 34 | +// solverPart=true; |
| 35 | +// } |
| 36 | +// } |
| 37 | +// if(!solverPart){ |
| 38 | +// throw std::runtime_error("Config file does not contain \"[SOLVER]\". "); |
| 39 | +// } |
| 40 | +// initFromStream(data); |
| 41 | +// data.close(); |
| 42 | + |
| 43 | +// } |
| 44 | + |
| 45 | +// catch (std::system_error& er) { |
| 46 | +// std::clog << er.what() << " (" << er.code() << ")" << std::endl; |
| 47 | +// } |
| 48 | + |
| 49 | +// } |
| 50 | + |
| 51 | +// template<class STR> |
| 52 | +// void initFromStream(STR& data){ |
| 53 | +// char delim='='; |
| 54 | +// std::string line; |
| 55 | +// std::vector<std::string> strings; |
| 56 | + |
| 57 | +// bool newSection=false; |
| 58 | +// while(!newSection&&std::getline(data, line)){ |
| 59 | +// size_t commentPos=line.find_first_of('#'); |
| 60 | +// if(commentPos<line.length()){ |
| 61 | +// line.erase(commentPos); |
| 62 | +// } |
| 63 | +// if(line.find("[")==line.npos){ |
| 64 | +// if(!line.empty()){ //TODO not split all delims, just the first occurence |
| 65 | +// strings=split<>(line,delim); |
| 66 | +// std::string whitespaces (" "); |
| 67 | + |
| 68 | +// size_t foundLast = strings[0].find_last_not_of(whitespaces); |
| 69 | +// size_t foundFirst=strings[0].find_first_not_of(whitespaces); |
| 70 | +// std::string key=strings[0].substr(foundFirst,foundLast-foundFirst+1); |
| 71 | + |
| 72 | +// foundLast = strings[1].find_last_not_of(whitespaces); |
| 73 | +// foundFirst=strings[1].find_first_not_of(whitespaces); |
| 74 | +// std::string value=strings[1].substr(foundFirst,foundLast-foundFirst+1); |
| 75 | + |
| 76 | +// parsedStrings[key]=value; |
| 77 | +// } |
| 78 | +// } |
| 79 | +// else{ |
| 80 | +// newSection=true; |
| 81 | +// } |
| 82 | +// } |
| 83 | + |
| 84 | +// } |
| 85 | + |
| 86 | + |
| 87 | +// std::map<std::string, std::string>& getParsedStrings(){ |
| 88 | +// return parsedStrings; |
| 89 | +// } |
| 90 | + |
| 91 | +// void clearParsedStrings(){ |
| 92 | +// parsedStrings.clear(); |
| 93 | +// } |
| 94 | + |
| 95 | +//private: |
| 96 | +// std::map<std::string,std::string> parsedStrings; |
| 97 | + |
| 98 | +//}; |
| 99 | + |
| 100 | + |
| 101 | +template<class STR> |
| 102 | +std::map<std::string,std::string> parseParametersFromStream(STR& data){ |
| 103 | + char delim='='; |
| 104 | + std::string line; |
| 105 | + std::vector<std::string> strings; |
| 106 | + std::map<std::string,std::string> parsedStrings; |
| 107 | + |
| 108 | + bool newSection=false; |
| 109 | + while(!newSection&&std::getline(data, line)){ |
| 110 | + size_t commentPos=line.find_first_of('#'); |
| 111 | + if(commentPos<line.length()){ |
| 112 | + line.erase(commentPos); |
| 113 | + } |
| 114 | + if(line.find("[")==line.npos){ |
| 115 | + if(!line.empty()){ //TODO not split all delims, just the first occurence |
| 116 | + strings=split<>(line,delim); |
| 117 | + std::string whitespaces (" "); |
| 118 | + |
| 119 | + size_t foundLast = strings[0].find_last_not_of(whitespaces); |
| 120 | + size_t foundFirst=strings[0].find_first_not_of(whitespaces); |
| 121 | + std::string key=strings[0].substr(foundFirst,foundLast-foundFirst+1); |
| 122 | + |
| 123 | + foundLast = strings[1].find_last_not_of(whitespaces); |
| 124 | + foundFirst=strings[1].find_first_not_of(whitespaces); |
| 125 | + std::string value=strings[1].substr(foundFirst,foundLast-foundFirst+1); |
| 126 | + |
| 127 | + parsedStrings[key]=value; |
| 128 | + } |
| 129 | + } |
| 130 | + else{ |
| 131 | + newSection=true; |
| 132 | + } |
| 133 | + } |
| 134 | + return parsedStrings; |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +std::map<std::string,std::string> parseParametersFromFile(std::string& fileName){ |
| 142 | + std::ifstream data; |
| 143 | + //data.exceptions( std::ifstream::failbit | std::ifstream::badbit ); |
| 144 | + try{ |
| 145 | + data.open(fileName); |
| 146 | + if (!data){ |
| 147 | + throw std::system_error(errno, std::system_category(), "failed to open parameter file "+fileName); |
| 148 | + } |
| 149 | + bool solverPart=false; |
| 150 | + std::string line; |
| 151 | + while (!solverPart&&std::getline(data, line) ) { |
| 152 | + size_t commentPos=line.find_first_of('#'); |
| 153 | + if(commentPos<line.length()){ |
| 154 | + line.erase(commentPos); |
| 155 | + } |
| 156 | + if(!line.empty()&&line.find("[SOLVER]")!=line.npos){ |
| 157 | + solverPart=true; |
| 158 | + } |
| 159 | + } |
| 160 | + if(!solverPart){ |
| 161 | + throw std::runtime_error("Config file does not contain \"[SOLVER]\". "); |
| 162 | + } |
| 163 | + data.close(); |
| 164 | + return parseParametersFromStream(data); |
| 165 | + |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + catch (std::system_error& er) { |
| 170 | + std::clog << er.what() << " (" << er.code() << ")" << std::endl; |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +} |
| 179 | +#endif // PARAMETERSCONSTRUCTOR_HXX |
| 180 | + |
0 commit comments