rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
file_get_contents()

Load a file from disk into a buffer. More...

Functions

void c4::yml::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.
size_t c4::yml::file_get_contents (const char *filename, FILE *fp, void *buf, size_t bufsz)
 load a file from disk into an existing contiguous buffer.
size_t c4::yml::file_get_contents (const char *filename, void *buf, size_t bufsz, const char *access="rb")
 load a file from disk into an existing contiguous buffer.
template<class ContiguousContainer>
void c4::yml::file_get_contents (ContiguousContainer *v, const char *filename, const char *access="rb")
 load a file from disk into an existing ContiguousContainer, resizing it to fit the file's contents
template<class ContiguousContainer>
ContiguousContainer c4::yml::file_get_contents (const char *filename, const char *access="rb")
 load a file from disk and return a newly created ContiguousContainer with the file contents

Detailed Description

Load a file from disk into a buffer.

Function Documentation

◆ file_get_contents() [1/5]

void c4::yml::file_get_contents ( const char * filename,
FILE * fp,
size_t filesz,
void * buf,
size_t bufsz )
inline

load a file of specified size from disk into an existing contiguous buffer.

Definition at line 106 of file file.hpp.

107{
108 _RYML_ASSERT_BASIC(filesz <= bufsz);(void)bufsz;
109 size_t read = std::fread(buf, 1, filesz, fp);
110 if(C4_UNLIKELY(read != filesz))
111 _RYML_ERR_BASIC("{}: failed file read: expected={}B actual={}B", filename, filesz, read); // LCOV_EXCL_LINE
112}
bool read(ConstNodeRef const &n, T *v)
Definition node.hpp:1739

◆ file_get_contents() [2/5]

size_t c4::yml::file_get_contents ( const char * filename,
FILE * fp,
void * buf,
size_t bufsz )
inline

load a file from disk into an existing contiguous buffer.

Returns
true if the file was successfully read and the buffer was large enough to fit the file size

Definition at line 119 of file file.hpp.

120{
121 std::fseek(fp, 0, SEEK_END); // NOLINT
122 size_t filesz = static_cast<size_t>(std::ftell(fp)); // NOLINT
123 std::rewind(fp); // NOLINT
124 if(filesz <= bufsz)
125 file_get_contents(filename, fp, filesz, buf, bufsz);
126 return filesz;
127}
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:106

◆ file_get_contents() [3/5]

size_t c4::yml::file_get_contents ( const char * filename,
void * buf,
size_t bufsz,
const char * access = "rb" )
inline

load a file from disk into an existing contiguous buffer.

Returns
the size required for the buffer. It is up to the caller to check that the returned size is smaller than the buffer's size.

Definition at line 135 of file file.hpp.

136{
137 detail::ScopedFILE f(filename, access);
138 return file_get_contents(filename, f.file, buf, bufsz);
139}

◆ file_get_contents() [4/5]

template<class ContiguousContainer>
void c4::yml::file_get_contents ( ContiguousContainer * v,
const char * filename,
const char * access = "rb" )

load a file from disk into an existing ContiguousContainer, resizing it to fit the file's contents

Definition at line 145 of file file.hpp.

146{
147 using value_type = typename ContiguousContainer::value_type;
148 using size_type = typename ContiguousContainer::size_type;
149 detail::ScopedFILE f(filename, access);
150 void * dat = !v->empty() ? &(*v)[0] : nullptr;
151 size_t vsz = static_cast<size_t>(v->size());
152 size_t fsz = file_get_contents(filename, f.file, dat, vsz);
153 size_t num_elms = fsz / sizeof(value_type);
154 if(C4_UNLIKELY(fsz != num_elms * sizeof(value_type)))
155 _RYML_ERR_BASIC("{}: file size ({}B) not a multiple of element size ({}B)", filename, fsz, sizeof(value_type));
156 v->resize(static_cast<size_type>(num_elms));
157 if(fsz > vsz * sizeof(value_type))
158 file_get_contents(filename, f.file, fsz, &(*v)[0], fsz);
159}

◆ file_get_contents() [5/5]

template<class ContiguousContainer>
ContiguousContainer c4::yml::file_get_contents ( const char * filename,
const char * access = "rb" )

load a file from disk and return a newly created ContiguousContainer with the file contents

Definition at line 165 of file file.hpp.

166{
167 ContiguousContainer cc;
168 file_get_contents(&cc, filename, access);
169 return cc;
170}