| 1 |
/* |
| 2 |
MIT License |
| 3 |
|
| 4 |
Copyright (c) 2017 - 2019 CK Tan |
| 5 |
https://github.com/cktan/tomlc99 |
| 6 |
|
| 7 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
of this software and associated documentation files (the "Software"), to deal |
| 9 |
in the Software without restriction, including without limitation the rights |
| 10 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
copies of the Software, and to permit persons to whom the Software is |
| 12 |
furnished to do so, subject to the following conditions: |
| 13 |
|
| 14 |
The above copyright notice and this permission notice shall be included in all |
| 15 |
copies or substantial portions of the Software. |
| 16 |
|
| 17 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
SOFTWARE. |
| 24 |
*/ |
| 25 |
#ifndef TOML_H |
| 26 |
#define TOML_H |
| 27 |
|
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdint.h> |
| 31 |
|
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
#define TOML_EXTERN extern "C" |
| 35 |
#else |
| 36 |
#define TOML_EXTERN extern |
| 37 |
#endif |
| 38 |
|
| 39 |
typedef struct toml_table_t toml_table_t; |
| 40 |
typedef struct toml_array_t toml_array_t; |
| 41 |
|
| 42 |
/* Parse a file. Return a table on success, or 0 otherwise. |
| 43 |
* Caller must toml_free(the-return-value) after use. |
| 44 |
*/ |
| 45 |
TOML_EXTERN toml_table_t* toml_parse_file(FILE* fp, |
| 46 |
char* errbuf, |
| 47 |
int errbufsz); |
| 48 |
|
| 49 |
/* Parse a string containing the full config. |
| 50 |
* Return a table on success, or 0 otherwise. |
| 51 |
* Caller must toml_free(the-return-value) after use. |
| 52 |
*/ |
| 53 |
TOML_EXTERN toml_table_t* toml_parse(char* conf, /* NUL terminated, please. */ |
| 54 |
char* errbuf, |
| 55 |
int errbufsz); |
| 56 |
|
| 57 |
/* Free the table returned by toml_parse() or toml_parse_file(). */ |
| 58 |
TOML_EXTERN void toml_free(toml_table_t* tab); |
| 59 |
|
| 60 |
/* Retrieve the key in table at keyidx. Return 0 if out of range. */ |
| 61 |
TOML_EXTERN const char* toml_key_in(toml_table_t* tab, int keyidx); |
| 62 |
|
| 63 |
/* Lookup table by key. Return the element or 0 if not found. */ |
| 64 |
TOML_EXTERN const char* toml_raw_in(toml_table_t* tab, const char* key); |
| 65 |
TOML_EXTERN toml_array_t* toml_array_in(toml_table_t* tab, const char* key); |
| 66 |
TOML_EXTERN toml_table_t* toml_table_in(toml_table_t* tab, const char* key); |
| 67 |
|
| 68 |
/* Return the array kind: 't'able, 'a'rray, 'v'alue */ |
| 69 |
TOML_EXTERN char toml_array_kind(toml_array_t* arr); |
| 70 |
|
| 71 |
/* For array kind 'v'alue, return the type of values |
| 72 |
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp |
| 73 |
0 if unknown |
| 74 |
*/ |
| 75 |
TOML_EXTERN char toml_array_type(toml_array_t* arr); |
| 76 |
|
| 77 |
|
| 78 |
/* Return the number of elements in the array */ |
| 79 |
TOML_EXTERN int toml_array_nelem(toml_array_t* arr); |
| 80 |
|
| 81 |
/* Return the key of an array */ |
| 82 |
TOML_EXTERN const char* toml_array_key(toml_array_t* arr); |
| 83 |
|
| 84 |
/* Return the number of key-values in a table */ |
| 85 |
TOML_EXTERN int toml_table_nkval(toml_table_t* tab); |
| 86 |
|
| 87 |
/* Return the number of arrays in a table */ |
| 88 |
TOML_EXTERN int toml_table_narr(toml_table_t* tab); |
| 89 |
|
| 90 |
/* Return the number of sub-tables in a table */ |
| 91 |
TOML_EXTERN int toml_table_ntab(toml_table_t* tab); |
| 92 |
|
| 93 |
/* Return the key of a table*/ |
| 94 |
TOML_EXTERN const char* toml_table_key(toml_table_t* tab); |
| 95 |
|
| 96 |
/* Deref array by index. Return the element at idx or 0 if out of range. */ |
| 97 |
TOML_EXTERN const char* toml_raw_at(toml_array_t* arr, int idx); |
| 98 |
TOML_EXTERN toml_array_t* toml_array_at(toml_array_t* arr, int idx); |
| 99 |
TOML_EXTERN toml_table_t* toml_table_at(toml_array_t* arr, int idx); |
| 100 |
|
| 101 |
|
| 102 |
/* Raw to String. Caller must call free(ret) after use. |
| 103 |
* Return 0 on success, -1 otherwise. |
| 104 |
*/ |
| 105 |
TOML_EXTERN int toml_rtos(const char* s, char** ret); |
| 106 |
|
| 107 |
/* Raw to Boolean. Return 0 on success, -1 otherwise. */ |
| 108 |
TOML_EXTERN int toml_rtob(const char* s, int* ret); |
| 109 |
|
| 110 |
/* Raw to Integer. Return 0 on success, -1 otherwise. */ |
| 111 |
TOML_EXTERN int toml_rtoi(const char* s, int64_t* ret); |
| 112 |
|
| 113 |
/* Raw to Double. Return 0 on success, -1 otherwise. */ |
| 114 |
TOML_EXTERN int toml_rtod(const char* s, double* ret); |
| 115 |
/* Same as toml_rtod, but return the sanitized double in string form as well */ |
| 116 |
TOML_EXTERN int toml_rtod_ex(const char* s, double* ret, char* buf, int buflen); |
| 117 |
|
| 118 |
/* Timestamp types. The year, month, day, hour, minute, second, z |
| 119 |
* fields may be NULL if they are not relevant. e.g. In a DATE |
| 120 |
* type, the hour, minute, second and z fields will be NULLs. |
| 121 |
*/ |
| 122 |
typedef struct toml_timestamp_t toml_timestamp_t; |
| 123 |
struct toml_timestamp_t { |
| 124 |
struct { /* internal. do not use. */ |
| 125 |
int year, month, day; |
| 126 |
int hour, minute, second, millisec; |
| 127 |
char z[10]; |
| 128 |
} __buffer; |
| 129 |
int *year, *month, *day; |
| 130 |
int *hour, *minute, *second, *millisec; |
| 131 |
char* z; |
| 132 |
}; |
| 133 |
|
| 134 |
/* Raw to Timestamp. Return 0 on success, -1 otherwise. */ |
| 135 |
TOML_EXTERN int toml_rtots(const char* s, toml_timestamp_t* ret); |
| 136 |
|
| 137 |
/* misc */ |
| 138 |
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret); |
| 139 |
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]); |
| 140 |
|
| 141 |
|
| 142 |
#endif /* TOML_H */ |