| 1 |
|
| /* |
| 2 |
|
| |
| 3 |
|
| MIT License |
| 4 |
|
| |
| 5 |
|
| Copyright (c) 2017 - 2021 CK Tan |
| 6 |
|
| https://github.com/cktan/tomlc99 |
| 7 |
|
| |
| 8 |
|
| Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
|
| of this software and associated documentation files (the "Software"), to deal |
| 10 |
|
| in the Software without restriction, including without limitation the rights |
| 11 |
|
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
|
| copies of the Software, and to permit persons to whom the Software is |
| 13 |
|
| furnished to do so, subject to the following conditions: |
| 14 |
|
| |
| 15 |
|
| The above copyright notice and this permission notice shall be included in all |
| 16 |
|
| copies or substantial portions of the Software. |
| 17 |
|
| |
| 18 |
|
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
|
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
|
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
|
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
|
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
|
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
|
| SOFTWARE. |
| 25 |
|
| |
| 26 |
|
| */ |
| 27 |
|
| #define _POSIX_C_SOURCE 200809L |
| 28 |
|
| #include <stdio.h> |
| 29 |
|
| #include <stdlib.h> |
| 30 |
|
| #include <assert.h> |
| 31 |
|
| #include <errno.h> |
| 32 |
|
| #include <stdint.h> |
| 33 |
|
| #include <ctype.h> |
| 34 |
|
| #include <string.h> |
| 35 |
|
| #include <stdbool.h> |
| 36 |
|
| #include "toml.h" |
| 37 |
|
| |
| 38 |
|
| |
| 39 |
|
| static void* (*ppmalloc)(size_t) = malloc; |
| 40 |
|
| static void (*ppfree)(void*) = free; |
| 41 |
|
| |
| 42 |
0 |
| void toml_set_memutil(void* (*xxmalloc)(size_t), |
| 43 |
|
| void (*xxfree)(void*)) |
| 44 |
|
| { |
| 45 |
0 |
▶ | if (xxmalloc) ppmalloc = xxmalloc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (xxmalloc) ppmalloc = xxmalloc; |
0 |
0 |
0 |
|
| 46 |
0 |
▶ | if (xxfree) ppfree = xxfree; |
Branch: 1 branch
| Branch | True | False | Total |
| if (xxfree) ppfree = xxfree; |
0 |
0 |
0 |
|
| 47 |
0 |
| } |
| 48 |
|
| |
| 49 |
|
| |
| 50 |
|
| #define MALLOC(a) ppmalloc(a) |
| 51 |
|
| #define FREE(a) ppfree(a) |
| 52 |
|
| |
| 53 |
24 |
| static void* CALLOC(size_t nmemb, size_t sz) |
| 54 |
|
| { |
| 55 |
24 |
| int nb = sz * nmemb; |
| 56 |
24 |
| void* p = MALLOC(nb); |
| 57 |
24 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
24 |
0 |
24 |
|
| 58 |
24 |
| memset(p, 0, nb); |
| 59 |
|
| } |
| 60 |
24 |
| return p; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| |
| 64 |
0 |
| static char* STRDUP(const char* s) |
| 65 |
|
| { |
| 66 |
0 |
| int len = strlen(s); |
| 67 |
0 |
| char* p = MALLOC(len+1); |
| 68 |
0 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
0 |
0 |
0 |
|
| 69 |
0 |
| memcpy(p, s, len); |
| 70 |
0 |
| p[len] = 0; |
| 71 |
|
| } |
| 72 |
0 |
| return p; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
45 |
| static char* STRNDUP(const char* s, size_t n) |
| 76 |
|
| { |
| 77 |
45 |
| size_t len = strnlen(s, n); |
| 78 |
45 |
| char* p = MALLOC(len+1); |
| 79 |
45 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
45 |
0 |
45 |
|
| 80 |
45 |
| memcpy(p, s, len); |
| 81 |
45 |
| p[len] = 0; |
| 82 |
|
| } |
| 83 |
45 |
| return p; |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
|
| |
| 87 |
|
| |
| 88 |
|
| /** |
| 89 |
|
| * Convert a char in utf8 into UCS, and store it in *ret. |
| 90 |
|
| * Return #bytes consumed or -1 on failure. |
| 91 |
|
| */ |
| 92 |
0 |
| int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret) |
| 93 |
|
| { |
| 94 |
0 |
| const unsigned char* buf = (const unsigned char*) orig; |
| 95 |
0 |
| unsigned i = *buf++; |
| 96 |
|
| int64_t v; |
| 97 |
|
| |
| 98 |
|
| /* 0x00000000 - 0x0000007F: |
| 99 |
|
| 0xxxxxxx |
| 100 |
|
| */ |
| 101 |
0 |
▶ | if (0 == (i >> 7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (i >> 7)) { |
0 |
0 |
0 |
|
| 102 |
0 |
▶ | if (len < 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 1) return -1; |
0 |
0 |
0 |
|
| 103 |
0 |
| v = i; |
| 104 |
0 |
| return *ret = v, 1; |
| 105 |
|
| } |
| 106 |
|
| /* 0x00000080 - 0x000007FF: |
| 107 |
|
| 110xxxxx 10xxxxxx |
| 108 |
|
| */ |
| 109 |
0 |
▶ | if (0x6 == (i >> 5)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x6 == (i >> 5)) { |
0 |
0 |
0 |
|
| 110 |
0 |
▶ | if (len < 2) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 2) return -1; |
0 |
0 |
0 |
|
| 111 |
0 |
| v = i & 0x1f; |
| 112 |
0 |
▶ | for (int j = 0; j < 1; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int j = 0; j < 1; j++) { |
0 |
0 |
0 |
|
| 113 |
0 |
| i = *buf++; |
| 114 |
0 |
▶ | if (0x2 != (i >> 6)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x2 != (i >> 6)) return -1; |
0 |
0 |
0 |
|
| 115 |
0 |
| v = (v << 6) | (i & 0x3f); |
| 116 |
|
| } |
| 117 |
0 |
| return *ret = v, (const char*) buf - orig; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* 0x00000800 - 0x0000FFFF: |
| 121 |
|
| 1110xxxx 10xxxxxx 10xxxxxx |
| 122 |
|
| */ |
| 123 |
0 |
▶ | if (0xE == (i >> 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0xE == (i >> 4)) { |
0 |
0 |
0 |
|
| 124 |
0 |
▶ | if (len < 3) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 3) return -1; |
0 |
0 |
0 |
|
| 125 |
0 |
| v = i & 0x0F; |
| 126 |
0 |
▶ | for (int j = 0; j < 2; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int j = 0; j < 2; j++) { |
0 |
0 |
0 |
|
| 127 |
0 |
| i = *buf++; |
| 128 |
0 |
▶ | if (0x2 != (i >> 6)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x2 != (i >> 6)) return -1; |
0 |
0 |
0 |
|
| 129 |
0 |
| v = (v << 6) | (i & 0x3f); |
| 130 |
|
| } |
| 131 |
0 |
| return *ret = v, (const char*) buf - orig; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* 0x00010000 - 0x001FFFFF: |
| 135 |
|
| 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 136 |
|
| */ |
| 137 |
0 |
▶ | if (0x1E == (i >> 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x1E == (i >> 3)) { |
0 |
0 |
0 |
|
| 138 |
0 |
▶ | if (len < 4) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 4) return -1; |
0 |
0 |
0 |
|
| 139 |
0 |
| v = i & 0x07; |
| 140 |
0 |
▶ | for (int j = 0; j < 3; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int j = 0; j < 3; j++) { |
0 |
0 |
0 |
|
| 141 |
0 |
| i = *buf++; |
| 142 |
0 |
▶ | if (0x2 != (i >> 6)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x2 != (i >> 6)) return -1; |
0 |
0 |
0 |
|
| 143 |
0 |
| v = (v << 6) | (i & 0x3f); |
| 144 |
|
| } |
| 145 |
0 |
| return *ret = v, (const char*) buf - orig; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /* 0x00200000 - 0x03FFFFFF: |
| 149 |
|
| 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 150 |
|
| */ |
| 151 |
0 |
▶ | if (0x3E == (i >> 2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x3E == (i >> 2)) { |
0 |
0 |
0 |
|
| 152 |
0 |
▶ | if (len < 5) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 5) return -1; |
0 |
0 |
0 |
|
| 153 |
0 |
| v = i & 0x03; |
| 154 |
0 |
▶ | for (int j = 0; j < 4; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int j = 0; j < 4; j++) { |
0 |
0 |
0 |
|
| 155 |
0 |
| i = *buf++; |
| 156 |
0 |
▶ | if (0x2 != (i >> 6)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x2 != (i >> 6)) return -1; |
0 |
0 |
0 |
|
| 157 |
0 |
| v = (v << 6) | (i & 0x3f); |
| 158 |
|
| } |
| 159 |
0 |
| return *ret = v, (const char*) buf - orig; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| /* 0x04000000 - 0x7FFFFFFF: |
| 163 |
|
| 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 164 |
|
| */ |
| 165 |
0 |
▶ | if (0x7e == (i >> 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x7e == (i >> 1)) { |
0 |
0 |
0 |
|
| 166 |
0 |
▶ | if (len < 6) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 6) return -1; |
0 |
0 |
0 |
|
| 167 |
0 |
| v = i & 0x01; |
| 168 |
0 |
▶ | for (int j = 0; j < 5; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int j = 0; j < 5; j++) { |
0 |
0 |
0 |
|
| 169 |
0 |
| i = *buf++; |
| 170 |
0 |
▶ | if (0x2 != (i >> 6)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0x2 != (i >> 6)) return -1; |
0 |
0 |
0 |
|
| 171 |
0 |
| v = (v << 6) | (i & 0x3f); |
| 172 |
|
| } |
| 173 |
0 |
| return *ret = v, (const char*) buf - orig; |
| 174 |
|
| } |
| 175 |
0 |
| return -1; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| |
| 179 |
|
| /** |
| 180 |
|
| * Convert a UCS char to utf8 code, and return it in buf. |
| 181 |
|
| * Return #bytes used in buf to encode the char, or |
| 182 |
|
| * -1 on error. |
| 183 |
|
| */ |
| 184 |
0 |
| int toml_ucs_to_utf8(int64_t code, char buf[6]) |
| 185 |
|
| { |
| 186 |
|
| /* http://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16 */ |
| 187 |
|
| /* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well |
| 188 |
|
| * as 0xfffe and 0xffff (UCS noncharacters) should not appear in |
| 189 |
|
| * conforming UTF-8 streams. |
| 190 |
|
| */ |
| 191 |
0 |
▶ | if (0xd800 <= code && code <= 0xdfff) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (0xd800 <= code && code <= 0xdfff) return -1; |
0 |
0 |
0 |
| if (0xd800 <= code && code <= 0xdfff) return -1; |
0 |
0 |
0 |
|
| 192 |
0 |
▶ | if (0xfffe <= code && code <= 0xffff) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (0xfffe <= code && code <= 0xffff) return -1; |
0 |
0 |
0 |
| if (0xfffe <= code && code <= 0xffff) return -1; |
0 |
0 |
0 |
|
| 193 |
|
| |
| 194 |
|
| /* 0x00000000 - 0x0000007F: |
| 195 |
|
| 0xxxxxxx |
| 196 |
|
| */ |
| 197 |
0 |
▶ | if (code < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (code < 0) return -1; |
0 |
0 |
0 |
|
| 198 |
0 |
▶ | if (code <= 0x7F) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x7F) { |
0 |
0 |
0 |
|
| 199 |
0 |
| buf[0] = (unsigned char) code; |
| 200 |
0 |
| return 1; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
|
| /* 0x00000080 - 0x000007FF: |
| 204 |
|
| 110xxxxx 10xxxxxx |
| 205 |
|
| */ |
| 206 |
0 |
▶ | if (code <= 0x000007FF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x000007FF) { |
0 |
0 |
0 |
|
| 207 |
0 |
| buf[0] = 0xc0 | (code >> 6); |
| 208 |
0 |
| buf[1] = 0x80 | (code & 0x3f); |
| 209 |
0 |
| return 2; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* 0x00000800 - 0x0000FFFF: |
| 213 |
|
| 1110xxxx 10xxxxxx 10xxxxxx |
| 214 |
|
| */ |
| 215 |
0 |
▶ | if (code <= 0x0000FFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x0000FFFF) { |
0 |
0 |
0 |
|
| 216 |
0 |
| buf[0] = 0xe0 | (code >> 12); |
| 217 |
0 |
| buf[1] = 0x80 | ((code >> 6) & 0x3f); |
| 218 |
0 |
| buf[2] = 0x80 | (code & 0x3f); |
| 219 |
0 |
| return 3; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| /* 0x00010000 - 0x001FFFFF: |
| 223 |
|
| 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 224 |
|
| */ |
| 225 |
0 |
▶ | if (code <= 0x001FFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x001FFFFF) { |
0 |
0 |
0 |
|
| 226 |
0 |
| buf[0] = 0xf0 | (code >> 18); |
| 227 |
0 |
| buf[1] = 0x80 | ((code >> 12) & 0x3f); |
| 228 |
0 |
| buf[2] = 0x80 | ((code >> 6) & 0x3f); |
| 229 |
0 |
| buf[3] = 0x80 | (code & 0x3f); |
| 230 |
0 |
| return 4; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| /* 0x00200000 - 0x03FFFFFF: |
| 234 |
|
| 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 235 |
|
| */ |
| 236 |
0 |
▶ | if (code <= 0x03FFFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x03FFFFFF) { |
0 |
0 |
0 |
|
| 237 |
0 |
| buf[0] = 0xf8 | (code >> 24); |
| 238 |
0 |
| buf[1] = 0x80 | ((code >> 18) & 0x3f); |
| 239 |
0 |
| buf[2] = 0x80 | ((code >> 12) & 0x3f); |
| 240 |
0 |
| buf[3] = 0x80 | ((code >> 6) & 0x3f); |
| 241 |
0 |
| buf[4] = 0x80 | (code & 0x3f); |
| 242 |
0 |
| return 5; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| /* 0x04000000 - 0x7FFFFFFF: |
| 246 |
|
| 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 247 |
|
| */ |
| 248 |
0 |
▶ | if (code <= 0x7FFFFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code <= 0x7FFFFFFF) { |
0 |
0 |
0 |
|
| 249 |
0 |
| buf[0] = 0xfc | (code >> 30); |
| 250 |
0 |
| buf[1] = 0x80 | ((code >> 24) & 0x3f); |
| 251 |
0 |
| buf[2] = 0x80 | ((code >> 18) & 0x3f); |
| 252 |
0 |
| buf[3] = 0x80 | ((code >> 12) & 0x3f); |
| 253 |
0 |
| buf[4] = 0x80 | ((code >> 6) & 0x3f); |
| 254 |
0 |
| buf[5] = 0x80 | (code & 0x3f); |
| 255 |
0 |
| return 6; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
0 |
| return -1; |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* |
| 262 |
|
| * TOML has 3 data structures: value, array, table. |
| 263 |
|
| * Each of them can have identification key. |
| 264 |
|
| */ |
| 265 |
|
| typedef struct toml_keyval_t toml_keyval_t; |
| 266 |
|
| struct toml_keyval_t { |
| 267 |
|
| const char* key; /* key to this value */ |
| 268 |
|
| const char* val; /* the raw value */ |
| 269 |
|
| }; |
| 270 |
|
| |
| 271 |
|
| typedef struct toml_arritem_t toml_arritem_t; |
| 272 |
|
| struct toml_arritem_t { |
| 273 |
|
| int valtype; /* for value kind: 'i'nt, 'd'ouble, 'b'ool, 's'tring, 't'ime, 'D'ate, 'T'imestamp */ |
| 274 |
|
| char* val; |
| 275 |
|
| toml_array_t* arr; |
| 276 |
|
| toml_table_t* tab; |
| 277 |
|
| }; |
| 278 |
|
| |
| 279 |
|
| |
| 280 |
|
| struct toml_array_t { |
| 281 |
|
| const char* key; /* key to this array */ |
| 282 |
|
| int kind; /* element kind: 'v'alue, 'a'rray, or 't'able, 'm'ixed */ |
| 283 |
|
| int type; /* for value kind: 'i'nt, 'd'ouble, 'b'ool, 's'tring, 't'ime, 'D'ate, 'T'imestamp, 'm'ixed */ |
| 284 |
|
| |
| 285 |
|
| int nitem; /* number of elements */ |
| 286 |
|
| toml_arritem_t* item; |
| 287 |
|
| }; |
| 288 |
|
| |
| 289 |
|
| |
| 290 |
|
| struct toml_table_t { |
| 291 |
|
| const char* key; /* key to this table */ |
| 292 |
|
| bool implicit; /* table was created implicitly */ |
| 293 |
|
| bool readonly; /* no more modification allowed */ |
| 294 |
|
| |
| 295 |
|
| /* key-values in the table */ |
| 296 |
|
| int nkval; |
| 297 |
|
| toml_keyval_t** kval; |
| 298 |
|
| |
| 299 |
|
| /* arrays in the table */ |
| 300 |
|
| int narr; |
| 301 |
|
| toml_array_t** arr; |
| 302 |
|
| |
| 303 |
|
| /* tables in the table */ |
| 304 |
|
| int ntab; |
| 305 |
|
| toml_table_t** tab; |
| 306 |
|
| }; |
| 307 |
|
| |
| 308 |
|
| |
| 309 |
104 |
▶ | static inline void xfree(const void* x) { if (x) FREE((void*)(intptr_t)x); } |
Branch: 1 branch
| Branch | True | False | Total |
| static inline void xfree(const void* x) { if (x) FREE((void*)(intptr_t)x); } |
84 |
20 |
104 |
|
| 310 |
|
| |
| 311 |
|
| |
| 312 |
|
| enum tokentype_t { |
| 313 |
|
| INVALID, |
| 314 |
|
| DOT, |
| 315 |
|
| COMMA, |
| 316 |
|
| EQUAL, |
| 317 |
|
| LBRACE, |
| 318 |
|
| RBRACE, |
| 319 |
|
| NEWLINE, |
| 320 |
|
| LBRACKET, |
| 321 |
|
| RBRACKET, |
| 322 |
|
| STRING, |
| 323 |
|
| }; |
| 324 |
|
| typedef enum tokentype_t tokentype_t; |
| 325 |
|
| |
| 326 |
|
| typedef struct token_t token_t; |
| 327 |
|
| struct token_t { |
| 328 |
|
| tokentype_t tok; |
| 329 |
|
| int lineno; |
| 330 |
|
| char* ptr; /* points into context->start */ |
| 331 |
|
| int len; |
| 332 |
|
| int eof; |
| 333 |
|
| }; |
| 334 |
|
| |
| 335 |
|
| |
| 336 |
|
| typedef struct context_t context_t; |
| 337 |
|
| struct context_t { |
| 338 |
|
| char* start; |
| 339 |
|
| char* stop; |
| 340 |
|
| char* errbuf; |
| 341 |
|
| int errbufsz; |
| 342 |
|
| |
| 343 |
|
| token_t tok; |
| 344 |
|
| toml_table_t* root; |
| 345 |
|
| toml_table_t* curtab; |
| 346 |
|
| |
| 347 |
|
| struct { |
| 348 |
|
| int top; |
| 349 |
|
| char* key[10]; |
| 350 |
|
| token_t tok[10]; |
| 351 |
|
| } tpath; |
| 352 |
|
| |
| 353 |
|
| }; |
| 354 |
|
| |
| 355 |
|
| #define STRINGIFY(x) #x |
| 356 |
|
| #define TOSTRING(x) STRINGIFY(x) |
| 357 |
|
| #define FLINE __FILE__ ":" TOSTRING(__LINE__) |
| 358 |
|
| |
| 359 |
|
| static int next_token(context_t* ctx, int dotisspecial); |
| 360 |
|
| |
| 361 |
|
| /* |
| 362 |
|
| Error reporting. Call when an error is detected. Always return -1. |
| 363 |
|
| */ |
| 364 |
0 |
| static int e_outofmemory(context_t* ctx, const char* fline) |
| 365 |
|
| { |
| 366 |
0 |
| snprintf(ctx->errbuf, ctx->errbufsz, "ERROR: out of memory (%s)", fline); |
| 367 |
0 |
| return -1; |
| 368 |
|
| } |
| 369 |
|
| |
| 370 |
|
| |
| 371 |
0 |
| static int e_internal(context_t* ctx, const char* fline) |
| 372 |
|
| { |
| 373 |
0 |
| snprintf(ctx->errbuf, ctx->errbufsz, "internal error (%s)", fline); |
| 374 |
0 |
| return -1; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
1 |
| static int e_syntax(context_t* ctx, int lineno, const char* msg) |
| 378 |
|
| { |
| 379 |
1 |
| snprintf(ctx->errbuf, ctx->errbufsz, "line %d: %s", lineno, msg); |
| 380 |
1 |
| return -1; |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
0 |
| static int e_badkey(context_t* ctx, int lineno) |
| 384 |
|
| { |
| 385 |
0 |
| snprintf(ctx->errbuf, ctx->errbufsz, "line %d: bad key", lineno); |
| 386 |
0 |
| return -1; |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
0 |
| static int e_keyexists(context_t* ctx, int lineno) |
| 390 |
|
| { |
| 391 |
0 |
| snprintf(ctx->errbuf, ctx->errbufsz, "line %d: key exists", lineno); |
| 392 |
0 |
| return -1; |
| 393 |
|
| } |
| 394 |
|
| |
| 395 |
0 |
| static int e_forbid(context_t* ctx, int lineno, const char* msg) |
| 396 |
|
| { |
| 397 |
0 |
| snprintf(ctx->errbuf, ctx->errbufsz, "line %d: %s", lineno, msg); |
| 398 |
0 |
| return -1; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
17 |
| static void* expand(void* p, int sz, int newsz) |
| 402 |
|
| { |
| 403 |
17 |
| void* s = MALLOC(newsz); |
| 404 |
17 |
▶ | if (!s) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return 0; |
0 |
17 |
17 |
|
| 405 |
|
| |
| 406 |
17 |
| memcpy(s, p, sz); |
| 407 |
17 |
| FREE(p); |
| 408 |
17 |
| return s; |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
20 |
| static void** expand_ptrarr(void** p, int n) |
| 412 |
|
| { |
| 413 |
20 |
| void** s = MALLOC((n+1) * sizeof(void*)); |
| 414 |
20 |
▶ | if (!s) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return 0; |
0 |
20 |
20 |
|
| 415 |
|
| |
| 416 |
20 |
| s[n] = 0; |
| 417 |
20 |
| memcpy(s, p, n * sizeof(void*)); |
| 418 |
20 |
| FREE(p); |
| 419 |
20 |
| return s; |
| 420 |
|
| } |
| 421 |
|
| |
| 422 |
8 |
| static toml_arritem_t* expand_arritem(toml_arritem_t* p, int n) |
| 423 |
|
| { |
| 424 |
8 |
| toml_arritem_t* pp = expand(p, n*sizeof(*p), (n+1)*sizeof(*p)); |
| 425 |
8 |
▶ | if (!pp) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pp) return 0; |
0 |
8 |
8 |
|
| 426 |
|
| |
| 427 |
8 |
| memset(&pp[n], 0, sizeof(pp[n])); |
| 428 |
8 |
| return pp; |
| 429 |
|
| } |
| 430 |
|
| |
| 431 |
|
| |
| 432 |
0 |
| static char* norm_lit_str(const char* src, int srclen, |
| 433 |
|
| int multiline, |
| 434 |
|
| char* errbuf, int errbufsz) |
| 435 |
|
| { |
| 436 |
0 |
| char* dst = 0; /* will write to dst[] and return it */ |
| 437 |
0 |
| int max = 0; /* max size of dst[] */ |
| 438 |
0 |
| int off = 0; /* cur offset in dst[] */ |
| 439 |
0 |
| const char* sp = src; |
| 440 |
0 |
| const char* sq = src + srclen; |
| 441 |
|
| int ch; |
| 442 |
|
| |
| 443 |
|
| /* scan forward on src */ |
| 444 |
|
| for (;;) { |
| 445 |
0 |
▶ | if (off >= max - 10) { /* have some slack for misc stuff */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (off >= max - 10) { /* have some slack for misc stuff */ |
0 |
0 |
0 |
|
| 446 |
0 |
| int newmax = max + 50; |
| 447 |
0 |
| char* x = expand(dst, max, newmax); |
| 448 |
0 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
0 |
0 |
0 |
|
| 449 |
0 |
| xfree(dst); |
| 450 |
0 |
| snprintf(errbuf, errbufsz, "out of memory"); |
| 451 |
0 |
| return 0; |
| 452 |
|
| } |
| 453 |
0 |
| dst = x; |
| 454 |
0 |
| max = newmax; |
| 455 |
|
| } |
| 456 |
|
| |
| 457 |
|
| /* finished? */ |
| 458 |
0 |
▶ | if (sp >= sq) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp >= sq) break; |
0 |
0 |
0 |
|
| 459 |
|
| |
| 460 |
0 |
| ch = *sp++; |
| 461 |
|
| /* control characters other than tab is not allowed */ |
| 462 |
0 |
▶ | if ((0 <= ch && ch <= 0x08) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((0 <= ch && ch <= 0x08) |
0 |
0 |
0 |
| if ((0 <= ch && ch <= 0x08) |
0 |
0 |
0 |
|
| 463 |
0 |
▶ | || (0x0a <= ch && ch <= 0x1f) |
Branch: 2 branches
| Branch | True | False | Total |
| || (0x0a <= ch && ch <= 0x1f) |
0 |
0 |
0 |
| || (0x0a <= ch && ch <= 0x1f) |
0 |
0 |
0 |
|
| 464 |
0 |
▶ | || (ch == 0x7f)) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (ch == 0x7f)) { |
0 |
0 |
0 |
|
| 465 |
0 |
▶ | if (! (multiline && (ch == '\r' || ch == '\n'))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
|
| 466 |
0 |
| xfree(dst); |
| 467 |
0 |
| snprintf(errbuf, errbufsz, "invalid char U+%04x", ch); |
| 468 |
0 |
| return 0; |
| 469 |
|
| } |
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| // a plain copy suffice |
| 473 |
0 |
| dst[off++] = ch; |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
0 |
| dst[off++] = 0; |
| 477 |
0 |
| return dst; |
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
|
| |
| 481 |
|
| |
| 482 |
|
| |
| 483 |
|
| /* |
| 484 |
|
| * Convert src to raw unescaped utf-8 string. |
| 485 |
|
| * Returns NULL if error with errmsg in errbuf. |
| 486 |
|
| */ |
| 487 |
9 |
| static char* norm_basic_str(const char* src, int srclen, |
| 488 |
|
| int multiline, |
| 489 |
|
| char* errbuf, int errbufsz) |
| 490 |
|
| { |
| 491 |
9 |
| char* dst = 0; /* will write to dst[] and return it */ |
| 492 |
9 |
| int max = 0; /* max size of dst[] */ |
| 493 |
9 |
| int off = 0; /* cur offset in dst[] */ |
| 494 |
9 |
| const char* sp = src; |
| 495 |
9 |
| const char* sq = src + srclen; |
| 496 |
|
| int ch; |
| 497 |
|
| |
| 498 |
|
| /* scan forward on src */ |
| 499 |
|
| for (;;) { |
| 500 |
84 |
▶ | if (off >= max - 10) { /* have some slack for misc stuff */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (off >= max - 10) { /* have some slack for misc stuff */ |
9 |
75 |
84 |
|
| 501 |
9 |
| int newmax = max + 50; |
| 502 |
9 |
| char* x = expand(dst, max, newmax); |
| 503 |
9 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
0 |
9 |
9 |
|
| 504 |
0 |
| xfree(dst); |
| 505 |
0 |
| snprintf(errbuf, errbufsz, "out of memory"); |
| 506 |
0 |
| return 0; |
| 507 |
|
| } |
| 508 |
9 |
| dst = x; |
| 509 |
9 |
| max = newmax; |
| 510 |
|
| } |
| 511 |
|
| |
| 512 |
|
| /* finished? */ |
| 513 |
84 |
▶ | if (sp >= sq) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp >= sq) break; |
9 |
75 |
84 |
|
| 514 |
|
| |
| 515 |
75 |
| ch = *sp++; |
| 516 |
75 |
▶ | if (ch != '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch != '\\') { |
75 |
0 |
75 |
|
| 517 |
|
| /* these chars must be escaped: U+0000 to U+0008, U+000A to U+001F, U+007F */ |
| 518 |
75 |
▶ | if ((0 <= ch && ch <= 0x08) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((0 <= ch && ch <= 0x08) |
71 |
4 |
75 |
| if ((0 <= ch && ch <= 0x08) |
71 |
0 |
71 |
|
| 519 |
75 |
▶ | || (0x0a <= ch && ch <= 0x1f) |
Branch: 2 branches
| Branch | True | False | Total |
| || (0x0a <= ch && ch <= 0x1f) |
71 |
4 |
75 |
| || (0x0a <= ch && ch <= 0x1f) |
71 |
0 |
71 |
|
| 520 |
75 |
▶ | || (ch == 0x7f)) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (ch == 0x7f)) { |
0 |
75 |
75 |
|
| 521 |
0 |
▶ | if (! (multiline && (ch == '\r' || ch == '\n'))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
| if (! (multiline && (ch == '\r' || ch == '\n'))) { |
0 |
0 |
0 |
|
| 522 |
0 |
| xfree(dst); |
| 523 |
0 |
| snprintf(errbuf, errbufsz, "invalid char U+%04x", ch); |
| 524 |
0 |
| return 0; |
| 525 |
|
| } |
| 526 |
|
| } |
| 527 |
|
| |
| 528 |
|
| // a plain copy suffice |
| 529 |
75 |
| dst[off++] = ch; |
| 530 |
75 |
| continue; |
| 531 |
|
| } |
| 532 |
|
| |
| 533 |
|
| /* ch was backslash. we expect the escape char. */ |
| 534 |
0 |
▶ | if (sp >= sq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp >= sq) { |
0 |
0 |
0 |
|
| 535 |
0 |
| snprintf(errbuf, errbufsz, "last backslash is invalid"); |
| 536 |
0 |
| xfree(dst); |
| 537 |
0 |
| return 0; |
| 538 |
|
| } |
| 539 |
|
| |
| 540 |
|
| /* for multi-line, we want to kill line-ending-backslash ... */ |
| 541 |
0 |
▶ | if (multiline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (multiline) { |
0 |
0 |
0 |
|
| 542 |
|
| |
| 543 |
|
| // if there is only whitespace after the backslash ... |
| 544 |
0 |
▶ | if (sp[strspn(sp, " \t\r")] == '\n') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp[strspn(sp, " \t\r")] == '\n') { |
0 |
0 |
0 |
|
| 545 |
|
| /* skip all the following whitespaces */ |
| 546 |
0 |
| sp += strspn(sp, " \t\r\n"); |
| 547 |
0 |
| continue; |
| 548 |
|
| } |
| 549 |
|
| } |
| 550 |
|
| |
| 551 |
|
| /* get the escaped char */ |
| 552 |
0 |
| ch = *sp++; |
| 553 |
0 |
| switch (ch) { |
| 554 |
0 |
| case 'u': case 'U': |
| 555 |
|
| { |
| 556 |
0 |
| int64_t ucs = 0; |
| 557 |
0 |
▶ | int nhex = (ch == 'u' ? 4 : 8); |
Branch: 1 branch
| Branch | True | False | Total |
| int nhex = (ch == 'u' ? 4 : 8); |
0 |
0 |
0 |
|
| 558 |
0 |
▶ | for (int i = 0; i < nhex; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < nhex; i++) { |
0 |
0 |
0 |
|
| 559 |
0 |
▶ | if (sp >= sq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp >= sq) { |
0 |
0 |
0 |
|
| 560 |
0 |
| snprintf(errbuf, errbufsz, "\\%c expects %d hex chars", ch, nhex); |
| 561 |
0 |
| xfree(dst); |
| 562 |
0 |
| return 0; |
| 563 |
|
| } |
| 564 |
0 |
| ch = *sp++; |
| 565 |
0 |
▶ | int v = ('0' <= ch && ch <= '9') |
Branch: 1 branch
| Branch | True | False | Total |
| int v = ('0' <= ch && ch <= '9') |
0 |
0 |
0 |
|
| 566 |
|
| ? ch - '0' |
| 567 |
0 |
▶ | : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); |
Branch: 3 branches
| Branch | True | False | Total |
| : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); |
0 |
0 |
0 |
| : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); |
0 |
0 |
0 |
| : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); |
0 |
0 |
0 |
|
| 568 |
0 |
▶ | if (-1 == v) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == v) { |
0 |
0 |
0 |
|
| 569 |
0 |
| snprintf(errbuf, errbufsz, "invalid hex chars for \\u or \\U"); |
| 570 |
0 |
| xfree(dst); |
| 571 |
0 |
| return 0; |
| 572 |
|
| } |
| 573 |
0 |
| ucs = ucs * 16 + v; |
| 574 |
|
| } |
| 575 |
0 |
| int n = toml_ucs_to_utf8(ucs, &dst[off]); |
| 576 |
0 |
▶ | if (-1 == n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == n) { |
0 |
0 |
0 |
|
| 577 |
0 |
| snprintf(errbuf, errbufsz, "illegal ucs code in \\u or \\U"); |
| 578 |
0 |
| xfree(dst); |
| 579 |
0 |
| return 0; |
| 580 |
|
| } |
| 581 |
0 |
| off += n; |
| 582 |
|
| } |
| 583 |
0 |
| continue; |
| 584 |
|
| |
| 585 |
0 |
| case 'b': ch = '\b'; break; |
| 586 |
0 |
| case 't': ch = '\t'; break; |
| 587 |
0 |
| case 'n': ch = '\n'; break; |
| 588 |
0 |
| case 'f': ch = '\f'; break; |
| 589 |
0 |
| case 'r': ch = '\r'; break; |
| 590 |
0 |
| case '"': ch = '"'; break; |
| 591 |
0 |
| case '\\': ch = '\\'; break; |
| 592 |
0 |
| default: |
| 593 |
0 |
| snprintf(errbuf, errbufsz, "illegal escape char \\%c", ch); |
| 594 |
0 |
| xfree(dst); |
| 595 |
0 |
| return 0; |
| 596 |
|
| } |
| 597 |
|
| |
| 598 |
0 |
| dst[off++] = ch; |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
|
| // Cap with NUL and return it. |
| 602 |
9 |
| dst[off++] = 0; |
| 603 |
9 |
| return dst; |
| 604 |
|
| } |
| 605 |
|
| |
| 606 |
|
| |
| 607 |
|
| /* Normalize a key. Convert all special chars to raw unescaped utf-8 chars. */ |
| 608 |
28 |
| static char* normalize_key(context_t* ctx, token_t strtok) |
| 609 |
|
| { |
| 610 |
28 |
| const char* sp = strtok.ptr; |
| 611 |
28 |
| const char* sq = strtok.ptr + strtok.len; |
| 612 |
28 |
| int lineno = strtok.lineno; |
| 613 |
|
| char* ret; |
| 614 |
28 |
| int ch = *sp; |
| 615 |
|
| char ebuf[80]; |
| 616 |
|
| |
| 617 |
|
| /* handle quoted string */ |
| 618 |
28 |
▶ | if (ch == '\'' || ch == '\"') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch == '\'' || ch == '\"') { |
28 |
0 |
28 |
| if (ch == '\'' || ch == '\"') { |
0 |
28 |
28 |
|
| 619 |
|
| /* if ''' or """, take 3 chars off front and back. Else, take 1 char off. */ |
| 620 |
0 |
| int multiline = 0; |
| 621 |
0 |
▶ | if (sp[1] == ch && sp[2] == ch) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (sp[1] == ch && sp[2] == ch) { |
0 |
0 |
0 |
| if (sp[1] == ch && sp[2] == ch) { |
0 |
0 |
0 |
|
| 622 |
0 |
| sp += 3, sq -= 3; |
| 623 |
0 |
| multiline = 1; |
| 624 |
|
| } |
| 625 |
|
| else |
| 626 |
0 |
| sp++, sq--; |
| 627 |
|
| |
| 628 |
0 |
▶ | if (ch == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '\'') { |
0 |
0 |
0 |
|
| 629 |
|
| /* for single quote, take it verbatim. */ |
| 630 |
0 |
▶ | if (! (ret = STRNDUP(sp, sq - sp))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! (ret = STRNDUP(sp, sq - sp))) { |
0 |
0 |
0 |
|
| 631 |
0 |
| e_outofmemory(ctx, FLINE); |
| 632 |
0 |
| return 0; |
| 633 |
|
| } |
| 634 |
|
| } else { |
| 635 |
|
| /* for double quote, we need to normalize */ |
| 636 |
0 |
| ret = norm_basic_str(sp, sq - sp, multiline, ebuf, sizeof(ebuf)); |
| 637 |
0 |
▶ | if (!ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) { |
0 |
0 |
0 |
|
| 638 |
0 |
| e_syntax(ctx, lineno, ebuf); |
| 639 |
0 |
| return 0; |
| 640 |
|
| } |
| 641 |
|
| } |
| 642 |
|
| |
| 643 |
|
| /* newlines are not allowed in keys */ |
| 644 |
0 |
▶ | if (strchr(ret, '\n')) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr(ret, '\n')) { |
0 |
0 |
0 |
|
| 645 |
0 |
| xfree(ret); |
| 646 |
0 |
| e_badkey(ctx, lineno); |
| 647 |
0 |
| return 0; |
| 648 |
|
| } |
| 649 |
0 |
| return ret; |
| 650 |
|
| } |
| 651 |
|
| |
| 652 |
|
| /* for bare-key allow only this regex: [A-Za-z0-9_-]+ */ |
| 653 |
|
| const char* xp; |
| 654 |
182 |
▶ | for (xp = sp; xp != sq; xp++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (xp = sp; xp != sq; xp++) { |
154 |
28 |
182 |
|
| 655 |
154 |
| int k = *xp; |
| 656 |
154 |
▶ | if (isalnum(k)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (isalnum(k)) continue; |
153 |
1 |
154 |
|
| 657 |
1 |
▶ | if (k == '_' || k == '-') continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (k == '_' || k == '-') continue; |
0 |
1 |
1 |
| if (k == '_' || k == '-') continue; |
0 |
0 |
0 |
|
| 658 |
0 |
| e_badkey(ctx, lineno); |
| 659 |
0 |
| return 0; |
| 660 |
|
| } |
| 661 |
|
| |
| 662 |
|
| /* dup and return it */ |
| 663 |
28 |
▶ | if (! (ret = STRNDUP(sp, sq - sp))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! (ret = STRNDUP(sp, sq - sp))) { |
0 |
28 |
28 |
|
| 664 |
0 |
| e_outofmemory(ctx, FLINE); |
| 665 |
0 |
| return 0; |
| 666 |
|
| } |
| 667 |
28 |
| return ret; |
| 668 |
|
| } |
| 669 |
|
| |
| 670 |
|
| |
| 671 |
|
| /* |
| 672 |
|
| * Look up key in tab. Return 0 if not found, or |
| 673 |
|
| * 'v'alue, 'a'rray or 't'able depending on the element. |
| 674 |
|
| */ |
| 675 |
22 |
| static int check_key(toml_table_t* tab, const char* key, |
| 676 |
|
| toml_keyval_t** ret_val, |
| 677 |
|
| toml_array_t** ret_arr, |
| 678 |
|
| toml_table_t** ret_tab) |
| 679 |
|
| { |
| 680 |
|
| int i; |
| 681 |
|
| void* dummy; |
| 682 |
|
| |
| 683 |
22 |
▶ | if (!ret_tab) ret_tab = (toml_table_t**) &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret_tab) ret_tab = (toml_table_t**) &dummy; |
13 |
9 |
22 |
|
| 684 |
22 |
▶ | if (!ret_arr) ret_arr = (toml_array_t**) &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret_arr) ret_arr = (toml_array_t**) &dummy; |
20 |
2 |
22 |
|
| 685 |
22 |
▶ | if (!ret_val) ret_val = (toml_keyval_t**) &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret_val) ret_val = (toml_keyval_t**) &dummy; |
20 |
2 |
22 |
|
| 686 |
|
| |
| 687 |
22 |
| *ret_tab = 0; *ret_arr = 0; *ret_val = 0; |
| 688 |
|
| |
| 689 |
35 |
▶ | for (i = 0; i < tab->nkval; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->nkval; i++) { |
13 |
22 |
35 |
|
| 690 |
13 |
▶ | if (0 == strcmp(key, tab->kval[i]->key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->kval[i]->key)) { |
0 |
13 |
13 |
|
| 691 |
0 |
| *ret_val = tab->kval[i]; |
| 692 |
0 |
| return 'v'; |
| 693 |
|
| } |
| 694 |
|
| } |
| 695 |
25 |
▶ | for (i = 0; i < tab->narr; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->narr; i++) { |
3 |
22 |
25 |
|
| 696 |
3 |
▶ | if (0 == strcmp(key, tab->arr[i]->key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->arr[i]->key)) { |
0 |
3 |
3 |
|
| 697 |
0 |
| *ret_arr = tab->arr[i]; |
| 698 |
0 |
| return 'a'; |
| 699 |
|
| } |
| 700 |
|
| } |
| 701 |
33 |
▶ | for (i = 0; i < tab->ntab; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->ntab; i++) { |
13 |
20 |
33 |
|
| 702 |
13 |
▶ | if (0 == strcmp(key, tab->tab[i]->key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->tab[i]->key)) { |
2 |
11 |
13 |
|
| 703 |
2 |
| *ret_tab = tab->tab[i]; |
| 704 |
2 |
| return 't'; |
| 705 |
|
| } |
| 706 |
|
| } |
| 707 |
20 |
| return 0; |
| 708 |
|
| } |
| 709 |
|
| |
| 710 |
|
| |
| 711 |
13 |
| static int key_kind(toml_table_t* tab, const char* key) |
| 712 |
|
| { |
| 713 |
13 |
| return check_key(tab, key, 0, 0, 0); |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
|
| /* Create a keyval in the table. |
| 717 |
|
| */ |
| 718 |
11 |
| static toml_keyval_t* create_keyval_in_table(context_t* ctx, toml_table_t* tab, token_t keytok) |
| 719 |
|
| { |
| 720 |
|
| /* first, normalize the key to be used for lookup. |
| 721 |
|
| * remember to free it if we error out. |
| 722 |
|
| */ |
| 723 |
11 |
| char* newkey = normalize_key(ctx, keytok); |
| 724 |
11 |
▶ | if (!newkey) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newkey) return 0; |
0 |
11 |
11 |
|
| 725 |
|
| |
| 726 |
|
| /* if key exists: error out. */ |
| 727 |
11 |
| toml_keyval_t* dest = 0; |
| 728 |
11 |
▶ | if (key_kind(tab, newkey)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_kind(tab, newkey)) { |
0 |
11 |
11 |
|
| 729 |
0 |
| xfree(newkey); |
| 730 |
0 |
| e_keyexists(ctx, keytok.lineno); |
| 731 |
0 |
| return 0; |
| 732 |
|
| } |
| 733 |
|
| |
| 734 |
|
| /* make a new entry */ |
| 735 |
11 |
| int n = tab->nkval; |
| 736 |
|
| toml_keyval_t** base; |
| 737 |
11 |
▶ | if (0 == (base = (toml_keyval_t**) expand_ptrarr((void**)tab->kval, n))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base = (toml_keyval_t**) expand_ptrarr((void**)tab->kval, n))) { |
0 |
11 |
11 |
|
| 738 |
0 |
| xfree(newkey); |
| 739 |
0 |
| e_outofmemory(ctx, FLINE); |
| 740 |
0 |
| return 0; |
| 741 |
|
| } |
| 742 |
11 |
| tab->kval = base; |
| 743 |
|
| |
| 744 |
11 |
▶ | if (0 == (base[n] = (toml_keyval_t*) CALLOC(1, sizeof(*base[n])))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base[n] = (toml_keyval_t*) CALLOC(1, sizeof(*base[n])))) { |
0 |
11 |
11 |
|
| 745 |
0 |
| xfree(newkey); |
| 746 |
0 |
| e_outofmemory(ctx, FLINE); |
| 747 |
0 |
| return 0; |
| 748 |
|
| } |
| 749 |
11 |
| dest = tab->kval[tab->nkval++]; |
| 750 |
|
| |
| 751 |
|
| /* save the key in the new value struct */ |
| 752 |
11 |
| dest->key = newkey; |
| 753 |
11 |
| return dest; |
| 754 |
|
| } |
| 755 |
|
| |
| 756 |
|
| |
| 757 |
|
| /* Create a table in the table. |
| 758 |
|
| */ |
| 759 |
7 |
| static toml_table_t* create_keytable_in_table(context_t* ctx, toml_table_t* tab, token_t keytok) |
| 760 |
|
| { |
| 761 |
|
| /* first, normalize the key to be used for lookup. |
| 762 |
|
| * remember to free it if we error out. |
| 763 |
|
| */ |
| 764 |
7 |
| char* newkey = normalize_key(ctx, keytok); |
| 765 |
7 |
▶ | if (!newkey) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newkey) return 0; |
0 |
7 |
7 |
|
| 766 |
|
| |
| 767 |
|
| /* if key exists: error out */ |
| 768 |
7 |
| toml_table_t* dest = 0; |
| 769 |
7 |
▶ | if (check_key(tab, newkey, 0, 0, &dest)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_key(tab, newkey, 0, 0, &dest)) { |
0 |
7 |
7 |
|
| 770 |
0 |
| xfree(newkey); /* don't need this anymore */ |
| 771 |
|
| |
| 772 |
|
| /* special case: if table exists, but was created implicitly ... */ |
| 773 |
0 |
▶ | if (dest && dest->implicit) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dest && dest->implicit) { |
0 |
0 |
0 |
| if (dest && dest->implicit) { |
0 |
0 |
0 |
|
| 774 |
|
| /* we make it explicit now, and simply return it. */ |
| 775 |
0 |
| dest->implicit = false; |
| 776 |
0 |
| return dest; |
| 777 |
|
| } |
| 778 |
0 |
| e_keyexists(ctx, keytok.lineno); |
| 779 |
0 |
| return 0; |
| 780 |
|
| } |
| 781 |
|
| |
| 782 |
|
| /* create a new table entry */ |
| 783 |
7 |
| int n = tab->ntab; |
| 784 |
|
| toml_table_t** base; |
| 785 |
7 |
▶ | if (0 == (base = (toml_table_t**) expand_ptrarr((void**)tab->tab, n))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base = (toml_table_t**) expand_ptrarr((void**)tab->tab, n))) { |
0 |
7 |
7 |
|
| 786 |
0 |
| xfree(newkey); |
| 787 |
0 |
| e_outofmemory(ctx, FLINE); |
| 788 |
0 |
| return 0; |
| 789 |
|
| } |
| 790 |
7 |
| tab->tab = base; |
| 791 |
|
| |
| 792 |
7 |
▶ | if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) { |
0 |
7 |
7 |
|
| 793 |
0 |
| xfree(newkey); |
| 794 |
0 |
| e_outofmemory(ctx, FLINE); |
| 795 |
0 |
| return 0; |
| 796 |
|
| } |
| 797 |
7 |
| dest = tab->tab[tab->ntab++]; |
| 798 |
|
| |
| 799 |
|
| /* save the key in the new table struct */ |
| 800 |
7 |
| dest->key = newkey; |
| 801 |
7 |
| return dest; |
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
|
| |
| 805 |
|
| /* Create an array in the table. |
| 806 |
|
| */ |
| 807 |
2 |
| static toml_array_t* create_keyarray_in_table(context_t* ctx, |
| 808 |
|
| toml_table_t* tab, |
| 809 |
|
| token_t keytok, |
| 810 |
|
| char kind) |
| 811 |
|
| { |
| 812 |
|
| /* first, normalize the key to be used for lookup. |
| 813 |
|
| * remember to free it if we error out. |
| 814 |
|
| */ |
| 815 |
2 |
| char* newkey = normalize_key(ctx, keytok); |
| 816 |
2 |
▶ | if (!newkey) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newkey) return 0; |
0 |
2 |
2 |
|
| 817 |
|
| |
| 818 |
|
| /* if key exists: error out */ |
| 819 |
2 |
▶ | if (key_kind(tab, newkey)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_kind(tab, newkey)) { |
0 |
2 |
2 |
|
| 820 |
0 |
| xfree(newkey); /* don't need this anymore */ |
| 821 |
0 |
| e_keyexists(ctx, keytok.lineno); |
| 822 |
0 |
| return 0; |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
|
| /* make a new array entry */ |
| 826 |
2 |
| int n = tab->narr; |
| 827 |
|
| toml_array_t** base; |
| 828 |
2 |
▶ | if (0 == (base = (toml_array_t**) expand_ptrarr((void**)tab->arr, n))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base = (toml_array_t**) expand_ptrarr((void**)tab->arr, n))) { |
0 |
2 |
2 |
|
| 829 |
0 |
| xfree(newkey); |
| 830 |
0 |
| e_outofmemory(ctx, FLINE); |
| 831 |
0 |
| return 0; |
| 832 |
|
| } |
| 833 |
2 |
| tab->arr = base; |
| 834 |
|
| |
| 835 |
2 |
▶ | if (0 == (base[n] = (toml_array_t*) CALLOC(1, sizeof(*base[n])))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base[n] = (toml_array_t*) CALLOC(1, sizeof(*base[n])))) { |
0 |
2 |
2 |
|
| 836 |
0 |
| xfree(newkey); |
| 837 |
0 |
| e_outofmemory(ctx, FLINE); |
| 838 |
0 |
| return 0; |
| 839 |
|
| } |
| 840 |
2 |
| toml_array_t* dest = tab->arr[tab->narr++]; |
| 841 |
|
| |
| 842 |
|
| /* save the key in the new array struct */ |
| 843 |
2 |
| dest->key = newkey; |
| 844 |
2 |
| dest->kind = kind; |
| 845 |
2 |
| return dest; |
| 846 |
|
| } |
| 847 |
|
| |
| 848 |
|
| |
| 849 |
6 |
| static toml_arritem_t* create_value_in_array(context_t* ctx, |
| 850 |
|
| toml_array_t* parent) |
| 851 |
|
| { |
| 852 |
6 |
| const int n = parent->nitem; |
| 853 |
6 |
| toml_arritem_t* base = expand_arritem(parent->item, n); |
| 854 |
6 |
▶ | if (!base) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!base) { |
0 |
6 |
6 |
|
| 855 |
0 |
| e_outofmemory(ctx, FLINE); |
| 856 |
0 |
| return 0; |
| 857 |
|
| } |
| 858 |
6 |
| parent->item = base; |
| 859 |
6 |
| parent->nitem++; |
| 860 |
6 |
| return &parent->item[n]; |
| 861 |
|
| } |
| 862 |
|
| |
| 863 |
|
| /* Create an array in an array |
| 864 |
|
| */ |
| 865 |
2 |
| static toml_array_t* create_array_in_array(context_t* ctx, |
| 866 |
|
| toml_array_t* parent) |
| 867 |
|
| { |
| 868 |
2 |
| const int n = parent->nitem; |
| 869 |
2 |
| toml_arritem_t* base = expand_arritem(parent->item, n); |
| 870 |
2 |
▶ | if (!base) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!base) { |
0 |
2 |
2 |
|
| 871 |
0 |
| e_outofmemory(ctx, FLINE); |
| 872 |
0 |
| return 0; |
| 873 |
|
| } |
| 874 |
2 |
| toml_array_t* ret = (toml_array_t*) CALLOC(1, sizeof(toml_array_t)); |
| 875 |
2 |
▶ | if (!ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) { |
0 |
2 |
2 |
|
| 876 |
0 |
| e_outofmemory(ctx, FLINE); |
| 877 |
0 |
| return 0; |
| 878 |
|
| } |
| 879 |
2 |
| base[n].arr = ret; |
| 880 |
2 |
| parent->item = base; |
| 881 |
2 |
| parent->nitem++; |
| 882 |
2 |
| return ret; |
| 883 |
|
| } |
| 884 |
|
| |
| 885 |
|
| /* Create a table in an array |
| 886 |
|
| */ |
| 887 |
0 |
| static toml_table_t* create_table_in_array(context_t* ctx, |
| 888 |
|
| toml_array_t* parent) |
| 889 |
|
| { |
| 890 |
0 |
| int n = parent->nitem; |
| 891 |
0 |
| toml_arritem_t* base = expand_arritem(parent->item, n); |
| 892 |
0 |
▶ | if (!base) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!base) { |
0 |
0 |
0 |
|
| 893 |
0 |
| e_outofmemory(ctx, FLINE); |
| 894 |
0 |
| return 0; |
| 895 |
|
| } |
| 896 |
0 |
| toml_table_t* ret = (toml_table_t*) CALLOC(1, sizeof(toml_table_t)); |
| 897 |
0 |
▶ | if (!ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ret) { |
0 |
0 |
0 |
|
| 898 |
0 |
| e_outofmemory(ctx, FLINE); |
| 899 |
0 |
| return 0; |
| 900 |
|
| } |
| 901 |
0 |
| base[n].tab = ret; |
| 902 |
0 |
| parent->item = base; |
| 903 |
0 |
| parent->nitem++; |
| 904 |
0 |
| return ret; |
| 905 |
|
| } |
| 906 |
|
| |
| 907 |
|
| |
| 908 |
16 |
| static int skip_newlines(context_t* ctx, int isdotspecial) |
| 909 |
|
| { |
| 910 |
16 |
▶ | while (ctx->tok.tok == NEWLINE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ctx->tok.tok == NEWLINE) { |
0 |
16 |
16 |
|
| 911 |
0 |
▶ | if (next_token(ctx, isdotspecial)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, isdotspecial)) return -1; |
0 |
0 |
0 |
|
| 912 |
0 |
▶ | if (ctx->tok.eof) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.eof) break; |
0 |
0 |
0 |
|
| 913 |
|
| } |
| 914 |
16 |
| return 0; |
| 915 |
|
| } |
| 916 |
|
| |
| 917 |
|
| |
| 918 |
|
| static int parse_keyval(context_t* ctx, toml_table_t* tab); |
| 919 |
|
| |
| 920 |
60 |
| static inline int eat_token(context_t* ctx, tokentype_t typ, int isdotspecial, const char* fline) |
| 921 |
|
| { |
| 922 |
60 |
▶ | if (ctx->tok.tok != typ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != typ) |
0 |
60 |
60 |
|
| 923 |
0 |
| return e_internal(ctx, fline); |
| 924 |
|
| |
| 925 |
60 |
▶ | if (next_token(ctx, isdotspecial)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, isdotspecial)) |
0 |
60 |
60 |
|
| 926 |
0 |
| return -1; |
| 927 |
|
| |
| 928 |
60 |
| return 0; |
| 929 |
|
| } |
| 930 |
|
| |
| 931 |
|
| |
| 932 |
|
| |
| 933 |
|
| /* We are at '{ ... }'. |
| 934 |
|
| * Parse the table. |
| 935 |
|
| */ |
| 936 |
1 |
| static int parse_inline_table(context_t* ctx, toml_table_t* tab) |
| 937 |
|
| { |
| 938 |
1 |
▶ | if (eat_token(ctx, LBRACE, 1, FLINE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, LBRACE, 1, FLINE)) |
1 |
0 |
1 |
|
| 939 |
0 |
| return -1; |
| 940 |
|
| |
| 941 |
|
| for (;;) { |
| 942 |
2 |
▶ | if (ctx->tok.tok == NEWLINE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == NEWLINE) |
0 |
2 |
2 |
|
| 943 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "newline not allowed in inline table"); |
| 944 |
|
| |
| 945 |
|
| /* until } */ |
| 946 |
2 |
▶ | if (ctx->tok.tok == RBRACE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == RBRACE) |
0 |
2 |
2 |
|
| 947 |
0 |
| break; |
| 948 |
|
| |
| 949 |
2 |
▶ | if (ctx->tok.tok != STRING) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != STRING) |
0 |
2 |
2 |
|
| 950 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "expect a string"); |
| 951 |
|
| |
| 952 |
2 |
▶ | if (parse_keyval(ctx, tab)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_keyval(ctx, tab)) |
0 |
2 |
2 |
|
| 953 |
0 |
| return -1; |
| 954 |
|
| |
| 955 |
2 |
▶ | if (ctx->tok.tok == NEWLINE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == NEWLINE) |
0 |
2 |
2 |
|
| 956 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "newline not allowed in inline table"); |
| 957 |
|
| |
| 958 |
|
| /* on comma, continue to scan for next keyval */ |
| 959 |
2 |
▶ | if (ctx->tok.tok == COMMA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == COMMA) { |
1 |
1 |
2 |
|
| 960 |
1 |
▶ | if (eat_token(ctx, COMMA, 1, FLINE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, COMMA, 1, FLINE)) |
0 |
1 |
1 |
|
| 961 |
0 |
| return -1; |
| 962 |
1 |
| continue; |
| 963 |
|
| } |
| 964 |
1 |
| break; |
| 965 |
|
| } |
| 966 |
|
| |
| 967 |
1 |
▶ | if (eat_token(ctx, RBRACE, 1, FLINE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, RBRACE, 1, FLINE)) |
0 |
1 |
1 |
|
| 968 |
0 |
| return -1; |
| 969 |
|
| |
| 970 |
1 |
| tab->readonly = 1; |
| 971 |
|
| |
| 972 |
1 |
| return 0; |
| 973 |
|
| } |
| 974 |
|
| |
| 975 |
6 |
| static int valtype(const char* val) |
| 976 |
|
| { |
| 977 |
|
| toml_timestamp_t ts; |
| 978 |
6 |
▶ | if (*val == '\'' || *val == '"') return 's'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*val == '\'' || *val == '"') return 's'; |
6 |
0 |
6 |
| if (*val == '\'' || *val == '"') return 's'; |
2 |
4 |
6 |
|
| 979 |
4 |
▶ | if (0 == toml_rtob(val, 0)) return 'b'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == toml_rtob(val, 0)) return 'b'; |
0 |
4 |
4 |
|
| 980 |
4 |
▶ | if (0 == toml_rtoi(val, 0)) return 'i'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == toml_rtoi(val, 0)) return 'i'; |
3 |
1 |
4 |
|
| 981 |
1 |
▶ | if (0 == toml_rtod(val, 0)) return 'd'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == toml_rtod(val, 0)) return 'd'; |
1 |
0 |
1 |
|
| 982 |
0 |
▶ | if (0 == toml_rtots(val, &ts)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == toml_rtots(val, &ts)) { |
0 |
0 |
0 |
|
| 983 |
0 |
▶ | if (ts.year && ts.hour) return 'T'; /* timestamp */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (ts.year && ts.hour) return 'T'; /* timestamp */ |
0 |
0 |
0 |
| if (ts.year && ts.hour) return 'T'; /* timestamp */ |
0 |
0 |
0 |
|
| 984 |
0 |
▶ | if (ts.year) return 'D'; /* date */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts.year) return 'D'; /* date */ |
0 |
0 |
0 |
|
| 985 |
0 |
| return 't'; /* time */ |
| 986 |
|
| } |
| 987 |
0 |
| return 'u'; /* unknown */ |
| 988 |
|
| } |
| 989 |
|
| |
| 990 |
|
| |
| 991 |
|
| /* We are at '[...]' */ |
| 992 |
4 |
| static int parse_array(context_t* ctx, toml_array_t* arr) |
| 993 |
|
| { |
| 994 |
4 |
▶ | if (eat_token(ctx, LBRACKET, 0, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, LBRACKET, 0, FLINE)) return -1; |
4 |
0 |
4 |
|
| 995 |
|
| |
| 996 |
|
| for (;;) { |
| 997 |
8 |
▶ | if (skip_newlines(ctx, 0)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (skip_newlines(ctx, 0)) return -1; |
0 |
8 |
8 |
|
| 998 |
|
| |
| 999 |
|
| /* until ] */ |
| 1000 |
8 |
▶ | if (ctx->tok.tok == RBRACKET) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == RBRACKET) break; |
0 |
8 |
8 |
|
| 1001 |
|
| |
| 1002 |
8 |
| switch (ctx->tok.tok) { |
| 1003 |
6 |
| case STRING: |
| 1004 |
|
| { |
| 1005 |
|
| /* set array kind if this will be the first entry */ |
| 1006 |
6 |
▶ | if (arr->kind == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->kind == 0) |
3 |
3 |
6 |
|
| 1007 |
3 |
| arr->kind = 'v'; |
| 1008 |
3 |
▶ | else if (arr->kind != 'v') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (arr->kind != 'v') |
0 |
3 |
3 |
|
| 1009 |
0 |
| arr->kind = 'm'; |
| 1010 |
|
| |
| 1011 |
6 |
| char* val = ctx->tok.ptr; |
| 1012 |
6 |
| int vlen = ctx->tok.len; |
| 1013 |
|
| |
| 1014 |
|
| /* make a new value in array */ |
| 1015 |
6 |
| toml_arritem_t* newval = create_value_in_array(ctx, arr); |
| 1016 |
6 |
▶ | if (!newval) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!newval) |
0 |
6 |
6 |
|
| 1017 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1018 |
|
| |
| 1019 |
6 |
▶ | if (! (newval->val = STRNDUP(val, vlen))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! (newval->val = STRNDUP(val, vlen))) |
0 |
6 |
6 |
|
| 1020 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1021 |
|
| |
| 1022 |
6 |
| newval->valtype = valtype(newval->val); |
| 1023 |
|
| |
| 1024 |
|
| /* set array type if this is the first entry */ |
| 1025 |
6 |
▶ | if (arr->nitem == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->nitem == 1) |
3 |
3 |
6 |
|
| 1026 |
3 |
| arr->type = newval->valtype; |
| 1027 |
3 |
▶ | else if (arr->type != newval->valtype) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (arr->type != newval->valtype) |
0 |
3 |
3 |
|
| 1028 |
0 |
| arr->type = 'm'; /* mixed */ |
| 1029 |
|
| |
| 1030 |
6 |
▶ | if (eat_token(ctx, STRING, 0, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, STRING, 0, FLINE)) return -1; |
0 |
6 |
6 |
|
| 1031 |
6 |
| break; |
| 1032 |
|
| } |
| 1033 |
|
| |
| 1034 |
2 |
| case LBRACKET: |
| 1035 |
|
| { /* [ [array], [array] ... ] */ |
| 1036 |
|
| /* set the array kind if this will be the first entry */ |
| 1037 |
2 |
▶ | if (arr->kind == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->kind == 0) |
1 |
1 |
2 |
|
| 1038 |
1 |
| arr->kind = 'a'; |
| 1039 |
1 |
▶ | else if (arr->kind != 'a') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (arr->kind != 'a') |
0 |
1 |
1 |
|
| 1040 |
0 |
| arr->kind = 'm'; |
| 1041 |
|
| |
| 1042 |
2 |
| toml_array_t* subarr = create_array_in_array(ctx, arr); |
| 1043 |
2 |
▶ | if (!subarr) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!subarr) return -1; |
0 |
2 |
2 |
|
| 1044 |
2 |
▶ | if (parse_array(ctx, subarr)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_array(ctx, subarr)) return -1; |
0 |
2 |
2 |
|
| 1045 |
2 |
| break; |
| 1046 |
|
| } |
| 1047 |
|
| |
| 1048 |
0 |
| case LBRACE: |
| 1049 |
|
| { /* [ {table}, {table} ... ] */ |
| 1050 |
|
| /* set the array kind if this will be the first entry */ |
| 1051 |
0 |
▶ | if (arr->kind == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->kind == 0) |
0 |
0 |
0 |
|
| 1052 |
0 |
| arr->kind = 't'; |
| 1053 |
0 |
▶ | else if (arr->kind != 't') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (arr->kind != 't') |
0 |
0 |
0 |
|
| 1054 |
0 |
| arr->kind = 'm'; |
| 1055 |
|
| |
| 1056 |
0 |
| toml_table_t* subtab = create_table_in_array(ctx, arr); |
| 1057 |
0 |
▶ | if (!subtab) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!subtab) return -1; |
0 |
0 |
0 |
|
| 1058 |
0 |
▶ | if (parse_inline_table(ctx, subtab)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_inline_table(ctx, subtab)) return -1; |
0 |
0 |
0 |
|
| 1059 |
0 |
| break; |
| 1060 |
|
| } |
| 1061 |
|
| |
| 1062 |
0 |
| default: |
| 1063 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "syntax error"); |
| 1064 |
|
| } |
| 1065 |
|
| |
| 1066 |
8 |
▶ | if (skip_newlines(ctx, 0)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (skip_newlines(ctx, 0)) return -1; |
0 |
8 |
8 |
|
| 1067 |
|
| |
| 1068 |
|
| /* on comma, continue to scan for next element */ |
| 1069 |
8 |
▶ | if (ctx->tok.tok == COMMA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == COMMA) { |
4 |
4 |
8 |
|
| 1070 |
4 |
▶ | if (eat_token(ctx, COMMA, 0, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, COMMA, 0, FLINE)) return -1; |
0 |
4 |
4 |
|
| 1071 |
4 |
| continue; |
| 1072 |
|
| } |
| 1073 |
4 |
| break; |
| 1074 |
|
| } |
| 1075 |
|
| |
| 1076 |
4 |
▶ | if (eat_token(ctx, RBRACKET, 1, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, RBRACKET, 1, FLINE)) return -1; |
0 |
4 |
4 |
|
| 1077 |
4 |
| return 0; |
| 1078 |
|
| } |
| 1079 |
|
| |
| 1080 |
|
| |
| 1081 |
|
| /* handle lines like these: |
| 1082 |
|
| key = "value" |
| 1083 |
|
| key = [ array ] |
| 1084 |
|
| key = { table } |
| 1085 |
|
| */ |
| 1086 |
15 |
| static int parse_keyval(context_t* ctx, toml_table_t* tab) |
| 1087 |
|
| { |
| 1088 |
15 |
▶ | if (tab->readonly) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tab->readonly) { |
0 |
15 |
15 |
|
| 1089 |
0 |
| return e_forbid(ctx, ctx->tok.lineno, "cannot insert new entry into existing table"); |
| 1090 |
|
| } |
| 1091 |
|
| |
| 1092 |
15 |
| token_t key = ctx->tok; |
| 1093 |
15 |
▶ | if (eat_token(ctx, STRING, 1, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, STRING, 1, FLINE)) return -1; |
0 |
15 |
15 |
|
| 1094 |
|
| |
| 1095 |
15 |
▶ | if (ctx->tok.tok == DOT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == DOT) { |
0 |
15 |
15 |
|
| 1096 |
|
| /* handle inline dotted key. |
| 1097 |
|
| e.g. |
| 1098 |
|
| physical.color = "orange" |
| 1099 |
|
| physical.shape = "round" |
| 1100 |
|
| */ |
| 1101 |
0 |
| toml_table_t* subtab = 0; |
| 1102 |
|
| { |
| 1103 |
0 |
| char* subtabstr = normalize_key(ctx, key); |
| 1104 |
0 |
▶ | if (!subtabstr) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!subtabstr) return -1; |
0 |
0 |
0 |
|
| 1105 |
|
| |
| 1106 |
0 |
| subtab = toml_table_in(tab, subtabstr); |
| 1107 |
0 |
| xfree(subtabstr); |
| 1108 |
|
| } |
| 1109 |
0 |
▶ | if (!subtab) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!subtab) { |
0 |
0 |
0 |
|
| 1110 |
0 |
| subtab = create_keytable_in_table(ctx, tab, key); |
| 1111 |
0 |
▶ | if (!subtab) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!subtab) return -1; |
0 |
0 |
0 |
|
| 1112 |
|
| } |
| 1113 |
0 |
▶ | if (next_token(ctx, 1)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, 1)) return -1; |
0 |
0 |
0 |
|
| 1114 |
0 |
▶ | if (parse_keyval(ctx, subtab)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_keyval(ctx, subtab)) return -1; |
0 |
0 |
0 |
|
| 1115 |
0 |
| return 0; |
| 1116 |
|
| } |
| 1117 |
|
| |
| 1118 |
15 |
▶ | if (ctx->tok.tok != EQUAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != EQUAL) { |
1 |
14 |
15 |
|
| 1119 |
1 |
| return e_syntax(ctx, ctx->tok.lineno, "missing ="); |
| 1120 |
|
| } |
| 1121 |
|
| |
| 1122 |
14 |
▶ | if (next_token(ctx, 0)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, 0)) return -1; |
0 |
14 |
14 |
|
| 1123 |
|
| |
| 1124 |
14 |
| switch (ctx->tok.tok) { |
| 1125 |
11 |
| case STRING: |
| 1126 |
|
| { /* key = "value" */ |
| 1127 |
11 |
| toml_keyval_t* keyval = create_keyval_in_table(ctx, tab, key); |
| 1128 |
11 |
▶ | if (!keyval) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!keyval) return -1; |
0 |
11 |
11 |
|
| 1129 |
11 |
| token_t val = ctx->tok; |
| 1130 |
|
| |
| 1131 |
11 |
▶ | assert(keyval->val == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(keyval->val == 0); |
0 |
11 |
11 |
|
| 1132 |
11 |
▶ | if (! (keyval->val = STRNDUP(val.ptr, val.len))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! (keyval->val = STRNDUP(val.ptr, val.len))) |
0 |
11 |
11 |
|
| 1133 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1134 |
|
| |
| 1135 |
11 |
▶ | if (next_token(ctx, 1)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, 1)) return -1; |
0 |
11 |
11 |
|
| 1136 |
|
| |
| 1137 |
11 |
| return 0; |
| 1138 |
|
| } |
| 1139 |
|
| |
| 1140 |
2 |
| case LBRACKET: |
| 1141 |
|
| { /* key = [ array ] */ |
| 1142 |
2 |
| toml_array_t* arr = create_keyarray_in_table(ctx, tab, key, 0); |
| 1143 |
2 |
▶ | if (!arr) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return -1; |
0 |
2 |
2 |
|
| 1144 |
2 |
▶ | if (parse_array(ctx, arr)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_array(ctx, arr)) return -1; |
0 |
2 |
2 |
|
| 1145 |
2 |
| return 0; |
| 1146 |
|
| } |
| 1147 |
|
| |
| 1148 |
1 |
| case LBRACE: |
| 1149 |
|
| { /* key = { table } */ |
| 1150 |
1 |
| toml_table_t* nxttab = create_keytable_in_table(ctx, tab, key); |
| 1151 |
1 |
▶ | if (!nxttab) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nxttab) return -1; |
0 |
1 |
1 |
|
| 1152 |
1 |
▶ | if (parse_inline_table(ctx, nxttab)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_inline_table(ctx, nxttab)) return -1; |
0 |
1 |
1 |
|
| 1153 |
1 |
| return 0; |
| 1154 |
|
| } |
| 1155 |
|
| |
| 1156 |
0 |
| default: |
| 1157 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "syntax error"); |
| 1158 |
|
| } |
| 1159 |
|
| return 0; |
| 1160 |
|
| } |
| 1161 |
|
| |
| 1162 |
|
| |
| 1163 |
|
| typedef struct tabpath_t tabpath_t; |
| 1164 |
|
| struct tabpath_t { |
| 1165 |
|
| int cnt; |
| 1166 |
|
| token_t key[10]; |
| 1167 |
|
| }; |
| 1168 |
|
| |
| 1169 |
|
| /* at [x.y.z] or [[x.y.z]] |
| 1170 |
|
| * Scan forward and fill tabpath until it enters ] or ]] |
| 1171 |
|
| * There will be at least one entry on return. |
| 1172 |
|
| */ |
| 1173 |
6 |
| static int fill_tabpath(context_t* ctx) |
| 1174 |
|
| { |
| 1175 |
6 |
| int lineno = ctx->tok.lineno; |
| 1176 |
|
| int i; |
| 1177 |
|
| |
| 1178 |
|
| /* clear tpath */ |
| 1179 |
8 |
▶ | for (i = 0; i < ctx->tpath.top; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->tpath.top; i++) { |
2 |
6 |
8 |
|
| 1180 |
2 |
| char** p = &ctx->tpath.key[i]; |
| 1181 |
2 |
| xfree(*p); |
| 1182 |
2 |
| *p = 0; |
| 1183 |
|
| } |
| 1184 |
6 |
| ctx->tpath.top = 0; |
| 1185 |
|
| |
| 1186 |
2 |
| for (;;) { |
| 1187 |
8 |
▶ | if (ctx->tpath.top >= 10) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tpath.top >= 10) |
0 |
8 |
8 |
|
| 1188 |
0 |
| return e_syntax(ctx, lineno, "table path is too deep; max allowed is 10."); |
| 1189 |
|
| |
| 1190 |
8 |
▶ | if (ctx->tok.tok != STRING) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != STRING) |
0 |
8 |
8 |
|
| 1191 |
0 |
| return e_syntax(ctx, lineno, "invalid or missing key"); |
| 1192 |
|
| |
| 1193 |
8 |
| char* key = normalize_key(ctx, ctx->tok); |
| 1194 |
8 |
▶ | if (!key) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) return -1; |
0 |
8 |
8 |
|
| 1195 |
8 |
| ctx->tpath.tok[ctx->tpath.top] = ctx->tok; |
| 1196 |
8 |
| ctx->tpath.key[ctx->tpath.top] = key; |
| 1197 |
8 |
| ctx->tpath.top++; |
| 1198 |
|
| |
| 1199 |
8 |
▶ | if (next_token(ctx, 1)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, 1)) return -1; |
0 |
8 |
8 |
|
| 1200 |
|
| |
| 1201 |
8 |
▶ | if (ctx->tok.tok == RBRACKET) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok == RBRACKET) break; |
6 |
2 |
8 |
|
| 1202 |
|
| |
| 1203 |
2 |
▶ | if (ctx->tok.tok != DOT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != DOT) |
0 |
2 |
2 |
|
| 1204 |
0 |
| return e_syntax(ctx, lineno, "invalid key"); |
| 1205 |
|
| |
| 1206 |
2 |
▶ | if (next_token(ctx, 1)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(ctx, 1)) return -1; |
0 |
2 |
2 |
|
| 1207 |
|
| } |
| 1208 |
|
| |
| 1209 |
6 |
▶ | if (ctx->tpath.top <= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tpath.top <= 0) |
0 |
6 |
6 |
|
| 1210 |
0 |
| return e_syntax(ctx, lineno, "empty table selector"); |
| 1211 |
|
| |
| 1212 |
6 |
| return 0; |
| 1213 |
|
| } |
| 1214 |
|
| |
| 1215 |
|
| |
| 1216 |
|
| /* Walk tabpath from the root, and create new tables on the way. |
| 1217 |
|
| * Sets ctx->curtab to the final table. |
| 1218 |
|
| */ |
| 1219 |
6 |
| static int walk_tabpath(context_t* ctx) |
| 1220 |
|
| { |
| 1221 |
|
| /* start from root */ |
| 1222 |
6 |
| toml_table_t* curtab = ctx->root; |
| 1223 |
|
| |
| 1224 |
8 |
▶ | for (int i = 0; i < ctx->tpath.top; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < ctx->tpath.top; i++) { |
2 |
6 |
8 |
|
| 1225 |
2 |
| const char* key = ctx->tpath.key[i]; |
| 1226 |
|
| |
| 1227 |
2 |
| toml_keyval_t* nextval = 0; |
| 1228 |
2 |
| toml_array_t* nextarr = 0; |
| 1229 |
2 |
| toml_table_t* nexttab = 0; |
| 1230 |
2 |
| switch (check_key(curtab, key, &nextval, &nextarr, &nexttab)) { |
| 1231 |
2 |
| case 't': |
| 1232 |
|
| /* found a table. nexttab is where we will go next. */ |
| 1233 |
2 |
| break; |
| 1234 |
|
| |
| 1235 |
0 |
| case 'a': |
| 1236 |
|
| /* found an array. nexttab is the last table in the array. */ |
| 1237 |
0 |
▶ | if (nextarr->kind != 't') |
Branch: 1 branch
| Branch | True | False | Total |
| if (nextarr->kind != 't') |
0 |
0 |
0 |
|
| 1238 |
0 |
| return e_internal(ctx, FLINE); |
| 1239 |
|
| |
| 1240 |
0 |
▶ | if (nextarr->nitem == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nextarr->nitem == 0) |
0 |
0 |
0 |
|
| 1241 |
0 |
| return e_internal(ctx, FLINE); |
| 1242 |
|
| |
| 1243 |
0 |
| nexttab = nextarr->item[nextarr->nitem-1].tab; |
| 1244 |
0 |
| break; |
| 1245 |
|
| |
| 1246 |
0 |
| case 'v': |
| 1247 |
0 |
| return e_keyexists(ctx, ctx->tpath.tok[i].lineno); |
| 1248 |
|
| |
| 1249 |
0 |
| default: |
| 1250 |
|
| { /* Not found. Let's create an implicit table. */ |
| 1251 |
0 |
| int n = curtab->ntab; |
| 1252 |
0 |
| toml_table_t** base = (toml_table_t**) expand_ptrarr((void**)curtab->tab, n); |
| 1253 |
0 |
▶ | if (0 == base) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == base) |
0 |
0 |
0 |
|
| 1254 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1255 |
|
| |
| 1256 |
0 |
| curtab->tab = base; |
| 1257 |
|
| |
| 1258 |
0 |
▶ | if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) |
0 |
0 |
0 |
|
| 1259 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1260 |
|
| |
| 1261 |
0 |
▶ | if (0 == (base[n]->key = STRDUP(key))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (base[n]->key = STRDUP(key))) |
0 |
0 |
0 |
|
| 1262 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1263 |
|
| |
| 1264 |
0 |
| nexttab = curtab->tab[curtab->ntab++]; |
| 1265 |
|
| |
| 1266 |
|
| /* tabs created by walk_tabpath are considered implicit */ |
| 1267 |
0 |
| nexttab->implicit = true; |
| 1268 |
|
| } |
| 1269 |
0 |
| break; |
| 1270 |
|
| } |
| 1271 |
|
| |
| 1272 |
|
| /* switch to next tab */ |
| 1273 |
2 |
| curtab = nexttab; |
| 1274 |
|
| } |
| 1275 |
|
| |
| 1276 |
|
| /* save it */ |
| 1277 |
6 |
| ctx->curtab = curtab; |
| 1278 |
|
| |
| 1279 |
6 |
| return 0; |
| 1280 |
|
| } |
| 1281 |
|
| |
| 1282 |
|
| |
| 1283 |
|
| /* handle lines like [x.y.z] or [[x.y.z]] */ |
| 1284 |
6 |
| static int parse_select(context_t* ctx) |
| 1285 |
|
| { |
| 1286 |
6 |
▶ | assert(ctx->tok.tok == LBRACKET); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ctx->tok.tok == LBRACKET); |
0 |
6 |
6 |
|
| 1287 |
|
| |
| 1288 |
|
| /* true if [[ */ |
| 1289 |
6 |
▶ | int llb = (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == '['); |
Branch: 2 branches
| Branch | True | False | Total |
| int llb = (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == '['); |
6 |
0 |
6 |
| int llb = (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == '['); |
0 |
6 |
6 |
|
| 1290 |
|
| /* need to detect '[[' on our own because next_token() will skip whitespace, |
| 1291 |
|
| and '[ [' would be taken as '[[', which is wrong. */ |
| 1292 |
|
| |
| 1293 |
|
| /* eat [ or [[ */ |
| 1294 |
6 |
▶ | if (eat_token(ctx, LBRACKET, 1, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, LBRACKET, 1, FLINE)) return -1; |
0 |
6 |
6 |
|
| 1295 |
6 |
▶ | if (llb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (llb) { |
0 |
6 |
6 |
|
| 1296 |
0 |
▶ | assert(ctx->tok.tok == LBRACKET); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ctx->tok.tok == LBRACKET); |
0 |
0 |
0 |
|
| 1297 |
0 |
▶ | if (eat_token(ctx, LBRACKET, 1, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, LBRACKET, 1, FLINE)) return -1; |
0 |
0 |
0 |
|
| 1298 |
|
| } |
| 1299 |
|
| |
| 1300 |
6 |
▶ | if (fill_tabpath(ctx)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fill_tabpath(ctx)) return -1; |
0 |
6 |
6 |
|
| 1301 |
|
| |
| 1302 |
|
| /* For [x.y.z] or [[x.y.z]], remove z from tpath. |
| 1303 |
|
| */ |
| 1304 |
6 |
| token_t z = ctx->tpath.tok[ctx->tpath.top-1]; |
| 1305 |
6 |
| xfree(ctx->tpath.key[ctx->tpath.top-1]); |
| 1306 |
6 |
| ctx->tpath.top--; |
| 1307 |
|
| |
| 1308 |
|
| /* set up ctx->curtab */ |
| 1309 |
6 |
▶ | if (walk_tabpath(ctx)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (walk_tabpath(ctx)) return -1; |
0 |
6 |
6 |
|
| 1310 |
|
| |
| 1311 |
6 |
▶ | if (! llb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! llb) { |
6 |
0 |
6 |
|
| 1312 |
|
| /* [x.y.z] -> create z = {} in x.y */ |
| 1313 |
6 |
| toml_table_t* curtab = create_keytable_in_table(ctx, ctx->curtab, z); |
| 1314 |
6 |
▶ | if (!curtab) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!curtab) return -1; |
0 |
6 |
6 |
|
| 1315 |
6 |
| ctx->curtab = curtab; |
| 1316 |
|
| } else { |
| 1317 |
|
| /* [[x.y.z]] -> create z = [] in x.y */ |
| 1318 |
0 |
| toml_array_t* arr = 0; |
| 1319 |
|
| { |
| 1320 |
0 |
| char* zstr = normalize_key(ctx, z); |
| 1321 |
0 |
▶ | if (!zstr) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!zstr) return -1; |
0 |
0 |
0 |
|
| 1322 |
0 |
| arr = toml_array_in(ctx->curtab, zstr); |
| 1323 |
0 |
| xfree(zstr); |
| 1324 |
|
| } |
| 1325 |
0 |
▶ | if (!arr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) { |
0 |
0 |
0 |
|
| 1326 |
0 |
| arr = create_keyarray_in_table(ctx, ctx->curtab, z, 't'); |
| 1327 |
0 |
▶ | if (!arr) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return -1; |
0 |
0 |
0 |
|
| 1328 |
|
| } |
| 1329 |
0 |
▶ | if (arr->kind != 't') |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->kind != 't') |
0 |
0 |
0 |
|
| 1330 |
0 |
| return e_syntax(ctx, z.lineno, "array mismatch"); |
| 1331 |
|
| |
| 1332 |
|
| /* add to z[] */ |
| 1333 |
|
| toml_table_t* dest; |
| 1334 |
|
| { |
| 1335 |
0 |
| toml_table_t* t = create_table_in_array(ctx, arr); |
| 1336 |
0 |
▶ | if (!t) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return -1; |
0 |
0 |
0 |
|
| 1337 |
|
| |
| 1338 |
0 |
▶ | if (0 == (t->key = STRDUP("__anon__"))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (t->key = STRDUP("__anon__"))) |
0 |
0 |
0 |
|
| 1339 |
0 |
| return e_outofmemory(ctx, FLINE); |
| 1340 |
|
| |
| 1341 |
0 |
| dest = t; |
| 1342 |
|
| } |
| 1343 |
|
| |
| 1344 |
0 |
| ctx->curtab = dest; |
| 1345 |
|
| } |
| 1346 |
|
| |
| 1347 |
6 |
▶ | if (ctx->tok.tok != RBRACKET) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != RBRACKET) { |
0 |
6 |
6 |
|
| 1348 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "expects ]"); |
| 1349 |
|
| } |
| 1350 |
6 |
▶ | if (llb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (llb) { |
0 |
6 |
6 |
|
| 1351 |
0 |
▶ | if (! (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == ']')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == ']')) { |
0 |
0 |
0 |
| if (! (ctx->tok.ptr + 1 < ctx->stop && ctx->tok.ptr[1] == ']')) { |
0 |
0 |
0 |
|
| 1352 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "expects ]]"); |
| 1353 |
|
| } |
| 1354 |
0 |
▶ | if (eat_token(ctx, RBRACKET, 1, FLINE)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, RBRACKET, 1, FLINE)) return -1; |
0 |
0 |
0 |
|
| 1355 |
|
| } |
| 1356 |
|
| |
| 1357 |
6 |
▶ | if (eat_token(ctx, RBRACKET, 1, FLINE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(ctx, RBRACKET, 1, FLINE)) |
0 |
6 |
6 |
|
| 1358 |
0 |
| return -1; |
| 1359 |
|
| |
| 1360 |
6 |
▶ | if (ctx->tok.tok != NEWLINE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->tok.tok != NEWLINE) |
0 |
6 |
6 |
|
| 1361 |
0 |
| return e_syntax(ctx, ctx->tok.lineno, "extra chars after ] or ]]"); |
| 1362 |
|
| |
| 1363 |
6 |
| return 0; |
| 1364 |
|
| } |
| 1365 |
|
| |
| 1366 |
|
| |
| 1367 |
|
| |
| 1368 |
|
| |
| 1369 |
2 |
| toml_table_t* toml_parse(char* conf, |
| 1370 |
|
| char* errbuf, |
| 1371 |
|
| int errbufsz) |
| 1372 |
|
| { |
| 1373 |
|
| context_t ctx; |
| 1374 |
|
| |
| 1375 |
|
| // clear errbuf |
| 1376 |
2 |
▶ | if (errbufsz <= 0) errbufsz = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbufsz <= 0) errbufsz = 0; |
0 |
2 |
2 |
|
| 1377 |
2 |
▶ | if (errbufsz > 0) errbuf[0] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errbufsz > 0) errbuf[0] = 0; |
2 |
0 |
2 |
|
| 1378 |
|
| |
| 1379 |
|
| // init context |
| 1380 |
2 |
| memset(&ctx, 0, sizeof(ctx)); |
| 1381 |
2 |
| ctx.start = conf; |
| 1382 |
2 |
| ctx.stop = ctx.start + strlen(conf); |
| 1383 |
2 |
| ctx.errbuf = errbuf; |
| 1384 |
2 |
| ctx.errbufsz = errbufsz; |
| 1385 |
|
| |
| 1386 |
|
| // start with an artificial newline of length 0 |
| 1387 |
2 |
| ctx.tok.tok = NEWLINE; |
| 1388 |
2 |
| ctx.tok.lineno = 1; |
| 1389 |
2 |
| ctx.tok.ptr = conf; |
| 1390 |
2 |
| ctx.tok.len = 0; |
| 1391 |
|
| |
| 1392 |
|
| // make a root table |
| 1393 |
2 |
▶ | if (0 == (ctx.root = CALLOC(1, sizeof(*ctx.root)))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == (ctx.root = CALLOC(1, sizeof(*ctx.root)))) { |
0 |
2 |
2 |
|
| 1394 |
0 |
| e_outofmemory(&ctx, FLINE); |
| 1395 |
|
| // Do not goto fail, root table not set up yet |
| 1396 |
0 |
| return 0; |
| 1397 |
|
| } |
| 1398 |
|
| |
| 1399 |
|
| // set root as default table |
| 1400 |
2 |
| ctx.curtab = ctx.root; |
| 1401 |
|
| |
| 1402 |
|
| /* Scan forward until EOF */ |
| 1403 |
36 |
▶ | for (token_t tok = ctx.tok; ! tok.eof ; tok = ctx.tok) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (token_t tok = ctx.tok; ! tok.eof ; tok = ctx.tok) { |
35 |
1 |
36 |
|
| 1404 |
35 |
| switch (tok.tok) { |
| 1405 |
|
| |
| 1406 |
16 |
| case NEWLINE: |
| 1407 |
17 |
▶ | if (next_token(&ctx, 1)) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_token(&ctx, 1)) goto fail; |
0 |
16 |
16 |
|
| 1408 |
16 |
| break; |
| 1409 |
|
| |
| 1410 |
13 |
| case STRING: |
| 1411 |
13 |
▶ | if (parse_keyval(&ctx, ctx.curtab)) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_keyval(&ctx, ctx.curtab)) goto fail; |
1 |
12 |
13 |
|
| 1412 |
|
| |
| 1413 |
12 |
▶ | if (ctx.tok.tok != NEWLINE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.tok.tok != NEWLINE) { |
0 |
12 |
12 |
|
| 1414 |
0 |
| e_syntax(&ctx, ctx.tok.lineno, "extra chars after value"); |
| 1415 |
0 |
| goto fail; |
| 1416 |
|
| } |
| 1417 |
|
| |
| 1418 |
12 |
▶ | if (eat_token(&ctx, NEWLINE, 1, FLINE)) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eat_token(&ctx, NEWLINE, 1, FLINE)) goto fail; |
0 |
12 |
12 |
|
| 1419 |
12 |
| break; |
| 1420 |
|
| |
| 1421 |
6 |
| case LBRACKET: /* [ x.y.z ] or [[ x.y.z ]] */ |
| 1422 |
6 |
▶ | if (parse_select(&ctx)) goto fail; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_select(&ctx)) goto fail; |
0 |
6 |
6 |
|
| 1423 |
6 |
| break; |
| 1424 |
|
| |
| 1425 |
0 |
| default: |
| 1426 |
0 |
| e_syntax(&ctx, tok.lineno, "syntax error"); |
| 1427 |
0 |
| goto fail; |
| 1428 |
|
| } |
| 1429 |
|
| } |
| 1430 |
|
| |
| 1431 |
|
| /* success */ |
| 1432 |
1 |
▶ | for (int i = 0; i < ctx.tpath.top; i++) xfree(ctx.tpath.key[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < ctx.tpath.top; i++) xfree(ctx.tpath.key[i]); |
0 |
1 |
1 |
|
| 1433 |
1 |
| return ctx.root; |
| 1434 |
|
| |
| 1435 |
1 |
| fail: |
| 1436 |
|
| // Something bad has happened. Free resources and return error. |
| 1437 |
1 |
▶ | for (int i = 0; i < ctx.tpath.top; i++) xfree(ctx.tpath.key[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < ctx.tpath.top; i++) xfree(ctx.tpath.key[i]); |
0 |
1 |
1 |
|
| 1438 |
1 |
| toml_free(ctx.root); |
| 1439 |
1 |
| return 0; |
| 1440 |
|
| } |
| 1441 |
|
| |
| 1442 |
|
| |
| 1443 |
0 |
| toml_table_t* toml_parse_file(FILE* fp, |
| 1444 |
|
| char* errbuf, |
| 1445 |
|
| int errbufsz) |
| 1446 |
|
| { |
| 1447 |
0 |
| int bufsz = 0; |
| 1448 |
0 |
| char* buf = 0; |
| 1449 |
0 |
| int off = 0; |
| 1450 |
|
| |
| 1451 |
|
| /* read from fp into buf */ |
| 1452 |
0 |
▶ | while (! feof(fp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (! feof(fp)) { |
0 |
0 |
0 |
|
| 1453 |
|
| |
| 1454 |
0 |
▶ | if (off == bufsz) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (off == bufsz) { |
0 |
0 |
0 |
|
| 1455 |
0 |
| int xsz = bufsz + 1000; |
| 1456 |
0 |
| char* x = expand(buf, bufsz, xsz); |
| 1457 |
0 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
0 |
0 |
0 |
|
| 1458 |
0 |
| snprintf(errbuf, errbufsz, "out of memory"); |
| 1459 |
0 |
| xfree(buf); |
| 1460 |
0 |
| return 0; |
| 1461 |
|
| } |
| 1462 |
0 |
| buf = x; |
| 1463 |
0 |
| bufsz = xsz; |
| 1464 |
|
| } |
| 1465 |
|
| |
| 1466 |
0 |
| errno = 0; |
| 1467 |
0 |
| int n = fread(buf + off, 1, bufsz - off, fp); |
| 1468 |
0 |
▶ | if (ferror(fp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ferror(fp)) { |
0 |
0 |
0 |
|
| 1469 |
0 |
| snprintf(errbuf, errbufsz, "%s", |
| 1470 |
0 |
▶ | errno ? strerror(errno) : "Error reading file"); |
Branch: 1 branch
| Branch | True | False | Total |
| errno ? strerror(errno) : "Error reading file"); |
0 |
0 |
0 |
|
| 1471 |
0 |
| xfree(buf); |
| 1472 |
0 |
| return 0; |
| 1473 |
|
| } |
| 1474 |
0 |
| off += n; |
| 1475 |
|
| } |
| 1476 |
|
| |
| 1477 |
|
| /* tag on a NUL to cap the string */ |
| 1478 |
0 |
▶ | if (off == bufsz) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (off == bufsz) { |
0 |
0 |
0 |
|
| 1479 |
0 |
| int xsz = bufsz + 1; |
| 1480 |
0 |
| char* x = expand(buf, bufsz, xsz); |
| 1481 |
0 |
▶ | if (!x) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!x) { |
0 |
0 |
0 |
|
| 1482 |
0 |
| snprintf(errbuf, errbufsz, "out of memory"); |
| 1483 |
0 |
| xfree(buf); |
| 1484 |
0 |
| return 0; |
| 1485 |
|
| } |
| 1486 |
0 |
| buf = x; |
| 1487 |
0 |
| bufsz = xsz; |
| 1488 |
|
| } |
| 1489 |
0 |
| buf[off] = 0; |
| 1490 |
|
| |
| 1491 |
|
| /* parse it, cleanup and finish */ |
| 1492 |
0 |
| toml_table_t* ret = toml_parse(buf, errbuf, errbufsz); |
| 1493 |
0 |
| xfree(buf); |
| 1494 |
0 |
| return ret; |
| 1495 |
|
| } |
| 1496 |
|
| |
| 1497 |
|
| |
| 1498 |
11 |
| static void xfree_kval(toml_keyval_t* p) |
| 1499 |
|
| { |
| 1500 |
11 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
11 |
11 |
|
| 1501 |
11 |
| xfree(p->key); |
| 1502 |
11 |
| xfree(p->val); |
| 1503 |
11 |
| xfree(p); |
| 1504 |
|
| } |
| 1505 |
|
| |
| 1506 |
|
| static void xfree_tab(toml_table_t* p); |
| 1507 |
|
| |
| 1508 |
4 |
| static void xfree_arr(toml_array_t* p) |
| 1509 |
|
| { |
| 1510 |
4 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
4 |
4 |
|
| 1511 |
|
| |
| 1512 |
4 |
| xfree(p->key); |
| 1513 |
4 |
| const int n = p->nitem; |
| 1514 |
12 |
▶ | for (int i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < n; i++) { |
8 |
4 |
12 |
|
| 1515 |
8 |
| toml_arritem_t* a = &p->item[i]; |
| 1516 |
8 |
▶ | if (a->val) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->val) |
6 |
2 |
8 |
|
| 1517 |
6 |
| xfree(a->val); |
| 1518 |
2 |
▶ | else if (a->arr) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->arr) |
2 |
0 |
2 |
|
| 1519 |
2 |
| xfree_arr(a->arr); |
| 1520 |
0 |
▶ | else if (a->tab) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (a->tab) |
0 |
0 |
0 |
|
| 1521 |
0 |
| xfree_tab(a->tab); |
| 1522 |
|
| } |
| 1523 |
4 |
| xfree(p->item); |
| 1524 |
4 |
| xfree(p); |
| 1525 |
|
| } |
| 1526 |
|
| |
| 1527 |
|
| |
| 1528 |
9 |
| static void xfree_tab(toml_table_t* p) |
| 1529 |
|
| { |
| 1530 |
|
| int i; |
| 1531 |
|
| |
| 1532 |
9 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
9 |
9 |
|
| 1533 |
|
| |
| 1534 |
9 |
| xfree(p->key); |
| 1535 |
|
| |
| 1536 |
20 |
▶ | for (i = 0; i < p->nkval; i++) xfree_kval(p->kval[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < p->nkval; i++) xfree_kval(p->kval[i]); |
11 |
9 |
20 |
|
| 1537 |
9 |
| xfree(p->kval); |
| 1538 |
|
| |
| 1539 |
11 |
▶ | for (i = 0; i < p->narr; i++) xfree_arr(p->arr[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < p->narr; i++) xfree_arr(p->arr[i]); |
2 |
9 |
11 |
|
| 1540 |
9 |
| xfree(p->arr); |
| 1541 |
|
| |
| 1542 |
16 |
▶ | for (i = 0; i < p->ntab; i++) xfree_tab(p->tab[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < p->ntab; i++) xfree_tab(p->tab[i]); |
7 |
9 |
16 |
|
| 1543 |
9 |
| xfree(p->tab); |
| 1544 |
|
| |
| 1545 |
9 |
| xfree(p); |
| 1546 |
|
| } |
| 1547 |
|
| |
| 1548 |
|
| |
| 1549 |
2 |
| void toml_free(toml_table_t* tab) |
| 1550 |
|
| { |
| 1551 |
2 |
| xfree_tab(tab); |
| 1552 |
2 |
| } |
| 1553 |
|
| |
| 1554 |
|
| |
| 1555 |
111 |
| static void set_token(context_t* ctx, tokentype_t tok, int lineno, char* ptr, int len) |
| 1556 |
|
| { |
| 1557 |
|
| token_t t; |
| 1558 |
111 |
| t.tok = tok; |
| 1559 |
111 |
| t.lineno = lineno; |
| 1560 |
111 |
| t.ptr = ptr; |
| 1561 |
111 |
| t.len = len; |
| 1562 |
111 |
| t.eof = 0; |
| 1563 |
111 |
| ctx->tok = t; |
| 1564 |
111 |
| } |
| 1565 |
|
| |
| 1566 |
2 |
| static void set_eof(context_t* ctx, int lineno) |
| 1567 |
|
| { |
| 1568 |
2 |
| set_token(ctx, NEWLINE, lineno, ctx->stop, 0); |
| 1569 |
2 |
| ctx->tok.eof = 1; |
| 1570 |
2 |
| } |
| 1571 |
|
| |
| 1572 |
|
| |
| 1573 |
|
| /* Scan p for n digits compositing entirely of [0-9] */ |
| 1574 |
69 |
| static int scan_digits(const char* p, int n) |
| 1575 |
|
| { |
| 1576 |
69 |
| int ret = 0; |
| 1577 |
119 |
▶ | for ( ; n > 0 && isdigit(*p); n--, p++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for ( ; n > 0 && isdigit(*p); n--, p++) { |
102 |
17 |
119 |
| for ( ; n > 0 && isdigit(*p); n--, p++) { |
50 |
52 |
102 |
|
| 1578 |
50 |
| ret = 10 * ret + (*p - '0'); |
| 1579 |
|
| } |
| 1580 |
69 |
▶ | return n ? -1 : ret; |
Branch: 1 branch
| Branch | True | False | Total |
| return n ? -1 : ret; |
17 |
52 |
69 |
|
| 1581 |
|
| } |
| 1582 |
|
| |
| 1583 |
32 |
| static int scan_date(const char* p, int* YY, int* MM, int* DD) |
| 1584 |
|
| { |
| 1585 |
|
| int year, month, day; |
| 1586 |
32 |
| year = scan_digits(p, 4); |
| 1587 |
32 |
▶ | month = (year >= 0 && p[4] == '-') ? scan_digits(p+5, 2) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| month = (year >= 0 && p[4] == '-') ? scan_digits(p+5, 2) : -1; |
5 |
27 |
32 |
| month = (year >= 0 && p[4] == '-') ? scan_digits(p+5, 2) : -1; |
2 |
3 |
5 |
|
| 1588 |
32 |
▶ | day = (month >= 0 && p[7] == '-') ? scan_digits(p+8, 2) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| day = (month >= 0 && p[7] == '-') ? scan_digits(p+8, 2) : -1; |
2 |
30 |
32 |
| day = (month >= 0 && p[7] == '-') ? scan_digits(p+8, 2) : -1; |
2 |
0 |
2 |
|
| 1589 |
32 |
▶ | if (YY) *YY = year; |
Branch: 1 branch
| Branch | True | False | Total |
| if (YY) *YY = year; |
1 |
31 |
32 |
|
| 1590 |
32 |
▶ | if (MM) *MM = month; |
Branch: 1 branch
| Branch | True | False | Total |
| if (MM) *MM = month; |
1 |
31 |
32 |
|
| 1591 |
32 |
▶ | if (DD) *DD = day; |
Branch: 1 branch
| Branch | True | False | Total |
| if (DD) *DD = day; |
1 |
31 |
32 |
|
| 1592 |
32 |
▶ | return (year >= 0 && month >= 0 && day >= 0) ? 0 : -1; |
Branch: 3 branches
| Branch | True | False | Total |
| return (year >= 0 && month >= 0 && day >= 0) ? 0 : -1; |
5 |
27 |
32 |
| return (year >= 0 && month >= 0 && day >= 0) ? 0 : -1; |
2 |
3 |
5 |
| return (year >= 0 && month >= 0 && day >= 0) ? 0 : -1; |
2 |
0 |
2 |
|
| 1593 |
|
| } |
| 1594 |
|
| |
| 1595 |
31 |
| static int scan_time(const char* p, int* hh, int* mm, int* ss) |
| 1596 |
|
| { |
| 1597 |
|
| int hour, minute, second; |
| 1598 |
31 |
| hour = scan_digits(p, 2); |
| 1599 |
31 |
▶ | minute = (hour >= 0 && p[2] == ':') ? scan_digits(p+3, 2) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| minute = (hour >= 0 && p[2] == ':') ? scan_digits(p+3, 2) : -1; |
6 |
25 |
31 |
| minute = (hour >= 0 && p[2] == ':') ? scan_digits(p+3, 2) : -1; |
1 |
5 |
6 |
|
| 1600 |
31 |
▶ | second = (minute >= 0 && p[5] == ':') ? scan_digits(p+6, 2) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| second = (minute >= 0 && p[5] == ':') ? scan_digits(p+6, 2) : -1; |
1 |
30 |
31 |
| second = (minute >= 0 && p[5] == ':') ? scan_digits(p+6, 2) : -1; |
1 |
0 |
1 |
|
| 1601 |
31 |
▶ | if (hh) *hh = hour; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hh) *hh = hour; |
1 |
30 |
31 |
|
| 1602 |
31 |
▶ | if (mm) *mm = minute; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mm) *mm = minute; |
1 |
30 |
31 |
|
| 1603 |
31 |
▶ | if (ss) *ss = second; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ss) *ss = second; |
1 |
30 |
31 |
|
| 1604 |
31 |
▶ | return (hour >= 0 && minute >= 0 && second >= 0) ? 0 : -1; |
Branch: 3 branches
| Branch | True | False | Total |
| return (hour >= 0 && minute >= 0 && second >= 0) ? 0 : -1; |
6 |
25 |
31 |
| return (hour >= 0 && minute >= 0 && second >= 0) ? 0 : -1; |
1 |
5 |
6 |
| return (hour >= 0 && minute >= 0 && second >= 0) ? 0 : -1; |
1 |
0 |
1 |
|
| 1605 |
|
| } |
| 1606 |
|
| |
| 1607 |
|
| |
| 1608 |
40 |
| static int scan_string(context_t* ctx, char* p, int lineno, int dotisspecial) |
| 1609 |
|
| { |
| 1610 |
40 |
| char* orig = p; |
| 1611 |
40 |
▶ | if (0 == strncmp(p, "'''", 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(p, "'''", 3)) { |
0 |
40 |
40 |
|
| 1612 |
0 |
| char* q = p + 3; |
| 1613 |
|
| |
| 1614 |
|
| while (1) { |
| 1615 |
0 |
| q = strstr(q, "'''"); |
| 1616 |
0 |
▶ | if (0 == q) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == q) { |
0 |
0 |
0 |
|
| 1617 |
0 |
| return e_syntax(ctx, lineno, "unterminated triple-s-quote"); |
| 1618 |
|
| } |
| 1619 |
0 |
▶ | while (q[3] == '\'') q++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (q[3] == '\'') q++; |
0 |
0 |
0 |
|
| 1620 |
0 |
| break; |
| 1621 |
|
| } |
| 1622 |
|
| |
| 1623 |
0 |
| set_token(ctx, STRING, lineno, orig, q + 3 - orig); |
| 1624 |
0 |
| return 0; |
| 1625 |
|
| } |
| 1626 |
|
| |
| 1627 |
40 |
▶ | if (0 == strncmp(p, "\"\"\"", 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strncmp(p, "\"\"\"", 3)) { |
0 |
40 |
40 |
|
| 1628 |
0 |
| char* q = p + 3; |
| 1629 |
|
| |
| 1630 |
|
| while (1) { |
| 1631 |
0 |
| q = strstr(q, "\"\"\""); |
| 1632 |
0 |
▶ | if (0 == q) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == q) { |
0 |
0 |
0 |
|
| 1633 |
0 |
| return e_syntax(ctx, lineno, "unterminated triple-d-quote"); |
| 1634 |
|
| } |
| 1635 |
0 |
▶ | if (q[-1] == '\\') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (q[-1] == '\\') { |
0 |
0 |
0 |
|
| 1636 |
0 |
| q++; |
| 1637 |
0 |
| continue; |
| 1638 |
|
| } |
| 1639 |
0 |
▶ | while (q[3] == '\"') q++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (q[3] == '\"') q++; |
0 |
0 |
0 |
|
| 1640 |
0 |
| break; |
| 1641 |
|
| } |
| 1642 |
|
| |
| 1643 |
|
| // the string is [p+3, q-1] |
| 1644 |
|
| |
| 1645 |
0 |
| int hexreq = 0; /* #hex required */ |
| 1646 |
0 |
| int escape = 0; |
| 1647 |
0 |
▶ | for (p += 3; p < q; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p += 3; p < q; p++) { |
0 |
0 |
0 |
|
| 1648 |
0 |
▶ | if (escape) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (escape) { |
0 |
0 |
0 |
|
| 1649 |
0 |
| escape = 0; |
| 1650 |
0 |
▶ | if (strchr("btnfr\"\\", *p)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("btnfr\"\\", *p)) continue; |
0 |
0 |
0 |
|
| 1651 |
0 |
▶ | if (*p == 'u') { hexreq = 4; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == 'u') { hexreq = 4; continue; } |
0 |
0 |
0 |
|
| 1652 |
0 |
▶ | if (*p == 'U') { hexreq = 8; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == 'U') { hexreq = 8; continue; } |
0 |
0 |
0 |
|
| 1653 |
0 |
▶ | if (p[strspn(p, " \t\r")] == '\n') continue; /* allow for line ending backslash */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p[strspn(p, " \t\r")] == '\n') continue; /* allow for line ending backslash */ |
0 |
0 |
0 |
|
| 1654 |
0 |
| return e_syntax(ctx, lineno, "bad escape char"); |
| 1655 |
|
| } |
| 1656 |
0 |
▶ | if (hexreq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hexreq) { |
0 |
0 |
0 |
|
| 1657 |
0 |
| hexreq--; |
| 1658 |
0 |
▶ | if (strchr("0123456789ABCDEF", *p)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("0123456789ABCDEF", *p)) continue; |
0 |
0 |
0 |
|
| 1659 |
0 |
| return e_syntax(ctx, lineno, "expect hex char"); |
| 1660 |
|
| } |
| 1661 |
0 |
▶ | if (*p == '\\') { escape = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\\') { escape = 1; continue; } |
0 |
0 |
0 |
|
| 1662 |
|
| } |
| 1663 |
0 |
▶ | if (escape) |
Branch: 1 branch
| Branch | True | False | Total |
| if (escape) |
0 |
0 |
0 |
|
| 1664 |
0 |
| return e_syntax(ctx, lineno, "expect an escape char"); |
| 1665 |
0 |
▶ | if (hexreq) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hexreq) |
0 |
0 |
0 |
|
| 1666 |
0 |
| return e_syntax(ctx, lineno, "expected more hex char"); |
| 1667 |
|
| |
| 1668 |
0 |
| set_token(ctx, STRING, lineno, orig, q + 3 - orig); |
| 1669 |
0 |
| return 0; |
| 1670 |
|
| } |
| 1671 |
|
| |
| 1672 |
40 |
▶ | if ('\'' == *p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\'' == *p) { |
0 |
40 |
40 |
|
| 1673 |
0 |
▶ | for (p++; *p && *p != '\n' && *p != '\''; p++); |
Branch: 3 branches
| Branch | True | False | Total |
| for (p++; *p && *p != '\n' && *p != '\''; p++); |
0 |
0 |
0 |
| for (p++; *p && *p != '\n' && *p != '\''; p++); |
0 |
0 |
0 |
| for (p++; *p && *p != '\n' && *p != '\''; p++); |
0 |
0 |
0 |
|
| 1674 |
0 |
▶ | if (*p != '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p != '\'') { |
0 |
0 |
0 |
|
| 1675 |
0 |
| return e_syntax(ctx, lineno, "unterminated s-quote"); |
| 1676 |
|
| } |
| 1677 |
|
| |
| 1678 |
0 |
| set_token(ctx, STRING, lineno, orig, p + 1 - orig); |
| 1679 |
0 |
| return 0; |
| 1680 |
|
| } |
| 1681 |
|
| |
| 1682 |
40 |
▶ | if ('\"' == *p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\"' == *p) { |
9 |
31 |
40 |
|
| 1683 |
9 |
| char* tsq = strstr(p, "\'\'\'"); |
| 1684 |
9 |
| int hexreq = 0; /* #hex required */ |
| 1685 |
9 |
| int escape = 0; |
| 1686 |
84 |
▶ | for (p++; *p; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p++; *p; p++) { |
84 |
0 |
84 |
|
| 1687 |
84 |
▶ | if (escape) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (escape) { |
0 |
84 |
84 |
|
| 1688 |
0 |
| escape = 0; |
| 1689 |
0 |
▶ | if (strchr("btnfr\"\\", *p)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("btnfr\"\\", *p)) continue; |
0 |
0 |
0 |
|
| 1690 |
0 |
▶ | if (*p == 'u') { hexreq = 4; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == 'u') { hexreq = 4; continue; } |
0 |
0 |
0 |
|
| 1691 |
0 |
▶ | if (*p == 'U') { hexreq = 8; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == 'U') { hexreq = 8; continue; } |
0 |
0 |
0 |
|
| 1692 |
0 |
| return e_syntax(ctx, lineno, "bad escape char"); |
| 1693 |
|
| } |
| 1694 |
84 |
▶ | if (hexreq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hexreq) { |
0 |
84 |
84 |
|
| 1695 |
0 |
| hexreq--; |
| 1696 |
0 |
▶ | if (strchr("0123456789ABCDEF", *p)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("0123456789ABCDEF", *p)) continue; |
0 |
0 |
0 |
|
| 1697 |
0 |
| return e_syntax(ctx, lineno, "expect hex char"); |
| 1698 |
|
| } |
| 1699 |
84 |
▶ | if (*p == '\\') { escape = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\\') { escape = 1; continue; } |
0 |
84 |
84 |
|
| 1700 |
84 |
▶ | if (*p == '\n') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') break; |
0 |
84 |
84 |
|
| 1701 |
84 |
▶ | if (*p == '"') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '"') break; |
9 |
75 |
84 |
|
| 1702 |
|
| } |
| 1703 |
9 |
▶ | if (*p != '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p != '"') { |
0 |
9 |
9 |
|
| 1704 |
0 |
| return e_syntax(ctx, lineno, "unterminated quote"); |
| 1705 |
|
| } |
| 1706 |
|
| |
| 1707 |
9 |
▶ | if (tsq && tsq < p) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tsq && tsq < p) { |
0 |
9 |
9 |
| if (tsq && tsq < p) { |
0 |
0 |
0 |
|
| 1708 |
0 |
| return e_syntax(ctx, lineno, "triple-s-quote inside string lit"); |
| 1709 |
|
| } |
| 1710 |
|
| |
| 1711 |
9 |
| set_token(ctx, STRING, lineno, orig, p + 1 - orig); |
| 1712 |
9 |
| return 0; |
| 1713 |
|
| } |
| 1714 |
|
| |
| 1715 |
|
| /* check for timestamp without quotes */ |
| 1716 |
31 |
▶ | if (0 == scan_date(p, 0, 0, 0) || 0 == scan_time(p, 0, 0, 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (0 == scan_date(p, 0, 0, 0) || 0 == scan_time(p, 0, 0, 0)) { |
30 |
1 |
31 |
| if (0 == scan_date(p, 0, 0, 0) || 0 == scan_time(p, 0, 0, 0)) { |
0 |
30 |
30 |
|
| 1717 |
|
| // forward thru the timestamp |
| 1718 |
26 |
▶ | for ( ; strchr("0123456789.:+-T Z", toupper(*p)); p++); |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; strchr("0123456789.:+-T Z", toupper(*p)); p++); |
25 |
1 |
26 |
|
| 1719 |
|
| // squeeze out any spaces at end of string |
| 1720 |
1 |
▶ | for ( ; p[-1] == ' '; p--); |
Branch: 1 branch
| Branch | True | False | Total |
| for ( ; p[-1] == ' '; p--); |
0 |
1 |
1 |
|
| 1721 |
|
| // tokenize |
| 1722 |
1 |
| set_token(ctx, STRING, lineno, orig, p - orig); |
| 1723 |
1 |
| return 0; |
| 1724 |
|
| } |
| 1725 |
|
| |
| 1726 |
|
| /* literals */ |
| 1727 |
186 |
▶ | for ( ; *p && *p != '\n'; p++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for ( ; *p && *p != '\n'; p++) { |
185 |
1 |
186 |
| for ( ; *p && *p != '\n'; p++) { |
184 |
1 |
185 |
|
| 1728 |
184 |
| int ch = *p; |
| 1729 |
184 |
▶ | if (ch == '.' && dotisspecial) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch == '.' && dotisspecial) break; |
5 |
179 |
184 |
| if (ch == '.' && dotisspecial) break; |
2 |
3 |
5 |
|
| 1730 |
182 |
▶ | if ('A' <= ch && ch <= 'Z') continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if ('A' <= ch && ch <= 'Z') continue; |
139 |
43 |
182 |
| if ('A' <= ch && ch <= 'Z') continue; |
0 |
139 |
139 |
|
| 1731 |
182 |
▶ | if ('a' <= ch && ch <= 'z') continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if ('a' <= ch && ch <= 'z') continue; |
131 |
51 |
182 |
| if ('a' <= ch && ch <= 'z') continue; |
131 |
0 |
131 |
|
| 1732 |
51 |
▶ | if (strchr("0123456789+-_.", ch)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strchr("0123456789+-_.", ch)) continue; |
25 |
26 |
51 |
|
| 1733 |
26 |
| break; |
| 1734 |
|
| } |
| 1735 |
|
| |
| 1736 |
30 |
| set_token(ctx, STRING, lineno, orig, p - orig); |
| 1737 |
30 |
| return 0; |
| 1738 |
|
| } |
| 1739 |
|
| |
| 1740 |
|
| |
| 1741 |
111 |
| static int next_token(context_t* ctx, int dotisspecial) |
| 1742 |
|
| { |
| 1743 |
111 |
| int lineno = ctx->tok.lineno; |
| 1744 |
111 |
| char* p = ctx->tok.ptr; |
| 1745 |
|
| int i; |
| 1746 |
|
| |
| 1747 |
|
| /* eat this tok */ |
| 1748 |
454 |
▶ | for (i = 0; i < ctx->tok.len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < ctx->tok.len; i++) { |
343 |
111 |
454 |
|
| 1749 |
343 |
▶ | if (*p++ == '\n') |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p++ == '\n') |
26 |
317 |
343 |
|
| 1750 |
26 |
| lineno++; |
| 1751 |
|
| } |
| 1752 |
|
| |
| 1753 |
|
| /* make next tok */ |
| 1754 |
151 |
▶ | while (p < ctx->stop) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < ctx->stop) { |
149 |
2 |
151 |
|
| 1755 |
|
| /* skip comment. stop just before the \n. */ |
| 1756 |
149 |
▶ | if (*p == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') { |
1 |
148 |
149 |
|
| 1757 |
25 |
▶ | for (p++; p < ctx->stop && *p != '\n'; p++); |
Branch: 2 branches
| Branch | True | False | Total |
| for (p++; p < ctx->stop && *p != '\n'; p++); |
25 |
0 |
25 |
| for (p++; p < ctx->stop && *p != '\n'; p++); |
24 |
1 |
25 |
|
| 1758 |
1 |
| continue; |
| 1759 |
|
| } |
| 1760 |
|
| |
| 1761 |
148 |
▶ | if (dotisspecial && *p == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dotisspecial && *p == '.') { |
99 |
49 |
148 |
| if (dotisspecial && *p == '.') { |
2 |
97 |
99 |
|
| 1762 |
2 |
| set_token(ctx, DOT, lineno, p, 1); |
| 1763 |
2 |
| return 0; |
| 1764 |
|
| } |
| 1765 |
|
| |
| 1766 |
146 |
| switch (*p) { |
| 1767 |
5 |
| case ',': set_token(ctx, COMMA, lineno, p, 1); return 0; |
| 1768 |
14 |
| case '=': set_token(ctx, EQUAL, lineno, p, 1); return 0; |
| 1769 |
1 |
| case '{': set_token(ctx, LBRACE, lineno, p, 1); return 0; |
| 1770 |
1 |
| case '}': set_token(ctx, RBRACE, lineno, p, 1); return 0; |
| 1771 |
10 |
| case '[': set_token(ctx, LBRACKET, lineno, p, 1); return 0; |
| 1772 |
10 |
| case ']': set_token(ctx, RBRACKET, lineno, p, 1); return 0; |
| 1773 |
26 |
| case '\n': set_token(ctx, NEWLINE, lineno, p, 1); return 0; |
| 1774 |
39 |
| case '\r': case ' ': case '\t': |
| 1775 |
|
| /* ignore white spaces */ |
| 1776 |
39 |
| p++; |
| 1777 |
39 |
| continue; |
| 1778 |
|
| } |
| 1779 |
|
| |
| 1780 |
40 |
| return scan_string(ctx, p, lineno, dotisspecial); |
| 1781 |
|
| } |
| 1782 |
|
| |
| 1783 |
2 |
| set_eof(ctx, lineno); |
| 1784 |
2 |
| return 0; |
| 1785 |
|
| } |
| 1786 |
|
| |
| 1787 |
|
| |
| 1788 |
28 |
| const char* toml_key_in(const toml_table_t* tab, int keyidx) |
| 1789 |
|
| { |
| 1790 |
28 |
▶ | if (keyidx < tab->nkval) return tab->kval[keyidx]->key; |
Branch: 1 branch
| Branch | True | False | Total |
| if (keyidx < tab->nkval) return tab->kval[keyidx]->key; |
11 |
17 |
28 |
|
| 1791 |
|
| |
| 1792 |
17 |
| keyidx -= tab->nkval; |
| 1793 |
17 |
▶ | if (keyidx < tab->narr) return tab->arr[keyidx]->key; |
Branch: 1 branch
| Branch | True | False | Total |
| if (keyidx < tab->narr) return tab->arr[keyidx]->key; |
2 |
15 |
17 |
|
| 1794 |
|
| |
| 1795 |
15 |
| keyidx -= tab->narr; |
| 1796 |
15 |
▶ | if (keyidx < tab->ntab) return tab->tab[keyidx]->key; |
Branch: 1 branch
| Branch | True | False | Total |
| if (keyidx < tab->ntab) return tab->tab[keyidx]->key; |
7 |
8 |
15 |
|
| 1797 |
|
| |
| 1798 |
8 |
| return 0; |
| 1799 |
|
| } |
| 1800 |
|
| |
| 1801 |
22 |
| toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key) |
| 1802 |
|
| { |
| 1803 |
|
| int i; |
| 1804 |
33 |
▶ | for (i = 0; i < tab->nkval; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->nkval; i++) { |
33 |
0 |
33 |
|
| 1805 |
33 |
▶ | if (0 == strcmp(key, tab->kval[i]->key)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->kval[i]->key)) |
22 |
11 |
33 |
|
| 1806 |
22 |
| return tab->kval[i]->val; |
| 1807 |
|
| } |
| 1808 |
0 |
| return 0; |
| 1809 |
|
| } |
| 1810 |
|
| |
| 1811 |
20 |
| toml_array_t* toml_array_in(const toml_table_t* tab, const char* key) |
| 1812 |
|
| { |
| 1813 |
|
| int i; |
| 1814 |
25 |
▶ | for (i = 0; i < tab->narr; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->narr; i++) { |
7 |
18 |
25 |
|
| 1815 |
7 |
▶ | if (0 == strcmp(key, tab->arr[i]->key)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->arr[i]->key)) |
2 |
5 |
7 |
|
| 1816 |
2 |
| return tab->arr[i]; |
| 1817 |
|
| } |
| 1818 |
18 |
| return 0; |
| 1819 |
|
| } |
| 1820 |
|
| |
| 1821 |
|
| |
| 1822 |
18 |
| toml_table_t* toml_table_in(const toml_table_t* tab, const char* key) |
| 1823 |
|
| { |
| 1824 |
|
| int i; |
| 1825 |
30 |
▶ | for (i = 0; i < tab->ntab; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < tab->ntab; i++) { |
19 |
11 |
30 |
|
| 1826 |
19 |
▶ | if (0 == strcmp(key, tab->tab[i]->key)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(key, tab->tab[i]->key)) |
7 |
12 |
19 |
|
| 1827 |
7 |
| return tab->tab[i]; |
| 1828 |
|
| } |
| 1829 |
11 |
| return 0; |
| 1830 |
|
| } |
| 1831 |
|
| |
| 1832 |
15 |
| toml_raw_t toml_raw_at(const toml_array_t* arr, int idx) |
| 1833 |
|
| { |
| 1834 |
15 |
▶ | return (0 <= idx && idx < arr->nitem) ? arr->item[idx].val : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].val : 0; |
15 |
0 |
15 |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].val : 0; |
15 |
0 |
15 |
|
| 1835 |
|
| } |
| 1836 |
|
| |
| 1837 |
0 |
| char toml_array_kind(const toml_array_t* arr) |
| 1838 |
|
| { |
| 1839 |
0 |
| return arr->kind; |
| 1840 |
|
| } |
| 1841 |
|
| |
| 1842 |
0 |
| char toml_array_type(const toml_array_t* arr) |
| 1843 |
|
| { |
| 1844 |
0 |
▶ | if (arr->kind != 'v') |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->kind != 'v') |
0 |
0 |
0 |
|
| 1845 |
0 |
| return 0; |
| 1846 |
|
| |
| 1847 |
0 |
▶ | if (arr->nitem == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (arr->nitem == 0) |
0 |
0 |
0 |
|
| 1848 |
0 |
| return 0; |
| 1849 |
|
| |
| 1850 |
0 |
| return arr->type; |
| 1851 |
|
| } |
| 1852 |
|
| |
| 1853 |
|
| |
| 1854 |
4 |
| int toml_array_nelem(const toml_array_t* arr) |
| 1855 |
|
| { |
| 1856 |
4 |
| return arr->nitem; |
| 1857 |
|
| } |
| 1858 |
|
| |
| 1859 |
0 |
| const char* toml_array_key(const toml_array_t* arr) |
| 1860 |
|
| { |
| 1861 |
0 |
▶ | return arr ? arr->key : (const char*) NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return arr ? arr->key : (const char*) NULL; |
0 |
0 |
0 |
|
| 1862 |
|
| } |
| 1863 |
|
| |
| 1864 |
0 |
| int toml_table_nkval(const toml_table_t* tab) |
| 1865 |
|
| { |
| 1866 |
0 |
| return tab->nkval; |
| 1867 |
|
| } |
| 1868 |
|
| |
| 1869 |
0 |
| int toml_table_narr(const toml_table_t* tab) |
| 1870 |
|
| { |
| 1871 |
0 |
| return tab->narr; |
| 1872 |
|
| } |
| 1873 |
|
| |
| 1874 |
0 |
| int toml_table_ntab(const toml_table_t* tab) |
| 1875 |
|
| { |
| 1876 |
0 |
| return tab->ntab; |
| 1877 |
|
| } |
| 1878 |
|
| |
| 1879 |
0 |
| const char* toml_table_key(const toml_table_t* tab) |
| 1880 |
|
| { |
| 1881 |
0 |
▶ | return tab ? tab->key : (const char*) NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return tab ? tab->key : (const char*) NULL; |
0 |
0 |
0 |
|
| 1882 |
|
| } |
| 1883 |
|
| |
| 1884 |
8 |
| toml_array_t* toml_array_at(const toml_array_t* arr, int idx) |
| 1885 |
|
| { |
| 1886 |
8 |
▶ | return (0 <= idx && idx < arr->nitem) ? arr->item[idx].arr : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].arr : 0; |
8 |
0 |
8 |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].arr : 0; |
8 |
0 |
8 |
|
| 1887 |
|
| } |
| 1888 |
|
| |
| 1889 |
6 |
| toml_table_t* toml_table_at(const toml_array_t* arr, int idx) |
| 1890 |
|
| { |
| 1891 |
6 |
▶ | return (0 <= idx && idx < arr->nitem) ? arr->item[idx].tab : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].tab : 0; |
6 |
0 |
6 |
| return (0 <= idx && idx < arr->nitem) ? arr->item[idx].tab : 0; |
6 |
0 |
6 |
|
| 1892 |
|
| } |
| 1893 |
|
| |
| 1894 |
|
| |
| 1895 |
1 |
| int toml_rtots(toml_raw_t src_, toml_timestamp_t* ret) |
| 1896 |
|
| { |
| 1897 |
1 |
▶ | if (! src_) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (! src_) return -1; |
0 |
1 |
1 |
|
| 1898 |
|
| |
| 1899 |
1 |
| const char* p = src_; |
| 1900 |
1 |
| int must_parse_time = 0; |
| 1901 |
|
| |
| 1902 |
1 |
| memset(ret, 0, sizeof(*ret)); |
| 1903 |
|
| |
| 1904 |
1 |
| int* year = &ret->__buffer.year; |
| 1905 |
1 |
| int* month = &ret->__buffer.month; |
| 1906 |
1 |
| int* day = &ret->__buffer.day; |
| 1907 |
1 |
| int* hour = &ret->__buffer.hour; |
| 1908 |
1 |
| int* minute = &ret->__buffer.minute; |
| 1909 |
1 |
| int* second = &ret->__buffer.second; |
| 1910 |
1 |
| int* millisec = &ret->__buffer.millisec; |
| 1911 |
|
| |
| 1912 |
|
| /* parse date YYYY-MM-DD */ |
| 1913 |
1 |
▶ | if (0 == scan_date(p, year, month, day)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == scan_date(p, year, month, day)) { |
1 |
0 |
1 |
|
| 1914 |
1 |
| ret->year = year; |
| 1915 |
1 |
| ret->month = month; |
| 1916 |
1 |
| ret->day = day; |
| 1917 |
|
| |
| 1918 |
1 |
| p += 10; |
| 1919 |
1 |
▶ | if (*p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p) { |
1 |
0 |
1 |
|
| 1920 |
|
| // parse the T or space separator |
| 1921 |
1 |
▶ | if (*p != 'T' && *p != ' ') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p != 'T' && *p != ' ') return -1; |
0 |
1 |
1 |
| if (*p != 'T' && *p != ' ') return -1; |
0 |
0 |
0 |
|
| 1922 |
1 |
| must_parse_time = 1; |
| 1923 |
1 |
| p++; |
| 1924 |
|
| } |
| 1925 |
|
| } |
| 1926 |
|
| |
| 1927 |
|
| /* parse time HH:MM:SS */ |
| 1928 |
1 |
▶ | if (0 == scan_time(p, hour, minute, second)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == scan_time(p, hour, minute, second)) { |
1 |
0 |
1 |
|
| 1929 |
1 |
| ret->hour = hour; |
| 1930 |
1 |
| ret->minute = minute; |
| 1931 |
1 |
| ret->second = second; |
| 1932 |
|
| |
| 1933 |
|
| /* optionally, parse millisec */ |
| 1934 |
1 |
| p += 8; |
| 1935 |
1 |
▶ | if (*p == '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '.') { |
0 |
1 |
1 |
|
| 1936 |
|
| char* qq; |
| 1937 |
0 |
| p++; |
| 1938 |
0 |
| errno = 0; |
| 1939 |
0 |
| *millisec = strtol(p, &qq, 0); |
| 1940 |
0 |
▶ | if (errno) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno) { |
0 |
0 |
0 |
|
| 1941 |
0 |
| return -1; |
| 1942 |
|
| } |
| 1943 |
0 |
▶ | while (*millisec > 999) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*millisec > 999) { |
0 |
0 |
0 |
|
| 1944 |
0 |
| *millisec /= 10; |
| 1945 |
|
| } |
| 1946 |
|
| |
| 1947 |
0 |
| ret->millisec = millisec; |
| 1948 |
0 |
| p = qq; |
| 1949 |
|
| } |
| 1950 |
|
| |
| 1951 |
1 |
▶ | if (*p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p) { |
1 |
0 |
1 |
|
| 1952 |
|
| /* parse and copy Z */ |
| 1953 |
1 |
| char* z = ret->__buffer.z; |
| 1954 |
1 |
| ret->z = z; |
| 1955 |
1 |
▶ | if (*p == 'Z' || *p == 'z') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == 'Z' || *p == 'z') { |
1 |
0 |
1 |
| if (*p == 'Z' || *p == 'z') { |
0 |
1 |
1 |
|
| 1956 |
0 |
| *z++ = 'Z'; p++; |
| 1957 |
0 |
| *z = 0; |
| 1958 |
|
| |
| 1959 |
1 |
▶ | } else if (*p == '+' || *p == '-') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (*p == '+' || *p == '-') { |
1 |
0 |
1 |
| } else if (*p == '+' || *p == '-') { |
1 |
0 |
1 |
|
| 1960 |
1 |
| *z++ = *p++; |
| 1961 |
|
| |
| 1962 |
1 |
▶ | if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
1 |
0 |
1 |
| if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
0 |
1 |
1 |
|
| 1963 |
1 |
| *z++ = *p++; |
| 1964 |
1 |
| *z++ = *p++; |
| 1965 |
|
| |
| 1966 |
1 |
▶ | if (*p == ':') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ':') { |
1 |
0 |
1 |
|
| 1967 |
1 |
| *z++ = *p++; |
| 1968 |
|
| |
| 1969 |
1 |
▶ | if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
1 |
0 |
1 |
| if (! (isdigit(p[0]) && isdigit(p[1]))) return -1; |
0 |
1 |
1 |
|
| 1970 |
1 |
| *z++ = *p++; |
| 1971 |
1 |
| *z++ = *p++; |
| 1972 |
|
| } |
| 1973 |
|
| |
| 1974 |
1 |
| *z = 0; |
| 1975 |
|
| } |
| 1976 |
|
| } |
| 1977 |
|
| } |
| 1978 |
1 |
▶ | if (*p != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p != 0) |
0 |
1 |
1 |
|
| 1979 |
0 |
| return -1; |
| 1980 |
|
| |
| 1981 |
1 |
▶ | if (must_parse_time && !ret->hour) |
Branch: 2 branches
| Branch | True | False | Total |
| if (must_parse_time && !ret->hour) |
1 |
0 |
1 |
| if (must_parse_time && !ret->hour) |
0 |
1 |
1 |
|
| 1982 |
0 |
| return -1; |
| 1983 |
|
| |
| 1984 |
1 |
| return 0; |
| 1985 |
|
| } |
| 1986 |
|
| |
| 1987 |
|
| |
| 1988 |
|
| /* Raw to boolean */ |
| 1989 |
12 |
| int toml_rtob(toml_raw_t src, int* ret_) |
| 1990 |
|
| { |
| 1991 |
12 |
▶ | if (!src) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return -1; |
0 |
12 |
12 |
|
| 1992 |
|
| int dummy; |
| 1993 |
12 |
▶ | int* ret = ret_ ? ret_ : &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| int* ret = ret_ ? ret_ : &dummy; |
4 |
8 |
12 |
|
| 1994 |
|
| |
| 1995 |
12 |
▶ | if (0 == strcmp(src, "true")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(src, "true")) { |
1 |
11 |
12 |
|
| 1996 |
1 |
| *ret = 1; |
| 1997 |
1 |
| return 0; |
| 1998 |
|
| } |
| 1999 |
11 |
▶ | if (0 == strcmp(src, "false")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 == strcmp(src, "false")) { |
0 |
11 |
11 |
|
| 2000 |
0 |
| *ret = 0; |
| 2001 |
0 |
| return 0; |
| 2002 |
|
| } |
| 2003 |
11 |
| return -1; |
| 2004 |
|
| } |
| 2005 |
|
| |
| 2006 |
|
| |
| 2007 |
|
| /* Raw to integer */ |
| 2008 |
11 |
| int toml_rtoi(toml_raw_t src, int64_t* ret_) |
| 2009 |
|
| { |
| 2010 |
11 |
▶ | if (!src) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return -1; |
0 |
11 |
11 |
|
| 2011 |
|
| |
| 2012 |
|
| char buf[100]; |
| 2013 |
11 |
| char* p = buf; |
| 2014 |
11 |
| char* q = p + sizeof(buf); |
| 2015 |
11 |
| const char* s = src; |
| 2016 |
11 |
| int base = 0; |
| 2017 |
|
| int64_t dummy; |
| 2018 |
11 |
▶ | int64_t* ret = ret_ ? ret_ : &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| int64_t* ret = ret_ ? ret_ : &dummy; |
4 |
7 |
11 |
|
| 2019 |
|
| |
| 2020 |
|
| |
| 2021 |
|
| /* allow +/- */ |
| 2022 |
11 |
▶ | if (s[0] == '+' || s[0] == '-') |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[0] == '+' || s[0] == '-') |
11 |
0 |
11 |
| if (s[0] == '+' || s[0] == '-') |
0 |
11 |
11 |
|
| 2023 |
0 |
| *p++ = *s++; |
| 2024 |
|
| |
| 2025 |
|
| /* disallow +_100 */ |
| 2026 |
11 |
▶ | if (s[0] == '_') |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '_') |
0 |
11 |
11 |
|
| 2027 |
0 |
| return -1; |
| 2028 |
|
| |
| 2029 |
|
| /* if 0 ... */ |
| 2030 |
11 |
▶ | if ('0' == s[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('0' == s[0]) { |
0 |
11 |
11 |
|
| 2031 |
0 |
| switch (s[1]) { |
| 2032 |
0 |
| case 'x': base = 16; s += 2; break; |
| 2033 |
0 |
| case 'o': base = 8; s += 2; break; |
| 2034 |
0 |
| case 'b': base = 2; s += 2; break; |
| 2035 |
0 |
| case '\0': return *ret = 0, 0; |
| 2036 |
0 |
| default: |
| 2037 |
|
| /* ensure no other digits after it */ |
| 2038 |
0 |
▶ | if (s[1]) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[1]) return -1; |
0 |
0 |
0 |
|
| 2039 |
|
| } |
| 2040 |
|
| } |
| 2041 |
|
| |
| 2042 |
|
| /* just strip underscores and pass to strtoll */ |
| 2043 |
76 |
▶ | while (*s && p < q) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s && p < q) { |
65 |
11 |
76 |
| while (*s && p < q) { |
65 |
0 |
65 |
|
| 2044 |
65 |
| int ch = *s++; |
| 2045 |
65 |
▶ | switch (ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (ch) { |
0 |
65 |
65 |
|
| 2046 |
0 |
| case '_': |
| 2047 |
|
| // disallow '__' |
| 2048 |
0 |
▶ | if (s[0] == '_') return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '_') return -1; |
0 |
0 |
0 |
|
| 2049 |
0 |
| continue; /* skip _ */ |
| 2050 |
65 |
| default: |
| 2051 |
65 |
| break; |
| 2052 |
|
| } |
| 2053 |
65 |
| *p++ = ch; |
| 2054 |
|
| } |
| 2055 |
11 |
▶ | if (*s || p == q) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s || p == q) return -1; |
11 |
0 |
11 |
| if (*s || p == q) return -1; |
0 |
11 |
11 |
|
| 2056 |
|
| |
| 2057 |
|
| /* last char cannot be '_' */ |
| 2058 |
11 |
▶ | if (s[-1] == '_') return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[-1] == '_') return -1; |
0 |
11 |
11 |
|
| 2059 |
|
| |
| 2060 |
|
| /* cap with NUL */ |
| 2061 |
11 |
| *p = 0; |
| 2062 |
|
| |
| 2063 |
|
| /* Run strtoll on buf to get the integer */ |
| 2064 |
|
| char* endp; |
| 2065 |
11 |
| errno = 0; |
| 2066 |
11 |
| *ret = strtoll(buf, &endp, base); |
| 2067 |
11 |
▶ | return (errno || *endp) ? -1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (errno || *endp) ? -1 : 0; |
11 |
0 |
11 |
| return (errno || *endp) ? -1 : 0; |
5 |
6 |
11 |
|
| 2068 |
|
| } |
| 2069 |
|
| |
| 2070 |
|
| |
| 2071 |
5 |
| int toml_rtod_ex(toml_raw_t src, double* ret_, char* buf, int buflen) |
| 2072 |
|
| { |
| 2073 |
5 |
▶ | if (!src) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return -1; |
0 |
5 |
5 |
|
| 2074 |
|
| |
| 2075 |
5 |
| char* p = buf; |
| 2076 |
5 |
| char* q = p + buflen; |
| 2077 |
5 |
| const char* s = src; |
| 2078 |
|
| double dummy; |
| 2079 |
5 |
▶ | double* ret = ret_ ? ret_ : &dummy; |
Branch: 1 branch
| Branch | True | False | Total |
| double* ret = ret_ ? ret_ : &dummy; |
1 |
4 |
5 |
|
| 2080 |
|
| |
| 2081 |
|
| |
| 2082 |
|
| /* allow +/- */ |
| 2083 |
5 |
▶ | if (s[0] == '+' || s[0] == '-') |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[0] == '+' || s[0] == '-') |
5 |
0 |
5 |
| if (s[0] == '+' || s[0] == '-') |
0 |
5 |
5 |
|
| 2084 |
0 |
| *p++ = *s++; |
| 2085 |
|
| |
| 2086 |
|
| /* disallow +_1.00 */ |
| 2087 |
5 |
▶ | if (s[0] == '_') |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '_') |
0 |
5 |
5 |
|
| 2088 |
0 |
| return -1; |
| 2089 |
|
| |
| 2090 |
|
| /* decimal point, if used, must be surrounded by at least one digit on each side */ |
| 2091 |
|
| { |
| 2092 |
5 |
| char* dot = strchr(s, '.'); |
| 2093 |
5 |
▶ | if (dot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dot) { |
4 |
1 |
5 |
|
| 2094 |
4 |
▶ | if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1])) |
Branch: 3 branches
| Branch | True | False | Total |
| if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1])) |
4 |
0 |
4 |
| if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1])) |
4 |
0 |
4 |
| if (dot == s || !isdigit(dot[-1]) || !isdigit(dot[1])) |
0 |
4 |
4 |
|
| 2095 |
0 |
| return -1; |
| 2096 |
|
| } |
| 2097 |
|
| } |
| 2098 |
|
| |
| 2099 |
|
| /* zero must be followed by . or 'e', or NUL */ |
| 2100 |
5 |
▶ | if (s[0] == '0' && s[1] && !strchr("eE.", s[1])) |
Branch: 3 branches
| Branch | True | False | Total |
| if (s[0] == '0' && s[1] && !strchr("eE.", s[1])) |
0 |
5 |
5 |
| if (s[0] == '0' && s[1] && !strchr("eE.", s[1])) |
0 |
0 |
0 |
| if (s[0] == '0' && s[1] && !strchr("eE.", s[1])) |
0 |
0 |
0 |
|
| 2101 |
0 |
| return -1; |
| 2102 |
|
| |
| 2103 |
|
| /* just strip underscores and pass to strtod */ |
| 2104 |
46 |
▶ | while (*s && p < q) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*s && p < q) { |
41 |
5 |
46 |
| while (*s && p < q) { |
41 |
0 |
41 |
|
| 2105 |
41 |
| int ch = *s++; |
| 2106 |
41 |
▶ | if (ch == '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '_') { |
0 |
41 |
41 |
|
| 2107 |
|
| // disallow '__' |
| 2108 |
0 |
▶ | if (s[0] == '_') return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == '_') return -1; |
0 |
0 |
0 |
|
| 2109 |
|
| // disallow last char '_' |
| 2110 |
0 |
▶ | if (s[0] == 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == 0) return -1; |
0 |
0 |
0 |
|
| 2111 |
0 |
| continue; /* skip _ */ |
| 2112 |
|
| } |
| 2113 |
41 |
| *p++ = ch; |
| 2114 |
|
| } |
| 2115 |
5 |
▶ | if (*s || p == q) return -1; /* reached end of string or buffer is full? */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s || p == q) return -1; /* reached end of string or buffer is full? */ |
5 |
0 |
5 |
| if (*s || p == q) return -1; /* reached end of string or buffer is full? */ |
0 |
5 |
5 |
|
| 2116 |
|
| |
| 2117 |
|
| /* cap with NUL */ |
| 2118 |
5 |
| *p = 0; |
| 2119 |
|
| |
| 2120 |
|
| /* Run strtod on buf to get the value */ |
| 2121 |
|
| char* endp; |
| 2122 |
5 |
| errno = 0; |
| 2123 |
5 |
| *ret = strtod(buf, &endp); |
| 2124 |
5 |
▶ | return (errno || *endp) ? -1 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return (errno || *endp) ? -1 : 0; |
5 |
0 |
5 |
| return (errno || *endp) ? -1 : 0; |
1 |
4 |
5 |
|
| 2125 |
|
| } |
| 2126 |
|
| |
| 2127 |
5 |
| int toml_rtod(toml_raw_t src, double* ret_) |
| 2128 |
|
| { |
| 2129 |
|
| char buf[100]; |
| 2130 |
5 |
| return toml_rtod_ex(src, ret_, buf, sizeof(buf)); |
| 2131 |
|
| } |
| 2132 |
|
| |
| 2133 |
|
| |
| 2134 |
|
| |
| 2135 |
|
| |
| 2136 |
17 |
| int toml_rtos(toml_raw_t src, char** ret) |
| 2137 |
|
| { |
| 2138 |
17 |
| int multiline = 0; |
| 2139 |
|
| const char* sp; |
| 2140 |
|
| const char* sq; |
| 2141 |
|
| |
| 2142 |
17 |
| *ret = 0; |
| 2143 |
17 |
▶ | if (!src) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return -1; |
0 |
17 |
17 |
|
| 2144 |
|
| |
| 2145 |
17 |
| int qchar = src[0]; |
| 2146 |
17 |
| int srclen = strlen(src); |
| 2147 |
17 |
▶ | if (! (qchar == '\'' || qchar == '"')) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (qchar == '\'' || qchar == '"')) { |
17 |
0 |
17 |
| if (! (qchar == '\'' || qchar == '"')) { |
8 |
9 |
17 |
|
| 2148 |
8 |
| return -1; |
| 2149 |
|
| } |
| 2150 |
|
| |
| 2151 |
|
| // triple quotes? |
| 2152 |
9 |
▶ | if (qchar == src[1] && qchar == src[2]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (qchar == src[1] && qchar == src[2]) { |
0 |
9 |
9 |
| if (qchar == src[1] && qchar == src[2]) { |
0 |
0 |
0 |
|
| 2153 |
0 |
| multiline = 1; |
| 2154 |
0 |
| sp = src + 3; |
| 2155 |
0 |
| sq = src + srclen - 3; |
| 2156 |
|
| /* last 3 chars in src must be qchar */ |
| 2157 |
0 |
▶ | if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) |
0 |
0 |
0 |
| if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) |
0 |
0 |
0 |
| if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) |
0 |
0 |
0 |
| if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) |
0 |
0 |
0 |
|
| 2158 |
0 |
| return -1; |
| 2159 |
|
| |
| 2160 |
|
| /* skip new line immediate after qchar */ |
| 2161 |
0 |
▶ | if (sp[0] == '\n') |
Branch: 1 branch
| Branch | True | False | Total |
| if (sp[0] == '\n') |
0 |
0 |
0 |
|
| 2162 |
0 |
| sp++; |
| 2163 |
0 |
▶ | else if (sp[0] == '\r' && sp[1] == '\n') |
Branch: 2 branches
| Branch | True | False | Total |
| else if (sp[0] == '\r' && sp[1] == '\n') |
0 |
0 |
0 |
| else if (sp[0] == '\r' && sp[1] == '\n') |
0 |
0 |
0 |
|
| 2164 |
0 |
| sp += 2; |
| 2165 |
|
| |
| 2166 |
|
| } else { |
| 2167 |
9 |
| sp = src + 1; |
| 2168 |
9 |
| sq = src + srclen - 1; |
| 2169 |
|
| /* last char in src must be qchar */ |
| 2170 |
9 |
▶ | if (! (sp <= sq && *sq == qchar)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (sp <= sq && *sq == qchar)) |
9 |
0 |
9 |
| if (! (sp <= sq && *sq == qchar)) |
0 |
9 |
9 |
|
| 2171 |
0 |
| return -1; |
| 2172 |
|
| } |
| 2173 |
|
| |
| 2174 |
9 |
▶ | if (qchar == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (qchar == '\'') { |
0 |
9 |
9 |
|
| 2175 |
0 |
| *ret = norm_lit_str(sp, sq - sp, |
| 2176 |
|
| multiline, |
| 2177 |
|
| 0, 0); |
| 2178 |
|
| } else { |
| 2179 |
9 |
| *ret = norm_basic_str(sp, sq - sp, |
| 2180 |
|
| multiline, |
| 2181 |
|
| 0, 0); |
| 2182 |
|
| } |
| 2183 |
|
| |
| 2184 |
9 |
▶ | return *ret ? 0 : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| return *ret ? 0 : -1; |
9 |
0 |
9 |
|
| 2185 |
|
| } |
| 2186 |
|
| |
| 2187 |
|
| |
| 2188 |
6 |
| toml_datum_t toml_string_at(const toml_array_t* arr, int idx) |
| 2189 |
|
| { |
| 2190 |
|
| toml_datum_t ret; |
| 2191 |
6 |
| memset(&ret, 0, sizeof(ret)); |
| 2192 |
6 |
| ret.ok = (0 == toml_rtos(toml_raw_at(arr, idx), &ret.u.s)); |
| 2193 |
6 |
| return ret; |
| 2194 |
|
| } |
| 2195 |
|
| |
| 2196 |
4 |
| toml_datum_t toml_bool_at(const toml_array_t* arr, int idx) |
| 2197 |
|
| { |
| 2198 |
|
| toml_datum_t ret; |
| 2199 |
4 |
| memset(&ret, 0, sizeof(ret)); |
| 2200 |
4 |
| ret.ok = (0 == toml_rtob(toml_raw_at(arr, idx), &ret.u.b)); |
| 2201 |
4 |
| return ret; |
| 2202 |
|
| } |
| 2203 |
|
| |
| 2204 |
4 |
| toml_datum_t toml_int_at(const toml_array_t* arr, int idx) |
| 2205 |
|
| { |
| 2206 |
|
| toml_datum_t ret; |
| 2207 |
4 |
| memset(&ret, 0, sizeof(ret)); |
| 2208 |
4 |
| ret.ok = (0 == toml_rtoi(toml_raw_at(arr, idx), &ret.u.i)); |
| 2209 |
4 |
| return ret; |
| 2210 |
|
| } |
| 2211 |
|
| |
| 2212 |
1 |
| toml_datum_t toml_double_at(const toml_array_t* arr, int idx) |
| 2213 |
|
| { |
| 2214 |
|
| toml_datum_t ret; |
| 2215 |
1 |
| memset(&ret, 0, sizeof(ret)); |
| 2216 |
1 |
| ret.ok = (0 == toml_rtod(toml_raw_at(arr, idx), &ret.u.d)); |
| 2217 |
1 |
| return ret; |
| 2218 |
|
| } |
| 2219 |
|
| |
| 2220 |
0 |
| toml_datum_t toml_timestamp_at(const toml_array_t* arr, int idx) |
| 2221 |
|
| { |
| 2222 |
|
| toml_timestamp_t ts; |
| 2223 |
|
| toml_datum_t ret; |
| 2224 |
0 |
| memset(&ret, 0, sizeof(ret)); |
| 2225 |
0 |
| ret.ok = (0 == toml_rtots(toml_raw_at(arr, idx), &ts)); |
| 2226 |
0 |
▶ | if (ret.ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.ok) { |
0 |
0 |
0 |
|
| 2227 |
0 |
| ret.ok = !!(ret.u.ts = malloc(sizeof(*ret.u.ts))); |
| 2228 |
0 |
▶ | if (ret.ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.ok) { |
0 |
0 |
0 |
|
| 2229 |
0 |
| *ret.u.ts = ts; |
| 2230 |
0 |
▶ | if (ret.u.ts->year) ret.u.ts->year = &ret.u.ts->__buffer.year; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->year) ret.u.ts->year = &ret.u.ts->__buffer.year; |
0 |
0 |
0 |
|
| 2231 |
0 |
▶ | if (ret.u.ts->month) ret.u.ts->month = &ret.u.ts->__buffer.month; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->month) ret.u.ts->month = &ret.u.ts->__buffer.month; |
0 |
0 |
0 |
|
| 2232 |
0 |
▶ | if (ret.u.ts->day) ret.u.ts->day = &ret.u.ts->__buffer.day; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->day) ret.u.ts->day = &ret.u.ts->__buffer.day; |
0 |
0 |
0 |
|
| 2233 |
0 |
▶ | if (ret.u.ts->hour) ret.u.ts->hour = &ret.u.ts->__buffer.hour; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->hour) ret.u.ts->hour = &ret.u.ts->__buffer.hour; |
0 |
0 |
0 |
|
| 2234 |
0 |
▶ | if (ret.u.ts->minute) ret.u.ts->minute = &ret.u.ts->__buffer.minute; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->minute) ret.u.ts->minute = &ret.u.ts->__buffer.minute; |
0 |
0 |
0 |
|
| 2235 |
0 |
▶ | if (ret.u.ts->second) ret.u.ts->second = &ret.u.ts->__buffer.second; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->second) ret.u.ts->second = &ret.u.ts->__buffer.second; |
0 |
0 |
0 |
|
| 2236 |
0 |
▶ | if (ret.u.ts->millisec) ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->millisec) ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; |
0 |
0 |
0 |
|
| 2237 |
0 |
▶ | if (ret.u.ts->z) ret.u.ts->z = ret.u.ts->__buffer.z; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->z) ret.u.ts->z = ret.u.ts->__buffer.z; |
0 |
0 |
0 |
|
| 2238 |
|
| } |
| 2239 |
|
| } |
| 2240 |
0 |
| return ret; |
| 2241 |
|
| } |
| 2242 |
|
| |
| 2243 |
11 |
| toml_datum_t toml_string_in(const toml_table_t* arr, const char* key) |
| 2244 |
|
| { |
| 2245 |
|
| toml_datum_t ret; |
| 2246 |
11 |
| memset(&ret, 0, sizeof(ret)); |
| 2247 |
11 |
| toml_raw_t raw = toml_raw_in(arr, key); |
| 2248 |
11 |
▶ | if (raw) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw) { |
11 |
0 |
11 |
|
| 2249 |
11 |
| ret.ok = (0 == toml_rtos(raw, &ret.u.s)); |
| 2250 |
|
| } |
| 2251 |
11 |
| return ret; |
| 2252 |
|
| } |
| 2253 |
|
| |
| 2254 |
4 |
| toml_datum_t toml_bool_in(const toml_table_t* arr, const char* key) |
| 2255 |
|
| { |
| 2256 |
|
| toml_datum_t ret; |
| 2257 |
4 |
| memset(&ret, 0, sizeof(ret)); |
| 2258 |
4 |
| ret.ok = (0 == toml_rtob(toml_raw_in(arr, key), &ret.u.b)); |
| 2259 |
4 |
| return ret; |
| 2260 |
|
| } |
| 2261 |
|
| |
| 2262 |
3 |
| toml_datum_t toml_int_in(const toml_table_t* arr, const char* key) |
| 2263 |
|
| { |
| 2264 |
|
| toml_datum_t ret; |
| 2265 |
3 |
| memset(&ret, 0, sizeof(ret)); |
| 2266 |
3 |
| ret.ok = (0 == toml_rtoi(toml_raw_in(arr, key), &ret.u.i)); |
| 2267 |
3 |
| return ret; |
| 2268 |
|
| } |
| 2269 |
|
| |
| 2270 |
3 |
| toml_datum_t toml_double_in(const toml_table_t* arr, const char* key) |
| 2271 |
|
| { |
| 2272 |
|
| toml_datum_t ret; |
| 2273 |
3 |
| memset(&ret, 0, sizeof(ret)); |
| 2274 |
3 |
| ret.ok = (0 == toml_rtod(toml_raw_in(arr, key), &ret.u.d)); |
| 2275 |
3 |
| return ret; |
| 2276 |
|
| } |
| 2277 |
|
| |
| 2278 |
1 |
| toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key) |
| 2279 |
|
| { |
| 2280 |
|
| toml_timestamp_t ts; |
| 2281 |
|
| toml_datum_t ret; |
| 2282 |
1 |
| memset(&ret, 0, sizeof(ret)); |
| 2283 |
1 |
| ret.ok = (0 == toml_rtots(toml_raw_in(arr, key), &ts)); |
| 2284 |
1 |
▶ | if (ret.ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.ok) { |
1 |
0 |
1 |
|
| 2285 |
1 |
| ret.ok = !!(ret.u.ts = malloc(sizeof(*ret.u.ts))); |
| 2286 |
1 |
▶ | if (ret.ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.ok) { |
1 |
0 |
1 |
|
| 2287 |
1 |
| *ret.u.ts = ts; |
| 2288 |
1 |
▶ | if (ret.u.ts->year) ret.u.ts->year = &ret.u.ts->__buffer.year; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->year) ret.u.ts->year = &ret.u.ts->__buffer.year; |
1 |
0 |
1 |
|
| 2289 |
1 |
▶ | if (ret.u.ts->month) ret.u.ts->month = &ret.u.ts->__buffer.month; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->month) ret.u.ts->month = &ret.u.ts->__buffer.month; |
1 |
0 |
1 |
|
| 2290 |
1 |
▶ | if (ret.u.ts->day) ret.u.ts->day = &ret.u.ts->__buffer.day; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->day) ret.u.ts->day = &ret.u.ts->__buffer.day; |
1 |
0 |
1 |
|
| 2291 |
1 |
▶ | if (ret.u.ts->hour) ret.u.ts->hour = &ret.u.ts->__buffer.hour; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->hour) ret.u.ts->hour = &ret.u.ts->__buffer.hour; |
1 |
0 |
1 |
|
| 2292 |
1 |
▶ | if (ret.u.ts->minute) ret.u.ts->minute = &ret.u.ts->__buffer.minute; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->minute) ret.u.ts->minute = &ret.u.ts->__buffer.minute; |
1 |
0 |
1 |
|
| 2293 |
1 |
▶ | if (ret.u.ts->second) ret.u.ts->second = &ret.u.ts->__buffer.second; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->second) ret.u.ts->second = &ret.u.ts->__buffer.second; |
1 |
0 |
1 |
|
| 2294 |
1 |
▶ | if (ret.u.ts->millisec) ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->millisec) ret.u.ts->millisec = &ret.u.ts->__buffer.millisec; |
0 |
1 |
1 |
|
| 2295 |
1 |
▶ | if (ret.u.ts->z) ret.u.ts->z = ret.u.ts->__buffer.z; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret.u.ts->z) ret.u.ts->z = ret.u.ts->__buffer.z; |
1 |
0 |
1 |
|
| 2296 |
|
| } |
| 2297 |
|
| } |
| 2298 |
1 |
| return ret; |
| 2299 |
|
| } |