Skip to content

Commit

Permalink
YAJL fixes and some other new util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Feb 2, 2025
1 parent 116424f commit 2c249e4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
18 changes: 11 additions & 7 deletions src/cdogs/c_array.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2018, 2021 Cong Xu
Copyright (c) 2013-2018, 2021, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -81,14 +81,16 @@ void CArrayCopy(CArray *dst, const CArray *src)
memcpy(dst->data, src->data, src->size * src->elemSize);
}

void CArrayPushBack(CArray *a, const void *elem)
void *CArrayPushBack(CArray *a, const void *elem)
{
CASSERT(a->elemSize > 0, "array has not been initialised");
GrowIfFull(a);
a->size++;
memcpy(CArrayGet(a, (int)a->size - 1), elem, a->elemSize);
void *loc = CArrayGet(a, (int)a->size - 1);
memcpy(loc, elem, a->elemSize);
return loc;
}
void CArrayInsert(CArray *a, const size_t idx, const void *elem)
void *CArrayInsert(CArray *a, const size_t idx, const void *elem)
{
CASSERT(a->elemSize > 0, "array has not been initialised");
GrowIfFull(a);
Expand All @@ -99,7 +101,7 @@ void CArrayInsert(CArray *a, const size_t idx, const void *elem)
CArrayGet(a, idx + 1), CArrayGet(a, idx),
a->elemSize * (a->size - 1 - idx));
}
CArraySet(a, idx, elem);
return CArraySet(a, idx, elem);
}
void CArrayDelete(CArray *a, const size_t idx)
{
Expand Down Expand Up @@ -137,9 +139,11 @@ void *CArrayGet(const CArray *a, const size_t idx)
CASSERT(idx < a->size, "array index out of bounds");
return &((char *)a->data)[idx * a->elemSize];
}
void CArraySet(CArray *a, const size_t idx, const void *elem)
void *CArraySet(CArray *a, const size_t idx, const void *elem)
{
memcpy(CArrayGet(a, idx), elem, a->elemSize);
void *loc = CArrayGet(a, idx);
memcpy(loc, elem, a->elemSize);
return loc;
}

void CArrayClear(CArray *a)
Expand Down
8 changes: 4 additions & 4 deletions src/cdogs/c_array.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2013-2018, 2021 Cong Xu
Copyright (c) 2013-2018, 2021, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -43,13 +43,13 @@ void CArrayInitFill(
void CArrayInitFillZero(CArray *a, const size_t elemSize, const size_t size);
void CArrayReserve(CArray *a, size_t capacity);
void CArrayCopy(CArray *dst, const CArray *src);
void CArrayPushBack(CArray *a, const void *elem); // insert address
void CArrayInsert(CArray *a, const size_t index, const void *elem);
void *CArrayPushBack(CArray *a, const void *elem); // insert address
void *CArrayInsert(CArray *a, const size_t index, const void *elem);
void CArrayDelete(CArray *a, const size_t index);
void CArrayPopBack(CArray *a);
void CArrayResize(CArray *a, const size_t size, const void *value);
void *CArrayGet(const CArray *a, const size_t index); // gets address
void CArraySet(CArray *a, const size_t idx, const void *elem);
void *CArraySet(CArray *a, const size_t idx, const void *elem);
void CArrayClear(CArray *a);
void CArrayRemoveIf(CArray *a, bool (*removeIf)(const void *));
void CArrayFill(CArray *a, const void *elem);
Expand Down
5 changes: 5 additions & 0 deletions src/cdogs/c_hashmap/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ typedef struct hashmap_map *map_t;
*/
map_t hashmap_new(void);

/*
* Perform a copy of the hashmap, with optional callback to perform
* per-element copies. Leave NULL to perform a shallow value-only
* copy.
*/
map_t hashmap_copy(const map_t in, any_t (*callback)(any_t));

/*
Expand Down
19 changes: 17 additions & 2 deletions src/cdogs/character_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2016-2017, 2019-2021, 2023 Cong Xu
Copyright (c) 2016-2017, 2019-2021, 2023, 2025 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -54,6 +54,22 @@ const CharacterClass *StrCharacterClass(const char *s)
LOG(LM_MAIN, LL_ERROR, "Cannot find character name: %s", s);
return NULL;
}

static void CharacterClassFree(CharacterClass *c);

void CharacterClassCopy(CharacterClass *dst, const CharacterClass *src)
{
CharacterClassFree(dst);
memcpy(dst, src, sizeof *dst);
CSTRDUP(dst->Name, src->Name);
if (src->HeadSprites) CSTRDUP(dst->HeadSprites, src->HeadSprites);
if (src->Body) CSTRDUP(dst->Body, src->Body);
if (src->DeathSprites) CSTRDUP(dst->DeathSprites, src->DeathSprites);
if (src->Sounds) CSTRDUP(dst->Sounds, src->Sounds);
if (src->Footsteps) CSTRDUP(dst->Footsteps, src->Footsteps);
if (src->Corpse) CSTRDUP(dst->Corpse, src->Corpse);
}

static const char *characterNames[] = {
"Jones", "Ice", "Ogre", "Dragon", "WarBaby", "Bug-eye",
"Smith", "Ogre Boss", "Grunt", "Professor", "Snake", "Wolf",
Expand Down Expand Up @@ -423,7 +439,6 @@ static void LoadCharacterClass(CharacterClass *c, json_t *node)

LoadStr(&c->Corpse, node, "Corpse");
}
static void CharacterClassFree(CharacterClass *c);
void CharacterClassesClear(CArray *classes)
{
for (int i = 0; i < (int)classes->size; i++)
Expand Down
3 changes: 2 additions & 1 deletion src/cdogs/character_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2016-2021, 2023 Cong Xu
Copyright (c) 2016-2021, 2023, 2025 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -60,6 +60,7 @@ extern CharacterClasses gCharacterClasses;
#define CHARACTER_DEFAULT_MASS 100

const CharacterClass *StrCharacterClass(const char *s);
void CharacterClassCopy(CharacterClass *dst, const CharacterClass *src);
// Legacy character class from "face" index
const char *IntCharacterFace(const int face);
void CharacterOldFaceToHeadParts(const char *face, char **newFace, char *headParts[HEAD_PART_COUNT]);
Expand Down
60 changes: 33 additions & 27 deletions src/cdogs/yajl_utils.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
Copyright (c) 2013-2017, 2025 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -28,7 +28,6 @@
#include <stdio.h>
#include <stdlib.h>


static char *ReadFile(const char *filename);
yajl_val YAJLReadFile(const char *filename)
{
Expand All @@ -43,7 +42,8 @@ yajl_val YAJLReadFile(const char *filename)
node = yajl_tree_parse(buf, errbuf, sizeof errbuf);
if (node == NULL)
{
fprintf(stderr, "Error parsing font JSON '%s': %s\n", filename, errbuf);
fprintf(
stderr, "Error parsing font JSON '%s': %s\n", filename, errbuf);
goto bail;
}

Expand All @@ -56,13 +56,18 @@ static char *ReadFile(const char *filename)
{
FILE *f = fopen(filename, "r");
char *buf = NULL;
if (f == NULL) goto bail;
if (fseek(f, 0L, SEEK_END) != 0) goto bail;
if (f == NULL)
goto bail;
if (fseek(f, 0L, SEEK_END) != 0)
goto bail;
const long bufsize = ftell(f);
if (bufsize == -1) goto bail;
if (fseek(f, 0L, SEEK_SET) != 0) goto bail;
if (bufsize == -1)
goto bail;
if (fseek(f, 0L, SEEK_SET) != 0)
goto bail;
buf = malloc(bufsize + 1);
if (buf == NULL) goto bail;
if (buf == NULL)
goto bail;
const size_t readlen = fread(buf, 1, bufsize, f);
buf[readlen] = '\0';

Expand All @@ -74,20 +79,20 @@ static char *ReadFile(const char *filename)
return buf;
}

#define YAJL_CHECK(func) \
{ \
yajl_gen_status _status = func;\
if (_status != yajl_gen_status_ok) \
{\
return _status;\
}\
}
#define YAJL_CHECK(func) \
{ \
yajl_gen_status _status = func; \
if (_status != yajl_gen_status_ok) \
{ \
return _status; \
} \
}

yajl_gen_status YAJLAddIntPair(yajl_gen g, const char *name, const int number)
{
char buf[32];
sprintf(buf, "%d", number);
return YAJLAddStringPair(g, name, buf);
YAJL_CHECK(yajl_gen_string(g, (const unsigned char *)name, strlen(name)));
YAJL_CHECK(yajl_gen_integer(g, number));
return yajl_gen_status_ok;
}
yajl_gen_status YAJLAddBoolPair(yajl_gen g, const char *name, const bool value)
{
Expand All @@ -98,7 +103,8 @@ 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_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));
YAJL_CHECK(yajl_gen_string(
g, (const unsigned char *)(s ? s : ""), s ? strlen(s) : 0));
return yajl_gen_status_ok;
}
yajl_gen_status YAJLAddColorPair(yajl_gen g, const char *name, const color_t c)
Expand All @@ -114,7 +120,7 @@ bool YAJLTryLoadValue(yajl_val *node, const char *name)
{
return false;
}
const char *path[] = { name, NULL };
const char *path[] = {name, NULL};
*node = yajl_tree_get(*node, path, yajl_t_any);
return *node != NULL;
}
Expand Down Expand Up @@ -156,7 +162,7 @@ void YAJLStr(char **value, yajl_val node, const char *name)
{
return;
}
*value = YAJLGetStr(node, name);
CSTRDUP(*value, node->u.string);
}
char *YAJLGetStr(yajl_val node, const char *name)
{
Expand Down Expand Up @@ -209,14 +215,14 @@ void YAJLLoadColor(color_t *c, yajl_val node, const char *name)
{
return;
}
char *in = YAJL_GET_STRING(YAJLFindNode(node, name));
*c = StrColor(in);
*c = StrColor(node->u.string);
}
yajl_val YAJLFindNode(yajl_val node, const char *path)
{
// max 256 levels
const char *pathSplit[256];
for (int i = 0; i < 256; i++) pathSplit[i] = NULL;
for (int i = 0; i < 256; i++)
pathSplit[i] = NULL;
char *pathCopy;
CSTRDUP(pathCopy, path);
char *pch = strtok(pathCopy, "/");
Expand Down

0 comments on commit 2c249e4

Please sign in to comment.