Mechanical whitespace-only change across src/, include/, tests/, and util/, applied with scripts/reindent.sh. No behavioral change. Most files already conformed. The genuine outliers indented at 2 columns (json_helpers.c, util.c from akgl_rectangle_points onward, assets.c, staticstring.c, akgl_actor_add_child, util.h, staticstring.h) or used spaces where the canonical form is a tab (draw.c). cc-mode also re-aligns line-continuation backslashes in multi-line macros to its default c-backslash-column, which is required for the tree to be a fixed point of the indenter. Verified whitespace-only: `git diff -w` over this change is empty apart from three trailing blank lines removed at EOF in src/heap.c, src/registry.c, and tests/tilemap.c. The build produces no new errors and ctest is unchanged at 13/14, with `character` still the one intentional failure. The tree is now a fixed point of `scripts/reindent.sh --check`. include/akgl/SDL_GameControllerDB.h is generated and excluded. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
171 lines
6.6 KiB
C
171 lines
6.6 KiB
C
/**
|
|
* @file json_helpers.c
|
|
* @brief Implements the json helpers subsystem.
|
|
*/
|
|
|
|
#include <jansson.h>
|
|
#include <string.h>
|
|
#include <akerror.h>
|
|
|
|
#include <akgl/json_helpers.h>
|
|
#include <akgl/game.h>
|
|
#include <akgl/heap.h>
|
|
#include <akgl/staticstring.h>
|
|
#include <akgl/registry.h>
|
|
|
|
akerr_ErrorContext *akgl_get_json_object_value(json_t *obj, char *key, json_t **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL object pointer");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_object(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = value;
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_boolean_value(json_t *obj, char *key, bool *dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL object pointer");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_boolean(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = json_boolean_value(value);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_integer_value(json_t *obj, char *key, int *dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL object pointer");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_integer(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = json_integer_value(value);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_number_value(json_t *obj, char *key, float *dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_number(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = json_number_value(value);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext AKERR_NOIGNORE *akgl_get_json_double_value(json_t *obj, char *key, double *dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_number(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = json_number_value(value);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_string_value(json_t *obj, char *key, akgl_String **dest)
|
|
{
|
|
json_t *value = NULL;
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
FAIL_ZERO_RETURN(errctx, key, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
FAIL_ZERO_RETURN(errctx, dest, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
|
|
value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_string(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
ATTEMPT {
|
|
if ( *dest == NULL ) {
|
|
CATCH(errctx, akgl_heap_next_string(dest));
|
|
CATCH(errctx, akgl_string_initialize(*dest, NULL));
|
|
}
|
|
} CLEANUP {
|
|
} PROCESS(errctx) {
|
|
} FINISH(errctx, false);
|
|
|
|
strncpy((char *)&(*dest)->data, json_string_value(value), AKGL_MAX_STRING_LENGTH);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_array_value(json_t *obj, char *key, json_t **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, obj, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
json_t *value = json_object_get(obj, key);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_KEY, "Key %s not found in object", key);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_array(value)), AKERR_TYPE, "Key %s in object has incorrect type", key);
|
|
*dest = value;
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_array_index_object(json_t *array, int index, json_t **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, array, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
json_t *value = json_array_get(array, index);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_OUTOFBOUNDS, "Index %d out of bounds for array", index);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_object(value)), AKERR_TYPE, "Index %d in object has incorrect type", index);
|
|
*dest = value;
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_array_index_integer(json_t *array, int index, int *dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, array, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
json_t *value = json_array_get(array, index);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_OUTOFBOUNDS, "Index %d out of bounds for array", index);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_integer(value)), AKERR_TYPE, "Index %d in object has incorrect type", index);
|
|
*dest = json_integer_value(value);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext *akgl_get_json_array_index_string(json_t *array, int index, akgl_String **dest)
|
|
{
|
|
PREPARE_ERROR(errctx);
|
|
FAIL_ZERO_RETURN(errctx, array, AKERR_NULLPOINTER, "NULL pointer reference");
|
|
FAIL_ZERO_RETURN(errctx, dest, AKERR_NULLPOINTER, "NULL destination pointer reference");
|
|
json_t *value = json_array_get(array, index);
|
|
FAIL_ZERO_RETURN(errctx, value, AKERR_OUTOFBOUNDS, "Index %d out of bounds for array", index);
|
|
FAIL_ZERO_RETURN(errctx, (json_is_string(value)), AKERR_TYPE, "Index %d in object has incorrect type", index);
|
|
ATTEMPT {
|
|
if ( *dest == NULL ) {
|
|
CATCH(errctx, akgl_heap_next_string(dest));
|
|
CATCH(errctx, akgl_string_initialize(*dest, NULL));
|
|
}
|
|
} CLEANUP {
|
|
} PROCESS(errctx) {
|
|
} FINISH(errctx, false);
|
|
|
|
strncpy((char *)&(*dest)->data, json_string_value(value), AKGL_MAX_STRING_LENGTH);
|
|
SUCCEED_RETURN(errctx);
|
|
}
|
|
|
|
akerr_ErrorContext AKERR_NOIGNORE *akgl_get_json_with_default(akerr_ErrorContext *err, void *defval, void *dest, uint32_t defsize)
|
|
{
|
|
PREPARE_ERROR(e);
|
|
if ( err == NULL ) {
|
|
SUCCEED_RETURN(e);
|
|
}
|
|
int docopy = 0;
|
|
|
|
FAIL_ZERO_RETURN(e, err, AKERR_NULLPOINTER, "err");
|
|
FAIL_ZERO_RETURN(e, defval, AKERR_NULLPOINTER, "defval");
|
|
FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "dest");
|
|
|
|
ATTEMPT {
|
|
} CLEANUP {
|
|
} PROCESS(err) {
|
|
} HANDLE_GROUP(err, AKERR_KEY) {
|
|
} HANDLE_GROUP(err, AKERR_INDEX) {
|
|
memcpy(dest, defval, defsize);
|
|
} FINISH(err, true);
|
|
|
|
SUCCEED_RETURN(e);
|
|
}
|