rapidyaml 0.14.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
1#ifndef _C4_YML_FILE_HPP_
2#define _C4_YML_FILE_HPP_
3
4/** @name file.hpp Helpers to read/write files from disk */
5
6
7#ifndef _C4_YML_ERROR_HPP_
8#include <c4/yml/error.hpp>
9#endif
10#include <stddef.h>
11#include <stdio.h>
12
13
14namespace c4 {
15namespace yml {
16
17C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4996) // fopen: this function may be unsafe
18C4_SUPPRESS_WARNING_CLANG_WITH_PUSH("-Wdeprecated-declarations") // fopen is deprecated
19
20/** @cond dev */
21namespace detail {
22struct ScopedFILE
23{
24 FILE *file;
25 inline ScopedFILE(const char *filename, const char *access) // NOLINT
26 {
27 file = std::fopen(filename, access); // NOLINT
28 if(file == nullptr)
29 _RYML_ERR_BASIC("{}: could not open file", filename);
30 }
31 inline ~ScopedFILE() noexcept // NOLINT
32 {
33 std::fclose(file); // NOLINT
34 }
35 ScopedFILE(const ScopedFILE&) = delete;
36 ScopedFILE( ScopedFILE&&) = delete;
37 ScopedFILE& operator=(const ScopedFILE&) = delete;
38 ScopedFILE& operator=( ScopedFILE&&) = delete;
39};
40} // detail
41/** @endcond */
42
43
44//-----------------------------------------------------------------------------
45
46/** @defgroup doc_file_put_contents file_put_contents()
47 *
48 * Save a buffer to disk
49 *
50 * @ingroup doc_file_utils
51 *
52 * @addtogroup doc_file_put_contents
53 * @{
54 */
55
56/** save a contiguous buffer into a file */
57inline void file_put_contents(void const* buf, size_t sz, const char *filename, const char* access="wb")
58{
59 detail::ScopedFILE f(filename, access);
60 size_t written = std::fwrite(buf, 1, sz, f.file); // NOLINT
61 if(C4_UNLIKELY(written != sz))
62 _RYML_ERR_BASIC("{}: failed file write: expected={}B actual={}B", filename, sz, written); // LCOV_EXCL_LINE
63}
64
65/** save a contiguous buffer into a file */
66template<class ContiguousContainer>
67void file_put_contents(ContiguousContainer const& v, const char *filename, const char* access="wb")
68{
69 size_t vsz = static_cast<size_t>(v.size()) * sizeof(typename ContiguousContainer::value_type);
70 void const* vbuf = v.empty() ? nullptr : &v[0];
71 file_put_contents(vbuf, vsz, filename, access);
72}
73
74/** @} */
75
76
77//-----------------------------------------------------------------------------
78
79/** @defgroup doc_file_get_contents file_get_contents()
80 *
81 * Load a file from disk into a buffer.
82 *
83 * @ingroup doc_file_utils
84 *
85 * @addtogroup doc_file_get_contents
86 * @{
87 */
88
89/** load a file of specified size from disk into an existing contiguous buffer.
90 */
91inline void file_get_contents(const char *filename, FILE *fp, size_t filesz, void *buf, size_t bufsz)
92{
93 _RYML_ASSERT_BASIC(filesz <= bufsz);(void)bufsz;
94 size_t read = std::fread(buf, 1, filesz, fp);
95 if(C4_UNLIKELY(read != filesz))
96 _RYML_ERR_BASIC("{}: failed file read: expected={}B actual={}B", filename, filesz, read); // LCOV_EXCL_LINE
97}
98
99
100/** load a file from disk into an existing contiguous buffer.
101 *
102 * @return true if the file was successfully read and the buffer was
103 * large enough to fit the file size */
104C4_NODISCARD inline size_t file_get_contents(const char *filename, FILE *fp, void *buf, size_t bufsz)
105{
106 std::fseek(fp, 0, SEEK_END); // NOLINT
107 size_t filesz = static_cast<size_t>(std::ftell(fp)); // NOLINT
108 std::rewind(fp); // NOLINT
109 if(filesz <= bufsz)
110 file_get_contents(filename, fp, filesz, buf, bufsz);
111 return filesz;
112}
113
114
115/** load a file from disk into an existing contiguous buffer.
116 *
117 * @return the size required for the buffer. It is up to the caller to
118 * check that the returned size is smaller than the buffer's size.
119 */
120C4_NODISCARD inline size_t file_get_contents(const char *filename, void *buf, size_t bufsz, const char *access="rb")
121{
122 detail::ScopedFILE f(filename, access);
123 return file_get_contents(filename, f.file, buf, bufsz);
124}
125
126
127/** load a file from disk into an existing ContiguousContainer,
128 * resizing it to fit the file's contents */
129template<class ContiguousContainer>
130void file_get_contents(ContiguousContainer *v, const char *filename, const char *access="rb")
131{
132 using value_type = typename ContiguousContainer::value_type;
133 using size_type = typename ContiguousContainer::size_type;
134 detail::ScopedFILE f(filename, access);
135 void * dat = !v->empty() ? &(*v)[0] : nullptr;
136 size_t vsz = static_cast<size_t>(v->size());
137 size_t fsz = file_get_contents(filename, f.file, dat, vsz);
138 size_t num_elms = fsz / sizeof(value_type);
139 if(C4_UNLIKELY(fsz != num_elms * sizeof(value_type)))
140 _RYML_ERR_BASIC("{}: file size ({}B) not a multiple of element size ({}B)", filename, fsz, sizeof(value_type));
141 v->resize(static_cast<size_type>(num_elms));
142 if(fsz > vsz * sizeof(value_type))
143 file_get_contents(filename, f.file, fsz, &(*v)[0], fsz);
144}
145
146
147/** load a file from disk and return a newly created
148 * ContiguousContainer with the file contents */
149template<class ContiguousContainer>
150ContiguousContainer file_get_contents(const char *filename, const char *access="rb")
151{
152 ContiguousContainer cc;
153 file_get_contents(&cc, filename, access);
154 return cc;
155}
156
157
158/** @} */
159
160
161
162//-----------------------------------------------------------------------------
163
164/** @defgroup doc_stdin_get_contents stdin_get_contents()
165 *
166 * Load a file from stdin (or similar stream-like file) into a buffer.
167 *
168 * @ingroup doc_file_utils
169 *
170 * @addtogroup doc_stdin_get_contents
171 * @{
172 */
173
174/** load a file from stdin (or similar stream-like file) and
175 * return a newly created ContiguousContainer with the file
176 * contents */
177template<class ContiguousContainer>
178void stdin_get_contents(ContiguousContainer *cont, FILE *f=stdin)
179{
180 using I = typename ContiguousContainer::size_type;
181 using T = typename ContiguousContainer::value_type;
182 int c;
183 I pos = 0;
184 I num_bytes = 128;
185 I elmsz = static_cast<I>(sizeof(T));
186 I sz = (num_bytes + elmsz - 1) / elmsz; // round up to next multiple of elmsz
187 num_bytes = sz * elmsz;
188 cont->resize(sz);
189 unsigned char *buf = reinterpret_cast<unsigned char*>(&(*cont)[0]); // NOLINT
190 while((c = fgetc(f)) != EOF)
191 {
192 if(pos == num_bytes)
193 {
194 num_bytes = 2 * num_bytes;
195 if(num_bytes % sizeof(T)) goto errsize; // NOLINT // LCOV_EXCL_LINE
196 cont->resize(num_bytes / sizeof(T));
197 buf = reinterpret_cast<unsigned char*>(&(*cont)[0]); // NOLINT
198 }
199 buf[pos++] = static_cast<unsigned char>(c);
200 }
201 if(pos % sizeof(T))
202 goto errsize; // NOLINT
203 cont->resize(pos / sizeof(T));
204 return;
205errsize:
206 _RYML_ERR_BASIC("file size is not multiple of element size");
207}
208
209/** load a file from stdin and return a newly created
210 * ContiguousContainer with the file contents */
211template<class ContiguousContainer>
212ContiguousContainer stdin_get_contents(FILE *f=stdin)
213{
214 ContiguousContainer cc;
215 stdin_get_contents(&cc, f);
216 return cc;
217}
218
219/** @} */
220
221C4_SUPPRESS_WARNING_CLANG_POP
222C4_SUPPRESS_WARNING_MSVC_POP
223
224
225} // namespace yml
226} // namespace c4
227
228#endif /* _C4_YML_FILE_HPP_ */
Error utilities used by ryml.
void file_get_contents(const char *filename, FILE *fp, size_t filesz, void *buf, size_t bufsz)
load a file of specified size from disk into an existing contiguous buffer.
Definition file.hpp:91
void file_put_contents(void const *buf, size_t sz, const char *filename, const char *access="wb")
save a contiguous buffer into a file
Definition file.hpp:57
bool read(ConstNodeRef const &n, T *v)
Definition node.hpp:1589
void stdin_get_contents(ContiguousContainer *cont, FILE *f=stdin)
load a file from stdin (or similar stream-like file) and return a newly created ContiguousContainer w...
Definition file.hpp:178
a CRTP base providing read-only methods for ConstNodeRef and NodeRef
Definition common.cpp:282
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14