1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/lib/webfuse_provider/impl/json/node.c
2020-07-11 21:05:00 +02:00

168 lines
3.3 KiB
C

#include "webfuse_provider/impl/json/node.h"
#include "webfuse_provider/impl/json/node_intern.h"
#include <stdlib.h>
#include <string.h>
bool
wfp_impl_json_is_bool(
struct wfp_json const * json)
{
return (WFP_JSON_BOOL == json->type);
}
bool
wfp_impl_json_is_int(
struct wfp_json const * json)
{
return (WFP_JSON_INT == json->type);
}
bool
wfp_impl_json_is_string(
struct wfp_json const * json)
{
return (WFP_JSON_STRING == json->type);
}
bool
wfp_impl_json_is_array(
struct wfp_json const * json)
{
return (WFP_JSON_ARRAY == json->type);
}
bool
wfp_impl_json_is_object(
struct wfp_json const * json)
{
return (WFP_JSON_OBJECT == json->type);
}
bool
wfp_impl_json_get_bool(
struct wfp_json const * json)
{
return (WFP_JSON_BOOL == json->type) ? json->value.b : false;
}
int
wfp_impl_json_get_int(
struct wfp_json const * json)
{
return (WFP_JSON_INT == json->type) ? json->value.i : 0;
}
const char *
wfp_impl_json_get_string(
struct wfp_json const * json)
{
return (WFP_JSON_STRING == json->type) ? json->value.s : "";
}
struct wfp_json const *
wfp_impl_json_array_get(
struct wfp_json const * json,
size_t pos)
{
struct wfp_json const * result = NULL;
if ((WFP_JSON_ARRAY == json->type) && (pos < json->value.a.size))
{
return &(json->value.a.items[pos]);
}
return result;
}
size_t
wfp_impl_json_array_size(
struct wfp_json const * json)
{
return (WFP_JSON_ARRAY == json->type) ? json->value.a.size : 0;
}
size_t
wfp_impl_json_object_size(
struct wfp_json const * json)
{
return (WFP_JSON_OBJECT == json->type) ? json->value.o.size : 0;
}
struct wfp_json const *
wfp_impl_json_object_get(
struct wfp_json const * json,
char const * key)
{
size_t const count = wfp_impl_json_object_size(json);
for (size_t i = 0; i < count; i++)
{
if (0 == strcmp(key, json->value.o.items[i].key))
{
return &(json->value.o.items[i].json);
}
}
return NULL;
}
char const *
wfp_impl_json_object_key(
struct wfp_json const * json,
size_t pos)
{
size_t const count = wfp_impl_json_object_size(json);
if (pos < count)
{
return json->value.o.items[pos].key;
}
return "";
}
struct wfp_json const *
wfp_impl_json_object_value(
struct wfp_json const * json,
size_t pos)
{
size_t const count = wfp_impl_json_object_size(json);
if (pos < count)
{
return &(json->value.o.items[pos].json);
}
return NULL;
}
void
wfp_impl_json_cleanup(
struct wfp_json * json)
{
switch (json->type)
{
case WFP_JSON_ARRAY:
{
size_t const count = json->value.a.size;
for (size_t i = 0; i < count; i++)
{
wfp_impl_json_cleanup(&(json->value.a.items[i]));
}
free(json->value.a.items);
}
break;
case WFP_JSON_OBJECT:
{
size_t const count = json->value.o.size;
for (size_t i = 0; i < count; i++)
{
wfp_impl_json_cleanup(&(json->value.o.items[i].json));
}
free(json->value.o.items);
}
break;
default:
break;
}
}