2020-03-12 15:23:25 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
2020-04-10 16:46:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-03-12 15:23:25 +00:00
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#pragma once
|
2020-01-11 21:15:24 +00:00
|
|
|
#include "toml_print_to_stream.h"
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_SWITCH_WARNINGS
|
|
|
|
TOML_DISABLE_PADDING_WARNINGS
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
namespace toml
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \brief Format flags for modifying how TOML data is printed to streams.
|
2020-01-12 15:37:02 +00:00
|
|
|
enum class format_flags : uint8_t
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
none,
|
2020-02-18 21:29:59 +00:00
|
|
|
quote_dates_and_times = 1
|
2020-01-12 15:37:02 +00:00
|
|
|
};
|
2020-04-13 20:23:11 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
TOML_GNU_ATTR(const)
|
|
|
|
TOML_ALWAYS_INLINE
|
|
|
|
constexpr format_flags operator & (format_flags lhs, format_flags rhs) noexcept
|
2020-01-12 15:37:02 +00:00
|
|
|
{
|
2020-02-03 09:12:43 +00:00
|
|
|
return static_cast<format_flags>(impl::unbox_enum(lhs) & impl::unbox_enum(rhs));
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
2020-04-13 20:23:11 +00:00
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
TOML_GNU_ATTR(const)
|
|
|
|
TOML_ALWAYS_INLINE
|
|
|
|
constexpr format_flags operator | (format_flags lhs, format_flags rhs) noexcept
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-02-03 09:12:43 +00:00
|
|
|
return static_cast<format_flags>( impl::unbox_enum(lhs) | impl::unbox_enum(rhs) );
|
2020-01-12 15:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
namespace toml::impl
|
2020-01-12 15:37:02 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char = char>
|
|
|
|
class TOML_API formatter
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
private:
|
2020-01-12 15:37:02 +00:00
|
|
|
const toml::node* source_;
|
2020-04-02 21:39:21 +00:00
|
|
|
std::basic_ostream<Char>* stream_ = nullptr;
|
2020-01-12 15:37:02 +00:00
|
|
|
format_flags flags_;
|
2020-02-29 20:34:08 +00:00
|
|
|
int8_t indent_;
|
2020-01-07 15:52:50 +00:00
|
|
|
bool naked_newline_;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
protected:
|
2020-01-07 15:52:50 +00:00
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
[[nodiscard]] const toml::node& source() const noexcept { return *source_; }
|
|
|
|
[[nodiscard]] format_flags flags() const noexcept { return flags_; }
|
2020-04-02 21:39:21 +00:00
|
|
|
[[nodiscard]] std::basic_ostream<Char>& stream() const noexcept { return *stream_; }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
static constexpr size_t indent_columns = 4;
|
|
|
|
static constexpr toml::string_view indent_string = TOML_STRING_PREFIX(" "sv);
|
2020-02-29 20:34:08 +00:00
|
|
|
[[nodiscard]] int8_t indent() const noexcept { return indent_; }
|
|
|
|
void indent(int8_t level) noexcept { indent_ = level; }
|
2020-01-07 15:52:50 +00:00
|
|
|
void increase_indent() noexcept { indent_++; }
|
|
|
|
void decrease_indent() noexcept { indent_--; }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void clear_naked_newline() noexcept { naked_newline_ = false; }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
void attach(std::basic_ostream<Char>& stream) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-29 20:34:08 +00:00
|
|
|
indent_ = {};
|
2020-01-07 15:52:50 +00:00
|
|
|
naked_newline_ = true;
|
|
|
|
stream_ = &stream;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
void detach() noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
stream_ = nullptr;
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
void print_newline(bool force = false)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if (!naked_newline_ || force)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream('\n', *stream_);
|
|
|
|
naked_newline_ = true;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
void print_indent()
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-29 20:34:08 +00:00
|
|
|
for (int8_t i = 0; i < indent_; i++)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
print_to_stream(indent_string, *stream_);
|
2020-01-07 15:52:50 +00:00
|
|
|
naked_newline_ = false;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
void print_quoted_string(toml::string_view str)
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
|
|
|
if (str.empty())
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream("\"\""sv, *stream_);
|
2020-01-06 18:21:16 +00:00
|
|
|
else
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream('"', *stream_);
|
2020-01-12 15:37:02 +00:00
|
|
|
print_to_stream_with_escapes(str, *stream_);
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream('"', *stream_);
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
naked_newline_ = false;
|
2020-01-06 18:21:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename T>
|
2020-03-28 16:56:59 +00:00
|
|
|
void print(const value<T>& val)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
if constexpr (std::is_same_v<T, string>)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
print_quoted_string(val.get());
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-01-07 15:52:50 +00:00
|
|
|
else
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-13 06:31:49 +00:00
|
|
|
static constexpr auto is_dt =
|
2020-01-07 15:52:50 +00:00
|
|
|
std::is_same_v<T, date>
|
|
|
|
|| std::is_same_v<T, time>
|
|
|
|
|| std::is_same_v<T, date_time>;
|
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
if constexpr (is_dt)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
if ((flags_ & format_flags::quote_dates_and_times) != format_flags::none)
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream('"', *stream_);
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
*stream_ << val;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-13 06:31:49 +00:00
|
|
|
if constexpr (is_dt)
|
2020-01-07 15:52:50 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
if ((flags_ & format_flags::quote_dates_and_times) != format_flags::none)
|
2020-01-07 15:52:50 +00:00
|
|
|
print_to_stream('"', *stream_);
|
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-01-07 15:52:50 +00:00
|
|
|
naked_newline_ = false;
|
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
void print_value(const node& val_node, node_type type)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_ASSUME(type > node_type::array);
|
2020-01-06 18:21:16 +00:00
|
|
|
switch (type)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
case node_type::string: print(*reinterpret_cast<const value<string>*>(&val_node)); break;
|
|
|
|
case node_type::integer: print(*reinterpret_cast<const value<int64_t>*>(&val_node)); break;
|
|
|
|
case node_type::floating_point: print(*reinterpret_cast<const value<double>*>(&val_node)); break;
|
|
|
|
case node_type::boolean: print(*reinterpret_cast<const value<bool>*>(&val_node)); break;
|
|
|
|
case node_type::date: print(*reinterpret_cast<const value<date>*>(&val_node)); break;
|
|
|
|
case node_type::time: print(*reinterpret_cast<const value<time>*>(&val_node)); break;
|
|
|
|
case node_type::date_time: print(*reinterpret_cast<const value<date_time>*>(&val_node)); break;
|
2020-01-04 14:21:38 +00:00
|
|
|
TOML_NO_DEFAULT_CASE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
formatter(const toml::node& source, format_flags flags) noexcept
|
|
|
|
: source_{ &source },
|
|
|
|
flags_{ flags }
|
|
|
|
{}
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
2020-04-02 21:39:21 +00:00
|
|
|
|
|
|
|
#if !TOML_ALL_INLINE
|
|
|
|
extern template class TOML_API formatter<char>;
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_POP_WARNINGS // TOML_DISABLE_SWITCH_WARNINGS, TOML_DISABLE_PADDING_WARNINGS
|