2021-10-23 09:22:41 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
2021-10-24 22:04:23 +00:00
|
|
|
|
|
|
|
#include "forward_declarations.h"
|
2021-10-23 09:22:41 +00:00
|
|
|
#include "print_to_stream.h"
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_start.h"
|
|
|
|
/// \cond
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
TOML_IMPL_NAMESPACE_START
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
class formatter
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
const toml::node* source_;
|
2021-10-24 22:04:23 +00:00
|
|
|
std::ostream* stream_ = {};
|
2021-10-27 13:03:05 +00:00
|
|
|
format_flags flags_; //
|
|
|
|
int indent_; // these are set in attach()
|
|
|
|
bool naked_newline_; //
|
2021-10-23 09:22:41 +00:00
|
|
|
#if TOML_PARSER && !TOML_EXCEPTIONS
|
|
|
|
const parse_result* result_ = {};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
protected:
|
2021-10-27 13:03:05 +00:00
|
|
|
static constexpr size_t indent_columns = 4;
|
|
|
|
static constexpr std::string_view indent_string = " "sv;
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
const toml::node& source() const noexcept
|
|
|
|
{
|
|
|
|
return *source_;
|
|
|
|
}
|
2021-10-24 10:21:32 +00:00
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-24 22:04:23 +00:00
|
|
|
std::ostream& stream() const noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
return *stream_;
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
int indent() const noexcept
|
|
|
|
{
|
|
|
|
return indent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void indent(int level) noexcept
|
|
|
|
{
|
|
|
|
indent_ = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
void increase_indent() noexcept
|
|
|
|
{
|
|
|
|
indent_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void decrease_indent() noexcept
|
|
|
|
{
|
|
|
|
indent_--;
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_array_elements() const noexcept
|
|
|
|
{
|
|
|
|
return !!(flags_ & format_flags::indent_array_elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_sub_tables() const noexcept
|
|
|
|
{
|
|
|
|
return !!(flags_ & format_flags::indent_sub_tables);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool quote_dates_and_times() const noexcept
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
return !!(flags_ & format_flags::quote_dates_and_times);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool literal_strings_allowed() const noexcept
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
return !!(flags_ & format_flags::allow_literal_strings);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool multi_line_strings_allowed() const noexcept
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
return !!(flags_ & format_flags::allow_multi_line_strings);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool value_format_flags_allowed() const noexcept
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
return !!(flags_ & format_flags::allow_value_format_flags);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool naked_newline() const noexcept
|
|
|
|
{
|
|
|
|
return naked_newline_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_naked_newline() noexcept
|
|
|
|
{
|
|
|
|
naked_newline_ = false;
|
|
|
|
}
|
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void attach(std::ostream& stream) noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void detach() noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print_newline(bool force = false);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print_indent();
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print_quoted_string(std::string_view str, bool allow_multi_line = true);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print(const value<std::string>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print(const value<int64_t>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print(const value<double>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print(const value<bool>&);
|
|
|
|
|
|
|
|
TOML_API
|
|
|
|
void print(const value<date>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_API
|
|
|
|
void print(const value<time>&);
|
|
|
|
|
|
|
|
TOML_API
|
|
|
|
void print(const value<date_time>&);
|
|
|
|
|
|
|
|
TOML_API
|
|
|
|
void print_value(const node&, node_type);
|
|
|
|
|
|
|
|
TOML_NODISCARD
|
|
|
|
TOML_API
|
|
|
|
bool dump_failed_parse_result() noexcept(!TOML_PARSER || TOML_EXCEPTIONS);
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2021-10-23 09:22:41 +00:00
|
|
|
formatter(const toml::node& source, format_flags flags) noexcept //
|
|
|
|
: source_{ &source },
|
|
|
|
flags_{ flags }
|
|
|
|
{}
|
|
|
|
|
|
|
|
#if TOML_PARSER && !TOML_EXCEPTIONS
|
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
TOML_API
|
|
|
|
formatter(const parse_result& result, format_flags flags) noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
|
|
|
|
/// \endcond
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|