LCOV - code coverage report
Current view: top level - jdx/util - TuplesParser.hpp (source / functions) Hit Total Coverage
Test: coverage_filtered.info Lines: 6 6 100.0 %
Date: 2025-06-15 16:18:27 Functions: 3 3 100.0 %

          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             : #ifndef JDX_TUPLESPARSER_HPP
      19             : #define JDX_TUPLESPARSER_HPP
      20             : 
      21             : #include "io/TextReader.hpp"
      22             : 
      23             : #include <map>
      24             : #include <optional>
      25             : #include <regex>
      26             : 
      27             : namespace libjdx::jdx::util
      28             : {
      29             : /**
      30             :  * @brief A parser base class for tuple parsers such as AUDIT TRAIL, PEAK
      31             :  * ASSIGNMENTS, and PEAK TABLE.
      32             :  */
      33             : class TuplesParser
      34             : {
      35             : protected:
      36             :     /**
      37             :      * @brief Base for parsing tuples.
      38             :      * @param variableList A variable list describing the tuples.
      39             :      * @param ldrName The name of the LDR.
      40             :      */
      41             :     explicit TuplesParser(std::string variableList, std::string ldrName);
      42             : 
      43             :     /**
      44             :      * @brief The variable list.
      45             :      * @return The variable list.
      46             :      */
      47             :     [[nodiscard]] const std::string& getVariableList() const;
      48             : 
      49             :     /**
      50             :      * @brief The LDR name.
      51             :      * @return The LDR name.
      52             :      */
      53             :     [[nodiscard]] const std::string& getLdrName() const;
      54             : 
      55             :     /**
      56             :      * @brief Extracts tokens from a tuple string.
      57             :      * @param tuple The tuple.
      58             :      * @param regex Regex extracting tokens into groups.
      59             :      * @param numTokens Number of groups (including 0 == full match) to extract.
      60             :      * @return The extracted tokens.
      61             :      */
      62             :     [[nodiscard]] std::vector<std::optional<std::string>> extractTokens(
      63             :         const std::string& tuple, const std::regex& regex,
      64             :         size_t numTokens) const;
      65             : 
      66             :     /**
      67             :      * @brief Checks for errors and throws if a condition is fulfilled.
      68             :      * @param varList Variable list.
      69             :      * @param errorMap Map of {varList, {error condition, error message}}.
      70             :      * @param ldrName Name of the LDR.
      71             :      */
      72             :     static void checkForErrors(const std::string& varList,
      73             :         const std::multimap<std::string, std::tuple<bool, std::string>>&
      74             :             errorMap,
      75             :         const std::string& ldrName);
      76             : 
      77             :     /**
      78             :      * @brief Parses token as double.
      79             :      * @param token The token string.
      80             :      * @return The double parsed from the token.
      81             :      */
      82             :     static double parseDoubleToken(const std::optional<std::string>& token);
      83             : 
      84             :     template<typename R>
      85             :     std::optional<R> next(
      86             :         const std::function<std::optional<std::string>()>& nextTuple,
      87             :         const std::function<R(const std::string&)>& create);
      88             : 
      89             : private:
      90             :     const std::string m_variableList;
      91             :     const std::string m_ldrName;
      92             : 
      93             :     static std::optional<std::string> extractToken(
      94             :         const std::smatch& matches, size_t index);
      95             :     [[nodiscard]] std::smatch match(
      96             :         const std::string& tuple, const std::regex& regex) const;
      97             : };
      98             : 
      99             : template<typename R>
     100         118 : std::optional<R> libjdx::jdx::util::TuplesParser::next(
     101             :     const std::function<std::optional<std::string>()>& nextTuple,
     102             :     const std::function<R(const std::string&)>& create)
     103             : {
     104         231 :     auto tuple = nextTuple();
     105         113 :     if (!tuple)
     106             :     {
     107          25 :         return std::nullopt;
     108             :     }
     109         164 :     auto nextObject = create(tuple.value());
     110          76 :     return nextObject;
     111             : }
     112             : 
     113             : }
     114             : 
     115             : #endif /* JDX_TUPLESPARSER_HPP */

Generated by: LCOV version 1.14