| 1 | /* Copyright (c) 2024-2026, CK Tan. |
| 2 | * https://github.com/cktan/tomlc17/blob/main/LICENSE |
| 3 | */ |
| 4 | #ifndef TOMLC17_H |
| 5 | #define TOMLC17_H |
| 6 | |
| 7 | /* |
| 8 | * USAGE: |
| 9 | * |
| 10 | * 1. Call toml_parse(), toml_parse_file(), or toml_parse_file_ex() |
| 11 | * 2. Check result.ok |
| 12 | * 3. Use toml_get() or toml_seek() to query and traverse the |
| 13 | * result.toptab |
| 14 | * 4. Call toml_free() to release resources. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <stdbool.h> |
| 19 | #include <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #ifdef __cplusplus |
| 23 | #define TOML_EXTERN extern "C" |
| 24 | #else |
| 25 | #define TOML_EXTERN extern |
| 26 | #endif |
| 27 | |
| 28 | enum toml_type_t { |
| 29 | TOML_UNKNOWN = 0, |
| 30 | TOML_STRING, |
| 31 | TOML_INT64, |
| 32 | TOML_FP64, |
| 33 | TOML_BOOLEAN, |
| 34 | TOML_DATE, |
| 35 | TOML_TIME, |
| 36 | TOML_DATETIME, |
| 37 | TOML_DATETIMETZ, |
| 38 | TOML_ARRAY, |
| 39 | TOML_TABLE, |
| 40 | }; |
| 41 | typedef enum toml_type_t toml_type_t; |
| 42 | |
| 43 | /* This is a Node in a Tree that represents a toml document rooted |
| 44 | * at toml_result_t::toptab. |
| 45 | */ |
| 46 | typedef struct toml_datum_t toml_datum_t; |
| 47 | struct toml_datum_t { |
| 48 | toml_type_t type; |
| 49 | uint32_t flag; // internal |
| 50 | union { |
| 51 | const char *s; // same as str.ptr; use if there are no NUL in string. |
| 52 | struct { |
| 53 | const char *ptr; // NUL terminated string |
| 54 | int len; // length excluding the terminating NUL. |
| 55 | } str; |
| 56 | int64_t int64; // integer |
| 57 | double fp64; // float |
| 58 | bool boolean; |
| 59 | struct { // date, time |
| 60 | int16_t year, month, day; |
| 61 | int16_t hour, minute, second; |
| 62 | int32_t usec; |
| 63 | int16_t tz; // in minutes |
| 64 | } ts; |
| 65 | struct { // array |
| 66 | int32_t size; // count elem |
| 67 | toml_datum_t *elem; // elem[] |
| 68 | } arr; |
| 69 | struct { // table |
| 70 | int32_t size; // count key |
| 71 | const char **key; // key[] |
| 72 | int *len; // len[] |
| 73 | toml_datum_t *value; // value[] |
| 74 | } tab; |
| 75 | } u; |
| 76 | }; |
| 77 | |
| 78 | /* Result returned by toml_parse() */ |
| 79 | typedef struct toml_result_t toml_result_t; |
| 80 | struct toml_result_t { |
| 81 | bool ok; // success flag |
| 82 | toml_datum_t toptab; // valid if ok |
| 83 | char errmsg[200]; // valid if not ok |
| 84 | void *__internal; // do not use |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Parse a toml document. Returns a toml_result which must be freed |
| 89 | * using toml_free() eventually. |
| 90 | * |
| 91 | * IMPORTANT: src[] must be a NUL terminated string! The len parameter |
| 92 | * does not include the NUL terminator. |
| 93 | */ |
| 94 | TOML_EXTERN toml_result_t toml_parse(const char *src, int len); |
| 95 | |
| 96 | /** |
| 97 | * Parse a toml file. Returns a toml_result which must be freed |
| 98 | * using toml_free() eventually. |
| 99 | * |
| 100 | * IMPORTANT: you are still responsible to fclose(fp). |
| 101 | */ |
| 102 | TOML_EXTERN toml_result_t toml_parse_file(FILE *fp); |
| 103 | |
| 104 | /** |
| 105 | * Parse a toml file. Returns a toml_result which must be freed |
| 106 | * using toml_free() eventually. |
| 107 | */ |
| 108 | TOML_EXTERN toml_result_t toml_parse_file_ex(const char *fname); |
| 109 | |
| 110 | /** |
| 111 | * Release the result. |
| 112 | */ |
| 113 | TOML_EXTERN void toml_free(toml_result_t result); |
| 114 | |
| 115 | /** |
| 116 | * Find a key in a toml_table. Return the value of the key if found, |
| 117 | * or a TOML_UNKNOWN otherwise. |
| 118 | */ |
| 119 | TOML_EXTERN toml_datum_t toml_get(toml_datum_t table, const char *key); |
| 120 | |
| 121 | /** |
| 122 | * Locate a value starting from a toml_table. Return the value of the key if |
| 123 | * found, or a TOML_UNKNOWN otherwise. |
| 124 | * |
| 125 | * Note: the multipart-key is separated by DOT, and must not have any escape |
| 126 | * chars. The maximum length of the multipart_key must not exceed 127 bytes. |
| 127 | */ |
| 128 | TOML_EXTERN toml_datum_t toml_seek(toml_datum_t table, |
| 129 | const char *multipart_key); |
| 130 | |
| 131 | /** |
| 132 | * OBSOLETE: use toml_get() instead. |
| 133 | * Find a key in a toml_table. Return the value of the key if found, |
| 134 | * or a TOML_UNKNOWN otherwise. ( |
| 135 | */ |
| 136 | static inline toml_datum_t toml_table_find(toml_datum_t table, |
| 137 | const char *key) { |
| 138 | return toml_get(table, key); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Override values in r1 using r2. Return a new result. All results |
| 143 | * (i.e., r1, r2 and the returned result) must be freed using toml_free() |
| 144 | * after use. |
| 145 | * |
| 146 | * LOGIC: |
| 147 | * ret = copy of r1 |
| 148 | * for each item x in r2: |
| 149 | * if x is not in ret: |
| 150 | * override |
| 151 | * elif x in ret is NOT of the same type: |
| 152 | * override |
| 153 | * elif x is an array of tables: |
| 154 | * append r2.x to ret.x |
| 155 | * elif x is a table: |
| 156 | * merge r2.x to ret.x |
| 157 | * else: |
| 158 | * override |
| 159 | */ |
| 160 | TOML_EXTERN toml_result_t toml_merge(const toml_result_t *r1, |
| 161 | const toml_result_t *r2); |
| 162 | |
| 163 | /** |
| 164 | * Check if two results are the same. Dictionary and array orders are |
| 165 | * sensitive. |
| 166 | */ |
| 167 | TOML_EXTERN bool toml_equiv(const toml_result_t *r1, const toml_result_t *r2); |
| 168 | |
| 169 | /* Options that override tomlc17 defaults globally */ |
| 170 | typedef struct toml_option_t toml_option_t; |
| 171 | struct toml_option_t { |
| 172 | bool check_utf8; // Check all chars are valid utf8; default: false. |
| 173 | void *(*mem_realloc)(void *ptr, size_t size); // default: realloc() |
| 174 | void (*mem_free)(void *ptr); // default: free() |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * Get the default options. IF NECESSARY, use this to initialize |
| 179 | * toml_option_t and override values before calling |
| 180 | * toml_set_option(). |
| 181 | */ |
| 182 | TOML_EXTERN toml_option_t toml_default_option(void); |
| 183 | |
| 184 | /** |
| 185 | * Set toml options globally. Do this ONLY IF you are not satisfied with the |
| 186 | * defaults. |
| 187 | */ |
| 188 | TOML_EXTERN void toml_set_option(toml_option_t opt); |
| 189 | |
| 190 | #endif // TOMLC17_H |