forked from Telodendria/Cytoplasm
Compare commits
23 commits
sha-revamp
...
master
Author | SHA1 | Date | |
---|---|---|---|
b4841fffaa | |||
|
39e81139f0 | ||
8df5f0f1c1 | |||
|
708c5daad9 | ||
4f316ff7b3 | |||
6827d4fc39 | |||
63bd879101 | |||
f7c51ee019 | |||
af4a142261 | |||
10c8784f25 | |||
4a21567bc5 | |||
ff094b50f2 | |||
8987802437 | |||
|
9fed42d2ac | ||
9c6781c458 | |||
|
33139510b9 | ||
4831f2e03d | |||
|
cc665ac7fc | ||
a121793795 | |||
9e1026d893 | |||
|
5889bec95f | ||
d0b5c441dd | |||
|
d3379d8157 |
10 changed files with 440 additions and 54 deletions
18
.forgejo/workflows/compile.yaml
Normal file
18
.forgejo/workflows/compile.yaml
Normal file
|
@ -0,0 +1,18 @@
|
|||
name: Compile Cytoplasm
|
||||
run-name: Compile Cytoplasm on ${{ forgejo.actor }}
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
"Compile Cytoplasm":
|
||||
strategy:
|
||||
matrix:
|
||||
os: [alpine]
|
||||
arch: [aarch64]
|
||||
runs-on: ["${{ matrix.os }}", "${{ matrix.arch }}"]
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
- name: Configure Cytoplasm
|
||||
run: ./configure
|
||||
- name: Build Cytoplasm
|
||||
run: make
|
|
@ -1,25 +0,0 @@
|
|||
name: Compile Cytoplasm
|
||||
run-name: Compile Cytoplasm on ${{ gitea.actor }}
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
"Compile Cytoplasm":
|
||||
strategy:
|
||||
matrix:
|
||||
os: [debian-v12.4, alpine-v3.19, openbsd-v7.4, freebsd-v14.0, netbsd-v9.3]
|
||||
arch: [x86, x86_64]
|
||||
exclude:
|
||||
# 32-bit OpenBSD does not behave well in QEMU. Even when using
|
||||
# QEMU to emulate i386, it utilizes 100% of its CPU core and is
|
||||
# still extremely sluggish. Thus, we don't have a working 32-bit
|
||||
# OpenBSD runner, so exclude it from the matrix configuration.
|
||||
- os: openbsd-v7.4
|
||||
arch: x86
|
||||
runs-on: ["${{ matrix.os }}", "${{ matrix.arch }}"]
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Configure Cytoplasm
|
||||
run: ./configure
|
||||
- name: Build Cytoplasm
|
||||
run: make
|
5
configure
vendored
5
configure
vendored
|
@ -37,7 +37,7 @@ case "$(uname)" in
|
|||
# These systems typically use GCC.
|
||||
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=gcc"
|
||||
;;
|
||||
OpenBSD|FreeBSD)
|
||||
OpenBSD|FreeBSD|Darwin)
|
||||
# These systems typically use Clang.
|
||||
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=clang"
|
||||
;;
|
||||
|
@ -269,7 +269,8 @@ ${TAB}done
|
|||
${LIB_NAME}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so
|
||||
|
||||
install: ${LIB_NAME}
|
||||
${TAB}mkdir -p ${OUT}/lib
|
||||
${TAB}mkdir -p \$(PREFIX)/${OUT}/lib
|
||||
${TAB}mkdir -p \$(PREFIX)/lib
|
||||
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.a \$(PREFIX)/lib/lib${LIB_NAME}.a
|
||||
${TAB}cp ${OUT}/lib/lib${LIB_NAME}.so \$(PREFIX)/lib/lib${LIB_NAME}.so
|
||||
$(collect ${INCLUDE}/ '' '' \$\(PREFIX\)/include/${LIB_NAME}/ install_out)
|
||||
|
|
|
@ -297,6 +297,12 @@ extern size_t JsonEncode(HashMap *, Stream *, int);
|
|||
*/
|
||||
extern HashMap * JsonDecode(Stream *);
|
||||
|
||||
/**
|
||||
* Decodes a JSON value from thr current input strram and parse it
|
||||
* into a JsonValue.
|
||||
*/
|
||||
extern JsonValue * JsonValueDecode(Stream *);
|
||||
|
||||
/**
|
||||
* A convenience function that allows the caller to retrieve and
|
||||
* arbitrarily deep keys within a JSON object. It takes a root JSON
|
||||
|
|
69
include/Cytoplasm/Platform.h
Normal file
69
include/Cytoplasm/Platform.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef CYTOPLASM_PLATFORM_H
|
||||
#define CYTOPLASM_PLATFORM_H
|
||||
|
||||
/***
|
||||
* @Nm Platform
|
||||
* @Nd A simple macro header that determines what platform the application
|
||||
* is being built for.
|
||||
* @Dd September 21, 2024
|
||||
*/
|
||||
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||
#define PLATFORM_WINDOWS
|
||||
|
||||
#ifdef _WIN64
|
||||
#define PLATFORM_WIN64
|
||||
#else
|
||||
#define PLATFORM_WIN32
|
||||
#endif
|
||||
#elif __APPLE__
|
||||
#define PLATFORM_DARWIN
|
||||
|
||||
#include <TargetConditionals.h>
|
||||
#if TARGET_IPHONE_SIMULATOR
|
||||
#define PLATFORM_IPHONE
|
||||
#elif TARGET_OS_MACCATALYST
|
||||
#define PLATFORM_CATALYST
|
||||
#elif TARGET_OS_IPHONE
|
||||
#define PLATFORM_IPHONE
|
||||
#elif TARGET_OS_MAC
|
||||
#define PLATFORM_MAC
|
||||
#else
|
||||
# error "Unknown Apple platform"
|
||||
#endif
|
||||
#elif __ANDROID__
|
||||
#define PLATFORM_ANDROID
|
||||
#elif __linux__
|
||||
#define PLATFORM_LINUX
|
||||
#elif __unix__
|
||||
#define PLATFORM_UNIX
|
||||
#elif defined(_POSIX_VERSION)
|
||||
#define PLATFORM_POSIX
|
||||
#else
|
||||
# error "Unknown compiler"
|
||||
#endif
|
||||
|
||||
#endif /* CYTOPLASM_PLATFORM_H */
|
|
@ -293,6 +293,7 @@ LMDBUnlock(Db *d, DbRef *r)
|
|||
Stream *stream;
|
||||
MDB_val key, val;
|
||||
bool ret = true;
|
||||
DbHint hint = r ? r->hint : 0;
|
||||
|
||||
if (!d || !r)
|
||||
{
|
||||
|
@ -324,7 +325,7 @@ LMDBUnlock(Db *d, DbRef *r)
|
|||
{
|
||||
free(val.mv_data);
|
||||
}
|
||||
if (ret && r->hint == DB_HINT_WRITE)
|
||||
if (ret && hint == DB_HINT_WRITE)
|
||||
{
|
||||
pthread_mutex_unlock(&d->lock);
|
||||
}
|
||||
|
|
19
src/Json.c
19
src/Json.c
|
@ -1348,6 +1348,25 @@ JsonDecode(Stream * stream)
|
|||
|
||||
return result;
|
||||
}
|
||||
JsonValue *
|
||||
JsonValueDecode(Stream *stream)
|
||||
{
|
||||
JsonValue *result;
|
||||
JsonParserState state;
|
||||
|
||||
state.stream = stream;
|
||||
state.token = NULL;
|
||||
|
||||
JsonTokenSeek(&state);
|
||||
result = JsonDecodeValue(&state);
|
||||
|
||||
if (state.token)
|
||||
{
|
||||
Free(state.token);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
JsonValue *
|
||||
JsonGet(HashMap * json, size_t nArgs,...)
|
||||
|
|
69
src/Memory.c
69
src/Memory.c
|
@ -42,26 +42,30 @@
|
|||
|
||||
#define MEMORY_FILE_SIZE 256
|
||||
|
||||
#define MEM_BOUND_TYPE uint64_t
|
||||
#define MEM_BOUND 0xDEADBEEFBEEFDEAD
|
||||
#define MEM_MAGIC 0xDEADBEEFDEADBEEF
|
||||
|
||||
struct MemoryInfo
|
||||
{
|
||||
uint64_t magic;
|
||||
|
||||
size_t size;
|
||||
size_t unalignedSize;
|
||||
char file[MEMORY_FILE_SIZE];
|
||||
int line;
|
||||
void *pointer;
|
||||
|
||||
MemoryInfo *prev;
|
||||
MemoryInfo *next;
|
||||
|
||||
MEM_BOUND_TYPE leftBoundary;
|
||||
};
|
||||
|
||||
#define MEM_BOUND_TYPE uint32_t
|
||||
#define MEM_BOUND 0xDEADBEEF
|
||||
#define MEM_MAGIC 0xDEADBEEFDEADBEEF
|
||||
#define MEM_SIZE_ACTUAL(x) (MemoryAlignBoundary((x) * sizeof(uint8_t)) + sizeof(MEM_BOUND_TYPE))
|
||||
#define MEM_START_BOUNDARY(info) (info->leftBoundary)
|
||||
#define MEM_END_BOUNDARY(info) (*(((MEM_BOUND_TYPE *) (((uint8_t *) info->pointer) + info->size)) - 1))
|
||||
|
||||
#define MEM_BOUND_LOWER(p) *((MEM_BOUND_TYPE *) p)
|
||||
#define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((uint8_t *) p) + x)) + 1)
|
||||
#define MEM_SIZE_ACTUAL(x) (((x) * sizeof(uint8_t)) + (2 * sizeof(MEM_BOUND_TYPE)))
|
||||
|
||||
static pthread_mutex_t lock;
|
||||
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;
|
||||
|
@ -71,6 +75,19 @@ static size_t allocationsLen = 0;
|
|||
|
||||
static MemoryInfo *allocationTail = NULL;
|
||||
|
||||
/* Simple range of "plausible" boundaries for heap, serving as an extra
|
||||
* check */
|
||||
static void *heapStart, *heapEnd;
|
||||
|
||||
static size_t MemoryAlignBoundary(size_t size)
|
||||
{
|
||||
size_t boundSize = sizeof(MEM_BOUND_TYPE);
|
||||
size_t remainder = size % boundSize;
|
||||
size_t closest = size / boundSize + !!remainder;
|
||||
|
||||
return closest * boundSize;
|
||||
}
|
||||
|
||||
int
|
||||
MemoryRuntimeInit(void)
|
||||
{
|
||||
|
@ -85,6 +102,8 @@ MemoryRuntimeInit(void)
|
|||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
ret = pthread_mutex_init(&lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
heapStart = NULL;
|
||||
heapEnd = NULL;
|
||||
|
||||
ret = (ret == 0);
|
||||
|
||||
|
@ -110,6 +129,15 @@ MemoryInsert(MemoryInfo * a)
|
|||
a->prev = allocationTail;
|
||||
a->magic = MEM_MAGIC;
|
||||
|
||||
if (!heapStart || heapStart > (void *) a)
|
||||
{
|
||||
heapStart = a;
|
||||
}
|
||||
if (!heapEnd || heapEnd < (void *) a)
|
||||
{
|
||||
heapEnd = a;
|
||||
}
|
||||
|
||||
allocationTail = a;
|
||||
allocationsLen++;
|
||||
|
||||
|
@ -142,8 +170,9 @@ MemoryDelete(MemoryInfo * a)
|
|||
static int
|
||||
MemoryCheck(MemoryInfo * a)
|
||||
{
|
||||
if (MEM_BOUND_LOWER(a->pointer) != MEM_BOUND ||
|
||||
MEM_BOUND_UPPER(a->pointer, a->size - (2 * sizeof(MEM_BOUND_TYPE))) != MEM_BOUND)
|
||||
if (MEM_START_BOUNDARY(a) != MEM_BOUND ||
|
||||
a->magic != MEM_MAGIC ||
|
||||
MEM_END_BOUNDARY(a) != MEM_BOUND)
|
||||
{
|
||||
if (hook)
|
||||
{
|
||||
|
@ -174,13 +203,14 @@ MemoryAllocate(size_t size, const char *file, int line)
|
|||
p = a + 1;
|
||||
|
||||
memset(p, 0, MEM_SIZE_ACTUAL(size));
|
||||
MEM_BOUND_LOWER(p) = MEM_BOUND;
|
||||
MEM_BOUND_UPPER(p, size) = MEM_BOUND;
|
||||
|
||||
a->size = MEM_SIZE_ACTUAL(size);
|
||||
a->unalignedSize = size;
|
||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
||||
a->line = line;
|
||||
a->pointer = p;
|
||||
MEM_START_BOUNDARY(a) = MEM_BOUND;
|
||||
MEM_END_BOUNDARY(a) = MEM_BOUND;
|
||||
|
||||
if (!MemoryInsert(a))
|
||||
{
|
||||
|
@ -195,7 +225,7 @@ MemoryAllocate(size_t size, const char *file, int line)
|
|||
}
|
||||
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ((MEM_BOUND_TYPE *) p) + 1;
|
||||
return p;
|
||||
}
|
||||
|
||||
void *
|
||||
|
@ -220,6 +250,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
|||
if (new)
|
||||
{
|
||||
a = new;
|
||||
a->unalignedSize = size;
|
||||
a->size = MEM_SIZE_ACTUAL(size);
|
||||
strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
|
||||
a->line = line;
|
||||
|
@ -228,8 +259,8 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
|||
a->pointer = a + 1;
|
||||
MemoryInsert(a);
|
||||
|
||||
MEM_BOUND_LOWER(a->pointer) = MEM_BOUND;
|
||||
MEM_BOUND_UPPER(a->pointer, size) = MEM_BOUND;
|
||||
MEM_START_BOUNDARY(a) = MEM_BOUND;
|
||||
MEM_END_BOUNDARY(a) = MEM_BOUND;
|
||||
|
||||
if (hook)
|
||||
{
|
||||
|
@ -253,7 +284,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
|
|||
}
|
||||
}
|
||||
|
||||
return ((MEM_BOUND_TYPE *) a->pointer) + 1;
|
||||
return a->pointer;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -344,8 +375,12 @@ MemoryInfoGet(void *po)
|
|||
|
||||
pthread_mutex_lock(&lock);
|
||||
|
||||
p = ((MEM_BOUND_TYPE *) po) - 1;
|
||||
p = ((MemoryInfo *) p) - 1;
|
||||
if (p < heapStart || p > heapEnd)
|
||||
{
|
||||
pthread_mutex_unlock(&lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (((MemoryInfo *)p)->magic != MEM_MAGIC)
|
||||
{
|
||||
|
@ -364,7 +399,7 @@ MemoryInfoGetSize(MemoryInfo * a)
|
|||
return 0;
|
||||
}
|
||||
|
||||
return a->size ? a->size - (2 * sizeof(MEM_BOUND_TYPE)) : 0;
|
||||
return a->size ? a->unalignedSize : 0;
|
||||
}
|
||||
|
||||
const char *
|
||||
|
@ -397,7 +432,7 @@ MemoryInfoGetPointer(MemoryInfo * a)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
return ((MEM_BOUND_TYPE *) a->pointer) + 1;
|
||||
return a->pointer;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <Util.h>
|
||||
|
||||
#include <Memory.h>
|
||||
#include <Platform.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -89,6 +90,10 @@ UtilTsMillis(void)
|
|||
return ts;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_DARWIN
|
||||
#define st_mtim st_mtimespec
|
||||
#endif
|
||||
|
||||
uint64_t
|
||||
UtilLastModified(char *path)
|
||||
{
|
||||
|
@ -105,6 +110,10 @@ UtilLastModified(char *path)
|
|||
return ts;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_DARWIN
|
||||
#undef st_mtim
|
||||
#endif
|
||||
|
||||
int
|
||||
UtilMkdir(const char *dir, const mode_t mode)
|
||||
{
|
||||
|
|
267
tools/j2s.c
267
tools/j2s.c
|
@ -38,6 +38,7 @@
|
|||
|
||||
#define MAX_DEPENDENCIES 32
|
||||
|
||||
#define IsDelimited(field, c1, c2) (*field == c1 && field[strlen(field) - 1] == c2)
|
||||
static char *
|
||||
Trim(char c, char *str)
|
||||
{
|
||||
|
@ -75,10 +76,14 @@ TypeToJsonType(char *type)
|
|||
}
|
||||
else
|
||||
{
|
||||
if (*type == '[' && type[strlen(type) - 1] == ']')
|
||||
if (IsDelimited(type, '[', ']'))
|
||||
{
|
||||
return JSON_ARRAY;
|
||||
}
|
||||
else if (IsDelimited(type, '{', '}'))
|
||||
{
|
||||
return JSON_OBJECT;
|
||||
}
|
||||
else
|
||||
{
|
||||
return JSON_OBJECT;
|
||||
|
@ -325,6 +330,7 @@ Main(Array * args)
|
|||
{
|
||||
char *fieldType;
|
||||
bool isArrType = false;
|
||||
bool isObjType = false;
|
||||
JsonValue *requiredVal;
|
||||
JsonValue *ignoreVal;
|
||||
|
||||
|
@ -343,12 +349,18 @@ Main(Array * args)
|
|||
goto finish;
|
||||
}
|
||||
|
||||
if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||
if (IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isArrType = true;
|
||||
}
|
||||
else if (IsDelimited(fieldType, '{', '}'))
|
||||
{
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isObjType = true;
|
||||
}
|
||||
|
||||
if (!StrEquals(fieldType, "object") &&
|
||||
!StrEquals(fieldType, "array") &&
|
||||
|
@ -374,6 +386,10 @@ Main(Array * args)
|
|||
{
|
||||
fieldType[strlen(fieldType)] = ']';
|
||||
}
|
||||
else if (isObjType)
|
||||
{
|
||||
fieldType[strlen(fieldType)] = '}';
|
||||
}
|
||||
|
||||
requiredVal = HashMapGet(fieldObj, "required");
|
||||
if (requiredVal && JsonValueType(requiredVal) != JSON_BOOLEAN)
|
||||
|
@ -529,11 +545,13 @@ Main(Array * args)
|
|||
{
|
||||
cType = "double";
|
||||
}
|
||||
else if (StrEquals(fieldType, "object"))
|
||||
else if (StrEquals(fieldType, "object") ||
|
||||
IsDelimited(fieldType, '{', '}'))
|
||||
{
|
||||
cType = "HashMap *";
|
||||
}
|
||||
else if (StrEquals(fieldType, "array") || (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']'))
|
||||
else if (StrEquals(fieldType, "array") ||
|
||||
IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
cType = "Array *";
|
||||
}
|
||||
|
@ -542,8 +560,21 @@ Main(Array * args)
|
|||
cType = fieldType;
|
||||
}
|
||||
|
||||
if (IsDelimited(fieldType, '{', '}') ||
|
||||
IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
char end = fieldType[strlen(fieldType) - 1];
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
|
||||
StreamPrintf(headerFile, " %s /* of %s */ %s;\n", cType, fieldType + 1, field);
|
||||
|
||||
fieldType[strlen(fieldType)] = end;
|
||||
}
|
||||
else
|
||||
{
|
||||
StreamPrintf(headerFile, " %s %s;\n", cType, field);
|
||||
}
|
||||
}
|
||||
|
||||
StreamPrintf(headerFile, "} %s;\n\n", type);
|
||||
}
|
||||
|
@ -673,7 +704,134 @@ Main(Array * args)
|
|||
StreamPrintf(implFile, " out->%s = JsonValueAsObject(val);\n", key);
|
||||
StreamPrintf(implFile, " Free(val); /* Not JsonValueFree() because we want the inner value. */\n");
|
||||
}
|
||||
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||
else if (IsDelimited(fieldType, '{', '}'))
|
||||
{
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||
jsonType = isEnum ? JSON_STRING : TypeToJsonType(fieldType);
|
||||
|
||||
StreamPrintf(implFile, " out->%s = HashMapCreate();\n", key);
|
||||
StreamPrintf(implFile, " if (!out->%s)\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"Failed to allocate memory for %s.%s.\";\n", type, key);
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " else\n");
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " HashMap *obj = JsonValueAsObject(val);\n");
|
||||
StreamPrintf(implFile, " char *objKey;\n");
|
||||
StreamPrintf(implFile, " JsonValue *v;\n");
|
||||
StreamPrintf(implFile, "\n");
|
||||
StreamPrintf(implFile, " while (HashMapIterate(obj, &objKey, (void **) &v))\n");
|
||||
StreamPrintf(implFile, " {\n");
|
||||
|
||||
if (StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "float") ||
|
||||
StrEquals(fieldType, "boolean"))
|
||||
{
|
||||
char *cType;
|
||||
|
||||
if (StrEquals(fieldType, "integer"))
|
||||
{
|
||||
cType = "int64_t";
|
||||
}
|
||||
else if (StrEquals(fieldType, "float"))
|
||||
{
|
||||
cType = "double";
|
||||
}
|
||||
else if (StrEquals(fieldType, "boolean"))
|
||||
{
|
||||
cType = "bool";
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Should never happen */
|
||||
cType = NULL;
|
||||
}
|
||||
|
||||
*fieldType = toupper(*fieldType);
|
||||
|
||||
StreamPrintf(implFile, " %s *ref;\n", cType);
|
||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"%s.%s{} contains an invalid value.\";\n", type, key);
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " ref = Malloc(sizeof(%s));\n", cType);
|
||||
StreamPrintf(implFile, " if (!ref)\n");
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for object value.\";\n");
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " *ref = JsonValueAs%s(v);\n", fieldType);
|
||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, ref);\n", key);
|
||||
|
||||
*fieldType = tolower(*fieldType);
|
||||
}
|
||||
else if (StrEquals(fieldType, "string"))
|
||||
{
|
||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, StrDuplicate(JsonValueAsString(v)));\n", key);
|
||||
}
|
||||
else if (StrEquals(fieldType, "object"))
|
||||
{
|
||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, JsonDuplicate(JsonValueAsObject(v)));\n", key);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isEnum)
|
||||
{
|
||||
StreamPrintf(implFile, " int parseResult;\n");
|
||||
}
|
||||
StreamPrintf(implFile, " %s *parsed;\n", fieldType);
|
||||
StreamPrintf(implFile, " if (JsonValueType(v) != %s)\n", JsonTypeToStr(jsonType));
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"%s.%s[] contains an invalid value.\";\n", type, key);
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " parsed = Malloc(sizeof(%s));\n", fieldType);
|
||||
StreamPrintf(implFile, " if (!parsed)\n");
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " *errp = \"Unable to allocate memory for array value.\";\n");
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
if (isEnum)
|
||||
{
|
||||
StreamPrintf(implFile, " parseResult = %sFromStr(JsonValueAsString(v));\n", fieldType);
|
||||
StreamPrintf(implFile, " *parsed = parseResult;\n");
|
||||
StreamPrintf(implFile, " if (parseResult == -1)\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
StreamPrintf(implFile, " if (!%sFromJson(JsonValueAsObject(v), parsed, errp))\n", fieldType);
|
||||
}
|
||||
StreamPrintf(implFile, " {\n");
|
||||
if (!isEnum)
|
||||
{
|
||||
StreamPrintf(implFile, " %sFree(parsed);\n", fieldType);
|
||||
}
|
||||
StreamPrintf(implFile, " Free(parsed);\n");
|
||||
StreamPrintf(implFile, " return false;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " HashMapSet(out->%s, objKey, parsed);\n", key);
|
||||
}
|
||||
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
|
||||
fieldType[strlen(fieldType)] = '}';
|
||||
}
|
||||
else if (IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
|
@ -915,7 +1073,73 @@ Main(Array * args)
|
|||
{
|
||||
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(JsonDuplicate(val->%s)));\n", Trim('_', key), key);
|
||||
}
|
||||
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||
else if (IsDelimited(fieldType, '{', '}'))
|
||||
{
|
||||
int isPrimitive;
|
||||
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||
isPrimitive = StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "boolean") ||
|
||||
StrEquals(fieldType, "float");
|
||||
|
||||
|
||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " char *objKey;\n");
|
||||
StreamPrintf(implFile, " void *objVal;\n");
|
||||
StreamPrintf(implFile, " HashMap *jsonObj = HashMapCreate();\n");
|
||||
StreamPrintf(implFile, " if (!jsonObj)\n");
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " JsonFree(json);\n");
|
||||
StreamPrintf(implFile, " return NULL;\n");
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
|
||||
if (StrEquals(fieldType, "string"))
|
||||
{
|
||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueString(objVal));\n", key);
|
||||
}
|
||||
else if (!isPrimitive)
|
||||
{
|
||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, JsonValueObject(%sToJson(objVal)));\n", fieldType, key);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *cType;
|
||||
|
||||
if (StrEquals(fieldType, "integer"))
|
||||
{
|
||||
cType = "int64_t";
|
||||
}
|
||||
else if (StrEquals(fieldType, "float"))
|
||||
{
|
||||
cType = "double";
|
||||
}
|
||||
else if (StrEquals(fieldType, "boolean"))
|
||||
{
|
||||
cType = "bool";
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Should never happen */
|
||||
cType = NULL;
|
||||
}
|
||||
|
||||
*fieldType = toupper(*fieldType);
|
||||
StreamPrintf(implFile, " HashMapSet(jsonObj, objKey, (JsonValue%s(*((%s *) objVal))));\n", fieldType, cType, key);
|
||||
*fieldType = tolower(*fieldType);
|
||||
}
|
||||
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " HashMapSet(json, \"%s\", JsonValueObject(jsonObj));\n", Trim('_', key));
|
||||
StreamPrintf(implFile, " }\n");
|
||||
|
||||
fieldType[strlen(fieldType)] = '}';
|
||||
}
|
||||
else if (IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
int isPrimitive;
|
||||
|
||||
|
@ -1036,7 +1260,36 @@ Main(Array * args)
|
|||
{
|
||||
StreamPrintf(implFile, " Free(val->%s);\n", key);
|
||||
}
|
||||
else if (*fieldType == '[' && fieldType[strlen(fieldType) - 1] == ']')
|
||||
else if (IsDelimited(fieldType, '{', '}'))
|
||||
{
|
||||
int isPrimitive;
|
||||
|
||||
fieldType++;
|
||||
fieldType[strlen(fieldType) - 1] = '\0';
|
||||
isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
|
||||
isPrimitive = StrEquals(fieldType, "boolean") ||
|
||||
StrEquals(fieldType, "float") ||
|
||||
StrEquals(fieldType, "integer") ||
|
||||
StrEquals(fieldType, "string");
|
||||
|
||||
StreamPrintf(implFile, " if (val->%s)\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " char *objKey;\n");
|
||||
StreamPrintf(implFile, " void *objVal;\n");
|
||||
StreamPrintf(implFile, " while (HashMapIterate(val->%s, &objKey, &objVal))\n", key);
|
||||
StreamPrintf(implFile, " {\n");
|
||||
StreamPrintf(implFile, " %sFree(objVal);\n", (!isEnum && !isPrimitive) ? fieldType : "", key);
|
||||
if (!isEnum && !isPrimitive)
|
||||
{
|
||||
StreamPrintf(implFile, " Free(objVal);\n", key);
|
||||
}
|
||||
StreamPrintf(implFile, " }\n");
|
||||
StreamPrintf(implFile, " HashMapFree(val->%s);\n", key);
|
||||
StreamPrintf(implFile, " }\n");
|
||||
|
||||
fieldType[strlen(fieldType)] = '}';
|
||||
}
|
||||
else if (IsDelimited(fieldType, '[', ']'))
|
||||
{
|
||||
int isPrimitive;
|
||||
|
||||
|
|
Loading…
Reference in a new issue