rapidyaml 0.14.0
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 91 of file file.hpp.

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}
bool read(ConstNodeRef const &n, T *v)
Definition node.hpp:1589

◆ 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 104 of file file.hpp.

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}
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

◆ 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 120 of file file.hpp.

121{
122 detail::ScopedFILE f(filename, access);
123 return file_get_contents(filename, f.file, buf, bufsz);
124}

◆ 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 130 of file file.hpp.

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}

◆ 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 150 of file file.hpp.

151{
152 ContiguousContainer cc;
153 file_get_contents(&cc, filename, access);
154 return cc;
155}