85 lines
3.4 KiB
C
85 lines
3.4 KiB
C
/* 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"
|
|
|
|
|
|
Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
|
|
TomlArray* key_path, bool is_array, bool create_if_not_exist)
|
|
{
|
|
Deferral(1);
|
|
TomlTable* real_table = table;
|
|
|
|
if (is_array) {
|
|
u64 i = 0;
|
|
for (; i < key_path->len - 1; i++) {
|
|
str part = *key_path->data[i].s;
|
|
TomlValue* t = TomlTable_tryGet(real_table, part);
|
|
if (t == NULL) {
|
|
if (create_if_not_exist) {
|
|
TomlValue new_table_value = TomlValue_new_table();
|
|
TomlTable_set(real_table, part, new_table_value);
|
|
real_table = new_table_value.table;
|
|
} else {
|
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
|
"%s:%d:%d: not found key '" FMT_str "'",
|
|
parser->filename, parser->lineno, parser->colno, part.len, part.data);
|
|
}
|
|
} else {
|
|
real_table = t->table;
|
|
}
|
|
}
|
|
|
|
str part = *key_path->data[i].s;
|
|
TomlValue* t = TomlTable_tryGet(real_table, part);
|
|
if (t == NULL) {
|
|
if (create_if_not_exist) {
|
|
TomlValue array_value = TomlValue_new_array();
|
|
TomlValue new_table_value = TomlValue_new_table();
|
|
TomlArray_append(array_value.array, new_table_value);
|
|
TomlTable_set(real_table, part, array_value);
|
|
real_table = new_table_value.table;
|
|
} else {
|
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
|
"%s:%d:%d: not found key '" FMT_str "'",
|
|
parser->filename, parser->lineno, parser->colno, part.len, part.data);
|
|
}
|
|
} else {
|
|
if (t->type != TLIBTOML_ARRAY) {
|
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
|
"%s:%d:%d: this key was not an array",
|
|
parser->filename, parser->lineno, parser->colno);
|
|
}
|
|
|
|
TomlValue new_table_value = TomlValue_new_table();
|
|
TomlArray_append(t->array, new_table_value);
|
|
real_table = new_table_value.table;
|
|
}
|
|
} else {
|
|
for (u64 i = 0; i < key_path->len; i++) {
|
|
str part = *key_path->data[i].s;
|
|
TomlValue* t = TomlTable_tryGet(real_table, part);
|
|
if (t == NULL) {
|
|
if (create_if_not_exist) {
|
|
TomlValue new_table_value = TomlValue_new_table();
|
|
TomlTable_set(real_table, part, new_table_value);
|
|
real_table = new_table_value.table;
|
|
} else {
|
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
|
"%s:%d:%d: not found key '" FMT_str "'",
|
|
parser->filename, parser->lineno, parser->colno, part.len, part.data);
|
|
}
|
|
} else {
|
|
if (t->type == TLIBTOML_ARRAY) {
|
|
real_table = t->array->data[t->array->len - 1].table;
|
|
} else if (t->type == TLIBTOML_TABLE) {
|
|
real_table = t->table;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Return RESULT_VALUE(p, real_table);
|
|
}
|