Skip to content

Commit

Permalink
Add YAJL functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Jan 28, 2025
1 parent db36160 commit c04152e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/cdogs/mission_static.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include "c_array.h"
#include "c_hashmap/hashmap.h"
#include "json_utils.h"
#include "map.h"
#include "map_object.h"
#include "mathc/mathc.h"
Expand Down
1 change: 1 addition & 0 deletions src/cdogs/tile_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "tile_class.h"

#include "door.h"
#include "json_utils.h"
#include "log.h"
#include "pics.h"
#include "sys_config.h"
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/yajl/api/yajl_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Interface to YAJL's JSON generation facilities.
*/

#include <yajl/yajl_common.h>
#include <yajl/api/yajl_common.h>

#ifndef __YAJL_GEN_H__
#define __YAJL_GEN_H__
Expand Down
59 changes: 27 additions & 32 deletions src/cdogs/yajl_utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2017 Cong Xu
Copyright (c) 2013-2017, 2025 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -74,42 +74,40 @@ static char *ReadFile(const char *filename)
return buf;
}

/*
#include "config.h"
#include "weapon.h"
#include "pic_manager.h"
#include "sys_config.h"
#define YAJL_CHECK(func) \
{ \
yajl_gen_status _status = func;\
if (_status != yajl_gen_status_ok) \
{\
return _status;\
}\
}

void AddIntPair(json_t *parent, const char *name, int number)
yajl_gen_status YAJLAddIntPair(yajl_gen g, const char *name, const int number)
{
char buf[32];
sprintf(buf, "%d", number);
json_insert_pair_into_object(parent, name, json_new_number(buf));
return YAJLAddStringPair(g, name, buf);
}
void AddBoolPair(json_t *parent, const char *name, int value)
yajl_gen_status YAJLAddBoolPair(yajl_gen g, const char *name, const bool value)
{
json_insert_pair_into_object(parent, name, json_new_bool(value));
YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
YAJL_CHECK(yajl_gen_bool(g, value));
return yajl_gen_status_ok;
}
void AddStringPair(json_t *parent, const char *name, const char *s)
yajl_gen_status YAJLAddStringPair(yajl_gen g, const char *name, const char *s)
{
if (!s)
{
json_insert_pair_into_object(parent, name, json_new_string(""));
}
else
{
json_insert_pair_into_object(
parent, name, json_new_string(json_escape(s)));
}
YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)(s ? s : ""), s ? strlen(s) : 0));
return yajl_gen_status_ok;
}
void AddColorPair(json_t *parent, const char *name, const color_t c)
yajl_gen_status YAJLAddColorPair(yajl_gen g, const char *name, const color_t c)
{
char buf[COLOR_STR_BUF];
ColorStr(buf, c);
AddStringPair(parent, name, buf);
return YAJLAddStringPair(g, name, buf);
}
*/

bool YAJLTryLoadValue(yajl_val *node, const char *name)
{
if (*node == NULL || !YAJL_IS_OBJECT(*node))
Expand Down Expand Up @@ -204,19 +202,16 @@ void LoadBulletGuns(CArray *guns, json_t *node, const char *name)
CArrayPushBack(guns, &g);
}
}
void LoadColor(color_t *c, json_t *node, const char *name)
*/
void YAJLLoadColor(color_t *c, yajl_val node, const char *name)
{
if (json_find_first_label(node, name) == NULL)
{
return;
}
if (!TryLoadValue(&node, name))
if (!YAJLTryLoadValue(&node, name) || !YAJL_IS_STRING(node))
{
return;
}
*c = StrColor(node->text);
char *in = YAJL_GET_STRING(YAJLFindNode(node, name));
*c = StrColor(in);
}
*/
yajl_val YAJLFindNode(yajl_val node, const char *path)
{
// max 256 levels
Expand Down
17 changes: 8 additions & 9 deletions src/cdogs/yajl_utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2017 Cong Xu
Copyright (c) 2013-2017, 2025 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -28,16 +28,15 @@
#include <stdbool.h>

#include "vector.h"
#include "yajl/api/yajl_gen.h"
#include "yajl/api/yajl_tree.h"


yajl_val YAJLReadFile(const char *filename);
/*
void AddIntPair(json_t *parent, const char *name, int number);
void AddBoolPair(json_t *parent, const char *name, int value);
void AddStringPair(json_t *parent, const char *name, const char *s);
void AddColorPair(json_t *parent, const char *name, const color_t c);
*/
yajl_gen_status YAJLAddIntPair(yajl_gen g, const char *name, const int number);
yajl_gen_status YAJLAddBoolPair(yajl_gen g, const char *name, const bool value);
yajl_gen_status YAJLAddStringPair(yajl_gen g, const char *name, const char *s);
yajl_gen_status YAJLAddColorPair(yajl_gen g, const char *name, const color_t c);
void YAJLBool(bool *value, yajl_val node, const char *name);
void YAJLInt(int *value, yajl_val node, const char *name);
void YAJLDouble(double *value, yajl_val node, const char *name);
Expand All @@ -54,8 +53,8 @@ void LoadSoundFromNode(Mix_Chunk **value, json_t *node, const char *name);
void LoadPic(const Pic **value, json_t *node, const char *name);
// Load an array of const WeaponClass *
void LoadBulletGuns(CArray *guns, json_t *node, const char *name);
void LoadColor(color_t *c, json_t *node, const char *name);
*/
*/
void YAJLLoadColor(color_t *c, yajl_val node, const char *name);
// Try to load a JSON node using a slash-delimited "path"
// If at any point the path fails, NULL is returned.
yajl_val YAJLFindNode(yajl_val node, const char *path);
Expand Down
13 changes: 13 additions & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,16 @@ if(APPLE)
BUILD_WITH_INSTALL_RPATH 1
INSTALL_RPATH "@loader_path/../Frameworks;/Library/Frameworks")
endif()

add_executable(yajl_test yajl_test.c)
target_link_libraries(yajl_test
cbehave
cdogs
SDL2::SDL2)
add_test(NAME yajl_test COMMAND yajl_test)
if(APPLE)
set_target_properties(yajl_test PROPERTIES
MACOSX_RPATH 1
BUILD_WITH_INSTALL_RPATH 1
INSTALL_RPATH "@loader_path/../Frameworks;/Library/Frameworks")
endif()
33 changes: 33 additions & 0 deletions src/tests/yajl_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#define SDL_MAIN_HANDLED
#include <cbehave/cbehave.h>

#include <yajl_utils.h>

FEATURE(string_pair, "String pair")
SCENARIO("String pair")
GIVEN("a string key/value pair")
const char *key = "key";
const char *value = "value";

WHEN("I add the pair")
yajl_gen g = yajl_gen_alloc(NULL);
yajl_gen_map_open(g);
YAJLAddStringPair(g, key, value);
yajl_gen_map_close(g);
const unsigned char *buf;
size_t len;
yajl_gen_get_buf(g, &buf, &len);
AND("I load the document back")
yajl_val node = yajl_tree_parse((const char *)buf, NULL, 0);

THEN("the loaded value should be the same")
char *loadedValue = YAJLGetStr(node, key);
SHOULD_STR_EQUAL(loadedValue, value);
CFREE(loadedValue);
yajl_tree_free(node);
yajl_gen_clear(g);
yajl_gen_free(g);
SCENARIO_END
FEATURE_END

CBEHAVE_RUN("YAJL features are:", TEST_FEATURE(string_pair))

0 comments on commit c04152e

Please sign in to comment.