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
|
|
|
|
2021-11-02 20:13:09 +00:00
|
|
|
#include "preprocessor.h"
|
|
|
|
#if TOML_ENABLE_FORMATTERS
|
|
|
|
|
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-29 20:59:53 +00:00
|
|
|
struct formatter_constants
|
|
|
|
{
|
2021-11-02 20:13:09 +00:00
|
|
|
format_flags mandatory_flags;
|
|
|
|
format_flags ignored_flags;
|
|
|
|
|
|
|
|
std::string_view float_pos_inf;
|
|
|
|
std::string_view float_neg_inf;
|
|
|
|
std::string_view float_nan;
|
|
|
|
|
|
|
|
std::string_view bool_true;
|
|
|
|
std::string_view bool_false;
|
2021-10-29 20:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct formatter_config
|
|
|
|
{
|
|
|
|
format_flags flags;
|
|
|
|
std::string_view indent;
|
|
|
|
};
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
class TOML_EXPORTED_CLASS formatter
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
private:
|
2021-10-29 20:59:53 +00:00
|
|
|
const node* source_;
|
2021-10-30 12:56:14 +00:00
|
|
|
#if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
|
2021-10-29 20:59:53 +00:00
|
|
|
const parse_result* result_;
|
2021-10-23 09:22:41 +00:00
|
|
|
#endif
|
2021-10-29 20:59:53 +00:00
|
|
|
const formatter_constants* constants_;
|
|
|
|
formatter_config config_;
|
|
|
|
size_t indent_columns_;
|
2021-11-02 20:13:09 +00:00
|
|
|
format_flags int_format_mask_;
|
2021-10-29 20:59:53 +00:00
|
|
|
std::ostream* stream_; //
|
|
|
|
int indent_; // these are set in attach()
|
|
|
|
bool naked_newline_; //
|
2021-10-27 13:03:05 +00:00
|
|
|
|
2021-10-29 08:12:41 +00:00
|
|
|
protected:
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-29 20:59:53 +00:00
|
|
|
const node& source() const noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
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-29 08:12:41 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
size_t indent_columns() const noexcept
|
|
|
|
{
|
|
|
|
return indent_columns_;
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_array_elements() const noexcept
|
|
|
|
{
|
2021-10-29 20:59:53 +00:00
|
|
|
return !!(config_.flags & format_flags::indent_array_elements);
|
2021-10-27 13:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool indent_sub_tables() const noexcept
|
|
|
|
{
|
2021-10-29 20:59:53 +00:00
|
|
|
return !!(config_.flags & format_flags::indent_sub_tables);
|
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-29 20:59:53 +00:00
|
|
|
return !!(config_.flags & format_flags::allow_literal_strings);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 14:23:45 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool multi_line_strings_allowed() const noexcept
|
|
|
|
{
|
|
|
|
return !!(config_.flags & format_flags::allow_multi_line_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool real_tabs_in_strings_allowed() const noexcept
|
|
|
|
{
|
|
|
|
return !!(config_.flags & format_flags::allow_real_tabs_in_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool unicode_strings_allowed() const noexcept
|
|
|
|
{
|
|
|
|
return !!(config_.flags & format_flags::allow_unicode_strings);
|
|
|
|
}
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void attach(std::ostream& stream) noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void detach() noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print_newline(bool force = false);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print_indent();
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-11-02 20:13:09 +00:00
|
|
|
void print_unformatted(char);
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-11-02 20:13:09 +00:00
|
|
|
void print_unformatted(std::string_view);
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-29 20:59:53 +00:00
|
|
|
void print_string(std::string_view str, bool allow_multi_line = true, bool allow_bare = false);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<std::string>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<int64_t>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<double>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<bool>&);
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<date>&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<time>&);
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const value<date_time>&);
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print_value(const node&, node_type);
|
|
|
|
|
|
|
|
TOML_NODISCARD
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-29 08:12:41 +00:00
|
|
|
bool dump_failed_parse_result();
|
2021-10-24 22:04:23 +00:00
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-29 20:59:53 +00:00
|
|
|
formatter(const node*, const parse_result*, const formatter_constants&, const formatter_config&) noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
|
|
|
|
/// \endcond
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|
2021-11-02 20:13:09 +00:00
|
|
|
#endif // TOML_ENABLE_FORMATTERS
|