slight change in returning timestamp. now returns a malloc-ed ptr

This commit is contained in:
CK Tan 2020-11-09 14:00:27 -08:00
parent 893f24e206
commit 94136b9f04
2 changed files with 17 additions and 3 deletions

18
toml.c
View file

@ -2195,11 +2195,18 @@ toml_datum_t toml_double_at(const toml_array_t* arr, int idx)
toml_datum_t toml_timestamp_at(const toml_array_t* arr, int idx) toml_datum_t toml_timestamp_at(const toml_array_t* arr, int idx)
{ {
toml_timestamp_t ts;
toml_datum_t ret; toml_datum_t ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
toml_raw_t raw = toml_raw_at(arr, idx); toml_raw_t raw = toml_raw_at(arr, idx);
if (raw) { if (raw) {
ret.ok = (0 == toml_rtots(raw, &ret.u.ts)); ret.ok = (0 == toml_rtots(raw, &ts));
if (ret.ok) {
ret.ok = !!(ret.u.ts = malloc(sizeof(*ret.u.ts)));
if (ret.ok) {
*ret.u.ts = ts;
}
}
} }
return ret; return ret;
} }
@ -2250,11 +2257,18 @@ toml_datum_t toml_double_in(const toml_table_t* arr, const char* key)
toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key) toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key)
{ {
toml_timestamp_t ts;
toml_datum_t ret; toml_datum_t ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
toml_raw_t raw = toml_raw_in(arr, key); toml_raw_t raw = toml_raw_in(arr, key);
if (raw) { if (raw) {
ret.ok = (0 == toml_rtots(raw, &ret.u.ts)); ret.ok = (0 == toml_rtots(raw, &ts));
if (ret.ok) {
ret.ok = !!(ret.u.ts = malloc(sizeof(*ret.u.ts)));
if (ret.ok) {
*ret.u.ts = ts;
}
}
} }
return ret; return ret;
} }

2
toml.h
View file

@ -85,11 +85,11 @@ struct toml_timestamp_t {
struct toml_datum_t { struct toml_datum_t {
int ok; int ok;
union { union {
toml_timestamp_t* ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */ char* s; /* string value. s must be freed after use */
int b; /* bool value */ int b; /* bool value */
int64_t i; /* int value */ int64_t i; /* int value */
double d; /* double value */ double d; /* double value */
toml_timestamp_t ts;
} u; } u;
}; };