Skip to content

Commit 1d2184c

Browse files
authored
Create toml.h
1 parent db007a9 commit 1d2184c

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

toml.h

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
*/
62+
TOML_EXTERN void toml_free(toml_table_t *tab);
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+
*/
68+
struct toml_timestamp_t {
69+
struct { /* internal. do not use. */
70+
int year, month, day;
71+
int hour, minute, second, millisec;
72+
char z[10];
73+
} __buffer;
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. */
95+
TOML_EXTERN int toml_array_nelem(const toml_array_t *arr);
96+
/* ... retrieve values using index. */
97+
TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx);
98+
TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx);
99+
TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx);
100+
TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx);
101+
TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx);
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. */
112+
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr,
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);
116+
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr,
117+
const char *key);
118+
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr,
119+
const char *key);
120+
/* .. retrieve array or table using key. */
121+
TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab,
122+
const char *key);
123+
TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab,
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 */
142+
TOML_EXTERN int toml_table_nkval(const toml_table_t *tab);
143+
144+
/* Return the number of arrays in a table */
145+
TOML_EXTERN int toml_table_narr(const toml_table_t *tab);
146+
147+
/* Return the number of sub-tables in a table */
148+
TOML_EXTERN int toml_table_ntab(const toml_table_t *tab);
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);
173+
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret);
174+
175+
#endif /* TOML_H */

0 commit comments

Comments
 (0)