GVM User Suite
User tools for the GVM open source project.
toml.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 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 #ifdef _MSC_VER
29 #pragma warning(disable: 4996)
30 #endif
31 
32 #include <stdint.h>
33 #include <stdio.h>
34 
35 #ifdef __cplusplus
36 #define TOML_EXTERN extern "C"
37 #else
38 #define TOML_EXTERN extern
39 #endif
40 
41 typedef struct toml_timestamp_t toml_timestamp_t;
42 typedef struct toml_table_t toml_table_t;
43 typedef struct toml_array_t toml_array_t;
44 typedef struct toml_datum_t toml_datum_t;
45 
46 /* Parse a file. Return a table on success, or 0 otherwise.
47  * Caller must toml_free(the-return-value) after use.
48  */
49 TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz);
50 
51 /* Parse a string containing the full config.
52  * Return a table on success, or 0 otherwise.
53  * Caller must toml_free(the-return-value) after use.
54  */
55 TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */
56  char *errbuf, int errbufsz);
57 
58 /* Free the table returned by toml_parse() or toml_parse_file(). Once
59  * this function is called, any handles accessed through this tab
60  * directly or indirectly are no longer valid.
61  */
63 
64 /* Timestamp types. The year, month, day, hour, minute, second, z
65  * fields may be NULL if they are not relevant. e.g. In a DATE
66  * type, the hour, minute, second and z fields will be NULLs.
67  */
69  struct { /* internal. do not use. */
70  int year, month, day;
72  char z[10];
74  int *year, *month, *day;
75  int *hour, *minute, *second, *millisec;
76  char *z;
77 };
78 
79 /*-----------------------------------------------------------------
80  * Enhanced access methods
81  */
82 struct toml_datum_t {
83  int ok;
84  union {
85  toml_timestamp_t *ts; /* ts must be freed after use */
86  char *s; /* string value. s must be freed after use */
87  int b; /* bool value */
88  int64_t i; /* int value */
89  double d; /* double value */
90  } u;
91 };
92 
93 /* on arrays: */
94 /* ... retrieve size of array. */
96 /* ... retrieve values using index. */
102 /* ... retrieve array or table using index. */
103 TOML_EXTERN toml_array_t *toml_array_at(const toml_array_t *arr, int idx);
104 TOML_EXTERN toml_table_t *toml_table_at(const toml_array_t *arr, int idx);
105 
106 /* on tables: */
107 /* ... retrieve the key in table at keyidx. Return 0 if out of range. */
108 TOML_EXTERN const char *toml_key_in(const toml_table_t *tab, int keyidx);
109 /* ... returns 1 if key exists in tab, 0 otherwise */
110 TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key);
111 /* ... retrieve values using key. */
113  const char *key);
114 TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key);
115 TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key);
117  const char *key);
119  const char *key);
120 /* .. retrieve array or table using key. */
122  const char *key);
124  const char *key);
125 
126 /*-----------------------------------------------------------------
127  * lesser used
128  */
129 /* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */
130 TOML_EXTERN char toml_array_kind(const toml_array_t *arr);
131 
132 /* For array kind 'v'alue, return the type of values
133  i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed
134  0 if unknown
135 */
136 TOML_EXTERN char toml_array_type(const toml_array_t *arr);
137 
138 /* Return the key of an array */
139 TOML_EXTERN const char *toml_array_key(const toml_array_t *arr);
140 
141 /* Return the number of key-values in a table */
143 
144 /* Return the number of arrays in a table */
146 
147 /* Return the number of sub-tables in a table */
149 
150 /* Return the key of a table*/
151 TOML_EXTERN const char *toml_table_key(const toml_table_t *tab);
152 
153 /*--------------------------------------------------------------
154  * misc
155  */
156 TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret);
157 TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
158 TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
159  void (*xxfree)(void *));
160 
161 /*--------------------------------------------------------------
162  * deprecated
163  */
164 /* A raw value, must be processed by toml_rto* before using. */
165 typedef const char *toml_raw_t;
166 TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key);
167 TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx);
168 TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret);
169 TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret);
170 TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret);
171 TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret);
172 TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen);
174 
175 #endif /* TOML_H */
union toml_datum_t::@1 u
int b
Definition: toml.h:87
double d
Definition: toml.h:89
char * s
Definition: toml.h:86
int ok
Definition: toml.h:83
int64_t i
Definition: toml.h:88
toml_timestamp_t * ts
Definition: toml.h:85
struct toml_timestamp_t::@0 __buffer
int millisec
Definition: toml.h:71
char z[10]
Definition: toml.h:72
int * day
Definition: toml.h:74
char * z
Definition: toml.h:76
int minute
Definition: toml.h:71
int second
Definition: toml.h:71
int * hour
Definition: toml.h:75
#define TOML_EXTERN
Definition: toml.h:38
TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t), void(*xxfree)(void *))
Definition: toml.c:41
TOML_EXTERN const char * toml_array_key(const toml_array_t *arr)
Definition: toml.c:1937
TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2327
TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret)
Definition: toml.c:2050
TOML_EXTERN toml_table_t * toml_parse(char *conf, char *errbuf, int errbufsz)
Definition: toml.c:1397
TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret)
Definition: toml.c:96
TOML_EXTERN const char * toml_key_in(const toml_table_t *tab, int keyidx)
Definition: toml.c:1860
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret)
Definition: toml.c:2068
const char * toml_raw_t
Definition: toml.h:165
TOML_EXTERN void toml_free(toml_table_t *tab)
Definition: toml.c:1578
TOML_EXTERN toml_table_t * toml_parse_file(FILE *fp, char *errbuf, int errbufsz)
Definition: toml.c:1475
TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key)
Definition: toml.c:1875
TOML_EXTERN toml_table_t * toml_table_at(const toml_array_t *arr, int idx)
Definition: toml.c:1955
TOML_EXTERN char toml_array_kind(const toml_array_t *arr)
Definition: toml.c:1923
TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx)
Definition: toml.c:2252
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2310
TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx)
Definition: toml.c:2273
TOML_EXTERN int toml_table_ntab(const toml_table_t *tab)
Definition: toml.c:1945
TOML_EXTERN int toml_table_narr(const toml_table_t *tab)
Definition: toml.c:1943
TOML_EXTERN int toml_array_nelem(const toml_array_t *arr)
Definition: toml.c:1935
TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret)
Definition: toml.c:2200
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2334
TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx)
Definition: toml.c:2259
TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret)
Definition: toml.c:2205
TOML_EXTERN toml_table_t * toml_table_in(const toml_table_t *tab, const char *key)
Definition: toml.c:1910
TOML_EXTERN int toml_table_nkval(const toml_table_t *tab)
Definition: toml.c:1941
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key)
Definition: toml.c:1892
TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen)
Definition: toml.c:2141
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2341
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret)
Definition: toml.c:1961
TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx)
Definition: toml.c:2266
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx)
Definition: toml.c:1919
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6])
Definition: toml.c:197
TOML_EXTERN toml_array_t * toml_array_in(const toml_table_t *tab, const char *key)
Definition: toml.c:1901
TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key)
Definition: toml.c:2320
TOML_EXTERN toml_array_t * toml_array_at(const toml_array_t *arr, int idx)
Definition: toml.c:1951
TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx)
Definition: toml.c:2280
TOML_EXTERN const char * toml_table_key(const toml_table_t *tab)
Definition: toml.c:1947
TOML_EXTERN char toml_array_type(const toml_array_t *arr)
Definition: toml.c:1925