Line data Source code
1 : /* Copyright (C) 2025 Robert Schiwon 2 : * 3 : * This file is part of libjdx. 4 : * 5 : * libjdx is free software: you can redistribute it and/or modify it under the 6 : * terms of the GNU Lesser General Public License as published by the Free 7 : * Software Foundation, either version 3 of the License, or (at your option) any 8 : * later version. 9 : * 10 : * libjdx is distributed in the hope that it will be useful, but WITHOUT ANY 11 : * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 : * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 13 : * details. 14 : * 15 : * You should have received a copy of the GNU Lesser General Public License 16 : * along with libjdx. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : #include "util/AuditTrailParser.hpp" 19 : #include "jdx/AuditTrailEntry.hpp" 20 : #include "jdx/ParseException.hpp" 21 : #include "util/LdrUtils.hpp" 22 : #include "util/StringUtils.hpp" 23 : 24 : #include <algorithm> 25 : #include <map> 26 : #include <regex> 27 : 28 8 : libjdx::jdx::util::AuditTrailParser::AuditTrailParser( 29 8 : io::TextReader& reader, std::string variableList) 30 8 : : MultilineTuplesParser(reader, std::move(variableList), s_ldrName, "\n") 31 : { 32 8 : } 33 : 34 : std::optional<libjdx::jdx::AuditTrailEntry> 35 23 : libjdx::jdx::util::AuditTrailParser::next() 36 : { 37 23 : return TuplesParser::next<AuditTrailEntry>([this]() { return nextTuple(); }, 38 15 : [this]( 39 40 : const std::string& tuple) { return createAuditTrailEntry(tuple); }); 40 : } 41 : 42 : libjdx::jdx::AuditTrailEntry 43 15 : libjdx::jdx::util::AuditTrailParser::createAuditTrailEntry( 44 : const std::string& tuple) const 45 : { 46 : // tokenize 47 : // token[0] is the full match so extract 1 + 7 tokens for 7 groups 48 30 : const auto tokens = extractTokens(tuple, m_regex, 1 + 7); 49 15 : const auto& varList = getVariableList(); 50 : 51 : // error conditions {varList, {error condition, error message}} 52 : const std::multimap<std::string, std::tuple<bool, std::string>> errorMap{ 53 15 : {s_varLists.at(0), 54 15 : {tokens.at(5) || tokens.at(6), 55 30 : std::string{"Illegal "} + s_ldrName + " entry for " 56 45 : + s_varLists.at(0) + ": " + tuple}}, 57 15 : {s_varLists.at(1), 58 15 : {!tokens.at(5) || tokens.at(6), 59 30 : std::string{"Illegal "} + s_ldrName + " entry for " 60 45 : + s_varLists.at(1) + ": " + tuple}}, 61 15 : {s_varLists.at(2), 62 15 : {!tokens.at(5) || !tokens.at(6), 63 30 : std::string{"Illegal "} + s_ldrName + " entry for " 64 45 : + s_varLists.at(2) + ": " + tuple}}, 65 165 : }; 66 : 67 15 : checkForErrors(varList, errorMap, s_ldrName); 68 : 69 : // map 70 15 : AuditTrailEntry entry{}; 71 15 : entry.number = strtol(tokens.at(1).value().data(), nullptr, 10); 72 15 : entry.when = tokens.at(2).value(); 73 15 : entry.who = tokens.at(3).value(); 74 15 : entry.where = tokens.at(4).value(); 75 15 : entry.what = tokens.at(7).value(); 76 : // nothing to do for varLists[0] 77 15 : if (s_varLists.at(1) == varList) 78 : { 79 2 : entry.version = tokens.at(5).value(); 80 : } 81 13 : else if (s_varLists.at(2) == varList) 82 : { 83 9 : entry.process = tokens.at(5).value(); 84 9 : entry.version = tokens.at(6).value(); 85 : } 86 : 87 30 : return entry; 88 : }