rapidyaml 0.15.1
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
writer.hpp
Go to the documentation of this file.
1#ifndef _C4_YML_WRITER_HPP_
2#define _C4_YML_WRITER_HPP_
3
4#ifndef _C4_YML_ERROR_HPP_
5#include "c4/yml/error.hpp"
6#endif
7
8#include <stdio.h> // fwrite(), fputc()
9#include <string.h> // memcpy()
10
11
12namespace c4 {
13namespace yml {
14
15/** @addtogroup doc_emit
16 * @{
17 */
18
19/** @defgroup doc_writers Writer objects to use with an Emitter
20 * @see Emitter
21 * @{
22 */
23
24
25//-----------------------------------------------------------------------------
26//-----------------------------------------------------------------------------
27//-----------------------------------------------------------------------------
28
29/** A writer that outputs to a C file handle, defaulting to
30 * stdout. This writer is *much* faster than @ref WriterOStream and
31 * should be preferred to it. */
33{
34 FILE * m_file;
35 size_t m_pos;
36
37 WriterFile(FILE *f = nullptr) : m_file(f ? f : stdout), m_pos(0) {}
38
39 substr _get(bool /*error_on_excess*/) const
40 {
41 substr sp;
42 sp.str = nullptr;
43 sp.len = m_pos;
44 return sp;
45 }
46
47 template<size_t N>
48 void _do_write(const char (&a)[N]) noexcept
49 {
50 static_assert(N > 1, "empty string");
51 (void)fwrite(a, sizeof(char), N - 1, m_file);
52 m_pos += N - 1;
53 }
54
55 void _do_write(csubstr s) noexcept
56 {
57 if(s.len)
58 {
59 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wsign-conversion")
60 (void)fwrite(s.str, sizeof(csubstr::char_type), s.len, m_file);
61 m_pos += s.len;
62 C4_SUPPRESS_WARNING_GCC_CLANG_POP
63 }
64 }
65
66 void _do_write(const char c) noexcept
67 {
68 (void)fputc(c, m_file);
69 ++m_pos;
70 }
71
72 void _do_write(const char c, size_t num_times) noexcept
73 {
74 for(size_t i = 0; i < num_times; ++i)
75 (void)fputc(c, m_file);
76 m_pos += num_times;
77 }
78};
79
80
81//-----------------------------------------------------------------------------
82//-----------------------------------------------------------------------------
83//-----------------------------------------------------------------------------
84
85/** A writer that outputs to an STL-like ostream.
86 *
87 * @warning This writer is *much* slower than @ref WriterFile, and
88 * @ref WriterFile should be preferred to this. The slowness is due to
89 * how std::ostream works, and not because of anything in the code of
90 * this class. */
91template<class OStream>
93{
94 OStream* m_stream;
95 size_t m_pos;
96
97 WriterOStream(OStream &s) : m_stream(&s), m_pos(0) {}
98
99 substr _get(bool /*error_on_excess*/) const noexcept
100 {
101 substr sp;
102 sp.str = nullptr;
103 sp.len = m_pos;
104 return sp;
105 }
106
107 template<size_t N>
108 void _do_write(const char (&a)[N]) noexcept
109 {
110 static_assert(N > 1, "empty string");
111 m_stream->write(a, N - 1);
112 m_pos += N - 1;
113 }
114
115 void _do_write(csubstr s) noexcept
116 {
117 if(s.len)
118 {
119 C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wsign-conversion")
120 m_stream->write(s.str, s.len);
121 m_pos += s.len;
122 C4_SUPPRESS_WARNING_GCC_CLANG_POP
123 }
124 }
125
126 void _do_write(const char c) noexcept
127 {
128 m_stream->put(c);
129 ++m_pos;
130 }
131
132 void _do_write(const char c, size_t num_times) noexcept
133 {
134 for(size_t i = 0; i < num_times; ++i)
135 m_stream->put(c);
136 m_pos += num_times;
137 }
138};
139
140
141//-----------------------------------------------------------------------------
142//-----------------------------------------------------------------------------
143//-----------------------------------------------------------------------------
144
145/** A writer to a memory buffer, in the form of a @ref substr */
147{
149 size_t m_pos;
150
151 WriterBuf(substr sp) noexcept : m_buf(sp), m_pos(0) {}
152
153 substr _get(bool error_on_excess) const
154 {
155 if(m_pos <= m_buf.len)
156 return m_buf.first(m_pos);
157 else if(error_on_excess)
158 _RYML_ERR_BASIC("not enough space in the given buffer");
159 substr sp;
160 sp.str = nullptr;
161 sp.len = m_pos;
162 return sp;
163 }
164
165 template<size_t N>
166 void _do_write(const char (&a)[N]) noexcept
167 {
168 static_assert(N > 1, "empty string");
169 _RYML_ASSERT_BASIC( ! m_buf.overlaps(a));
170 if(m_pos + N-1 <= m_buf.len)
171 memcpy(m_buf.str + m_pos, a, N-1);
172 m_pos += N-1;
173 }
174
175 void _do_write(csubstr s) noexcept
176 {
177 _RYML_ASSERT_BASIC( ! s.overlaps(m_buf));
178 if(s.len && m_pos + s.len <= m_buf.len)
179 memcpy(m_buf.str + m_pos, s.str, s.len);
180 m_pos += s.len;
181 }
182
183 void _do_write(const char c) noexcept
184 {
185 if(m_pos + 1 <= m_buf.len)
186 m_buf.str[m_pos] = c;
187 ++m_pos;
188 }
189
190 void _do_write(const char c, size_t num_times) noexcept
191 {
192 if(m_pos + num_times <= m_buf.len)
193 memset(m_buf.str + m_pos, c, num_times);
194 m_pos += num_times;
195 }
196};
197
198/** @ } */
199
200/** @ } */
201
202
203} // namespace yml
204} // namespace c4
205
206#endif /* _C4_YML_WRITER_HPP_ */
Error utilities used by ryml.
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
void _do_write(csubstr s) noexcept
Definition writer.hpp:55
void _do_write(const char(&a)[N]) noexcept
Definition writer.hpp:48
void _do_write(csubstr s) noexcept
Definition writer.hpp:175
WriterOStream(OStream &s)
Definition writer.hpp:97
void _do_write(const char c) noexcept
Definition writer.hpp:66
WriterFile(FILE *f=nullptr)
Definition writer.hpp:37
substr _get(bool error_on_excess) const
Definition writer.hpp:153
void _do_write(const char c) noexcept
Definition writer.hpp:126
void _do_write(const char(&a)[N]) noexcept
Definition writer.hpp:108
void _do_write(const char(&a)[N]) noexcept
Definition writer.hpp:166
void _do_write(const char c) noexcept
Definition writer.hpp:183
substr _get(bool) const
Definition writer.hpp:39
WriterBuf(substr sp) noexcept
Definition writer.hpp:151
void _do_write(const char c, size_t num_times) noexcept
Definition writer.hpp:72
substr _get(bool) const noexcept
Definition writer.hpp:99
void _do_write(const char c, size_t num_times) noexcept
Definition writer.hpp:190
void _do_write(csubstr s) noexcept
Definition writer.hpp:115
void _do_write(const char c, size_t num_times) noexcept
Definition writer.hpp:132
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216