Function parse

Summary

#include <toml/parser.hpp>

(1) template <typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY, template< typename ... > class Table = std::unordered_map, template< typename ... > class Array = std::vector>
    basic_value< Comment, Table, Array > parse(std::istream &is, const std::string &fname="unknown file")

(2) template <typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY, template< typename ... > class Table = std::unordered_map, template< typename ... > class Array = std::vector>
    basic_value< Comment, Table, Array > parse(const std::string &fname)

Function overload

Synopsis

#include <toml/parser.hpp>

template <typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY, template< typename ... > class Table = std::unordered_map, template< typename ... > class Array = std::vector>
basic_value< Comment, Table, Array > parse(std::istream &is, const std::string &fname="unknown file")

Description

No description yet.

Mentioned in

Source

Lines 2081-2130 in toml/parser.hpp.

template<typename                     Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
         template<typename ...> class Table   = std::unordered_map,
         template<typename ...> class Array   = std::vector>
basic_value<Comment, Table, Array>
parse(std::istream& is, const std::string& fname = "unknown file")
{
    using value_type = basic_value<Comment, Table, Array>;

    const auto beg = is.tellg();
    is.seekg(0, std::ios::end);
    const auto end = is.tellg();
    const auto fsize = end - beg;
    is.seekg(beg);

    // read whole file as a sequence of char
    assert(fsize >= 0);
    std::vector<char> letters(static_cast<std::size_t>(fsize));
    is.read(letters.data(), fsize);

    while(!letters.empty() && letters.back() == '\0')
    {
        letters.pop_back();
    }
    assert(letters.empty() || letters.back() != '\0');

    detail::location loc(std::move(fname), std::move(letters));

    // skip BOM if exists.
    // XXX component of BOM (like 0xEF) exceeds the representable range of
    // signed char, so on some (actually, most) of the environment, these cannot
    // be compared to char. However, since we are always out of luck, we need to
    // check our chars are equivalent to BOM. To do this, first we need to
    // convert char to unsigned char to guarantee the comparability.
    if(loc.source()->size() >= 3)
    {
        std::array<unsigned char, 3> BOM;
        std::memcpy(BOM.data(), loc.source()->data(), 3);
        if(BOM[0] == 0xEF && BOM[1] == 0xBB && BOM[2] == 0xBF)
        {
            loc.advance(3); // BOM found. skip.
        }
    }

    const auto data = detail::parse_toml_file<value_type>(loc);
    if(!data)
    {
        throw syntax_error(data.unwrap_err(), source_location(loc));
    }
    return data.unwrap();
}

Synopsis

#include <toml/parser.hpp>

template <typename Comment = TOML11_DEFAULT_COMMENT_STRATEGY, template< typename ... > class Table = std::unordered_map, template< typename ... > class Array = std::vector>
basic_value< Comment, Table, Array > parse(const std::string &fname)

Description

No description yet.

Mentioned in

Source

Lines 2132-2143 in toml/parser.hpp.

template<typename                     Comment = TOML11_DEFAULT_COMMENT_STRATEGY,
         template<typename ...> class Table   = std::unordered_map,
         template<typename ...> class Array   = std::vector>
basic_value<Comment, Table, Array> parse(const std::string& fname)
{
    std::ifstream ifs(fname.c_str(), std::ios_base::binary);
    if(!ifs.good())
    {
        throw std::runtime_error("toml::parse: file open error -> " + fname);
    }
    return parse<Comment, Table, Array>(ifs, fname);
}





Add Discussion as Guest

Log in