Program Listing for File ParseUtils.hpp

↰ Return to documentation for file (src/utils/ParseUtils.hpp)

#ifndef PARSEUTILS_HPP
#define PARSEUTILS_HPP

#include "../pdfs/PDF.hpp"
#include <cstddef>
#include <iterator>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <toml.hpp>
#include <vector>

namespace utils {

    inline auto stod(const std::string& str) -> double
    {
        size_t processedLen = 0;
        auto result = std::stod(str, &processedLen);
        if (processedLen != str.size())
        {
            throw std::invalid_argument("");
        }
        return result;
    }

    auto trim(const std::string& str) -> std::string;

    inline auto tokenize(const std::string& str)
        -> std::vector<std::string>
    {
        std::istringstream stream(str);
        std::vector<std::string> tokens(
            (std::istream_iterator<std::string>(stream)),
            std::istream_iterator<std::string>()
        );
        return tokens;
    }

    inline auto splitOnChar(const std::string& str, const char delim)
        -> std::vector<std::string>
    {
        std::vector<std::string> tokens;
        std::size_t start = 0;
        while (true)
        {
            const std::size_t pos = str.find(delim, start);
            if (pos == std::string::npos)
            {
                tokens.push_back(str.substr(start));
                break;
            }
            tokens.push_back(str.substr(start, pos - start));
            start = pos + 1;
        }
        return tokens;
    }

    auto initPDFFromString(const std::string& value,
        const std::string& prefix = "") -> pdfs::PDF;

    template<class T> auto getTOMLKeyWithError(
        const toml::table& inputDeck,
        const std::string& key,
        bool required = false) -> std::optional<T>;

} // namespace utils

#endif // PARSEUTILS_HPP