Adds toml_raw_t to seperate raw and processed data

This just makes it more obvious that toml_raw_at returns a incomplete
bit of data. Users can easily see how to use rto* with this.
This commit is contained in:
dcresswell 2020-07-04 16:48:10 -07:00
parent e1e7369907
commit 5d1d8e3389
3 changed files with 21 additions and 19 deletions

View File

@ -62,7 +62,7 @@ if (0 == (server = toml_table_in(conf, "server"))) {
4. Convert that value to the appropriate type (I.E. string, int). 4. Convert that value to the appropriate type (I.E. string, int).
```c ```c
const char* raw; toml_raw_t raw;
char* host; char* host;
int64_t port; int64_t port;

17
toml.c
View File

@ -1741,8 +1741,7 @@ const char* toml_key_in(const toml_table_t* tab, int keyidx)
return 0; return 0;
} }
toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key)
const char* toml_raw_in(const toml_table_t* tab, const char* key)
{ {
int i; int i;
for (i = 0; i < tab->nkval; i++) { for (i = 0; i < tab->nkval; i++) {
@ -1773,7 +1772,7 @@ toml_table_t* toml_table_in(const toml_table_t* tab, const char* key)
return 0; return 0;
} }
const char* toml_raw_at(const toml_array_t* arr, int idx) toml_raw_t toml_raw_at(const toml_array_t* arr, int idx)
{ {
if (arr->kind != 'v') if (arr->kind != 'v')
return 0; return 0;
@ -1848,7 +1847,7 @@ toml_table_t* toml_table_at(const toml_array_t* arr, int idx)
} }
int toml_rtots(const char* src_, toml_timestamp_t* ret) int toml_rtots(toml_raw_t src_, toml_timestamp_t* ret)
{ {
if (! src_) return -1; if (! src_) return -1;
@ -1942,7 +1941,7 @@ int toml_rtots(const char* src_, toml_timestamp_t* ret)
/* Raw to boolean */ /* Raw to boolean */
int toml_rtob(const char* src, int* ret_) int toml_rtob(toml_raw_t src, int* ret_)
{ {
if (!src) return -1; if (!src) return -1;
int dummy; int dummy;
@ -1961,7 +1960,7 @@ int toml_rtob(const char* src, int* ret_)
/* Raw to integer */ /* Raw to integer */
int toml_rtoi(const char* src, int64_t* ret_) int toml_rtoi(toml_raw_t src, int64_t* ret_)
{ {
if (!src) return -1; if (!src) return -1;
@ -2024,7 +2023,7 @@ int toml_rtoi(const char* src, int64_t* ret_)
} }
int toml_rtod_ex(const char* src, double* ret_, char* buf, int buflen) int toml_rtod_ex(toml_raw_t src, double* ret_, char* buf, int buflen)
{ {
if (!src) return -1; if (!src) return -1;
@ -2086,7 +2085,7 @@ int toml_rtod_ex(const char* src, double* ret_, char* buf, int buflen)
return (errno || *endp) ? -1 : 0; return (errno || *endp) ? -1 : 0;
} }
int toml_rtod(const char* src, double* ret_) int toml_rtod(toml_raw_t src, double* ret_)
{ {
char buf[100]; char buf[100];
return toml_rtod_ex(src, ret_, buf, sizeof(buf)); return toml_rtod_ex(src, ret_, buf, sizeof(buf));
@ -2095,7 +2094,7 @@ int toml_rtod(const char* src, double* ret_)
int toml_rtos(const char* src, char** ret) int toml_rtos(toml_raw_t src, char** ret)
{ {
int multiline = 0; int multiline = 0;
const char* sp; const char* sp;

21
toml.h
View File

@ -39,6 +39,9 @@
typedef struct toml_table_t toml_table_t; typedef struct toml_table_t toml_table_t;
typedef struct toml_array_t toml_array_t; typedef struct toml_array_t toml_array_t;
/* A raw value, must be processed by toml_rto* before using. */
typedef const char* toml_raw_t;
/* Parse a file. Return a table on success, or 0 otherwise. /* Parse a file. Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use. * Caller must toml_free(the-return-value) after use.
*/ */
@ -61,7 +64,7 @@ TOML_EXTERN void toml_free(toml_table_t* tab);
TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx); TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx);
/* Lookup table by key. Return the element or 0 if not found. */ /* Lookup table by key. Return the element or 0 if not found. */
TOML_EXTERN const char* toml_raw_in(const toml_table_t* tab, const char* key); TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key);
TOML_EXTERN toml_array_t* toml_array_in(const toml_table_t* tab, TOML_EXTERN toml_array_t* toml_array_in(const toml_table_t* tab,
const char* key); const char* key);
TOML_EXTERN toml_table_t* toml_table_in(const toml_table_t* tab, TOML_EXTERN toml_table_t* toml_table_in(const toml_table_t* tab,
@ -96,26 +99,26 @@ TOML_EXTERN int toml_table_ntab(const toml_table_t* tab);
TOML_EXTERN const char* toml_table_key(const toml_table_t* tab); TOML_EXTERN const char* toml_table_key(const toml_table_t* tab);
/* Deref array by index. Return the element at idx or 0 if out of range. */ /* Deref array by index. Return the element at idx or 0 if out of range. */
TOML_EXTERN const char* toml_raw_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t* arr, int idx);
TOML_EXTERN toml_array_t* toml_array_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_array_t* toml_array_at(const toml_array_t* arr, int idx);
TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx);
/* Raw to String. Caller must call free(ret) after use. /* Raw to String. Caller must call free(ret) after use.
* Return 0 on success, -1 otherwise. * Return 0 on success, -1 otherwise.
*/ */
TOML_EXTERN int toml_rtos(const char* s, char** ret); TOML_EXTERN int toml_rtos(toml_raw_t s, char** ret);
/* Raw to Boolean. Return 0 on success, -1 otherwise. */ /* Raw to Boolean. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtob(const char* s, int* ret); TOML_EXTERN int toml_rtob(toml_raw_t s, int* ret);
/* Raw to Integer. Return 0 on success, -1 otherwise. */ /* Raw to Integer. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtoi(const char* s, int64_t* ret); TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t* ret);
/* Raw to Double. Return 0 on success, -1 otherwise. */ /* Raw to Double. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtod(const char* s, double* ret); TOML_EXTERN int toml_rtod(toml_raw_t s, double* ret);
/* Same as toml_rtod, but return the sanitized double in string form as well */ /* Same as toml_rtod, but return the sanitized double in string form as well */
TOML_EXTERN int toml_rtod_ex(const char* s, double* ret, char* buf, int buflen); TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double* ret, char* buf, int buflen);
/* Timestamp types. The year, month, day, hour, minute, second, z /* Timestamp types. The year, month, day, hour, minute, second, z
* fields may be NULL if they are not relevant. e.g. In a DATE * fields may be NULL if they are not relevant. e.g. In a DATE
@ -134,7 +137,7 @@ struct toml_timestamp_t {
}; };
/* Raw to Timestamp. Return 0 on success, -1 otherwise. */ /* Raw to Timestamp. Return 0 on success, -1 otherwise. */
TOML_EXTERN int toml_rtots(const char* s, toml_timestamp_t* ret); TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t* ret);
/* misc */ /* misc */
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret); TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret);