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-01-12 15:37:02 +00:00
|
|
|
namespace toml
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
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,
|
|
|
|
always_print_as_inline = 1,
|
|
|
|
quote_dates_and_times = 2
|
|
|
|
};
|
|
|
|
[[nodiscard]] constexpr format_flags operator & (format_flags lhs, format_flags rhs) noexcept
|
|
|
|
{
|
|
|
|
return static_cast<format_flags>(impl::unwrap_enum(lhs) & impl::unwrap_enum(rhs));
|
2020-01-07 15:52:50 +00:00
|
|
|
}
|
2020-01-12 15:37:02 +00:00
|
|
|
[[nodiscard]] constexpr format_flags operator | (format_flags lhs, format_flags rhs) noexcept
|
2020-01-06 18:21:16 +00:00
|
|
|
{
|
2020-01-12 15:37:02 +00:00
|
|
|
return static_cast<format_flags>( impl::unwrap_enum(lhs) | impl::unwrap_enum(rhs) );
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 18:21:16 +00:00
|
|
|
|
2020-01-12 15:37:02 +00:00
|
|
|
namespace toml::impl
|
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
template <typename CHAR = char>
|
2020-01-06 18:21:16 +00:00
|
|
|
class formatter
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
private:
|
2020-01-12 15:37:02 +00:00
|
|
|
const toml::node* source_;
|
|
|
|
format_flags flags_;
|
2020-01-07 15:52:50 +00:00
|
|
|
int indent_;
|
|
|
|
bool naked_newline_;
|
2020-01-12 15:37:02 +00:00
|
|
|
std::basic_ostream<CHAR>* stream_ = nullptr;
|
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-01-07 15:52:50 +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-01-07 15:52:50 +00:00
|
|
|
[[nodiscard]] int indent() const noexcept { return indent_; }
|
|
|
|
void indent(int level) noexcept { indent_ = level; }
|
|
|
|
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-01-07 15:52:50 +00:00
|
|
|
void attach(std::basic_ostream<CHAR>& stream) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
indent_ = 0;
|
|
|
|
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-01-07 15:52:50 +00:00
|
|
|
void print_newline(bool force = false) TOML_MAY_THROW
|
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-01-07 15:52:50 +00:00
|
|
|
void print_indent() TOML_MAY_THROW
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-01-07 15:52:50 +00:00
|
|
|
for (int 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-01-07 15:52:50 +00:00
|
|
|
void print_quoted_string(toml::string_view str) TOML_MAY_THROW
|
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-01-07 15:52:50 +00:00
|
|
|
void print(const value<T>& val) TOML_MAY_THROW
|
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-01-07 15:52:50 +00:00
|
|
|
void print(const node& val_node, node_type type) TOML_MAY_THROW
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
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
|
|
|
};
|
|
|
|
}
|