NeBuild dev
Loading...
Searching...
No Matches
formatter.hpp
Go to the documentation of this file.
1//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8#if TOML_ENABLE_FORMATTERS
9
11#include "print_to_stream.hpp"
12#include "header_start.hpp"
14
16{
17 struct formatter_constants
18 {
19 format_flags mandatory_flags;
20 format_flags ignored_flags;
21
22 std::string_view float_pos_inf;
23 std::string_view float_neg_inf;
24 std::string_view float_nan;
25
26 std::string_view bool_true;
27 std::string_view bool_false;
28 };
29
30 struct formatter_config
31 {
32 format_flags flags;
33 std::string_view indent;
34 };
35
36 class TOML_EXPORTED_CLASS formatter
37 {
38 private:
39 const node* source_;
40#if TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
41 const parse_result* result_;
42#endif
43 const formatter_constants* constants_;
44 formatter_config config_;
45 size_t indent_columns_;
46 format_flags int_format_mask_;
47 std::ostream* stream_; //
48 int indent_; // these are set in attach()
49 bool naked_newline_; //
50
51 protected:
53 const node& source() const noexcept
54 {
55 return *source_;
56 }
57
59 std::ostream& stream() const noexcept
60 {
61 return *stream_;
62 }
63
65 int indent() const noexcept
66 {
67 return indent_;
68 }
69
70 void indent(int level) noexcept
71 {
72 indent_ = level;
73 }
74
75 void increase_indent() noexcept
76 {
77 indent_++;
78 }
79
80 void decrease_indent() noexcept
81 {
82 indent_--;
83 }
84
86 size_t indent_columns() const noexcept
87 {
88 return indent_columns_;
89 }
90
92 bool indent_array_elements() const noexcept
93 {
94 return !!(config_.flags & format_flags::indent_array_elements);
95 }
96
98 bool indent_sub_tables() const noexcept
99 {
100 return !!(config_.flags & format_flags::indent_sub_tables);
101 }
102
104 bool literal_strings_allowed() const noexcept
105 {
106 return !!(config_.flags & format_flags::allow_literal_strings);
107 }
108
110 bool multi_line_strings_allowed() const noexcept
111 {
112 return !!(config_.flags & format_flags::allow_multi_line_strings);
113 }
114
116 bool real_tabs_in_strings_allowed() const noexcept
117 {
118 return !!(config_.flags & format_flags::allow_real_tabs_in_strings);
119 }
120
122 bool unicode_strings_allowed() const noexcept
123 {
124 return !!(config_.flags & format_flags::allow_unicode_strings);
125 }
126
128 bool terse_kvps() const noexcept
129 {
130 return !!(config_.flags & format_flags::terse_key_value_pairs);
131 }
132
134 void attach(std::ostream& stream) noexcept;
135
137 void detach() noexcept;
138
140 void print_newline(bool force = false);
141
143 void print_indent();
144
146 void print_unformatted(char);
147
149 void print_unformatted(std::string_view);
150
152 void print_string(std::string_view str,
153 bool allow_multi_line = true,
154 bool allow_bare = false,
155 bool allow_literal_whitespace = true);
156
158 void print(const value<std::string>&);
159
161 void print(const value<int64_t>&);
162
164 void print(const value<double>&);
165
167 void print(const value<bool>&);
168
170 void print(const value<date>&);
171
173 void print(const value<time>&);
174
176 void print(const value<date_time>&);
177
179 void print_value(const node&, node_type);
180
183 bool dump_failed_parse_result();
184
187 formatter(const node*, const parse_result*, const formatter_constants&, const formatter_config&) noexcept;
188 };
189}
191
193#include "header_end.hpp"
194#endif // TOML_ENABLE_FORMATTERS
The result of a parsing operation.
Definition parse_result.hpp:53
enum TOML_CLOSED_FLAGS_ENUM format_flags
Format flags for modifying how TOML data is printed to streams.
Definition forward_declarations.hpp:297
enum TOML_CLOSED_FLAGS_ENUM indent_sub_tables
Apply indentation to tables nested within other tables/arrays.
Definition forward_declarations.hpp:330
enum TOML_CLOSED_FLAGS_ENUM indent_array_elements
Apply indentation to array elements when the array is forced to wrap over multiple lines.
Definition forward_declarations.hpp:333
#define TOML_EXPORTED_CLASS
An 'export' annotation to add to classes. \detail Not defined by default.
Definition preprocessor.hpp:979
#define TOML_EXPORTED_MEMBER_FUNCTION
An 'export' annotation to add to non-static class member functions. \detail Not defined by default.
Definition preprocessor.hpp:982
Definition json.h:5363
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_NODISCARD
Definition preprocessor.hpp:439
#define TOML_PURE_INLINE_GETTER
Definition preprocessor.hpp:479
#define TOML_IMPL_NAMESPACE_END
Definition preprocessor.hpp:1334
#define TOML_IMPL_NAMESPACE_START
Definition preprocessor.hpp:1333
A date-time.
Definition date_time.hpp:327
A local time-of-day.
Definition date_time.hpp:113