update JSON library

This commit is contained in:
rr- 2021-02-22 22:29:13 +01:00
parent 38bb80bdbb
commit 2a9bbfc078
5 changed files with 3522 additions and 1266 deletions

View file

@ -10,7 +10,7 @@
T1MConfig.OPT = JSONGetBooleanValue(json, QUOTE(OPT)); \
} while (0)
static int8_t ReadBarShowingMode(json_value* root, const char* name)
static int8_t ReadBarShowingMode(struct json_value_s* root, const char* name)
{
const char* value_str = JSONGetStringValue(root, name);
if (!value_str) {
@ -23,8 +23,8 @@ static int8_t ReadBarShowingMode(json_value* root, const char* name)
return T1M_BSM_DEFAULT;
}
static int8_t
ReadBarLocationConfig(json_value* root, const char* name, int8_t default_value)
static int8_t ReadBarLocationConfig(
struct json_value_s* root, const char* name, int8_t default_value)
{
const char* value_str = JSONGetStringValue(root, name);
if (!value_str) {
@ -64,7 +64,8 @@ int T1MReadConfig()
fread(cfg_data, 1, cfg_size, fp);
fclose(fp);
json_value* json = json_parse((const json_char*)cfg_data, cfg_size);
struct json_value_s* json = json_parse_ex(
cfg_data, cfg_size, json_parse_flags_allow_json5, NULL, NULL, NULL);
READ_BOOL(disable_healing_between_levels);
READ_BOOL(disable_medpacks);
@ -93,7 +94,7 @@ int T1MReadConfig()
T1MConfig.enemy_healthbar_location = ReadBarLocationConfig(
json, "enemy_healthbar_location", T1M_BL_VBOTTOM | T1M_BL_HLEFT);
json_value_free(json);
free(json);
free(cfg_data);
return 1;
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,39 +1,42 @@
#include "json_utils.h"
#include <string.h>
json_value* JSONGetField(
json_value* root, json_type field_type, const char* name, int* pIndex)
struct json_value_s* JSONGetField(struct json_value_s* root, const char* name)
{
if (root == NULL || root->type != json_object) {
if (root == NULL || root->type != json_type_object) {
return NULL;
}
json_value* result = NULL;
unsigned int len = name ? strlen(name) : 0;
unsigned int i = pIndex ? *pIndex : 0;
for (; i < root->u.object.length; ++i) {
if (root->u.object.values[i].value->type == field_type) {
if (!name
|| (len == root->u.object.values[i].name_length
&& !strncmp(root->u.object.values[i].name, name, len))) {
result = root->u.object.values[i].value;
break;
struct json_object_s* object = json_value_as_object(root);
struct json_object_element_s* item = object->start;
while (item) {
if (!strcmp(item->name->string, name)) {
return item->value;
}
item = item->next;
}
}
if (pIndex) {
*pIndex = i;
}
return result;
return NULL;
}
int JSONGetBooleanValue(json_value* root, const char* name)
int8_t JSONGetBooleanValue(struct json_value_s* root, const char* name)
{
json_value* field = JSONGetField(root, json_boolean, name, NULL);
return field ? field->u.boolean : 0;
struct json_value_s* field = JSONGetField(root, name);
if (!field
|| (field->type != json_type_true && field->type != json_type_false)) {
return 0;
}
return field->type == json_type_true;
}
const char* JSONGetStringValue(json_value* root, const char* name)
const char* JSONGetStringValue(struct json_value_s* root, const char* name)
{
json_value* field = JSONGetField(root, json_string, name, NULL);
return field ? field->u.string.ptr : NULL;
struct json_value_s* field = JSONGetField(root, name);
if (!field || field->type != json_type_string) {
return NULL;
}
struct json_string_s* string = json_value_as_string(field);
if (!string) {
return NULL;
}
const char* ret = string->string;
return ret;
}

View file

@ -3,10 +3,8 @@
#include "json-parser/json.h"
json_value* JSONGetField(
json_value* root, json_type field_type, const char* name, int* pIndex);
int JSONGetBooleanValue(json_value* root, const char* name);
const char* JSONGetStringValue(json_value* root, const char* name);
struct json_value_s* JSONGetField(struct json_value_s* root, const char* name);
int8_t JSONGetBooleanValue(struct json_value_s* root, const char* name);
const char* JSONGetStringValue(struct json_value_s* root, const char* name);
#endif