2021-10-23 12:22:41 +03: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-25 01:04:23 +03:00
|
|
|
|
|
|
|
#include "forward_declarations.h"
|
2021-10-23 12:22:41 +03:00
|
|
|
#include "print_to_stream.h"
|
2021-10-24 13:21:32 +03:00
|
|
|
#include "header_start.h"
|
|
|
|
/// \cond
|
2021-10-23 12:22:41 +03:00
|
|
|
|
|
|
|
TOML_IMPL_NAMESPACE_START
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
struct formatter_constants
|
|
|
|
{
|
|
|
|
std::string_view pos_inf;
|
|
|
|
std::string_view neg_inf;
|
|
|
|
std::string_view nan;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct formatter_config
|
|
|
|
{
|
|
|
|
format_flags flags;
|
|
|
|
std::string_view indent;
|
|
|
|
};
|
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
class formatter
|
2021-10-23 12:22:41 +03:00
|
|
|
{
|
|
|
|
private:
|
2021-10-29 23:59:53 +03:00
|
|
|
const node* source_;
|
2021-10-23 12:22:41 +03:00
|
|
|
#if TOML_PARSER && !TOML_EXCEPTIONS
|
2021-10-29 23:59:53 +03:00
|
|
|
const parse_result* result_;
|
2021-10-23 12:22:41 +03:00
|
|
|
#endif
|
2021-10-29 23:59:53 +03:00
|
|
|
const formatter_constants* constants_;
|
|
|
|
formatter_config config_;
|
|
|
|
size_t indent_columns_;
|
|
|
|
std::ostream* stream_; //
|
|
|
|
int indent_; // these are set in attach()
|
|
|
|
bool naked_newline_; //
|
2021-10-27 16:03:05 +03:00
|
|
|
|
2021-10-29 11:12:41 +03:00
|
|
|
protected:
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-29 23:59:53 +03:00
|
|
|
const node& source() const noexcept
|
2021-10-23 12:22:41 +03:00
|
|
|
{
|
|
|
|
return *source_;
|
|
|
|
}
|
2021-10-24 13:21:32 +03:00
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-25 01:04:23 +03:00
|
|
|
std::ostream& stream() const noexcept
|
2021-10-23 12:22:41 +03:00
|
|
|
{
|
|
|
|
return *stream_;
|
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03: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-29 11:12:41 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
size_t indent_columns() const noexcept
|
|
|
|
{
|
|
|
|
return indent_columns_;
|
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_array_elements() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::indent_array_elements);
|
2021-10-27 16:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_sub_tables() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::indent_sub_tables);
|
2021-10-27 16:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03:00
|
|
|
bool quote_dates_and_times() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::quote_dates_and_times);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool quote_infinities_and_nans() const noexcept
|
|
|
|
{
|
|
|
|
return !!(config_.flags & format_flags::quote_infinities_and_nans);
|
2021-10-23 12:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03:00
|
|
|
bool literal_strings_allowed() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::allow_literal_strings);
|
2021-10-23 12:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03:00
|
|
|
bool multi_line_strings_allowed() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::allow_multi_line_strings);
|
2021-10-23 12:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03:00
|
|
|
bool value_format_flags_allowed() const noexcept
|
|
|
|
{
|
2021-10-29 23:59:53 +03:00
|
|
|
return !!(config_.flags & format_flags::allow_value_format_flags);
|
2021-10-23 12:22:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 16:03:05 +03:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 12:22:41 +03:00
|
|
|
bool naked_newline() const noexcept
|
|
|
|
{
|
|
|
|
return naked_newline_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_naked_newline() noexcept
|
|
|
|
{
|
|
|
|
naked_newline_ = false;
|
|
|
|
}
|
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void attach(std::ostream& stream) noexcept;
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void detach() noexcept;
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print_newline(bool force = false);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print_indent();
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
2021-10-29 23:59:53 +03:00
|
|
|
void print_string(std::string_view str, bool allow_multi_line = true, bool allow_bare = false);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print(const value<std::string>&);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print(const value<int64_t>&);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print(const value<double>&);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03:00
|
|
|
TOML_API
|
|
|
|
void print(const value<bool>&);
|
|
|
|
|
|
|
|
TOML_API
|
|
|
|
void print(const value<date>&);
|
2021-10-23 12:22:41 +03:00
|
|
|
|
2021-10-25 01:04:23 +03: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
|
2021-10-29 11:12:41 +03:00
|
|
|
bool dump_failed_parse_result();
|
2021-10-25 01:04:23 +03:00
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
TOML_API
|
2021-10-29 23:59:53 +03:00
|
|
|
formatter(const node*, const parse_result*, const formatter_constants&, const formatter_config&) noexcept;
|
2021-10-23 12:22:41 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
|
|
|
|
/// \endcond
|
2021-10-24 13:21:32 +03:00
|
|
|
#include "header_end.h"
|