| 1 |
|
| #include "kdl/emitter.h" |
| 2 |
|
| |
| 3 |
|
| #include "grammar.h" |
| 4 |
|
| #include "str.h" |
| 5 |
|
| #include "utf8.h" |
| 6 |
|
| |
| 7 |
|
| #include <math.h> |
| 8 |
|
| #include <stdio.h> |
| 9 |
|
| |
| 10 |
|
| #define INITIAL_BUFFER_SIZE 4096 |
| 11 |
|
| |
| 12 |
|
| const kdl_emitter_options KDL_DEFAULT_EMITTER_OPTIONS = { |
| 13 |
|
| .indent = 4, |
| 14 |
|
| .escape_mode = KDL_ESCAPE_DEFAULT, |
| 15 |
|
| .identifier_mode = KDL_PREFER_BARE_IDENTIFIERS, |
| 16 |
|
| .float_mode = {.always_write_decimal_point = false, |
| 17 |
|
| .always_write_decimal_point_or_exponent = true, |
| 18 |
|
| .capital_e = false, |
| 19 |
|
| .exponent_plus = false, |
| 20 |
|
| .plus = false, |
| 21 |
|
| .min_exponent = 4}, |
| 22 |
|
| .version = KDL_VERSION_2 |
| 23 |
|
| }; |
| 24 |
|
| |
| 25 |
|
| struct _kdl_emitter { |
| 26 |
|
| kdl_emitter_options opt; |
| 27 |
|
| kdl_write_func write_func; |
| 28 |
|
| void* write_user_data; |
| 29 |
|
| int depth; |
| 30 |
|
| bool start_of_line; |
| 31 |
|
| _kdl_write_buffer buf; |
| 32 |
|
| }; |
| 33 |
|
| |
| 34 |
22038 |
| static size_t _buffer_write_func(void* user_data, char const* data, size_t nbytes) |
| 35 |
|
| { |
| 36 |
22038 |
| _kdl_write_buffer* buf = (_kdl_write_buffer*)user_data; |
| 37 |
22038 |
▶ | if (_kdl_buf_push_chars(buf, data, nbytes)) return nbytes; |
Branch: 1 branch
| Branch | True | False | Total |
| if (_kdl_buf_push_chars(buf, data, nbytes)) return nbytes; |
22038 |
0 |
22038 |
|
| 38 |
0 |
| else return 0; |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
184 |
| kdl_emitter* kdl_create_buffering_emitter(kdl_emitter_options const* opt) |
| 42 |
|
| { |
| 43 |
184 |
| kdl_emitter* self = malloc(sizeof(kdl_emitter)); |
| 44 |
184 |
▶ | if (self == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self == NULL) return NULL; |
0 |
184 |
184 |
|
| 45 |
184 |
| self->opt = *opt; |
| 46 |
184 |
| self->write_func = &_buffer_write_func; |
| 47 |
184 |
| self->write_user_data = &self->buf; |
| 48 |
184 |
| self->depth = 0; |
| 49 |
184 |
| self->start_of_line = true; |
| 50 |
184 |
| self->buf = _kdl_new_write_buffer(INITIAL_BUFFER_SIZE); |
| 51 |
184 |
▶ | if (self->buf.buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->buf.buf == NULL) { |
0 |
184 |
184 |
|
| 52 |
0 |
| free(self); |
| 53 |
0 |
| return NULL; |
| 54 |
|
| } |
| 55 |
184 |
| return self; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
0 |
| kdl_emitter* kdl_create_stream_emitter( |
| 59 |
|
| kdl_write_func write_func, void* user_data, kdl_emitter_options const* opt) |
| 60 |
|
| { |
| 61 |
0 |
| kdl_emitter* self = malloc(sizeof(kdl_emitter)); |
| 62 |
0 |
▶ | if (self == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self == NULL) return NULL; |
0 |
0 |
0 |
|
| 63 |
0 |
| self->opt = *opt; |
| 64 |
0 |
| self->write_func = write_func; |
| 65 |
0 |
| self->write_user_data = user_data; |
| 66 |
0 |
| self->depth = 0; |
| 67 |
0 |
| self->start_of_line = true; |
| 68 |
0 |
| self->buf = (_kdl_write_buffer){NULL, 0, 0}; |
| 69 |
0 |
| return self; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
184 |
| void kdl_destroy_emitter(kdl_emitter* self) |
| 73 |
|
| { |
| 74 |
184 |
| (void)kdl_emit_end(self); |
| 75 |
184 |
▶ | if (self->buf.buf != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->buf.buf != NULL) { |
184 |
0 |
184 |
|
| 76 |
184 |
| _kdl_free_write_buffer(&self->buf); |
| 77 |
|
| } |
| 78 |
184 |
| free(self); |
| 79 |
184 |
| } |
| 80 |
|
| |
| 81 |
237 |
| static bool _emit_quoted_str(kdl_emitter* self, kdl_str s) |
| 82 |
|
| { |
| 83 |
237 |
| kdl_owned_string escaped = kdl_escape_v(self->opt.version, &s, self->opt.escape_mode); |
| 84 |
237 |
| bool ok = self->write_func(self->write_user_data, "\"", 1) == 1 |
| 85 |
237 |
▶ | && self->write_func(self->write_user_data, escaped.data, escaped.len) == escaped.len |
Branch: 1 branch
| Branch | True | False | Total |
| && self->write_func(self->write_user_data, escaped.data, escaped.len) == escaped.len |
237 |
0 |
237 |
|
| 86 |
474 |
▶ | && self->write_func(self->write_user_data, "\"", 1) == 1; |
Branch: 2 branches
| Branch | True | False | Total |
| && self->write_func(self->write_user_data, "\"", 1) == 1; |
237 |
0 |
237 |
| && self->write_func(self->write_user_data, "\"", 1) == 1; |
237 |
0 |
237 |
|
| 87 |
237 |
| kdl_free_string(&escaped); |
| 88 |
237 |
| return ok; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
0 |
| static kdl_owned_string _float_to_string(double f, kdl_float_printing_options const* opts) |
| 92 |
|
| { |
| 93 |
|
| // emit #nan, #inf, #-inf even in KDLv1 because there is no alternative |
| 94 |
0 |
▶ | if (isnan(f)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isnan(f)) { |
0 |
0 |
0 |
|
| 95 |
0 |
| kdl_str result = kdl_str_from_cstr("#nan"); |
| 96 |
0 |
| return kdl_clone_str(&result); |
| 97 |
0 |
▶ | } else if (isinf(f)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (isinf(f)) { |
0 |
0 |
0 |
|
| 98 |
0 |
▶ | kdl_str result = f < 0.0 ? kdl_str_from_cstr("#-inf") : kdl_str_from_cstr("#inf"); |
Branch: 1 branch
| Branch | True | False | Total |
| kdl_str result = f < 0.0 ? kdl_str_from_cstr("#-inf") : kdl_str_from_cstr("#inf"); |
0 |
0 |
0 |
|
| 99 |
0 |
| return kdl_clone_str(&result); |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
0 |
| bool negative = f < 0.0; |
| 103 |
0 |
| f = fabs(f); |
| 104 |
0 |
▶ | int exponent = f != 0.0 ? (int)floor(log10(f)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int exponent = f != 0.0 ? (int)floor(log10(f)) : 0; |
0 |
0 |
0 |
|
| 105 |
0 |
| double exp_factor = 1.0; |
| 106 |
0 |
▶ | if (abs(exponent) < opts->min_exponent) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs(exponent) < opts->min_exponent) { |
0 |
0 |
0 |
|
| 107 |
|
| // don't use scientific notation |
| 108 |
0 |
| exponent = 0; |
| 109 |
|
| } else { |
| 110 |
|
| // do use scientific notation |
| 111 |
0 |
| exp_factor = pow(10.0, exponent); |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
0 |
| int integer_part = (int)floor(f / exp_factor); |
| 115 |
|
| |
| 116 |
|
| // f is now the positive fractional part as displayed |
| 117 |
0 |
| _kdl_write_buffer buf = _kdl_new_write_buffer(32); |
| 118 |
0 |
▶ | if (negative) _kdl_buf_push_char(&buf, '-'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (negative) _kdl_buf_push_char(&buf, '-'); |
0 |
0 |
0 |
|
| 119 |
0 |
▶ | else if (opts->plus) _kdl_buf_push_char(&buf, '+'); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (opts->plus) _kdl_buf_push_char(&buf, '+'); |
0 |
0 |
0 |
|
| 120 |
|
| |
| 121 |
|
| // Write the integer part |
| 122 |
0 |
| buf.str_len += snprintf(buf.buf + buf.str_len, buf.buf_len - buf.str_len, "%d", integer_part); |
| 123 |
|
| // Write the decimal part if required |
| 124 |
0 |
| double f_intpart = integer_part * exp_factor; |
| 125 |
0 |
| bool written_point = false; |
| 126 |
0 |
| int zeros = 0; |
| 127 |
0 |
| int nines = 0; |
| 128 |
0 |
| int queued_digit = -1; |
| 129 |
0 |
| unsigned long long fractional_part_so_far = 0; |
| 130 |
0 |
| double pos = 0.1 * exp_factor; |
| 131 |
|
| |
| 132 |
0 |
| double f_so_far = f_intpart; |
| 133 |
|
| |
| 134 |
0 |
▶ | while (f + pos != f && f_so_far < f) { // while this digit makes a difference |
Branch: 2 branches
| Branch | True | False | Total |
| while (f + pos != f && f_so_far < f) { // while this digit makes a difference |
0 |
0 |
0 |
| while (f + pos != f && f_so_far < f) { // while this digit makes a difference |
0 |
0 |
0 |
|
| 135 |
0 |
| double remainder = f - f_so_far; |
| 136 |
|
| |
| 137 |
0 |
| int next_digit = (int)floor(remainder / pos); |
| 138 |
0 |
| fractional_part_so_far = 10 * fractional_part_so_far + next_digit; |
| 139 |
|
| |
| 140 |
0 |
▶ | while (f_intpart + (fractional_part_so_far + 1) * pos <= f) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (f_intpart + (fractional_part_so_far + 1) * pos <= f) { |
0 |
0 |
0 |
|
| 141 |
0 |
| ++next_digit; |
| 142 |
0 |
| ++fractional_part_so_far; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
0 |
| f_so_far = f_intpart + fractional_part_so_far * pos; |
| 146 |
|
| |
| 147 |
0 |
▶ | if (next_digit == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_digit == 0) { |
0 |
0 |
0 |
|
| 148 |
0 |
| ++zeros; |
| 149 |
0 |
▶ | } else if (next_digit == 9) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (next_digit == 9) { |
0 |
0 |
0 |
|
| 150 |
0 |
| ++nines; |
| 151 |
0 |
▶ | } else if (next_digit >= 10) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (next_digit >= 10) { |
0 |
0 |
0 |
|
| 152 |
0 |
| fprintf(stderr, "- ckdl WARNING - _float_to_string calculated digit > 9\n"); |
| 153 |
0 |
| int overflow = next_digit - 9; |
| 154 |
0 |
| next_digit -= overflow; |
| 155 |
0 |
| fractional_part_so_far -= overflow; |
| 156 |
|
| } else { |
| 157 |
|
| // write the queued digit |
| 158 |
0 |
▶ | if (queued_digit >= 0 || zeros != 0 || nines != 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (queued_digit >= 0 || zeros != 0 || nines != 0) { |
0 |
0 |
0 |
| if (queued_digit >= 0 || zeros != 0 || nines != 0) { |
0 |
0 |
0 |
| if (queued_digit >= 0 || zeros != 0 || nines != 0) { |
0 |
0 |
0 |
|
| 159 |
0 |
▶ | if (!written_point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!written_point) { |
0 |
0 |
0 |
|
| 160 |
0 |
| _kdl_buf_push_char(&buf, '.'); |
| 161 |
0 |
| written_point = true; |
| 162 |
|
| } |
| 163 |
0 |
▶ | if (queued_digit >= 0) _kdl_buf_push_char(&buf, (char)('0' + queued_digit)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (queued_digit >= 0) _kdl_buf_push_char(&buf, (char)('0' + queued_digit)); |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | while (zeros != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (zeros != 0) { |
0 |
0 |
0 |
|
| 165 |
0 |
| _kdl_buf_push_char(&buf, '0'); |
| 166 |
0 |
| --zeros; |
| 167 |
|
| } |
| 168 |
0 |
▶ | while (nines != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (nines != 0) { |
0 |
0 |
0 |
|
| 169 |
0 |
| _kdl_buf_push_char(&buf, '9'); |
| 170 |
0 |
| --nines; |
| 171 |
|
| } |
| 172 |
|
| } |
| 173 |
|
| // queue this one |
| 174 |
0 |
| queued_digit = next_digit; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
0 |
| pos /= 10.0; |
| 178 |
|
| } |
| 179 |
|
| // Write the queued digit (if any) |
| 180 |
0 |
▶ | if (queued_digit != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (queued_digit != -1) { |
0 |
0 |
0 |
|
| 181 |
0 |
▶ | if (!written_point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!written_point) { |
0 |
0 |
0 |
|
| 182 |
0 |
| _kdl_buf_push_char(&buf, '.'); |
| 183 |
0 |
| written_point = true; |
| 184 |
|
| } |
| 185 |
0 |
▶ | if (nines != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nines != 0) { |
0 |
0 |
0 |
|
| 186 |
0 |
| ++queued_digit; |
| 187 |
|
| } |
| 188 |
0 |
| _kdl_buf_push_char(&buf, (char)('0' + queued_digit)); |
| 189 |
|
| } |
| 190 |
|
| // Adding more decimal digits now makes no difference to the number. |
| 191 |
|
| |
| 192 |
|
| // Add ".0" IFF requested |
| 193 |
0 |
▶ | if (!written_point && opts->always_write_decimal_point) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!written_point && opts->always_write_decimal_point) { |
0 |
0 |
0 |
| if (!written_point && opts->always_write_decimal_point) { |
0 |
0 |
0 |
|
| 194 |
0 |
| _kdl_buf_push_chars(&buf, ".0", 2); |
| 195 |
0 |
| written_point = true; |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
|
| // Add exponent (if any) |
| 199 |
0 |
▶ | if (exponent != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (exponent != 0) { |
0 |
0 |
0 |
|
| 200 |
|
| char exp_buf[32]; |
| 201 |
0 |
| int exp_len = snprintf(exp_buf, 32, "%d", exponent); |
| 202 |
0 |
▶ | _kdl_buf_push_char(&buf, opts->capital_e ? 'E' : 'e'); |
Branch: 1 branch
| Branch | True | False | Total |
| _kdl_buf_push_char(&buf, opts->capital_e ? 'E' : 'e'); |
0 |
0 |
0 |
|
| 203 |
0 |
▶ | if (exponent >= 0 && opts->exponent_plus) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (exponent >= 0 && opts->exponent_plus) { |
0 |
0 |
0 |
| if (exponent >= 0 && opts->exponent_plus) { |
0 |
0 |
0 |
|
| 204 |
0 |
| _kdl_buf_push_char(&buf, '+'); |
| 205 |
|
| } |
| 206 |
0 |
| _kdl_buf_push_chars(&buf, exp_buf, exp_len); |
| 207 |
0 |
▶ | } else if (!written_point && opts->always_write_decimal_point_or_exponent) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!written_point && opts->always_write_decimal_point_or_exponent) { |
0 |
0 |
0 |
| } else if (!written_point && opts->always_write_decimal_point_or_exponent) { |
0 |
0 |
0 |
|
| 208 |
|
| // Always write either an exponent or a decimal point, to mark this |
| 209 |
|
| // number as float |
| 210 |
0 |
| _kdl_buf_push_chars(&buf, ".0", 2); |
| 211 |
|
| } |
| 212 |
0 |
| return _kdl_buf_to_string(&buf); |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
10057 |
| static bool _emit_number(kdl_emitter* self, kdl_number const* n) |
| 216 |
|
| { |
| 217 |
|
| char int_buf[32]; |
| 218 |
10057 |
| int int_len = 0; |
| 219 |
|
| kdl_owned_string float_str; |
| 220 |
|
| bool ok; |
| 221 |
|
| |
| 222 |
10057 |
| switch (n->type) { |
| 223 |
0 |
| case KDL_NUMBER_TYPE_INTEGER: |
| 224 |
0 |
| int_len = snprintf(int_buf, 32, "%lld", n->integer); |
| 225 |
0 |
| return (int)self->write_func(self->write_user_data, int_buf, int_len) == int_len; |
| 226 |
0 |
| case KDL_NUMBER_TYPE_FLOATING_POINT: |
| 227 |
0 |
| float_str = _float_to_string(n->floating_point, &self->opt.float_mode); |
| 228 |
0 |
| ok = self->write_func(self->write_user_data, float_str.data, float_str.len) == float_str.len; |
| 229 |
0 |
| kdl_free_string(&float_str); |
| 230 |
0 |
| return ok; |
| 231 |
10057 |
| case KDL_NUMBER_TYPE_STRING_ENCODED: |
| 232 |
10057 |
| return self->write_func(self->write_user_data, n->string.data, n->string.len) == n->string.len; |
| 233 |
|
| } |
| 234 |
0 |
| return false; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
563 |
| static bool _emit_bare_string(kdl_emitter* self, kdl_str s) |
| 238 |
|
| { |
| 239 |
563 |
| bool bare = true; |
| 240 |
563 |
▶ | if (self->opt.identifier_mode == KDL_QUOTE_ALL_IDENTIFIERS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt.identifier_mode == KDL_QUOTE_ALL_IDENTIFIERS) { |
179 |
384 |
563 |
|
| 241 |
179 |
| bare = false; |
| 242 |
384 |
▶ | } else if (s.len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (s.len == 0) { |
1 |
383 |
384 |
|
| 243 |
1 |
| bare = false; |
| 244 |
|
| } else { |
| 245 |
|
| uint32_t c; |
| 246 |
383 |
| kdl_str tail = s; |
| 247 |
383 |
| bool first = true; |
| 248 |
383 |
| kdl_character_set charset |
| 249 |
383 |
▶ | = self->opt.version == KDL_VERSION_1 ? KDL_CHARACTER_SET_V1 : KDL_CHARACTER_SET_V2; |
Branch: 1 branch
| Branch | True | False | Total |
| = self->opt.version == KDL_VERSION_1 ? KDL_CHARACTER_SET_V1 : KDL_CHARACTER_SET_V2; |
52 |
331 |
383 |
|
| 250 |
766 |
| bool require_ascii = self->opt.identifier_mode == KDL_ASCII_IDENTIFIERS |
| 251 |
383 |
▶ | || (self->opt.escape_mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE; |
Branch: 2 branches
| Branch | True | False | Total |
| || (self->opt.escape_mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE; |
381 |
2 |
383 |
| || (self->opt.escape_mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE; |
2 |
379 |
381 |
|
| 252 |
1407 |
▶ | while (KDL_UTF8_OK == _kdl_pop_codepoint(&tail, &c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (KDL_UTF8_OK == _kdl_pop_codepoint(&tail, &c)) { |
1037 |
370 |
1407 |
|
| 253 |
1037 |
▶ | if ((first && !_kdl_is_id_start(charset, c)) || !_kdl_is_id(charset, c) |
Branch: 3 branches
| Branch | True | False | Total |
| if ((first && !_kdl_is_id_start(charset, c)) || !_kdl_is_id(charset, c) |
383 |
654 |
1037 |
| if ((first && !_kdl_is_id_start(charset, c)) || !_kdl_is_id(charset, c) |
374 |
9 |
383 |
| if ((first && !_kdl_is_id_start(charset, c)) || !_kdl_is_id(charset, c) |
1026 |
2 |
1028 |
|
| 254 |
1026 |
▶ | || (require_ascii && c >= 0x7f)) { |
Branch: 2 branches
| Branch | True | False | Total |
| || (require_ascii && c >= 0x7f)) { |
10 |
1016 |
1026 |
| || (require_ascii && c >= 0x7f)) { |
2 |
8 |
10 |
|
| 255 |
13 |
| bare = false; |
| 256 |
13 |
| break; |
| 257 |
|
| } |
| 258 |
1024 |
| first = false; |
| 259 |
|
| } |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
563 |
▶ | if (bare) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bare) { |
370 |
193 |
563 |
|
| 263 |
370 |
| return self->write_func(self->write_user_data, s.data, s.len) == s.len; |
| 264 |
|
| } else { |
| 265 |
193 |
| return _emit_quoted_str(self, s); |
| 266 |
|
| } |
| 267 |
|
| } |
| 268 |
|
| |
| 269 |
193 |
| static bool _emit_string(kdl_emitter* self, kdl_str s) |
| 270 |
|
| { |
| 271 |
193 |
▶ | if (self->opt.version == KDL_VERSION_1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt.version == KDL_VERSION_1) { |
44 |
149 |
193 |
|
| 272 |
44 |
| return _emit_quoted_str(self, s); |
| 273 |
|
| } else { |
| 274 |
149 |
| return _emit_bare_string(self, s); |
| 275 |
|
| } |
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
|
| #define _write_string_literal_ok(self, s) \ |
| 279 |
|
| (self->write_func(self->write_user_data, ("" s ""), sizeof(s) - 1) == sizeof(s) - 1) |
| 280 |
|
| |
| 281 |
10253 |
| static bool _emit_value(kdl_emitter* self, kdl_value const* v) |
| 282 |
|
| { |
| 283 |
10253 |
▶ | if (v->type_annotation.data != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->type_annotation.data != NULL) { |
64 |
10189 |
10253 |
|
| 284 |
128 |
| if (!(_write_string_literal_ok(self, "(") // |
| 285 |
64 |
▶ | && _emit_bare_string(self, v->type_annotation) // |
Branch: 1 branch
| Branch | True | False | Total |
| && _emit_bare_string(self, v->type_annotation) // |
64 |
0 |
64 |
|
| 286 |
64 |
| && _write_string_literal_ok(self, ")"))) |
| 287 |
0 |
| return false; |
| 288 |
|
| } |
| 289 |
10253 |
| switch (v->type) { |
| 290 |
1 |
| case KDL_TYPE_NULL: |
| 291 |
1 |
▶ | if (self->opt.version == KDL_VERSION_1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt.version == KDL_VERSION_1) { |
0 |
1 |
1 |
|
| 292 |
0 |
| return _write_string_literal_ok(self, "null"); |
| 293 |
|
| } else { |
| 294 |
1 |
| return _write_string_literal_ok(self, "#null"); |
| 295 |
|
| } |
| 296 |
193 |
| case KDL_TYPE_STRING: |
| 297 |
193 |
| return _emit_string(self, v->string); |
| 298 |
10057 |
| case KDL_TYPE_NUMBER: |
| 299 |
10057 |
| return _emit_number(self, &v->number); |
| 300 |
2 |
| case KDL_TYPE_BOOLEAN: |
| 301 |
2 |
▶ | if (self->opt.version == KDL_VERSION_1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->opt.version == KDL_VERSION_1) { |
0 |
2 |
2 |
|
| 302 |
0 |
▶ | if (v->boolean) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->boolean) { |
0 |
0 |
0 |
|
| 303 |
0 |
| return _write_string_literal_ok(self, "true"); |
| 304 |
|
| } else { |
| 305 |
0 |
| return _write_string_literal_ok(self, "false"); |
| 306 |
|
| } |
| 307 |
|
| } else { |
| 308 |
2 |
▶ | if (v->boolean) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->boolean) { |
1 |
1 |
2 |
|
| 309 |
1 |
| return _write_string_literal_ok(self, "#true"); |
| 310 |
|
| } else { |
| 311 |
1 |
| return _write_string_literal_ok(self, "#false"); |
| 312 |
|
| } |
| 313 |
|
| } |
| 314 |
|
| } |
| 315 |
0 |
| return false; |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
237 |
| static bool _emit_node_preamble(kdl_emitter* self) |
| 319 |
|
| { |
| 320 |
237 |
▶ | if (!self->start_of_line) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->start_of_line) { |
38 |
199 |
237 |
|
| 321 |
38 |
▶ | if (!_write_string_literal_ok(self, "\n")) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_write_string_literal_ok(self, "\n")) return false; |
0 |
38 |
38 |
|
| 322 |
|
| } |
| 323 |
237 |
| int indent = 0; |
| 324 |
260 |
▶ | for (int d = 0; d < self->depth; ++d) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int d = 0; d < self->depth; ++d) { |
23 |
237 |
260 |
|
| 325 |
23 |
| indent += self->opt.indent; |
| 326 |
|
| } |
| 327 |
323 |
▶ | for (int i = 0; i < indent; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < indent; ++i) { |
86 |
237 |
323 |
|
| 328 |
86 |
▶ | if (!_write_string_literal_ok(self, " ")) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_write_string_literal_ok(self, " ")) return false; |
0 |
86 |
86 |
|
| 329 |
|
| } |
| 330 |
237 |
| self->start_of_line = false; |
| 331 |
237 |
| return true; |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
157 |
| bool kdl_emit_node(kdl_emitter* self, kdl_str name) |
| 335 |
|
| { |
| 336 |
157 |
▶ | return _emit_node_preamble(self) && _emit_bare_string(self, name); |
Branch: 2 branches
| Branch | True | False | Total |
| return _emit_node_preamble(self) && _emit_bare_string(self, name); |
157 |
0 |
157 |
| return _emit_node_preamble(self) && _emit_bare_string(self, name); |
157 |
0 |
157 |
|
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
64 |
| bool kdl_emit_node_with_type(kdl_emitter* self, kdl_str type, kdl_str name) |
| 340 |
|
| { |
| 341 |
64 |
| return _emit_node_preamble(self) // |
| 342 |
64 |
▶ | && _write_string_literal_ok(self, "(") // |
Branch: 1 branch
| Branch | True | False | Total |
| && _write_string_literal_ok(self, "(") // |
64 |
0 |
64 |
|
| 343 |
64 |
▶ | && _emit_bare_string(self, type) // |
Branch: 1 branch
| Branch | True | False | Total |
| && _emit_bare_string(self, type) // |
64 |
0 |
64 |
|
| 344 |
64 |
▶ | && _write_string_literal_ok(self, ")") // |
Branch: 1 branch
| Branch | True | False | Total |
| && _write_string_literal_ok(self, ")") // |
64 |
0 |
64 |
|
| 345 |
128 |
▶ | && _emit_bare_string(self, name); |
Branch: 2 branches
| Branch | True | False | Total |
| && _emit_bare_string(self, name); |
64 |
0 |
64 |
| && _emit_bare_string(self, name); |
64 |
0 |
64 |
|
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
10188 |
| bool kdl_emit_arg(kdl_emitter* self, kdl_value const* value) |
| 349 |
|
| { |
| 350 |
10188 |
▶ | return _write_string_literal_ok(self, " ") && _emit_value(self, value); |
Branch: 2 branches
| Branch | True | False | Total |
| return _write_string_literal_ok(self, " ") && _emit_value(self, value); |
10188 |
0 |
10188 |
| return _write_string_literal_ok(self, " ") && _emit_value(self, value); |
10188 |
0 |
10188 |
|
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
65 |
| bool kdl_emit_property(kdl_emitter* self, kdl_str name, kdl_value const* value) |
| 354 |
|
| { |
| 355 |
65 |
| return (_write_string_literal_ok(self, " ")) // |
| 356 |
65 |
▶ | && _emit_bare_string(self, name) // |
Branch: 1 branch
| Branch | True | False | Total |
| && _emit_bare_string(self, name) // |
65 |
0 |
65 |
|
| 357 |
65 |
▶ | && _write_string_literal_ok(self, "=") // |
Branch: 1 branch
| Branch | True | False | Total |
| && _write_string_literal_ok(self, "=") // |
65 |
0 |
65 |
|
| 358 |
130 |
▶ | && _emit_value(self, value); |
Branch: 2 branches
| Branch | True | False | Total |
| && _emit_value(self, value); |
65 |
0 |
65 |
| && _emit_value(self, value); |
65 |
0 |
65 |
|
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
16 |
| bool kdl_start_emitting_children(kdl_emitter* self) |
| 362 |
|
| { |
| 363 |
16 |
| self->start_of_line = true; |
| 364 |
16 |
| ++self->depth; |
| 365 |
16 |
| return (_write_string_literal_ok(self, " {\n")); |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
16 |
| bool kdl_finish_emitting_children(kdl_emitter* self) |
| 369 |
|
| { |
| 370 |
16 |
▶ | if (self->depth == 0) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->depth == 0) return false; |
0 |
16 |
16 |
|
| 371 |
16 |
| --self->depth; |
| 372 |
16 |
▶ | if (!_emit_node_preamble(self)) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_emit_node_preamble(self)) return false; |
0 |
16 |
16 |
|
| 373 |
16 |
| self->start_of_line = true; |
| 374 |
16 |
| return (_write_string_literal_ok(self, "}\n")); |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
338 |
| bool kdl_emit_end(kdl_emitter* self) |
| 378 |
|
| { |
| 379 |
339 |
▶ | while (self->depth != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (self->depth != 0) { |
1 |
338 |
339 |
|
| 380 |
1 |
▶ | if (!kdl_finish_emitting_children(self)) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!kdl_finish_emitting_children(self)) return false; |
0 |
1 |
1 |
|
| 381 |
|
| } |
| 382 |
338 |
▶ | if (!self->start_of_line) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->start_of_line) { |
167 |
171 |
338 |
|
| 383 |
167 |
▶ | if (!_write_string_literal_ok(self, "\n")) return false; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!_write_string_literal_ok(self, "\n")) return false; |
0 |
167 |
167 |
|
| 384 |
167 |
| self->start_of_line = true; |
| 385 |
|
| } |
| 386 |
338 |
| return true; |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
154 |
| kdl_str kdl_get_emitter_buffer(kdl_emitter* self) { return (kdl_str){self->buf.buf, self->buf.str_len}; } |