rapidyaml  0.16.0
parseandemitYAML,anddoitfast
stdin_get_contents()

Load a file from stdin (or similar stream-like file) into a buffer. More...

Functions

template<class ContiguousContainer >
void c4::yml::stdin_get_contents (ContiguousContainer *cont, FILE *f=stdin)
 load a file from stdin (or similar stream-like file) and return a newly created ContiguousContainer with the file contents More...
 
template<class ContiguousContainer >
ContiguousContainer c4::yml::stdin_get_contents (FILE *f=stdin)
 load a file from stdin and return a newly created ContiguousContainer with the file contents More...
 

Detailed Description

Load a file from stdin (or similar stream-like file) into a buffer.

Function Documentation

template<class ContiguousContainer >
void c4::yml::stdin_get_contents ( ContiguousContainer *  cont,
FILE *  f = stdin 
)

load a file from stdin (or similar stream-like file) and return a newly created ContiguousContainer with the file contents

Definition at line 193 of file file.hpp.

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

194 {
195  using I = typename ContiguousContainer::size_type;
196  using T = typename ContiguousContainer::value_type;
197  int c;
198  I pos = 0;
199  I num_bytes = 128;
200  I elmsz = static_cast<I>(sizeof(T));
201  I sz = (num_bytes + elmsz - 1) / elmsz; // round up to next multiple of elmsz
202  num_bytes = sz * elmsz;
203  cont->resize(sz);
204  unsigned char *buf = reinterpret_cast<unsigned char*>(&(*cont)[0]); // NOLINT
205  while((c = fgetc(f)) != EOF)
206  {
207  if(pos == num_bytes)
208  {
209  num_bytes = 2 * num_bytes;
210  if(num_bytes % sizeof(T)) goto errsize; // NOLINT // LCOV_EXCL_LINE
211  cont->resize(num_bytes / sizeof(T));
212  buf = reinterpret_cast<unsigned char*>(&(*cont)[0]); // NOLINT
213  }
214  buf[pos++] = static_cast<unsigned char>(c);
215  }
216  if(pos % sizeof(T))
217  goto errsize; // NOLINT
218  cont->resize(pos / sizeof(T));
219  return;
220 errsize:
221  RYML_ERR_BASIC_("file size is not multiple of element size");
222 }
template<class ContiguousContainer >
ContiguousContainer c4::yml::stdin_get_contents ( FILE *  f = stdin)

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

Definition at line 227 of file file.hpp.

228 {
229  ContiguousContainer cc;
230  stdin_get_contents(&cc, f);
231  return cc;
232 }
ContiguousContainer stdin_get_contents(FILE *f=stdin)
load a file from stdin and return a newly created ContiguousContainer with the file contents ...
Definition: file.hpp:227