20 std::string tolower(std::string s) {
21 std::transform(s.begin(), s.end(), s.begin(),
22 [](
unsigned char c) {
return std::tolower(c); });
28 bool isValueLine(
const std::string &line) {
29 std::stringstream ss(line);
31 return (
bool)(ss >> value);
38 static const std::regex HeaderBegin(
"<header>", std::regex_constants::icase);
39 static const std::regex HeaderEnd(
"</header>", std::regex_constants::icase);
42 std::stringstream HeaderContent;
43 std::smatch RegexMatch;
45 auto pos = InputStream.tellg();
46 while (std::getline(InputStream, Line)) {
47 if (isValueLine(Line)) {
48 InputStream.seekg(pos);
51 if (std::regex_search(Line, RegexMatch, HeaderBegin)) {
52 HeaderContent << RegexMatch.suffix() << std::endl;
57 Line = HeaderContent.str();
58 if (std::regex_search(Line, RegexMatch, HeaderEnd)) {
59 Line = RegexMatch.prefix();
63 if (InputStream.eof()) {
65 InputStream.seekg(pos);
69 while (std::getline(InputStream, Line)) {
70 if (isValueLine(Line))
72 if (std::regex_search(Line, RegexMatch, HeaderEnd)) {
73 HeaderContent << RegexMatch.prefix() << std::endl;
74 return HeaderContent.str();
76 HeaderContent << Line << std::endl;
79 InputStream.seekg(pos);
86 std::stringstream StringStream(HeaderContent);
88 static const std::regex RegexKeyValue(
89 R
"(^\s*([^\s]+.*?)\s*:\s+([^\s]+.*?)\s*$)"); 90 static const std::regex RegexPID(R
"(-?\d+)"); 91 static const std::regex RegexEfirst(
"e.*[mpz]", std::regex_constants::icase);
92 static const std::regex RegexPfirst(
"[mp].*e", std::regex_constants::icase);
93 while (getline(StringStream, Line)) {
94 std::smatch RegexMatch;
95 std::regex_search(Line, RegexMatch, RegexKeyValue);
96 if (RegexMatch.size() < 3)
98 const std::string Key{std_fix::tolower(RegexMatch[1])};
99 const std::string Value{RegexMatch[2]};
101 std::sregex_iterator iter(Value.begin(), Value.end(), RegexPID), end;
102 std::vector<int>().swap(PIDs);
103 for (; iter != end; ++iter)
104 PIDs.push_back(std::stoi(iter->str()));
106 }
else if (Key ==
"unit") {
108 }
else if (Key ==
"order") {
109 if (std::regex_match(Value, RegexPfirst)) {
111 }
else if (std::regex_match(Value, RegexEfirst)) {
119 os <<
"<header>" << std::endl;
122 os <<
"\tPids: [" << PIDs[0];
123 for (
size_t i = 1; i < PIDs.size(); ++i)
124 os <<
", " << PIDs[i];
125 os <<
"]" << std::endl;
130 os <<
"E px py pz" << std::endl;
132 os <<
"px py pz E" << std::endl;
134 os <<
"\tUnit: " << Unit << std::endl;
135 os <<
"</header>" << std::endl;
Input data file is corrupt or incomplete.