37 #ifndef STREAMPARSER_H 38 #define STREAMPARSER_H 44 #include <unordered_map> 57 inline int is_match(
const std::string& line_,
const std::string& pattern_)
60 const auto lineCStr = line_.c_str();
61 const auto patternCStr = pattern_.c_str();
63 const char* line = &(lineCStr[0]);
64 const char* pattern = &(patternCStr[0]);
70 const char* last_pattern_start = 0;
71 const char* last_line_start = 0;
74 if (*pattern == *line)
77 last_line_start = line + 1;
83 else if (*pattern ==
'?')
88 last_line_start = line + 1;
93 else if (*pattern ==
'*')
95 if (*(pattern + 1) ==
'\0')
98 last_pattern_start = pattern;
106 if (*line == *pattern)
111 last_line_start = line + 1;
118 if ((*pattern) ==
'\0' && (*line) ==
'\0')
122 if (last_pattern_start != 0)
124 pattern = last_pattern_start;
125 line = last_line_start;
135 if (*pattern ==
'\0')
163 std::unordered_map<std::string, std::vector<std::string>>
database;
169 std::unordered_map<std::string, std::vector<std::string>>
defaults;
175 std::unordered_map<std::string, std::vector<std::string>>
vars;
181 std::unordered_map<std::string, std::vector<std::string>>
switchers;
194 std::istream& stream,
195 std::unordered_map<std::string, std::vector<std::string>>& database,
196 std::vector<std::string> specificKey = {},
197 bool replaceVars =
false,
198 char openBracket =
'(',
char closeBracket =
')' 222 StreamParser(
LogStream& infoStream,
const std::string& label, std::istream& mainStream, std::istream& defaultsStream, std::istream& switchersStream, std::istream& varsStream, std::vector<std::string> specificKey = {});
234 StreamParser(
LogStream& infoStream,
const std::string& label, std::istream& mainStream, std::istream& defaultsStream, std::vector<std::string> specificKey = {});
246 StreamParser(
LogStream& infoStream,
const std::string& label, std::istream& mainStream,
char openBracket =
'{',
char closeBracket =
'}');
255 static std::string
UpperCase(
const std::string& line);
264 static std::vector<std::string>
StringToVector(std::string line,
char openBracket =
'(',
char closeBracket =
')');
280 static std::pair<std::string, std::string>
SplitString(
LogStream& info, std::string line,
bool upcase =
true);
290 template <
typename T>
291 void SetDefault(
const std::string& name, T& res,
const T* defValue,
bool echoDefault)
const 293 if (defValue !=
nullptr)
297 info(
'i') <<
"parameter <" << name <<
" = " << res <<
"> set as default" << std::endl;
301 info(
'e') <<
"parameter " << name <<
" is not found" << std::endl;
314 bool get(
const std::string& name, std::vector<Point2D>& res,
const std::vector<Point2D>* defValue =
nullptr,
bool echoDefault =
true)
const 316 bool boolRes =
false;
320 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
322 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
323 if (
is_match(search_ita->first, UpperCase(name)))
326 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
327 if (
is_match(search_itb->first, UpperCase(name)))
333 if (((search_it = search_ita) != database.end()) ||
334 ((search_it = search_itb) != defaults.end()))
336 for (
size_t i = 0; i < (search_it->second).size(); ++i)
338 std::string s = (search_it->second)[i];
341 size_t pos = s.find(
',', 1);
342 std::stringstream(s.substr(1, pos - 1)) >> elem[0];
343 std::stringstream(s.substr(pos + 1, s.length() - pos - 2)) >> elem[1];
362 bool get(
const std::string& name, std::vector<v3D>& res,
const std::vector<v3D>* defValue =
nullptr,
bool echoDefault =
true)
const 364 bool boolRes =
false;
367 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
369 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
370 if (
is_match(search_ita->first, UpperCase(name)))
373 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
374 if (
is_match(search_itb->first, UpperCase(name)))
380 if (((search_it = search_ita) != database.end()) ||
381 ((search_it = search_itb) != defaults.end()))
383 for (
size_t i = 0; i < (search_it->second).size(); ++i)
385 std::string s = (search_it->second)[i];
388 size_t pos1 = s.find(
',', 1);
389 std::stringstream(s.substr(1, pos1 - 1)) >> elem[0];
390 size_t pos2 = s.find(
',', pos1 + 1);
391 std::stringstream(s.substr(pos1 + 1, pos2 - pos1 - 1)) >> elem[1];
392 std::stringstream(s.substr(pos2 + 1, s.length() - pos2 - 2)) >> elem[2];
411 template<
typename T,
typename P>
412 bool get(
const std::string& name, std::vector<std::pair<T,P>>& res,
const std::vector<std::pair<T, P>>* defValue =
nullptr,
bool echoDefault =
true)
const 414 bool boolRes =
false;
417 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
419 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
420 if (
is_match(search_ita->first, UpperCase(name)))
423 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
424 if (
is_match(search_itb->first, UpperCase(name)))
430 if (((search_it = search_ita) != database.end()) ||
431 ((search_it = search_itb) != defaults.end()))
433 for (
size_t i = 0; i < (search_it->second).size(); ++i)
435 std::string s = (search_it->second)[i];
438 size_t pos = s.find(
',', 1);
439 std::stringstream(s.substr(1, pos - 1)) >> elem.first;
440 std::stringstream(s.substr(pos + 1, s.length() - pos - 2)) >> elem.second;
461 bool get(
const std::string& name, std::vector<numvector<T,2>>& res,
const std::vector<numvector<T, 2>>* defValue =
nullptr,
bool echoDefault =
true)
const 463 bool boolRes =
false;
466 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
468 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
469 if (
is_match(search_ita->first, UpperCase(name)))
472 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
473 if (
is_match(search_itb->first, UpperCase(name)))
479 if (((search_it = search_ita) != database.end()) ||
480 ((search_it = search_itb) != defaults.end()))
482 for (
size_t i = 0; i < (search_it->second).size(); ++i)
484 std::string s = (search_it->second)[i];
487 size_t pos = s.find(
',', 1);
488 std::stringstream(s.substr(1, pos - 1)) >> elem[0];
489 std::stringstream(s.substr(pos + 1, s.length() - pos - 2)) >> elem[1];
509 bool get(
const std::string& name, std::vector<
Vortex2D>& res,
const std::vector<Vortex2D>* defValue =
nullptr,
bool echoDefault =
true)
const 511 bool boolRes =
false;
514 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
516 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
517 if (
is_match(search_ita->first, UpperCase(name)))
520 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
521 if (
is_match(search_itb->first, UpperCase(name)))
527 if (((search_it = search_ita) != database.end()) ||
528 ((search_it = search_itb) != defaults.end()))
530 for (
size_t i = 0; i < (search_it->second).size(); ++i)
532 std::string s = (search_it->second)[i];
536 size_t pos1 = s.find(
',', 1);
537 size_t pos2 = s.find(
',', pos1 + 1);
538 std::stringstream(s.substr(1, pos1 - 1)) >> r[0];
539 std::stringstream(s.substr(pos1 + 1, pos2 - pos1 - 1)) >> r[1];
540 std::stringstream(s.substr(pos2 + 1, s.length() - pos2 - 2)) >> g;
563 bool get(
const std::string& name, std::vector<GeomPoint>& res,
const std::vector<GeomPoint>* defValue =
nullptr,
bool echoDefault =
true)
const 565 bool boolRes =
false;
568 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
570 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
571 if (
is_match(search_ita->first, UpperCase(name)))
574 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
575 if (
is_match(search_itb->first, UpperCase(name)))
581 if (((search_it = search_ita) != database.end()) ||
582 ((search_it = search_itb) != defaults.end()))
584 for (
size_t i = 0; i < (search_it->second).size(); ++i)
586 std::string s = (search_it->second)[i];
590 size_t pos1 = s.find(
',', 1);
591 size_t pos2 = s.find(
',', pos1 + 1);
592 std::stringstream(s.substr(1, pos1 - 1)) >> r[0];
593 std::stringstream(s.substr(pos1 + 1, pos2 - pos1 - 1)) >> r[1];
594 std::stringstream(s.substr(pos2 + 1, s.length() - pos2 - 2)) >> type;
622 template <
typename T>
623 bool get(
const std::string& name, std::vector<T>& res,
const std::vector<T>* defValue =
nullptr,
bool echoDefault =
true)
const 625 bool boolRes =
false;
628 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
630 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
631 if (
is_match(search_ita->first, UpperCase(name)))
634 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
635 if (
is_match(search_itb->first, UpperCase(name)))
641 if (((search_it = search_ita) != database.end()) ||
642 ((search_it = search_itb) != defaults.end()))
644 for (
size_t i = 0; i < (search_it->second).size(); ++i)
646 std::string s = (search_it->second)[i];
648 std::stringstream ss(s);
651 if (
typeid(elem).name() ==
typeid(std::string(
"TestString")).name())
653 std::string* str =
reinterpret_cast<std::string*
>(&elem);
654 str->erase(
remove(str->begin(), str->end(),
'\"'), str->end());
676 template <
typename T>
677 bool get(
const std::string& name, std::pair<std::string, T>& res,
const std::pair<std::string, T>* defValue =
nullptr,
bool echoDefault =
true)
const 679 bool boolRes =
false;
681 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
683 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
684 if (
is_match(search_ita->first, UpperCase(name)))
687 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
688 if (
is_match(search_itb->first, UpperCase(name)))
694 if ((((search_it = search_ita) != database.end()) && ((search_it->second).size() > 0)) ||
695 (((search_it = search_itb) != defaults.end()) && ((search_it->second).size() > 0)))
697 if ((search_it->second).size() == 1)
699 std::string s = (search_it->second)[0];
703 if (((search_it = switchers.find(UpperCase(s))) != switchers.end()) && ((search_it->second).size() > 0))
704 s = search_it->second[0];
706 std::stringstream ss(s);
709 if (
typeid(elem).name() ==
typeid(std::string(
"TestString")).name())
711 std::string* str =
reinterpret_cast<std::string*
>(&elem);
712 str->erase(
remove(str->begin(), str->end(),
'\"'), str->end());
719 info(
'e') <<
"parameter " << name <<
" is list (only scalar is available)" << std::endl;
741 template <
typename T>
742 bool get(
const std::string& name, T& res,
const T* defValue =
nullptr,
bool echoDefault =
true)
const 744 bool boolRes =
false;
746 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
748 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
749 if (
is_match(search_ita->first, UpperCase(name)))
752 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
753 if (
is_match(search_itb->first, UpperCase(name)))
759 if ((((search_it = search_ita) != database.end()) && ((search_it->second).size() > 0)) ||
760 (((search_it = search_itb) != defaults.end()) && ((search_it->second).size() > 0)))
762 if ((search_it->second).size() == 1)
764 std::string s = (search_it->second)[0];
767 if (((search_it = switchers.find(UpperCase(s))) != switchers.end()) && ((search_it->second).size() > 0))
768 s = search_it->second[0];
770 std::stringstream ss(s);
773 if (
typeid(elem).name() ==
typeid(std::string(
"TestString")).name())
775 std::string* str =
reinterpret_cast<std::string*
>(&elem);
776 str->erase(
remove(str->begin(), str->end(),
'\"'), str->end());
783 info(
'e') <<
"parameter " << name <<
" is list (only scalar is available)" << std::endl;
803 bool get(
const std::string& name,
Point2D& res,
const Point2D* defValue =
nullptr,
bool echoDefault =
true)
const 805 bool boolRes =
false;
807 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
809 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
810 if (
is_match(search_ita->first, UpperCase(name)))
813 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
814 if (
is_match(search_itb->first, UpperCase(name)))
820 if (((search_it = search_ita) != database.end()) ||
821 ((search_it = search_itb) != defaults.end()))
823 if ((search_it->second).size() == res.size())
825 for (
size_t i = 0; i < (search_it->second).size(); ++i)
827 std::string s = (search_it->second)[i];
829 std::stringstream ss(s);
838 info(
'e') <<
"parameter " << name <<
" length differs from 2 (only Point2D is available)" << std::endl;
858 bool get(
const std::string& name,
v3D& res,
const v3D* defValue =
nullptr,
bool echoDefault =
true)
const 860 bool boolRes =
false;
862 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
864 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
865 if (
is_match(search_ita->first, UpperCase(name)))
868 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
869 if (
is_match(search_itb->first, UpperCase(name)))
875 if (((search_it = search_ita) != database.end()) ||
876 ((search_it = search_itb) != defaults.end()))
878 if ((search_it->second).size() == res.size())
880 for (
size_t i = 0; i < (search_it->second).size(); ++i)
882 std::string s = (search_it->second)[i];
884 std::stringstream ss(s);
893 info(
'e') <<
"parameter " << name <<
" length differs from 3 (only v3D is available)" << std::endl;
913 bool get(
const std::string& name,
bool& res,
const bool* defValue =
nullptr,
bool echoDefault =
true)
const 915 bool boolRes =
false;
917 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
919 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
920 if (
is_match(search_ita->first, UpperCase(name)))
923 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
924 if (
is_match(search_itb->first, UpperCase(name)))
930 if ((((search_it = search_ita) != database.end()) && ((search_it->second).size() > 0)) ||
931 (((search_it = search_itb) != defaults.end()) && ((search_it->second).size() > 0)))
933 if ((search_it->second).size() == 1)
935 std::string s = (search_it->second)[0];
938 if (((search_it = switchers.find(UpperCase(s))) != switchers.end()) && ((search_it->second).size() > 0))
939 s = search_it->second[0];
950 info(
'e') <<
"parameter " << name <<
" is list (only scalar is available)" << std::endl;
970 bool get(
const std::string& name, std::pair<std::pair<std::string, int>, std::string>& res,
const std::pair<std::pair<std::string, int>, std::string>* defValue =
nullptr,
bool echoDefault =
true)
const 972 bool boolRes =
false;
974 std::unordered_map<std::string, std::vector<std::string>>::const_iterator search_it, search_ita, search_itb;
976 for (search_ita = database.begin(); search_ita != database.end(); ++search_ita)
977 if (
is_match(search_ita->first, UpperCase(name)))
980 for (search_itb = defaults.begin(); search_itb != defaults.end(); ++search_itb)
981 if (
is_match(search_itb->first, UpperCase(name)))
987 if ((((search_it = search_ita) != database.end()) && ((search_it->second).size() > 0)) ||
988 (((search_it = search_itb) != defaults.end()) && ((search_it->second).size() > 0)))
990 if ((search_it->second).size() == 1)
992 std::string s = (search_it->second)[0];
994 size_t posBegin = s.find(
'(');
995 size_t posEnd = s.find(
')');
997 if ((posBegin != -1) && (posEnd == -1))
999 info(
'e') <<
"parameter " << name <<
" is given incorrectly" << std::endl;
1004 std::string str1a, str1b, str2;
1005 str1a = s.substr(0, posBegin);
1007 if (((search_it = switchers.find(UpperCase(str1a))) != switchers.end()) && ((search_it->second).size() > 0))
1008 str1b = search_it->second[0];
1012 std::stringstream ssStr1b;
1017 str2 = s.substr(posBegin + 1, posEnd - posBegin - 1);
1019 if ((posBegin == -1) && (posEnd == -1))
1020 res = { {str1a, res1b},
"" };
1022 res = { {str1a, res1b}, str2 };
1029 info(
'e') <<
"parameter " << name <<
" is list (only scalar is available)" << std::endl;
1034 SetDefault(name, res, defValue, echoDefault);
Шаблонный класс, определяющий вектор фиксированной длины Фактически представляет собой массив...
void ReplaceVarsInString(std::string &st)
Замена переменных в строке их значениями
Класс, определяющий работу с потоком логов
int is_match(const std::string &line_, const std::string &pattern_)
Функция сверки строки с шаблоном, который может содержать знаки * и ?
std::unordered_map< std::string, std::vector< std::string > > defaults
Данные считываемые из списка умолчаний
Описание базовых вспомогательных функций
LogStream info
Поток для вывода логов и сообщений об ошибках
void SetDefault(const std::string &name, T &res, const T *defValue, bool echoDefault) const
Установка значения параметра по умолчанию
static std::vector< std::string > StringToVector(std::string line, char openBracket= '(', char closeBracket= ')')
Pазбор строки, содержащей запятые, на отдельные строки
Класс, опеделяющий двумерный вектор
Класс, опеделяющий двумерный вектор
static std::pair< std::string, std::string > SplitString(LogStream &info, std::string line, bool upcase=true)
Разбор строки на пару ключ-значение
std::unordered_map< std::string, std::vector< std::string > > database
Данные считываемые из основного файла-паспорта расчета
std::unordered_map< std::string, std::vector< std::string > > vars
Данные считываемые из параметров конкретной задачи
Класс, опеделяющий двумерный вихревой элемент
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::string VectorStringToString(const std::vector< std::string > &_vecString)
Объединение вектора (списка) из строк в одну строку
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= ')')
Парсинг заданного потока
Заголовочный файл с описанием класса Gpu.
Класс, позволяющий выполнять разбор файлов и строк с настройками и параметрами
std::unordered_map< std::string, std::vector< std::string > > switchers
Данные считываемые из перечня параметров-переключателей
~StreamParser()
Деструктор
static std::string UpperCase(const std::string &line)
Перевод строки в верхний регистр