8 #if defined(RYML_SINGLE_HEADER) // using the single header directly in the executable
9 #define RYML_SINGLE_HDR_DEFINE_NOW
10 #include <ryml_ints.hpp>
11 #elif defined(RYML_SINGLE_HEADER_LIB) // using the single header from a library
12 #include <ryml_ints.hpp>
29 std::vector<char> yaml = {};
30 std::vector<char> arena = {};
31 std::vector<evt_bits> events = {};
34 yaml.assign(yaml_.
begin(), yaml_.
end());
38 events.resize(static_cast<size_t>(esz));
39 arena.resize(arena_size);
90 const char *filename =
nullptr;
99 printf(R
"(usage: %.*s [options] [<filename>]
101 if filename is missing, we default to a preset example.
105 -e <size>,--events-size <size> set size of events buffer. -1 means
106 estimate size. default=%d
107 -a <size>,--arena-size <size> set size of arena buffer. -1 means
108 take the source size as the arena size.
110 -n,--no-retry do not retry parsing if any buffer size
111 was insufficient. default=%s
112 -q,--quiet do not print events. default=%s
114 static_cast<int>(basename.len), basename.str,
117 defaults.retry ? "retry" :
"do not retry",
118 defaults.
quiet ?
"quiet" :
"print events");
124 int remaining = argc - 1;
126 auto advance = [&](
int num) { argv += num; remaining -= num; };
127 auto parse_arg = [&](
const char *name,
int *dst) {
134 printf(
"error: could not parse %s value: %.*s\n", name, static_cast<int>(val.
len), val.
str);
141 printf(
"error: missing value: %s\n", name);
145 while(remaining > 0 && status == 0)
148 if(arg ==
"-h" || arg ==
"--help")
154 else if(arg ==
"-e" || arg ==
"--events-size")
158 else if(arg ==
"-a" || arg ==
"--arena-size")
162 else if(arg ==
"-n" || arg ==
"--no-retry")
167 else if(arg ==
"-q" || arg ==
"--quiet")
180 else if(remaining > 1)
183 printf(
"error: too many arguments\n");
194 int main(
int argc,
const char *argv[])
203 bool is_demo = args.
filename ==
nullptr;
206 char demo_yaml[] =
""
207 "doe: a deer, a female deer\n"
208 "ray: a drop of golden sun\n"
209 "me : a name I call myself\n"
210 "far: a long long way to run\n"
215 const int demo_expected[] = {
253 const char *filename = args.
filename;
254 std::vector<char> yaml_file;
265 if(filename_ ==
"-" || filename_ ==
"stdin")
315 bool ok = (result.
events[pos] == demo_expected[pos]);
321 printf(
"pos=%zu\tevent[%zu]:\t%.*s = 0x%08x", pos, evt, static_cast<int>(len), flags, result.
events[pos]);
325 int offset = result.
events[pos + 1];
326 int length = result.
events[pos + 2];
329 const char *ptr = in_arena ? result.
arena.data() : yaml.
str;
330 const char *
str = ptr + offset;
332 printf(
"\tstr=(%d,%d)\t'%.*s'", offset, length, length, str);
333 ok = ok && (offset == demo_expected[pos + 1]);
334 ok = ok && (length == demo_expected[pos + 2]);
340 printf(
" ... fail!");
347 return success ? 0 : 1;
void reset(substr str, substr arena, ievt::evt_bits *dst, evt_size dst_size)
std::vector< evt_bits > events
result buffer with events
bool parse_in_place(const char *filename, c4::substr yaml, IntsResult *result)
int main(int argc, const char *argv[])
c4::yml::extra::EventHandlerInts handler
size_t required_size_arena() const
get the size needed for the arena from the previous parse
C * str
a restricted pointer to the first character of the substring
void resize_buffers(c4::yml::extra::EventHandlerInts const &handler)
A parser event handler that creates a compact representation of the YAML tree in a contiguous buffer ...
void reset_yaml(c4::csubstr yaml_)
substr to_substr(char(&s)[N]) noexcept
bool fits_buffers() const
Predicate to test if the event and arena buffers successfully accomodated all the parse events...
void parse_in_place_ev(csubstr filename, substr src)
parse YAML in place, emitting events to the current handler
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...
evt_size required_size_events() const
get the size needed for the event buffer from the previous parse
iterator begin() noexcept
evt_size estimate_events_ints_size(csubstr src)
Read YAML source and, without undergoing a full parse, estimate the size of the integer buffer requir...
void events_ints_print(csubstr parsed_yaml, csubstr arena, ievt::evt_bits const *evts, ievt::evt_bits evts_sz)
Print integer events to stdout.
size_t size() const noexcept
std::vector< char > arena
arena to place out-of-source filtered scalars or tags
csubstr to_csubstr(const char(&s)[N]) noexcept
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.
void resize_buffers(c4::yml::extra::evt_size esz, size_t arena_size)
bool atoi(csubstr str, T *v) noexcept
Convert a trimmed string to a signed integral value.
size_t len
the length of the substring
int32_t evt_size
data type for integer events size.
bool parse_or_resize_and_then_parse(const char *filename, c4::csubstr yaml, IntsResult *result)
void print_usage(c4::csubstr basename)
std::vector< char > yaml
source buffer
void parse_in_place(Parser *parser, csubstr filename, substr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node.
bool parse_args(int argc, const char **argv, Args *args, int &status)
c4::yml::ParseEngine< c4::yml::extra::EventHandlerInts > parser