rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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. More...
 
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. More...
 
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. More...
 
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 More...
 
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 More...
 

Detailed Description

Load a file from disk into a buffer.

Function Documentation

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.

Referenced by c4::yml::file_get_contents(), and main().

107 {
108  RYML_ASSERT_BASIC_(filesz <= bufsz);(void)bufsz;
109  size_t read = std::fread(buf, 1, filesz, fp); // NOLINT(clang-analyzer-unix.Errno)
110  if C4_UNLIKELY(read != filesz)
111  RYML_ERR_BASIC_("{}: failed file read: expected={}B actual={}B", filename, filesz, read); // LCOV_EXCL_LINE
112 }
ReadResult read(ConstNodeRef const &n, T *v)
Definition: node.hpp:2074
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 }
ContiguousContainer 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: file.hpp:165
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 }
ContiguousContainer 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: file.hpp:165
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 }
ContiguousContainer 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: file.hpp:165
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 }
ContiguousContainer 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: file.hpp:165