Skip to content

Commit

Permalink
added json parser to fs/surf
Browse files Browse the repository at this point in the history
  • Loading branch information
nesbox committed May 3, 2024
1 parent 2a1ae54 commit e69c53f
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 313 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@
[submodule "vendor/jsmn"]
path = vendor/jsmn
url = https://github.com/zserge/jsmn.git
shallow = true
3 changes: 1 addition & 2 deletions cmake/core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)

if(BUILD_STATIC)
if(BUILD_WITH_LUA)
# !TODO: make it PRIVATE
target_link_libraries(tic80core${SCRIPT} PUBLIC lua)
target_link_libraries(tic80core${SCRIPT} PRIVATE lua)
endif()

if(BUILD_WITH_MOON)
Expand Down
1 change: 1 addition & 0 deletions cmake/studio.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(TIC80STUDIO_SRC
${TIC80LIB_DIR}/ext/history.c
${TIC80LIB_DIR}/ext/gif.c
${TIC80LIB_DIR}/ext/png.c
${TIC80LIB_DIR}/ext/json.c
)

if(${BUILD_PRO})
Expand Down
110 changes: 110 additions & 0 deletions src/ext/json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// MIT License

// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "json.h"

#define JSMN_STATIC
#define JSMN_PARENT_LINKS
#include <jsmn.h>

#include <stdio.h>

static struct
{
jsmn_parser p;
jsmntok_t* t;
s32 tcount, count;
const char* json;
} state = {.tcount = 128};

bool json_parse(const char *json, s32 size)
{
if(state.t == NULL)
state.t = malloc(state.tcount * sizeof *state.t);

jsmn_init(&state.p);

state.json = json;

while((state.count = jsmn_parse(&state.p, state.json, size, state.t, state.tcount)) == JSMN_ERROR_NOMEM)
state.t = realloc(state.t, (state.tcount *= 2) * sizeof *state.t);

return state.count >= 0 && state.t[0].type == JSMN_OBJECT;
}

static s32 jsoneq(const char *json, const jsmntok_t *tok, const char *s)
{
if (tok->type == JSMN_STRING
&& strlen(s) == tok->end - tok->start
&& strncmp(json + tok->start, s, tok->end - tok->start) == 0)
return 0;

return -1;
}

static s32 getJsonItem(const char *var, s32 parent, jsmntype_t type)
{
for(s32 i = parent; i < state.count; i++)
if(jsoneq(state.json, &state.t[i], var) == 0 && state.t[i + 1].type & type)
return i + 1;

return 0;
}

s32 json_int(const char *var, s32 parent)
{
return atoi(state.json + state.t[getJsonItem(var, parent, JSMN_PRIMITIVE)].start);
}

bool json_string(const char *var, s32 parent, char* value, s32 size)
{
const jsmntok_t* t = &state.t[getJsonItem(var, parent, JSMN_STRING)];
return snprintf(value, size, "%.*s", t->end - t->start, state.json + t->start) > 0;
}

s32 json_array(const char *var, s32 parent)
{
return getJsonItem(var, parent, JSMN_ARRAY);
}

s32 json_array_size(s32 array)
{
s32 count = 0;

for(s32 i = state.t[array].parent; i < state.count; i++)
if(state.t[i].parent == array)
count++;

return count;
}

s32 json_array_item(s32 array, s32 index)
{
for(s32 i = state.t[array].parent, count = 0; i < state.count; i++)
if(state.t[i].parent == array && index == count++)
return i;
}

s32 json_object(const char *var, s32 parent)
{
return getJsonItem(var, parent, JSMN_OBJECT);
}
34 changes: 34 additions & 0 deletions src/ext/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// MIT License

// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "tic.h"

bool json_parse(const char *json, s32 size);

s32 json_int(const char *var, s32 parent);
bool json_string(const char *var, s32 parent, char* value, s32 size);
s32 json_array(const char *var, s32 parent);
s32 json_array_size(s32 array);
s32 json_array_item(s32 array, s32 index);
s32 json_object(const char *var, s32 parent);

0 comments on commit e69c53f

Please sign in to comment.