rapidyaml  0.16.0
parseandemitYAML,anddoitfast
writer_file.hpp
Go to the documentation of this file.
1 #ifndef C4_YML_WRITER_FILE_HPP_
2 #define C4_YML_WRITER_FILE_HPP_
3 
4 /** @file writer_file.hpp */
5 
6 #ifndef C4_YML_ERROR_HPP_
7 #include "c4/yml/error.hpp"
8 #endif
9 
10 #include <stdio.h> // fwrite(), fputc()
11 
12 namespace c4 {
13 namespace yml {
14 
15 /** A writer that outputs to a C file handle, defaulting to
16  * stdout. This writer is *much* faster than @ref WriterOStream and
17  * should be preferred to it.
18  * @ingroup doc_writers
19  * @ingroup doc_emit_to_file
20  */
22 {
23  FILE * m_file;
24 
25  WriterFile(FILE *f = nullptr) noexcept : m_file(f ? f : stdout) {}
26 
27  template<size_t N>
28  C4_ALWAYS_INLINE void append(const char (&a)[N]) noexcept // NOLINT(*-make-member-function-const)
29  {
30  static_assert(N > 1, "empty string");
31  (void)fwrite(a, sizeof(char), N - 1, m_file);
32  }
33 
34  C4_ALWAYS_INLINE void append(csubstr s) noexcept // NOLINT(*-make-member-function-const)
35  {
36  if(s.len)
37  {
38  C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wsign-conversion")
39  (void)fwrite(s.str, sizeof(csubstr::char_type), s.len, m_file);
40  C4_SUPPRESS_WARNING_GCC_CLANG_POP
41  }
42  }
43 
44  C4_ALWAYS_INLINE void append(const char c) noexcept // NOLINT(*-make-member-function-const)
45  {
46  (void)fputc(c, m_file);
47  }
48 
49  C4_ALWAYS_INLINE void append(const char c, size_t num_times) noexcept // NOLINT(*-make-member-function-const)
50  {
51  for(size_t i = 0; i < num_times; ++i)
52  (void)fputc(c, m_file);
53  }
54 };
55 
56 } // namespace yml
57 } // namespace c4
58 
59 #endif /* C4_YML_WRITER_FILE_HPP_ */
void append(const char(&a)[N]) noexcept
Definition: writer_file.hpp:28
void append(const char c) noexcept
Definition: writer_file.hpp:44
WriterFile(FILE *f=nullptr) noexcept
Definition: writer_file.hpp:25
Error utilities used by ryml.
A writer that outputs to a C file handle, defaulting to stdout.
Definition: writer_file.hpp:21
void append(const char c, size_t num_times) noexcept
Definition: writer_file.hpp:49
#define RYML_EXPORT
Definition: export.hpp:18
void append(csubstr s) noexcept
Definition: writer_file.hpp:34