From 5d1d8e33894947949c98eba5d77c096a6a28ff4c Mon Sep 17 00:00:00 2001 From: dcresswell Date: Sat, 4 Jul 2020 16:48:10 -0700 Subject: [PATCH] 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. --- README.md | 2 +- toml.c | 17 ++++++++--------- toml.h | 21 ++++++++++++--------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 708cd29..6b5b544 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ if (0 == (server = toml_table_in(conf, "server"))) { 4. Convert that value to the appropriate type (I.E. string, int). ```c -const char* raw; +toml_raw_t raw; char* host; int64_t port; diff --git a/toml.c b/toml.c index 4fd84a4..a5c8052 100644 --- a/toml.c +++ b/toml.c @@ -1741,8 +1741,7 @@ const char* toml_key_in(const toml_table_t* tab, int keyidx) return 0; } - -const char* toml_raw_in(const toml_table_t* tab, const char* key) +toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key) { int 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; } -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') 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; @@ -1942,7 +1941,7 @@ int toml_rtots(const char* src_, toml_timestamp_t* ret) /* Raw to boolean */ -int toml_rtob(const char* src, int* ret_) +int toml_rtob(toml_raw_t src, int* ret_) { if (!src) return -1; int dummy; @@ -1961,7 +1960,7 @@ int toml_rtob(const char* src, int* ret_) /* 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; @@ -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; @@ -2086,7 +2085,7 @@ int toml_rtod_ex(const char* src, double* ret_, char* buf, int buflen) 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]; 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; const char* sp; diff --git a/toml.h b/toml.h index f61135d..ff851ba 100644 --- a/toml.h +++ b/toml.h @@ -39,6 +39,9 @@ typedef struct toml_table_t toml_table_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. * 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); /* 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, const char* key); 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); /* 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_table_t* toml_table_at(const toml_array_t* arr, int idx); - /* Raw to String. Caller must call free(ret) after use. * 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. */ -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. */ -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. */ -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 */ -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 * 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. */ -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 */ TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret);