/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "toml_internal.h" TomlValue TomlValue_new(TomlType type) { TomlValue value = {0}; value.type = type; switch (type) { default: assert(false && "invalid type"); break; case TLIBTOML_TABLE: value.value.table = NULL; break; case TLIBTOML_ARRAY: value.value.array = NULL; break; case TLIBTOML_STRING: value.value.s = NULL; break; case TLIBTOML_INTEGER: value.value.i = 0; break; case TLIBTOML_FLOAT: value.value.f = 0.0; break; case TLIBTOML_BOOLEAN: value.value.b = false; break; case TLIBTOML_DATETIME: value.value.dt = (TomlDateTime*)malloc(sizeof(TomlDateTime)); memset(value.value.dt, 0, sizeof(TomlDateTime)); break; } return value; } TomlValue TomlValue_from_str(str s) { TomlValue value = {0}; value.value.s = str_copy(s); value.type = TLIBTOML_STRING; return value; } TomlValue TomlValue_new_table(void) { TomlValue value = {0}; value.value.table = TomlTable_new(); value.type = TLIBTOML_TABLE; return value; } TomlValue TomlValue_new_array(void) { TomlValue value = {0}; value.value.array = TomlArray_new(); value.type = TLIBTOML_ARRAY; return value; } TomlValue TomlValue_new_integer(i64 integer) { TomlValue value = {0}; value.value.i = integer; value.type = TLIBTOML_INTEGER; return value; } TomlValue TomlValue_new_float(f64 float_) { TomlValue value = {0}; value.value.f = float_; value.type = TLIBTOML_FLOAT; return value; } TomlValue TomlValue_new_datetime(void) { return TomlValue_new(TLIBTOML_BOOLEAN); } TomlValue TomlValue_new_bool(bool b) { TomlValue value = {0}; value.value.b = b; value.type = TLIBTOML_BOOLEAN; return value; } void TomlValue_destroy(TomlValue* self) { if (self == NULL) return; switch (self->type) { case TLIBTOML_STRING: str_destroy(self->value.s); break; case TLIBTOML_TABLE: TomlTable_free(self->value.table); break; case TLIBTOML_ARRAY: TomlArray_free(self->value.array); break; case TLIBTOML_DATETIME: free(self->value.dt); break; default: break; } }