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