45StreamParser::StreamParser(
LogStream& infoStream,
const std::string& label, std::istream& mainStream, std::istream& defaultsStream, std::istream& switchersStream, std::istream& varsStream, std::vector<std::string> specificKey)
88 std::string str(line);
112 if (line.length() > 0)
117 return std::vector<std::string>({});
124 std::vector<std::string> vecLine;
126 while ( ((pos = line.find(
',', 0)) < line.find(openBracket, 1)) ||
127 ( (pos = line.find(
',', std::max(1,
static_cast<int>(line.find(closeBracket, 1)) ))) != std::string::npos) ||
128 (pos = line.length()-1) )
130 std::string subline = line.substr(1, pos - 1);
132 vecLine.push_back(subline);
134 line.erase(1, subline.size() + 1);
141 return std::vector<std::string>({});
152 for (
size_t q = 0; q < _vecString.size(); ++q)
154 buf.append(_vecString[q]);
165 size_t posBegin = line.find(
'(', 0);
166 size_t posEnd = line.find(
')', 0);
167 if ((posBegin != -1) && (posEnd == -1))
169 info(
'e') <<
"error while parsing line " << line << std::endl;
174 std::string sub = line.substr(0, posBegin);
175 if ((posBegin == -1) && (posEnd == -1))
176 return std::pair<std::string, std::string>(upcase ?
UpperCase(sub) : sub,
"{}");
178 return std::pair<std::string, std::string>(upcase ?
UpperCase(sub) : sub,
"{" + line.substr(posBegin + 1, posEnd - posBegin - 1) +
"}");
184void StreamParser::ParseStream(std::istream& stream, std::unordered_map<std::string, std::vector<std::string>>& database, std::vector<std::string> specificKey,
bool replaceVars,
char openBracket,
char closeBracket)
186 std::string line, readline;
187 size_t defVarNum = 0;
189 while (stream.good())
195 getline(stream, readline);
197 size_t posComment = std::min(readline.find(
'#'), readline.find(
"//"));
199 if (posComment != std::string::npos)
200 readline = readline.substr(0, posComment);
204 readline.erase(remove(readline.begin(), readline.end(),
' '), readline.end());
205 readline.erase(remove(readline.begin(), readline.end(),
'\t'), readline.end());
207 }
while ( (readline.size()>0) && (readline[readline.size() - 1] ==
'\\') && (stream.good()));
209 line.erase(remove(line.begin(), line.end(),
'\\'), line.end());
210 line.erase(remove(line.begin(), line.end(),
' '), line.end());
211 line.erase(remove(line.begin(), line.end(),
'\t'), line.end());
213 size_t posEqual = line.find(
'=');
216 std::string key = line.substr(0, posEqual);
221 if ((specificKey.size() == 0) || (std::find(specificKey.begin(),specificKey.end(),key) != specificKey.end()) )
223 std::string value = line.substr(posEqual + 1, line.length());
231 info(
'e') <<
"key <" << key <<
"> is found twice" << std::endl;
235 else if (line.size() > 0)
237 std::string value = line;
241 std::stringstream ssDef;
242 ssDef <<
"_defVar_" << defVarNum;
252 stream.seekg(0, std::ios::beg);
259 for (
const auto& rec : add)
261 auto positionKey =
database.find(rec.first);
271 while (st.find(
"$") != -1)
273 size_t startVar = st.find(
'$');
275 size_t endVar = -1 + std::min({
276 st.find(
" ", startVar + 1),
277 st.find(
",", startVar + 1),
278 st.find(
";", startVar + 1),
279 st.find(
"$", startVar + 1),
280 st.find(
"(", startVar + 1),
281 st.find(
")", startVar + 1),
282 st.find(
"{", startVar + 1),
283 st.find(
"}", startVar + 1),
284 st.find(
"\n", startVar + 1),
288 std::string var = st.substr(startVar + 1, endVar - startVar);
291 if ((search_var !=
vars.end()) && ((search_var->second).size() > 0))
293 std::vector<std::string> findString = search_var->second;
295 std::stringstream ss;
297 ss << st.substr(0, startVar);
299 if (findString.size() == 1)
305 ss <<
"{" << findString[0];
306 if (findString.size() > 0)
308 for (
size_t sz = 1; sz < findString.size(); ++sz)
309 ss <<
"," << findString[sz];
315 size_t startP = endVar+1;
316 size_t endP = st.length() - 1;
319 ss << st.substr(startP, endP);
325 info(
'e') <<
"substitution $" << var <<
" is undefined" << std::endl;
Заголовочный файл с описанием класса StreamParser.
Класс, определяющий работу с потоком логов
void inheritStream(LogStream &infoStream_, const std::string &label_)
Связывание потока логов с потоком вывода от другого потока логов
std::unordered_map< std::string, std::vector< std::string > > switchers
Данные считываемые из перечня параметров-переключателей
static std::vector< std::string > StringToVector(std::string line, char openBracket='(', char closeBracket=')')
Pазбор строки, содержащей запятые, на отдельные строки
StreamParser(LogStream &infoStream, const std::string &label, std::istream &mainStream, std::istream &defaultsStream, std::istream &switchersStream, std::istream &varsStream, std::vector< std::string > specificKey={})
Конструктор, принимающий четыре потока
static std::pair< std::string, std::string > SplitString(LogStream &info, std::string line, bool upcase=true)
Разбор строки на пару ключ-значение
static std::string VectorStringToString(const std::vector< std::string > &_vecString)
Объединение вектора (списка) из строк в одну строку
std::unordered_map< std::string, std::vector< std::string > > database
Данные считываемые из основного файла-паспорта расчета
void MergeDbsWithoutRepeats(std::unordered_map< std::string, std::vector< std::string > > &database, const std::unordered_map< std::string, std::vector< std::string > > &)
Слияние двух баз данных
void ParseStream(std::istream &stream, std::unordered_map< std::string, std::vector< std::string > > &database, std::vector< std::string > specificKey={}, bool replaceVars=false, char openBracket='(', char closeBracket=')')
Парсинг заданного потока
LogStream info
Поток для вывода логов и сообщений об ошибках
std::unordered_map< std::string, std::vector< std::string > > vars
Данные считываемые из параметров конкретной задачи
static std::string UpperCase(const std::string &line)
Перевод строки в верхний регистр
void ReplaceVarsInString(std::string &st)
Замена переменных в строке их значениями
Глобальные параметры по умолчанию