• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

biojppm / rapidyaml / 18160471768

01 Oct 2025 11:19AM UTC coverage: 97.276% (-0.4%) from 97.65%
18160471768

Pull #503

github

web-flow
Merge 8e72e99dd into 653eac974
Pull Request #503: Improve error model, callbacks

1735 of 1782 new or added lines in 31 files covered. (97.36%)

64 existing lines in 7 files now uncovered.

12677 of 13032 relevant lines covered (97.28%)

187443.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.18
/samples/quickstart-ints.cpp
1
// This file shows a quick example of parsing YAML to an int events
2
// buffer. Since this functionality is meant to implement in other
3
// programming languages, the code is kept very simple, and using only
4
// C-like idioms.
5

6
// ryml can be used as a single header, or as a simple library:
7
#if defined(RYML_SINGLE_HEADER) // using the single header directly in the executable
8
    #define RYML_SINGLE_HDR_DEFINE_NOW
9
    #ifndef RYML_SINGLE_HEADER_INTS
10
        #include <ryml_all.hpp>
11
    #else
12
        #include <ryml_ints.hpp>
13
    #endif
14
#elif defined(RYML_SINGLE_HEADER_LIB) // using the single header from a library
15
    #ifndef RYML_SINGLE_HEADER_INTS
16
        #include <ryml_all.hpp>
17
    #else
18
        #include <ryml_ints.hpp>
19
    #endif
20
#else
21
#include <c4/yml/parse_engine.def.hpp>
22
#endif
23

24
#ifndef RYML_SINGLE_HEADER_INTS
25
#include <c4/yml/extra/event_handler_ints.hpp>
26
#include <c4/yml/extra/ints_utils.hpp> // to print
27
#endif
28

29

30
// NOLINTBEGIN(hicpp-signed-bitwise)
31

32
int main(int, const char *[])
4✔
33
{
34
    using namespace c4::yml::extra::ievt;
35
    auto PSTR_ = c4::yml::extra::ievt::PSTR; // PSTR does not work in windows
4✔
36
    // YAML code to be parsed in place
37
    char yaml[] = "do: a deer, a female deer\n"
4✔
38
                  "re: a drop of golden sun\n"
39
                  "mi: a name I call myself\n"
40
                  "fa: a long long way to run\n";
41
    // these are the event values we expect
42
    const int expected_events[] = {
4✔
43
        BSTR,
44
        BDOC,
45
        VAL_|BMAP|BLCK,
46
        //
47
        KEY_|SCLR|PLAI,        0,  2,  // "do"
48
        VAL_|SCLR|PLAI|PSTR_,  4, 21,  // "a deer, a female deer"
4✔
49
        //
50
        KEY_|SCLR|PLAI|PSTR_, 26,  2,  // "re"
4✔
51
        VAL_|SCLR|PLAI|PSTR_, 30, 20,  // "a drop of golden sun"
4✔
52
        //
53
        KEY_|SCLR|PLAI|PSTR_, 51,  2,  // "mi"
4✔
54
        VAL_|SCLR|PLAI|PSTR_, 55, 20,  // "a name I call myself"
4✔
55
        //
56
        KEY_|SCLR|PLAI|PSTR_, 76,  2,  // "fa"
4✔
57
        VAL_|SCLR|PLAI|PSTR_, 80, 22,  // "a long long way to run"
4✔
58
        //
59
        EMAP|PSTR_,
4✔
60
        EDOC,
61
        ESTR,
62
    };
4✔
63

64
    /* the output should be this:
65
     *
66
     * success! YAML requires event size 30, estimated=49 (required_arena=0 actual=99)
67
     * pos=0        event[0]:                        BSTR = 0x00000001
68
     * pos=1        event[1]:                        BDOC = 0x00000004
69
     * pos=2        event[2]:              VAL_|BMAP|BLCK = 0x00140010
70
     * pos=3        event[3]:              KEY_|SCLR|PLAI = 0x00081100        str=(0,2)        'do'
71
     * pos=6        event[4]:         VAL_|SCLR|PLAI|PSTR = 0x04101100        str=(4,21)        'a deer, a female deer'
72
     * pos=9        event[5]:         KEY_|SCLR|PLAI|PSTR = 0x04081100        str=(26,2)        're'
73
     * pos=12        event[6]:         VAL_|SCLR|PLAI|PSTR = 0x04101100        str=(30,20)        'a drop of golden sun'
74
     * pos=15        event[7]:         KEY_|SCLR|PLAI|PSTR = 0x04081100        str=(51,2)        'mi'
75
     * pos=18        event[8]:         VAL_|SCLR|PLAI|PSTR = 0x04101100        str=(55,20)        'a name I call myself'
76
     * pos=21        event[9]:         KEY_|SCLR|PLAI|PSTR = 0x04081100        str=(76,2)        'fa'
77
     * pos=24        event[10]:         VAL_|SCLR|PLAI|PSTR = 0x04101100        str=(80,22)        'a long long way to run'
78
     * pos=27        event[11]:                   EMAP|PSTR = 0x04000020
79
     * pos=28        event[12]:                        EDOC = 0x00000008
80
     * pos=29        event[13]:                        ESTR = 0x00000002
81
     */
82

83
    // buffer to where we will write the events
84
    constexpr const int events_size = 100;
4✔
85
    int events[events_size] = {};
4✔
86
    static_assert(events_size >= sizeof(expected_events)/sizeof(expected_events[0]), "buffer too small");
87
    // buffer for placing any scalars/tags that cannot be filtered
88
    // in-place
89
    char arena[100] = {};
4✔
90

91

92
    // ensure the estimation will succeed vs required size
93
    int estimated_size = c4::yml::extra::estimate_events_ints_size(yaml);
4✔
94
    if (estimated_size > events_size)
4✔
95
    {
96
        printf("the estimated size (%d) will not fit the events array (%d)\n", estimated_size, events_size); // LCOV_EXCL_LINE
97
        return 1; // LCOV_EXCL_LINE
98
    }
99

100
    // parse now. the parse should succeed (because the YAML above is
101
    // legit), but if there were would be a parse error, we would get
102
    // the default behavior which is abort on error, since we did not
103
    // set up the error callbacks
104
    c4::yml::extra::EventHandlerInts handler;
4✔
105
    c4::yml::ParseEngine<c4::yml::extra::EventHandlerInts> parser(&handler);
4✔
106
    handler.reset(yaml, arena, events, estimated_size); // note we pass the estimated size!
4✔
107
    parser.parse_in_place_ev("filename", yaml);
4✔
108

109
    // the YAML was successfully parsed, but it may happen that it
110
    // requires more events than may fit in the buffers. so we need to
111
    // check that it actually fits (this is mandatory):
112
    if(!handler.fits_buffers())
4✔
113
    {
114
        printf("error: buffers too small:   required_evt=%d actual_evt=%d\n   required_arena=%zu actual_arena=%zu\n",     // LCOV_EXCL_LINE
115
               handler.required_size_events(), estimated_size, handler.required_size_arena(), c4::to_csubstr(arena).len); // LCOV_EXCL_LINE
116
        // WATCHOUT: if you want to retry the parse, you need to set
117
        // up the source buffer again, because it is invalidated from
118
        // being parsed in place. refer to the doxygen documentation
119
        // for more details.
120
        return 1; // LCOV_EXCL_LINE
121
    }
122

123
    // done!
124
    printf("success! YAML requires event size %d, estimated=%d (required_arena=%zu actual_arena=%zu)\n",              // LCOV_EXCL_LINE
125
           handler.required_size_events(), estimated_size, handler.required_size_arena(), c4::to_csubstr(arena).len); // LCOV_EXCL_LINE
126

127
    // ensure the result is as expected
128
    bool compare = true;
4✔
129

130
    // example iterating through the events array: compare and print
131
    // the result
132
    char flags[100];
133
    for (int pos = 0, evt = 0; pos < handler.required_size_events(); ++pos, ++evt)
4✔
134
    {
135
        bool status = (events[pos] == expected_events[pos]);
4✔
136
        // let's format the event flags to print them as string.
137
        // we need to zero-terminate them to be able to align using printf.
138
        memset(flags, 0, sizeof(flags)); // ensure flags are zero-terminated
4✔
139
        size_t len = c4::yml::extra::ievt::to_chars(flags, events[pos]);
4✔
140
        if(len + 1 < sizeof(flags)) { printf("error: could not format flags"); return 1; } // ensure flags are zero-terminated
4✔
141
        // print the event
NEW
142
        printf("pos=%d\tevent[%d]:\t%20s = 0x%08x", pos, evt, flags, events[pos]);
×
UNCOV
143
        if(events[pos] & WSTR) // the event has a string following it
×
144
        {
UNCOV
145
            int offset = events[pos + 1];
×
UNCOV
146
            int length = events[pos + 2];
×
UNCOV
147
            bool in_arena = (events[pos] & AREN);
×
148
            // WATCHOUT! the string is NOT ZERO TERMINATED!
UNCOV
149
            const char *ptr = in_arena ? arena : yaml;
×
UNCOV
150
            const char *str = ptr + offset;
×
UNCOV
151
            printf("\tstr=(%d,%d)\t'%.*s'", offset, length, length, str);
×
UNCOV
152
            status = status && (offset == expected_events[pos + 1]);
×
UNCOV
153
            status = status && (length == expected_events[pos + 2]);
×
UNCOV
154
            pos += 2; // advance the two ints from the string
×
155
        }
UNCOV
156
        if(!status)
×
157
        {
158
            printf("  ... fail!"); // LCOV_EXCL_LINE
159
            compare = false;       // LCOV_EXCL_LINE
160
        }
UNCOV
161
        printf("\n");
×
162
    }
163

NEW
164
    return compare ? 0 : 1;
×
165
}
4✔
166

167
// NOLINTEND(hicpp-signed-bitwise)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc