Compare commits

..

4 Commits

74 changed files with 1934 additions and 584 deletions

View File

@ -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

View File

@ -7,9 +7,6 @@ releases. Final change log entries are published as [Releases](releases).
## v0.4.1
Cytoplasm is now a C99 library! Upgrading from C89 to C99 makes Cytoplasm more portable
than ever.
### New Features
- Added an option to `j2s` to allow additional fields in structures and ignore them in
@ -18,9 +15,6 @@ are not even initialized to a default value.
- Fixed a memory leak that would occur in code generated by `j2s` under
specific circumstances.
- Added `JsonMerge()` to the JSON API to merge two JSON objects together.
- Make `HttpRouter` decode path parts before matching them on regular expressions.
- Fixed a bug in `ArraySort()` that would crash programs if the passed array has no
elements.
## v0.4.0
@ -32,4 +26,4 @@ project with its own independent releases. This allows it to develop at a much m
rapid pace than Telodendria.
Changes in future releases will be reported here. Since this is the first release,
there are no changes to show.
there are no changes to show.

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,15 +1,15 @@
<p align="center"><img src="https://telodendria.io/user/themes/bancino/images/logo/Cytoplasm.png"></p>
<p align="center"><img src="https://telodendria.io/assets/Cytoplasm.png"></p>
<h1 align="center">Cytoplasm (<code>libcytoplasm</code>)</h1>
Cytoplasm is a general-purpose C library for creating high-level (particularly networked and multi-threaded) C applications. It allows applications to take advantage of the speed, flexibility, and simplicity of the C programming language, while providing helpful code to allow applications to perform various complex tasks with minimal effort. Cytoplasm provides high-level data structures, a basic logging facility, an HTTP client and server, and more. It also reports memory leaks, which can aid in debugging, particularly on systems that don't have advanced tools like `valgrind`.
Cytoplasm is a general-purpose C library for creating high-level (particularly networked and multi-threaded) C applications. It allows applications to take advantage of the speed, flexibility, and simplicity of the C programming language, while providing helpful code to allow applications to perform various complex tasks with minimal effort. Cytoplasm provides high-level data structures, a basic logging facility, an HTTP client and server, and more. It also reports memory leaks, which can aid in debugging.
Cytoplasm aims not to only do one thing well, but to do many things good enough. This is in contrast to other libraries, which only do one thing and thus require the developer to pull in many different libraries for a broad range of functionality. The primary target of Cytoplasm is simple yet higher level C applications that have to perform relatively complex tasks, but don't want to depend on a large number of dependencies.
Cytoplasm aims not to only do one thing well, but to do many things good enough. This is in contrast to other libraries, which only do one thing and thus require the developer to pull in many different libraries. The primary target of Cytoplasm is simple yet higher level C applications that have to perform relatively complex tasks, but don't want to depend on a large number of dependencies.
Cytoplasm is extremely opinionated on the way programs using it are written. It strives to create a comprehensive and tightly-integrated programming environment, while also maintaining C programming correctness. It doesn't do any macro magic or make C look like anything other than C. It is written entirely in C99, and depends only on a POSIX environment. This differentiates it from other general-purpose libraries that often require more modern compilers and non-standard language and environment features. Cytoplasm is intended to be extremely portable and simple, while still providing some of the functionality expected in higher-level programming languages in a platform-agnostic manner. In the case of TLS, Cytoplasm wraps low-level TLS libraries to offer a single, unified interface to TLS so that programs do not have to care about the underlying implementation.
Cytoplasm is extremely opinionated on the way programs using it are written. It strives to create a comprehensive and tightly-integrated programming environment, while also maintaining C programming correctness. It doesn't do any macro magic or make C look like anything other than C. It is written entirely in C89, and depends only on a POSIX environment. This differentiates it from other general-purpose libraries that often require modern compilers and non-standard language and environment features. Cytoplasm is intended to be extremely portable and simple, while still providing some of the functionality expected in higher-level programming languages in a platform-agnostic manner. In the case of TLS, Cytoplasm wraps low-level TLS libraries to offer a single, unified interface to TLS so that programs do not have to care about the underlying implementation.
Cytoplasm is probably not suitable for embedded programming. It makes liberal use of the heap, and while data structures are designed to conserve memory where possible and practical, minimal memory usage is not really a design goal for Cytoplasm, although Cytoplasm takes care not to use any more memory than it absolutely needs. Cytoplasm also wraps a few standard libraries with additional logic and checking. While this ensures better runtime safety, this inevitably adds a little overhead, which may be unsuitable for time- or space-critical tasks.
Originally a part of Telodendria ([Website](https://telodendria.io), [Repo](/Telodendria/Telodendria)), a Matrix homeserver written in C, Cytoplasm was split off into its own project due to the desire of some Telodendria developers to use Telodendria's code in other projects. Cytoplasm is still an official Telodendria project, but it is designed specifically to be distributed and used totally independent of Telodendria.
Originally a part of Telodendria (https://telodendria.io), a Matrix homeserver written in C, Cytoplasm was split off into its own project due to the desire of some Telodendria developers to use Telodendria's code in other projects. Cytoplasm is still an official Telodendria project, but it is designed specifically to be distributed and used totally independent of Telodendria.
The name "Cytoplasm" was chosen for a few reasons. It plays off the precedent set up by the Matrix organization in naming projects after the parts of a neuron. It also speaks to the function of Cytoplasm. The cytoplasm of a cell is the supporting material. It is what gives the cell its shape, and it facilitates the movement of materials to the other cell parts. Likewise, Cytoplasm aims to provide a support mechanism for C applications that have to perform complex tasks beyond what the C standard library provides.
@ -17,7 +17,14 @@ Cytoplasm also starts with a C, which I think is a nice touch for C libraries. I
## Requirements
Cytoplasm aims to have zero software dependencies beyond what is mandated by POSIX. You only need a standard C99 compiler, and the standard `math` and `pthread` libraries to build Cytoplasm. TLS support can optionally be enabled with the configuration script. The supported TLS implementations are as follows:
Cytoplasm makes the following assumptions about the underlying hardware:
- It has words sizes that are powers of 2, and a native 32-bit integer type exists.
- Integers are represented using two's compliment for negatives.
The ANSI C standard requires an integer type of at least 32 bits, but does not require any more. If Cytoplasm is built on 32-bit platforms that don't provide a native 64-bit integer type, Cytoplasm emulates 64-bit integers. This can make it more portable.
Cytoplasm aims to have zero software dependencies beyond what is mandated by POSIX. You only need the standard math and pthread libraries to build it. TLS support can optionally be enabled with the configuration script. The supported TLS implementations are as follows:
- OpenSSL
- LibreSSL
@ -53,11 +60,14 @@ The `configure` script has a number of optional flags, which are as follows:
- `--with-(openssl|libressl)`: Select the TLS implementation to use. OpenSSL is selected by default.
- `--disable-tls`: Disable TLS altogether.
- `--prefix=<path>`: Set the install prefix to set by default in the `Makefile`. This defaults to `/usr/local`, which should be appropriate for most Unix-like systems.
- `--(enable|disable)-ld-extra`: Control whether or not to enable additional linking flags that create a more optimized binary. For large compilers such as GCC and Clang, these flags should be enabled. However, if you are using a small or more obscure compiler, then these flags may not be supported, so you can disable them with this option.
- `--(enable|disable)-debug`: Control whether or not to enable debug mode. This sets the optimization level to 0 and builds with debug symbols. Useful for running with a debugger.
- `--static` and `--no-static`: Controls whether static binaries for tools are built by default. On BSD systems, `--static` is perfectly acceptable, but on GNU systems, `--no-static` is often desirable to silence warnings about static binaries emitted by the GNU linker.
Cytoplasm can be customized with the following options:
- `--lib-name=<name>`: The output name of the library. This defaults to `Cytoplasm` and should in most cases not be changed.
- `--lib-version=<version>`: The version string to embed in the library binaries. This can be used to indicate build customizations or non-release versions of Cytoplasm.
The following recipes are available in the generated `Makefile`:
@ -86,13 +96,13 @@ Cytoplasm provides the typical .so and .a files, which can be used to link progr
Here is the canonical Hello World written with Cytoplasm:
```c
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Log.h>
int Main(void)
{
Log(LOG_INFO, "Hello World!");
return 0;
}
int Main(void)
{
Log(LOG_INFO, "Hello World!");
return 0;
}
```
If this file is `Hello.c`, then you can compile it by doing this:

108
configure vendored
View File

@ -10,47 +10,26 @@ echo "-------------------"
BUILD="build"
OUT="out"
SRC="src"
INCLUDE="include/Cytoplasm"
INCLUDE="src/include"
TOOLS="tools"
# Default compiler flags. These must be supported by all POSIX C compilers.
# "Fancy" compilers that have additional options must be detected and set below.
CFLAGS="-O1 -D_DEFAULT_SOURCE -I${INCLUDE}"
LIBS="-lm -lpthread"
CFLAGS="-Wall -Wextra -pedantic -std=c89 -O3 -pipe -D_DEFAULT_SOURCE -I${INCLUDE}"
LIBS="-lm -pthread"
# Default args for all platforms.
SCRIPT_ARGS="--prefix=/usr/local --lib-name=Cytoplasm"
# Set SSL flags depending on the platform.
# Set default args for all platforms
SCRIPT_ARGS="--cc=cc --prefix=/usr/local --enable-ld-extra --lib-name=Cytoplasm --lib-version=0.4.1 --static $@"
# Set platform specific args
case "$(uname)" in
OpenBSD)
SCRIPT_ARGS="${SCRIPT_ARGS} --with-libressl"
SCRIPT_ARGS="--with-libressl $SCRIPT_ARGS"
;;
*)
SCRIPT_ARGS="${SCRIPT_ARGS} --with-openssl"
SCRIPT_ARGS="--with-openssl $SCRIPT_ARGS"
;;
esac
# Set compiler depending on the platform.
case "$(uname)" in
Linux|NetBSD)
# These systems typically use GCC.
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=gcc"
;;
OpenBSD|FreeBSD)
# These systems typically use Clang.
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=clang"
;;
*)
# Use default compiler which is required to be present on
# all POSIX-compliant systems.
SCRIPT_ARGS="${SCRIPT_ARGS} --cc=c99"
;;
esac
# Append any additional args specified by user
SCRIPT_ARGS="${SCRIPT_ARGS} $@"
echo "Processing options..."
echo "Ran with arguments: $SCRIPT_ARGS"
@ -59,14 +38,6 @@ for arg in $SCRIPT_ARGS; do
case "$arg" in
--cc=*)
CC=$(echo "$arg" | cut -d '=' -f 2-)
case "${CC}" in
gcc*|clang*)
# "Fancy" compilers that support a plethora of additional flags we
# want to enable if present.
CFLAGS="-Wall -Wextra -Werror -pedantic -std=c99 -O3 ${CFLAGS}"
LDFLAGS="-flto -fdata-sections -ffunction-sections -s -Wl,-gc-sections"
;;
esac
;;
--with-openssl)
TLS_IMPL="TLS_OPENSSL"
@ -83,15 +54,35 @@ for arg in $SCRIPT_ARGS; do
--prefix=*)
PREFIX=$(echo "$arg" | cut -d '=' -f 2-)
;;
--enable-ld-extra)
LD_EXTRA="-flto -fdata-sections -ffunction-sections -s -Wl,-gc-sections"
;;
--disable-ld-extra)
LD_EXTRA=""
;;
--lib-name=*)
LIB_NAME=$(echo "$arg" | cut -d '=' -f 2-)
;;
--lib-version=*)
LIB_VERSION=$(echo "$arg" | cut -d '=' -f 2-)
;;
--enable-debug)
DEBUG="-O0 -g"
echo "Notice: --enable-debug implies --disable-ld-extra and --no-static."
echo "You must explicitly provide --enable-ld-extra and/or --static after"
echo "specifying --enable-debug if you wish to enable these features in debug mode."
LD_EXTRA=""
STATIC=""
;;
--disable-debug)
DEBUG=""
;;
--static)
STATIC="-static -Wl,-static"
;;
--no-static)
STATIC=""
;;
*)
echo "Invalid argument: $arg"
exit 1
@ -104,8 +95,8 @@ if [ -n "$TLS_IMPL" ]; then
LIBS="${LIBS} ${TLS_LIBS}"
fi
CFLAGS="${CFLAGS} '-DLIB_NAME=\"${LIB_NAME}\"' ${DEBUG}"
LDFLAGS="${LIBS} ${LDFLAGS}"
CFLAGS="${CFLAGS} '-DLIB_NAME=\"${LIB_NAME}\"' '-DLIB_VERSION=\"${LIB_VERSION}\"' ${DEBUG}"
LDFLAGS="${LIBS} ${LD_EXTRA}"
#
# Makefile generation
@ -137,27 +128,11 @@ print_obj() {
printf '%s ' "$2"
}
get_deps() {
src="$1"
${CC} -I${INCLUDE} -E "$src" \
| grep '^#' \
| awk '{print $3}' \
| cut -d '"' -f 2 \
| sort \
| uniq \
| grep -v '^[/<]' \
| grep "^${SRC}/" \
| while IFS= read -r dep; do
printf "%s " "$dep"
done
}
compile_obj() {
src="$1"
obj="$2"
echo "${obj}: $(get_deps ${src})"
${CC} -I${INCLUDE} -MM -MT "${obj}" "${src}"
echo "${TAB}@mkdir -p $(dirname ${obj})"
echo "${TAB}\$(CC) \$(CFLAGS) -fPIC -c -o \"${obj}\" \"${src}\""
}
@ -168,13 +143,16 @@ compile_bin() {
echo "${out}: ${OUT}/lib/lib${LIB_NAME}.a ${OUT}/lib/lib${LIB_NAME}.so ${src}"
echo "${TAB}@mkdir -p ${OUT}/bin"
echo "${TAB}\$(CC) \$(CFLAGS) -o \"${out}\" \"${src}\" -L${OUT}/lib \$(LDFLAGS) -l${LIB_NAME}"
echo "${TAB}\$(CC) \$(CFLAGS) -o \"${out}\" \"${src}\" -L${OUT}/lib \$(LDFLAGS) -l${LIB_NAME} ${STATIC}"
}
compile_doc() {
src="$1"
out="$2"
pref="LD_LIBRARY_PATH=${OUT}/lib "
if [ -z "${STATIC}" ]; then
pref="LD_LIBRARY_PATH=${OUT}/lib "
fi
echo "${out}: ${OUT}/bin/hdoc ${src}"
echo "${TAB}@mkdir -p ${OUT}/man/man3"
@ -184,28 +162,22 @@ compile_doc() {
install_out() {
src="$1"
out="$2"
dir=$(dirname "$out")
echo "${TAB}mkdir -p \"$dir\""
echo "${TAB}cp \"$src\" \"$out\""
echo "${TAB}install -D \"$src\" \"$out\""
}
install_man() {
src="${OUT}/man/man3/${LIB_NAME}-$(basename $1 .h).3"
out="$2"
dir=$(dirname "$out")
echo "${TAB}mkdir -p \"$dir\""
echo "${TAB}cp \"$src\" \"$out\""
echo "${TAB}install -D \"$src\" \"$out\""
}
install_tool() {
src=${OUT}/bin/$(basename "$1" .c)
out="$2"
dir=$(dirname "$out")
echo "${TAB}mkdir -p \"$dir\""
echo "${TAB}cp \"$src\" \"$out\""
echo "${TAB}install -D \"$src\" \"$out\""
}
uninstall_out() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -38,12 +38,12 @@ struct Array
size_t size; /* Elements actually filled */
};
bool
int
ArrayAdd(Array * array, void *value)
{
if (!array)
{
return false;
return 0;
}
return ArrayInsert(array, array->size, value);
@ -122,14 +122,14 @@ ArrayGet(Array * array, size_t index)
}
bool
extern int
ArrayInsert(Array * array, size_t index, void *value)
{
size_t i;
if (!array || !value || index > array->size)
{
return false;
return 0;
}
if (array->size >= array->allocated)
@ -145,7 +145,7 @@ ArrayInsert(Array * array, size_t index, void *value)
if (!array->entries)
{
array->entries = tmp;
return false;
return 0;
}
array->allocated = newSize;
@ -160,7 +160,7 @@ ArrayInsert(Array * array, size_t index, void *value)
array->entries[index] = value;
return true;
return 1;
}
extern void *
@ -200,14 +200,14 @@ ArraySize(Array * array)
return array->size;
}
bool
int
ArrayTrim(Array * array)
{
void **tmp;
if (!array)
{
return false;
return 0;
}
tmp = array->entries;
@ -218,10 +218,10 @@ ArrayTrim(Array * array)
if (!array->entries)
{
array->entries = tmp;
return false;
return 0;
}
return true;
return 1;
}
static void
@ -267,9 +267,8 @@ ArrayQuickSort(Array * array, size_t low, size_t high, int (*compare) (void *, v
void
ArraySort(Array * array, int (*compare) (void *, void *))
{
if (!ArraySize(array))
if (!array)
{
// If a NULL ptr was given, or the array has no elements, do nothing.
return;
}
ArrayQuickSort(array, 0, array->size - 1, compare);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -212,7 +212,7 @@ Base64Unpad(char *base64, size_t length)
base64[length] = '\0';
}
bool
extern int
Base64Pad(char **base64Ptr, size_t length)
{
char *tmp;
@ -221,7 +221,7 @@ Base64Pad(char **base64Ptr, size_t length)
if (length % 4 == 0)
{
return true; /* Success: no padding needed */
return length; /* Success: no padding needed */
}
newSize = length + (4 - (length % 4));
@ -229,7 +229,7 @@ Base64Pad(char **base64Ptr, size_t length)
tmp = Realloc(*base64Ptr, newSize + 100);;
if (!tmp)
{
return false; /* Memory error */
return 0; /* Memory error */
}
*base64Ptr = tmp;
@ -240,5 +240,5 @@ Base64Pad(char **base64Ptr, size_t length)
(*base64Ptr)[newSize] = '\0';
return true;
return newSize;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -23,33 +23,32 @@
*/
#include <Cron.h>
#include <UInt64.h>
#include <Array.h>
#include <Memory.h>
#include <Util.h>
#include <stdbool.h>
#include <pthread.h>
struct Cron
{
uint64_t tick;
UInt64 tick;
Array *jobs;
pthread_mutex_t lock;
volatile unsigned int stop:1;
pthread_t thread;
volatile bool stop;
};
typedef struct Job
{
uint64_t interval;
uint64_t lastExec;
UInt64 interval;
UInt64 lastExec;
JobFunc *func;
void *args;
} Job;
static Job *
JobCreate(uint64_t interval, JobFunc * func, void *args)
JobCreate(UInt32 interval, JobFunc * func, void *args)
{
Job *job;
@ -64,8 +63,8 @@ JobCreate(uint64_t interval, JobFunc * func, void *args)
return NULL;
}
job->interval = interval;
job->lastExec = 0;
job->interval = UInt64Create(0, interval);
job->lastExec = UInt64Create(0, 0);
job->func = func;
job->args = args;
@ -80,51 +79,51 @@ CronThread(void *args)
while (!cron->stop)
{
size_t i;
uint64_t ts; /* tick start */
uint64_t te; /* tick end */
UInt64 ts; /* tick start */
UInt64 te; /* tick end */
pthread_mutex_lock(&cron->lock);
ts = UtilTsMillis();
ts = UtilServerTs();
for (i = 0; i < ArraySize(cron->jobs); i++)
{
Job *job = ArrayGet(cron->jobs, i);
if ((ts - job->lastExec) > job->interval)
if (UInt64Gt(UInt64Sub(ts, job->lastExec), job->interval))
{
job->func(job->args);
job->lastExec = ts;
}
if (!job->interval)
if (UInt64Eq(job->interval, UInt64Create(0, 0)))
{
ArrayDelete(cron->jobs, i);
Free(job);
}
}
te = UtilTsMillis();
te = UtilServerTs();
pthread_mutex_unlock(&cron->lock);
// Only sleep if the jobs didn't overrun the tick
if (cron->tick > (te - ts))
/* Only sleep if the jobs didn't overrun the tick */
if (UInt64Gt(cron->tick, UInt64Sub(te, ts)))
{
const uint64_t microTick = 100;
const UInt64 microTick = UInt64Create(0, 100);
uint64_t remainingTick = cron->tick - (te - ts);
UInt64 remainingTick = UInt64Sub(cron->tick, UInt64Sub(te, ts));
/* Only sleep for microTick ms at a time because if the job
* scheduler is supposed to stop before the tick is up, we
* don't want to be stuck in a long sleep */
while (remainingTick >= microTick && !cron->stop)
while (UInt64Geq(remainingTick, microTick) && !cron->stop)
{
UtilSleepMillis(microTick);
remainingTick -= microTick;
remainingTick = UInt64Sub(remainingTick, microTick);
}
if (remainingTick && !cron->stop)
if (UInt64Neq(remainingTick, UInt64Create(0, 0)) && !cron->stop)
{
UtilSleepMillis(remainingTick);
}
@ -135,7 +134,7 @@ CronThread(void *args)
}
Cron *
CronCreate(uint64_t tick)
CronCreate(UInt32 tick)
{
Cron *cron = Malloc(sizeof(Cron));
@ -151,8 +150,8 @@ CronCreate(uint64_t tick)
return NULL;
}
cron->tick = tick;
cron->stop = true;
cron->tick = UInt64Create(0, tick);
cron->stop = 1;
pthread_mutex_init(&cron->lock, NULL);
@ -181,7 +180,7 @@ CronOnce(Cron * cron, JobFunc * func, void *args)
}
void
CronEvery(Cron * cron, uint64_t interval, JobFunc * func, void *args)
CronEvery(Cron * cron, unsigned long interval, JobFunc * func, void *args)
{
Job *job;
@ -209,7 +208,7 @@ CronStart(Cron * cron)
return;
}
cron->stop = false;
cron->stop = 0;
pthread_create(&cron->thread, NULL, CronThread, cron);
}
@ -222,7 +221,7 @@ CronStop(Cron * cron)
return;
}
cron->stop = true;
cron->stop = 1;
pthread_join(cron->thread, NULL);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -24,22 +24,12 @@
#include <Cytoplasm.h>
int
CytoplasmGetVersion(void)
char *CytoplasmGetName()
{
return CYTOPLASM_VERSION;
return LIB_NAME;
}
const char *
CytoplasmGetVersionStr(void)
char *CytoplasmGetVersion()
{
return "v" STRINGIFY(CYTOPLASM_VERSION_MAJOR)
"." STRINGIFY(CYTOPLASM_VERSION_MINOR)
"." STRINGIFY(CYTOPLASM_VERSION_PATCH)
#if CYTOPLASM_VERSION_ALPHA
"-alpha" STRINGIFY(CYTOPLASM_VERSION_ALPHA)
#elif CYTOPLASM_VERSION_BETA
"-beta" STRINGIFY(CYTOPLASM_VERSION_BETA)
#endif
;
}
return LIB_VERSION;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -23,6 +23,7 @@
*/
#include <Db.h>
#include <UInt64.h>
#include <Memory.h>
#include <Json.h>
#include <Util.h>
@ -76,7 +77,7 @@ struct DbRef
{
HashMap *json;
uint64_t ts;
UInt64 ts;
size_t size;
Array *name;
@ -218,38 +219,19 @@ DbHashKey(Array * args)
return str;
}
static char
DbSanitiseChar(char input)
{
switch (input)
{
case '/':
return '_';
case '.':
return '-';
}
return input;
}
static char *
DbDirName(Db * db, Array * args, size_t strip)
{
size_t i, j;
size_t i;
char *str = StrConcat(2, db->dir, "/");
for (i = 0; i < ArraySize(args) - strip; i++)
{
char *tmp;
char *sanitise = StrDuplicate(ArrayGet(args, i));
for (j = 0; j < strlen(sanitise); j++)
{
sanitise[j] = DbSanitiseChar(sanitise[j]);
}
tmp = StrConcat(3, str, sanitise, "/");
tmp = StrConcat(3, str, ArrayGet(args, i), "/");
Free(str);
Free(sanitise);
str = tmp;
}
@ -272,7 +254,17 @@ DbFileName(Db * db, Array * args)
/* Sanitize name to prevent directory traversal attacks */
while (arg[j])
{
arg[j] = DbSanitiseChar(arg[j]);
switch (arg[j])
{
case '/':
arg[j] = '_';
break;
case '.':
arg[j] = '-';
break;
default:
break;
}
j++;
}
@ -503,12 +495,12 @@ DbLockFromArr(Db * db, Array * args)
if (ref) /* In cache */
{
uint64_t diskTs = UtilLastModified(file);
UInt64 diskTs = UtilLastModified(file);
ref->fd = fd;
ref->stream = stream;
if (diskTs > ref->ts)
if (UInt64Gt(diskTs, ref->ts))
{
/* File was modified on disk since it was cached */
HashMap *json = JsonDecode(ref->stream);
@ -596,7 +588,7 @@ DbLockFromArr(Db * db, Array * args)
if (db->cache)
{
ref->ts = UtilTsMillis();
ref->ts = UtilServerTs();
ref->size = DbComputeSize(ref->json);
HashMapSet(db->cache, hash, ref);
db->cacheSize += ref->size;
@ -660,7 +652,7 @@ DbCreate(Db * db, size_t nArgs,...)
file = DbFileName(db, args);
if (UtilLastModified(file))
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
{
Free(file);
ArrayFree(args);
@ -702,19 +694,19 @@ DbCreate(Db * db, size_t nArgs,...)
return ret;
}
bool
int
DbDelete(Db * db, size_t nArgs,...)
{
va_list ap;
Array *args;
char *file;
char *hash;
bool ret = true;
int ret = 1;
DbRef *ref;
if (!db)
{
return false;
return 0;
}
va_start(ap, nArgs);
@ -763,9 +755,9 @@ DbDelete(Db * db, size_t nArgs,...)
Free(hash);
if (UtilLastModified(file))
if (UInt64Neq(UtilLastModified(file), UInt64Create(0, 0)))
{
ret = (remove(file) == 0);
ret = remove(file) == 0;
}
pthread_mutex_unlock(&db->lock);
@ -798,14 +790,14 @@ DbLock(Db * db, size_t nArgs,...)
return ret;
}
bool
int
DbUnlock(Db * db, DbRef * ref)
{
bool destroy;
int destroy;
if (!db || !ref)
{
return false;
return 0;
}
lseek(ref->fd, 0L, SEEK_SET);
@ -814,7 +806,7 @@ DbUnlock(Db * db, DbRef * ref)
pthread_mutex_unlock(&db->lock);
Log(LOG_ERR, "Failed to truncate file on disk.");
Log(LOG_ERR, "Error on fd %d: %s", ref->fd, strerror(errno));
return false;
return 0;
}
JsonEncode(ref->json, ref->stream, JSON_DEFAULT);
@ -835,18 +827,18 @@ DbUnlock(Db * db, DbRef * ref)
* require some items to be evicted. */
DbCacheEvict(db);
destroy = false;
destroy = 0;
}
else
{
destroy = true;
destroy = 1;
}
Free(key);
}
else
{
destroy = true;
destroy = 1;
}
if (destroy)
@ -858,16 +850,16 @@ DbUnlock(Db * db, DbRef * ref)
}
pthread_mutex_unlock(&db->lock);
return true;
return 1;
}
bool
int
DbExists(Db * db, size_t nArgs,...)
{
va_list ap;
Array *args;
char *file;
bool ret;
int ret;
va_start(ap, nArgs);
args = ArrayFromVarArgs(nArgs, ap);
@ -875,13 +867,13 @@ DbExists(Db * db, size_t nArgs,...)
if (!args)
{
return false;
return 0;
}
pthread_mutex_lock(&db->lock);
file = DbFileName(db, args);
ret = (UtilLastModified(file) != 0);
ret = UInt64Neq(UtilLastModified(file), UInt64Create(0, 0));
pthread_mutex_unlock(&db->lock);
@ -963,15 +955,15 @@ DbJson(DbRef * ref)
return ref ? ref->json : NULL;
}
bool
int
DbJsonSet(DbRef * ref, HashMap * json)
{
if (!ref || !json)
{
return false;
return 0;
}
JsonFree(ref->json);
ref->json = JsonDuplicate(json);
return true;
return 1;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -247,12 +247,12 @@ HashMapGet(HashMap * map, const char *key)
return NULL;
}
bool
int
HashMapIterateReentrant(HashMap * map, char **key, void **value, size_t * i)
{
if (!map)
{
return false;
return 0;
}
if (*i >= map->capacity)
@ -260,7 +260,7 @@ HashMapIterateReentrant(HashMap * map, char **key, void **value, size_t * i)
*i = 0;
*key = NULL;
*value = NULL;
return false;
return 0;
}
while (*i < map->capacity)
@ -273,20 +273,20 @@ HashMapIterateReentrant(HashMap * map, char **key, void **value, size_t * i)
{
*key = bucket->key;
*value = bucket->value;
return true;
return 1;
}
}
*i = 0;
return false;
return 0;
}
bool
int
HashMapIterate(HashMap * map, char **key, void **value)
{
if (!map)
{
return false;
return 0;
}
else
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -34,7 +34,6 @@
#include <sys/socket.h>
#include <netdb.h>
#include <Cytoplasm.h>
#include <Http.h>
#include <Memory.h>
#include <Util.h>
@ -155,7 +154,7 @@ HttpRequest(HttpRequestMethod method, int flags, unsigned short port, char *host
HttpRequestMethodToString(method), path);
HttpRequestHeader(context, "Connection", "close");
HttpRequestHeader(context, "User-Agent", LIB_NAME "/" STRINGIFY(CYTOPLASM_VERSION));
HttpRequestHeader(context, "User-Agent", LIB_NAME "/" LIB_VERSION);
HttpRequestHeader(context, "Host", host);
return context;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -143,7 +143,7 @@ HttpRouterFree(HttpRouter * router)
Free(router);
}
bool
int
HttpRouterAdd(HttpRouter * router, char *regPath, HttpRouteFunc * exec)
{
RouteNode *node;
@ -152,19 +152,19 @@ HttpRouterAdd(HttpRouter * router, char *regPath, HttpRouteFunc * exec)
if (!router || !regPath || !exec)
{
return false;
return 0;
}
if (StrEquals(regPath, "/"))
{
router->root->exec = exec;
return true;
return 1;
}
regPath = StrDuplicate(regPath);
if (!regPath)
{
return false;
return 0;
}
tmp = regPath;
@ -187,10 +187,10 @@ HttpRouterAdd(HttpRouter * router, char *regPath, HttpRouteFunc * exec)
Free(regPath);
return true;
return 1;
}
bool
int
HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
{
RouteNode *node;
@ -199,17 +199,17 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
HttpRouteFunc *exec = NULL;
Array *matches = NULL;
size_t i;
bool retval;
int retval;
if (!router || !path)
{
return false;
return 0;
}
matches = ArrayCreate();
if (!matches)
{
return false;
return 0;
}
node = router->root;
@ -237,6 +237,7 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
{
if (regexec(&val->regex, pathPart, REG_MAX_SUB, pmatch, 0) == 0)
{
Free(pathPart);
break;
}
@ -267,6 +268,7 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
substr = StrSubstr(pathPart, cpmatch.rm_so, cpmatch.rm_eo);
if (pmatch[i].rm_so == -1)
{
Free(pathPart);
break;
}
@ -280,7 +282,7 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
if (!exec)
{
retval = false;
retval = 0;
goto finish;
}
@ -293,7 +295,7 @@ HttpRouterRoute(HttpRouter * router, char *path, void *args, void **ret)
exec(matches, args);
}
retval = true;
retval = 1;
finish:
for (i = 0; i < ArraySize(matches); i++)

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -465,7 +465,7 @@ HttpServerWorkerThread(void *args)
ssize_t i = 0;
HttpRequestMethod requestMethod;
uint64_t firstRead;
UInt64 firstRead;
fp = DequeueConnection(server);
@ -473,7 +473,7 @@ HttpServerWorkerThread(void *args)
{
/* Block for 1 millisecond before continuing so we don't
* murder the CPU if the queue is empty. */
UtilSleepMillis(1);
UtilSleepMillis(UInt64Create(0, 1));
continue;
}
@ -483,25 +483,21 @@ HttpServerWorkerThread(void *args)
* happens, UtilGetLine() sets errno to EAGAIN. If we get
* EAGAIN, then clear the error on the stream and try again
* after a few ms. This is typically more than enough time for
* the client to send data.
*
* TODO: Instead of looping, abort immediately, and place the request
* at the end of the queue.
*/
firstRead = UtilTsMillis();
* the client to send data. */
firstRead = UtilServerTs();
while ((lineLen = UtilGetLine(&line, &lineSize, fp)) == -1
&& errno == EAGAIN)
{
StreamClearError(fp);
// If the server is stopped, or it's been a while, just
// give up so we aren't wasting a thread on this client.
if (server->stop || (UtilTsMillis() - firstRead) > (1000 * 30))
/* If the server is stopped, or it's been a while, just
* give up so we aren't wasting a thread on this client. */
if (server->stop || UInt64Gt(UInt64Sub(UtilServerTs(), firstRead), UInt64Create(0, 1000 * 30)))
{
goto finish;
}
UtilSleepMillis(5);
UtilSleepMillis(UInt64Create(0, 5));
}
if (lineLen == -1)
@ -715,25 +711,25 @@ HttpServerEventThread(void *args)
return NULL;
}
bool
int
HttpServerStart(HttpServer * server)
{
if (!server)
{
return false;
return 0;
}
if (server->isRunning)
{
return true;
return 1;
}
if (pthread_create(&server->socketThread, NULL, HttpServerEventThread, server) != 0)
{
return false;
return 0;
}
return true;
return 1;
}
void

399
src/Int64.c Normal file
View File

@ -0,0 +1,399 @@
/*
* Copyright (C) 2022-2023 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.
*/
#include <Int64.h>
#include <stddef.h>
#include <signal.h>
#include <Log.h>
#ifdef INT64_NATIVE
#define Int64Sign(x) ((int) (((UInt64) (x)) >> 63))
#else
#define Int64Sign(x) ((int) ((x).i[1] >> 31))
#endif
size_t
Int64Str(Int64 x, int base, char *out, size_t len)
{
static const char symbols[] = "0123456789ABCDEF";
size_t i = len - 1;
size_t j = 0;
int neg = Int64Sign(x);
Int64 base64 = Int64Create(0, base);
/* We only have symbols up to base 16 */
if (base < 2 || base > 16)
{
return 0;
}
/*
* This algorithm doesn't work on INT64_MIN.
*
* But it works on all other integers in the range, so we
* just scoot the range in by one for now. It's a hack and
* I'm not a huge fan of it, but this function is mostly
* used in Json, which shouldn't have a range this large
* anyway (Json is limited to -2^53 -> 2^53-1).
*
* Proper fixes are always welcome.
*/
if (Int64Eq(x, Int64Create(0x80000000, 0x00000000)))
{
x = Int64Add(x, Int64Create(0, 1));
}
#if 0
else if (Int64Eq(x, Int64Create(0x7FFFFFFF, 0xFFFFFFFF)))
{
x = Int64Sub(x, Int64Create(0, 1));
}
#endif
if (base != 2 && base != 8 && base != 16 && neg)
{
x = Int64Neg(x);
}
do
{
Int64 mod = Int64Rem(x, base64);
Int32 low = Int64Low(mod);
out[i] = symbols[low];
i--;
x = Int64Div(x, base64);
} while (Int64Gt(x, Int64Create(0, 0)));
if (base != 2 && base != 8 && base != 16)
{
/*
* Binary, octal, and hexadecimal are known to
* be bit representations. Everything else (notably
* decimal) should include the negative sign.
*/
if (neg)
{
out[i] = '-';
i--;
}
}
while (++i < len)
{
out[j++] = out[i];
}
out[j] = '\0';
return j;
}
#ifndef INT64_NATIVE
/* No native 64-bit support, add our own */
Int64
Int64Create(UInt32 high, UInt32 low)
{
Int64 x;
x.i[0] = low;
x.i[1] = high;
return x;
}
Int64
Int64Add(Int64 x, Int64 y)
{
Int64 z = Int64Create(0, 0);
int carry;
z.i[0] = x.i[0] + y.i[0];
carry = z.i[0] < x.i[0];
z.i[1] = x.i[1] + y.i[1] + carry;
return z;
}
Int64
Int64Sub(Int64 x, Int64 y)
{
return Int64Add(x, Int64Neg(y));
}
Int64
Int64Mul(Int64 x, Int64 y)
{
Int64 z = Int64Create(0, 0);
int xneg = Int64Sign(x);
int yneg = Int64Sign(y);
if (xneg)
{
x = Int64Neg(x);
}
if (yneg)
{
y = Int64Neg(y);
}
/* while (y > 0) */
while (Int64Gt(y, Int64Create(0, 0)))
{
/* if (y & 1 != 0) */
if (Int64Neq(Int64And(y, Int64Create(0, 1)), Int64Create(0, 0)))
{
z = Int64Add(z, x);
}
x = Int64Sll(x, 1);
y = Int64Sra(y, 1);
}
if (xneg != yneg)
{
z = Int64Neg(z);
}
return z;
}
typedef struct
{
Int64 q;
Int64 r;
} Int64Ldiv;
static Int64Ldiv
Int64LongDivision(Int64 n, Int64 d)
{
Int64Ldiv o;
int i;
int nneg = Int64Sign(n);
int dneg = Int64Sign(d);
o.q = Int64Create(0, 0);
o.r = Int64Create(0, 0);
if (Int64Eq(d, Int64Create(0, 0)))
{
raise(SIGFPE);
return o;
}
if (nneg)
{
n = Int64Neg(n);
}
if (dneg)
{
d = Int64Neg(d);
}
for (i = 63; i >= 0; i--)
{
Int64 bit = Int64And(Int64Sra(n, i), Int64Create(0, 1));
o.r = Int64Sll(o.r, 1);
o.r = Int64Or(o.r, bit);
if (Int64Geq(o.r, d))
{
o.r = Int64Sub(o.r, d);
o.q = Int64Or(o.q, Int64Sll(Int64Create(0, 1), i));
}
}
if (nneg != dneg)
{
o.r = Int64Neg(o.r);
o.q = Int64Neg(o.q);
}
return o;
}
Int64
Int64Div(Int64 x, Int64 y)
{
return Int64LongDivision(x, y).q;
}
Int64
Int64Rem(Int64 x, Int64 y)
{
return Int64LongDivision(x, y).r;
}
Int64
Int64Sll(Int64 x, int y)
{
Int64 z;
if (!y)
{
return x;
}
z = Int64Create(0, 0);
if (y < 32)
{
z.i[1] = (x.i[0] >> (32 - y)) | (x.i[1] << y);
z.i[0] = x.i[0] << y;
}
else
{
z.i[1] = x.i[0] << (y - 32);
}
return z;
}
Int64
Int64Sra(Int64 x, int y)
{
Int64 z;
int neg = Int64Sign(x);
if (!y)
{
return x;
}
z = Int64Create(0, 0);
if (y < 32)
{
z.i[0] = (x.i[1] << (32 - y)) | (x.i[0] >> y);
z.i[1] = x.i[1] >> y;
}
else
{
z.i[0] = x.i[1] >> (y - 32);
}
if (neg)
{
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
z = Int64Or(Int64Sll(mask, (64 - y)), z);
}
return z;
}
Int64
Int64And(Int64 x, Int64 y)
{
return Int64Create(x.i[1] & y.i[1], x.i[0] & y.i[0]);
}
Int64
Int64Or(Int64 x, Int64 y)
{
return Int64Create(x.i[1] | y.i[1], x.i[0] | y.i[0]);
}
Int64
Int64Xor(Int64 x, Int64 y)
{
return Int64Create(x.i[1] ^ y.i[1], x.i[0] ^ y.i[0]);
}
Int64
Int64Not(Int64 x)
{
return Int64Create(~(x.i[1]), ~(x.i[0]));
}
int
Int64Eq(Int64 x, Int64 y)
{
return x.i[0] == y.i[0] && x.i[1] == y.i[1];
}
int
Int64Lt(Int64 x, Int64 y)
{
int xneg = Int64Sign(x);
int yneg = Int64Sign(y);
if (xneg != yneg)
{
return xneg > yneg;
}
else
{
if (xneg)
{
/* Both negative */
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
}
else
{
/* Both positive */
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
}
}
}
int
Int64Gt(Int64 x, Int64 y)
{
int xneg = Int64Sign(x);
int yneg = Int64Sign(y);
if (xneg != yneg)
{
return xneg < yneg;
}
else
{
if (xneg)
{
/* Both negative */
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
}
else
{
/* Both positive */
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
}
}
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -26,15 +26,14 @@
#include <Memory.h>
#include <Str.h>
#include <Util.h>
#include <Int.h>
#include <Int64.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <inttypes.h>
#include <errno.h>
struct JsonValue
@ -45,9 +44,9 @@ struct JsonValue
HashMap *object;
Array *array;
char *string;
uint64_t integer;
Int64 integer;
double floating;
bool boolean;
int boolean:1;
} as;
};
@ -202,7 +201,7 @@ JsonValueAsString(JsonValue * value)
}
JsonValue *
JsonValueInteger(uint64_t integer)
JsonValueInteger(Int64 integer)
{
JsonValue *value;
@ -218,12 +217,12 @@ JsonValueInteger(uint64_t integer)
return value;
}
uint64_t
Int64
JsonValueAsInteger(JsonValue * value)
{
if (!value || value->type != JSON_INTEGER)
{
return 0;
return Int64Create(0, 0);
}
return value->as.integer;
@ -259,7 +258,7 @@ JsonValueAsFloat(JsonValue * value)
}
JsonValue *
JsonValueBoolean(bool boolean)
JsonValueBoolean(int boolean)
{
JsonValue *value;
@ -275,12 +274,12 @@ JsonValueBoolean(bool boolean)
return value;
}
bool
int
JsonValueAsBoolean(JsonValue * value)
{
if (!value || value->type != JSON_BOOLEAN)
{
return false;
return 0;
}
return value->as.boolean;
@ -336,12 +335,12 @@ JsonValueFree(JsonValue * value)
Free(value);
}
size_t
int
JsonEncodeString(const char *str, Stream * out)
{
size_t i;
char c;
size_t length = 0;
int length = 0;
StreamPutc(out, '"');
length++;
@ -404,9 +403,9 @@ JsonDecodeString(Stream * in)
int c;
char a[5];
uint32_t codepoint;
uint16_t high;
uint16_t low;
UInt32 codepoint;
UInt16 high;
UInt16 low;
char *utf8Ptr;
@ -423,7 +422,7 @@ JsonDecodeString(Stream * in)
{
if (c <= 0x001F)
{
/* Bad byte; these must be escaped */
/* Bad byte; these must be escaped */
Free(str);
return NULL;
}
@ -599,13 +598,15 @@ JsonDecodeString(Stream * in)
return NULL;
}
size_t
int
JsonEncodeValue(JsonValue * value, Stream * out, int level)
{
size_t i;
size_t len;
Array *arr;
size_t length = 0;
int length = 0;
char ibuf[INT64_STRBUF];
switch (value->type)
{
@ -643,7 +644,8 @@ JsonEncodeValue(JsonValue * value, Stream * out, int level)
length += JsonEncodeString(value->as.string, out);
break;
case JSON_INTEGER:
length += StreamPrintf(out, "%" PRId64, value->as.integer);
Int64Str(value->as.integer, 10, ibuf, INT64_STRBUF);
length += StreamPrintf(out, "%s", ibuf);
break;
case JSON_FLOAT:
length += StreamPrintf(out, "%f", value->as.floating);
@ -671,14 +673,14 @@ JsonEncodeValue(JsonValue * value, Stream * out, int level)
return length;
}
size_t
int
JsonEncode(HashMap * object, Stream * out, int level)
{
size_t index;
size_t count;
char *key;
JsonValue *value;
size_t length;
int length;
if (!object)
{
@ -861,7 +863,6 @@ JsonConsumeWhitespace(JsonParserState * state)
break;
}
// TODO: This logic should be moved into Stream as a sync function.
if (StreamError(state->stream))
{
if (errno == EAGAIN)
@ -875,7 +876,7 @@ JsonConsumeWhitespace(JsonParserState * state)
}
else
{
UtilSleepMillis(delay);
UtilSleepMillis(UInt64Create(0, delay));
continue;
}
}
@ -1122,7 +1123,7 @@ JsonDecodeValue(JsonParserState * state)
JsonValue *value;
char *strValue;
int64_t iValue;
Int64 iValue;
size_t i;
int neg;
@ -1145,7 +1146,7 @@ JsonDecodeValue(JsonParserState * state)
Free(strValue);
break;
case TOKEN_INTEGER:
iValue = 0;
iValue = Int64Create(0, 0);
i = 0;
neg = 0;
@ -1161,14 +1162,14 @@ JsonDecodeValue(JsonParserState * state)
}
d = state->token[i] - '0';
iValue *= 10;
iValue += d;
iValue = Int64Mul(iValue, Int64Create(0, 10));
iValue = Int64Add(iValue, Int64Create(0, d));
i++;
}
if (neg)
{
iValue *= -1;
iValue = Int64Neg(iValue);
}
value = JsonValueInteger(iValue);
break;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -32,6 +32,8 @@
#include <unistd.h>
#include <pthread.h>
#include <Int.h>
#ifndef MEMORY_TABLE_CHUNK
#define MEMORY_TABLE_CHUNK 256
#endif
@ -48,12 +50,12 @@ struct MemoryInfo
void *pointer;
};
#define MEM_BOUND_TYPE uint32_t
#define MEM_BOUND_TYPE UInt32
#define MEM_BOUND 0xDEADBEEF
#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)))
#define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((UInt8 *) p) + x)) + 1)
#define MEM_SIZE_ACTUAL(x) (((x) * sizeof(UInt8)) + (2 * sizeof(MEM_BOUND_TYPE)))
static pthread_mutex_t lock;
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -75,39 +75,39 @@ QueueFree(Queue * q)
Free(q);
}
bool
int
QueueFull(Queue * q)
{
if (!q)
{
return false;
return 0;
}
return ((q->front == q->rear + 1) || (q->front == 0 && q->rear == q->size - 1));
}
bool
int
QueueEmpty(Queue * q)
{
if (!q)
{
return false;
return 0;
}
return (q->front == (q->size + 1));
return q->front == q->size + 1;
}
bool
int
QueuePush(Queue * q, void *element)
{
if (!q || !element)
{
return false;
return 0;
}
if (QueueFull(q))
{
return false;
return 0;
}
if (q->front == q->size + 1)
@ -126,7 +126,7 @@ QueuePush(Queue * q, void *element)
q->items[q->rear] = element;
return true;
return 1;
}
void *

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -23,12 +23,12 @@
*/
#include <Rand.h>
#include <Int.h>
#include <UInt64.h>
#include <Util.h>
#include <Memory.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <unistd.h>
@ -42,12 +42,12 @@
typedef struct RandState
{
uint32_t mt[RAND_STATE_VECTOR_LENGTH];
UInt32 mt[RAND_STATE_VECTOR_LENGTH];
int index;
} RandState;
static void
RandSeed(RandState * state, uint32_t seed)
RandSeed(RandState * state, UInt32 seed)
{
state->mt[0] = seed & 0xFFFFFFFF;
@ -57,12 +57,12 @@ RandSeed(RandState * state, uint32_t seed)
}
}
static uint32_t
static UInt32
RandGenerate(RandState * state)
{
static const uint32_t mag[2] = {0x0, 0x9908B0DF};
static const UInt32 mag[2] = {0x0, 0x9908B0DF};
uint32_t result;
UInt32 result;
if (state->index >= RAND_STATE_VECTOR_LENGTH || state->index < 0)
{
@ -118,22 +118,22 @@ RandDestructor(void *p)
/* This algorithm therefore computes N random numbers generally in O(N)
* time, while being less biased. */
void
RandIntN(uint32_t *buf, size_t size, uint32_t max)
RandIntN(int *buf, size_t size, unsigned int max)
{
static pthread_key_t stateKey;
static bool createdKey = false;
static int createdKey = 0;
/* Limit the range to banish all previously biased results */
const uint32_t allowed = RAND_MAX - RAND_MAX % max;
const int allowed = RAND_MAX - RAND_MAX % max;
RandState *state;
uint32_t tmp;
int tmp;
size_t i;
if (!createdKey)
{
pthread_key_create(&stateKey, RandDestructor);
createdKey = true;
createdKey = 1;
}
state = pthread_getspecific(stateKey);
@ -141,8 +141,8 @@ RandIntN(uint32_t *buf, size_t size, uint32_t max)
if (!state)
{
/* Generate a seed from the system time, PID, and TID */
uint64_t ts = UtilTsMillis();
uint32_t seed = ts ^ getpid() ^ (unsigned long) pthread_self();
UInt64 ts = UtilServerTs();
UInt32 seed = UInt64Low(ts) ^ getpid() ^ (unsigned long) pthread_self();
state = Malloc(sizeof(RandState));
RandSeed(state, seed);
@ -164,10 +164,10 @@ RandIntN(uint32_t *buf, size_t size, uint32_t max)
}
/* Generate just 1 random number */
uint32_t
RandInt(uint32_t max)
int
RandInt(unsigned int max)
{
uint32_t val = 0;
int val = 0;
RandIntN(&val, 1, max);
return val;

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -38,11 +38,11 @@ HexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
if (hexBuf && asciiBuf)
{
fprintf(report, "%04zx: %s | %s |\n", off, hexBuf, asciiBuf);
fprintf(report, "%04lx: %s | %s |\n", off, hexBuf, asciiBuf);
}
else
{
fprintf(report, "%04zx\n", off);
fprintf(report, "%04lx\n", off);
}
}
@ -52,7 +52,7 @@ MemoryIterator(MemoryInfo * i, void *args)
{
FILE *report = args;
fprintf(report, "%s:%d: %zu bytes at %p\n",
fprintf(report, "%s:%d: %lu bytes at %p\n",
MemoryInfoGetFile(i), MemoryInfoGetLine(i),
MemoryInfoGetSize(i), MemoryInfoGetPointer(i));
@ -107,7 +107,7 @@ GenerateMemoryReport(int argc, char **argv)
fprintf(report, " '%s'", argv[i]);
}
fprintf(report, "\nDate: %s\n", tsBuffer);
fprintf(report, "Total Bytes: %zu\n", MemoryAllocated());
fprintf(report, "Total Bytes: %lu\n", MemoryAllocated());
fprintf(report, "\n");
MemoryIterate(MemoryIterator, report);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -23,6 +23,7 @@
*/
#include <Sha.h>
#include <Memory.h>
#include <Int.h>
#include <string.h>
@ -30,10 +31,10 @@
#define LOAD32H(x, y) \
{ \
x = ((uint32_t)((y)[0] & 255) << 24) | \
((uint32_t)((y)[1] & 255) << 16) | \
((uint32_t)((y)[2] & 255) << 8) | \
((uint32_t)((y)[3] & 255)); \
x = ((UInt32)((y)[0] & 255) << 24) | \
((UInt32)((y)[1] & 255) << 16) | \
((UInt32)((y)[2] & 255) << 8) | \
((UInt32)((y)[3] & 255)); \
}
#define ROL(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
@ -48,22 +49,22 @@
typedef union
{
uint8_t c[64];
uint32_t l[16];
UInt8 c[64];
UInt32 l[16];
} Char64Long16;
typedef struct
{
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
UInt32 state[5];
UInt32 count[2];
UInt8 buffer[64];
} Sha1Context;
static void
Sha1Transform(uint32_t state[5], const uint8_t *buffer)
Sha1Transform(UInt32 state[5], const UInt8 buffer[64])
{
uint32_t a, b, c, d, e, i;
uint8_t workspace[64];
UInt32 a, b, c, d, e, i;
UInt8 workspace[64];
Char64Long16 *block = (Char64Long16 *) workspace;
for (i = 0; i < 16; i++)
@ -179,9 +180,9 @@ Sha1Init(Sha1Context * ctx)
}
static void
Sha1Update(Sha1Context * ctx, const void *buf, uint32_t size)
Sha1Update(Sha1Context * ctx, const void *buf, UInt32 size)
{
uint32_t i, j;
UInt32 i, j;
j = (ctx->count[0] >> 3) & 63;
@ -201,7 +202,7 @@ Sha1Update(Sha1Context * ctx, const void *buf, uint32_t size)
for (; i + 63 < size; i += 64)
{
Sha1Transform(ctx->state, (uint8_t *) buf + i);
Sha1Transform(ctx->state, (UInt8 *) buf + i);
}
j = 0;
@ -211,14 +212,14 @@ Sha1Update(Sha1Context * ctx, const void *buf, uint32_t size)
i = 0;
}
memcpy(&ctx->buffer[j], &((uint8_t *) buf)[i], size - i);
memcpy(&ctx->buffer[j], &((UInt8 *) buf)[i], size - i);
}
static void
Sha1Calculate(Sha1Context * ctx, unsigned char *out)
{
uint32_t i;
uint8_t count[8];
UInt32 i;
UInt8 count[8];
for (i = 0; i < 8; i++)
{
@ -226,16 +227,16 @@ Sha1Calculate(Sha1Context * ctx, unsigned char *out)
>> ((3 - (i & 3)) * 8)) & 255);
}
Sha1Update(ctx, (uint8_t *) "\x80", 1);
Sha1Update(ctx, (UInt8 *) "\x80", 1);
while ((ctx->count[0] & 504) != 448)
{
Sha1Update(ctx, (uint8_t *) "\0", 1);
Sha1Update(ctx, (UInt8 *) "\0", 1);
}
Sha1Update(ctx, count, 8);
for (i = 0; i < (160 / 8); i++)
{
out[i] = (uint8_t) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
out[i] = (UInt8) ((ctx->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -23,6 +23,7 @@
*/
#include <Sha.h>
#include <Memory.h>
#include <Int.h>
#include <stdio.h>
#include <string.h>
@ -30,10 +31,10 @@
#include <limits.h>
#define GET_UINT32(x) \
(((uint32_t)(x)[0] << 24) | \
((uint32_t)(x)[1] << 16) | \
((uint32_t)(x)[2] << 8) | \
((uint32_t)(x)[3]))
(((UInt32)(x)[0] << 24) | \
((UInt32)(x)[1] << 16) | \
((UInt32)(x)[2] << 8) | \
((UInt32)(x)[3]))
#define PUT_UINT32(dst, x) { \
(dst)[0] = (x) >> 24; \
@ -55,8 +56,8 @@
#define WW(i) (w[i] = w[i - 16] + S0(w[i - 15]) + w[i - 7] + S1(w[i - 2]))
#define ROUND(a, b, c, d, e, f, g, h, k, w) { \
uint32_t tmp0 = h + T0(e) + CH(e, f, g) + k + w; \
uint32_t tmp1 = T1(a) + MAJ(a, b, c); \
UInt32 tmp0 = h + T0(e) + CH(e, f, g) + k + w; \
UInt32 tmp1 = T1(a) + MAJ(a, b, c); \
h = tmp0 + tmp1; \
d += tmp0; \
}
@ -64,7 +65,7 @@
typedef struct Sha256Context
{
size_t length;
uint32_t state[8];
UInt32 state[8];
size_t bufLen;
unsigned char buffer[64];
} Sha256Context;
@ -72,7 +73,7 @@ typedef struct Sha256Context
static void
Sha256Chunk(Sha256Context * context, unsigned char chunk[64])
{
const uint32_t rk[64] = {
const UInt32 rk[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
@ -86,8 +87,8 @@ Sha256Chunk(Sha256Context * context, unsigned char chunk[64])
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
uint32_t w[64];
uint32_t a, b, c, d, e, f, g, h;
UInt32 w[64];
UInt32 a, b, c, d, e, f, g, h;
int i;
@ -177,10 +178,10 @@ Sha256(char *str)
unsigned char *out;
unsigned char fill[64];
uint32_t fillLen;
UInt32 fillLen;
unsigned char buf[8];
uint32_t hiLen;
uint32_t loLen;
UInt32 hiLen;
UInt32 loLen;
if (!str)
{
@ -212,8 +213,8 @@ Sha256(char *str)
fill[0] = 0x80;
fillLen = (context.bufLen < 56) ? 56 - context.bufLen : 120 - context.bufLen;
hiLen = (uint32_t) (context.length >> 29);
loLen = (uint32_t) (context.length << 3);
hiLen = (UInt32) (context.length >> 29);
loLen = (UInt32) (context.length << 3);
PUT_UINT32(&buf[0], hiLen);
PUT_UINT32(&buf[4], loLen);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -26,6 +26,7 @@
#include <Memory.h>
#include <Util.h>
#include <Rand.h>
#include <Int.h>
#include <stdlib.h>
#include <string.h>
@ -34,8 +35,8 @@
#include <pthread.h>
#include <unistd.h>
uint32_t
StrUtf16Decode(uint16_t high, uint16_t low)
UInt32
StrUtf16Decode(UInt16 high, UInt16 low)
{
if (high <= 0xD7FF)
{
@ -55,7 +56,7 @@ StrUtf16Decode(uint16_t high, uint16_t low)
}
char *
StrUtf8Encode(uint32_t codepoint)
StrUtf8Encode(UInt32 codepoint)
{
char *str;
@ -219,10 +220,10 @@ StrConcat(size_t nStr,...)
return str;
}
bool
int
StrBlank(const char *str)
{
bool blank = true;
int blank = 1;
size_t i = 0;
while (str[i])
@ -244,7 +245,7 @@ StrRandom(size_t len)
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *str;
uint32_t *nums;
int *nums;
size_t i;
if (!len)
@ -259,7 +260,7 @@ StrRandom(size_t len)
return NULL;
}
nums = Malloc(len * sizeof(uint32_t));
nums = Malloc(len * sizeof(int));
if (!nums)
{
Free(str);
@ -322,21 +323,21 @@ StrLower(char *str)
return ret;
}
bool
int
StrEquals(const char *str1, const char *str2)
{
/* Both strings are NULL, they're equal */
if (!str1 && !str2)
{
return true;
return 1;
}
/* One or the other is NULL, they're not equal */
if (!str1 || !str2)
{
return false;
return 0;
}
/* Neither are NULL, do a regular string comparison */
return (strcmp(str1, str2) == 0);
return strcmp(str1, str2) == 0;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -26,6 +26,7 @@
#include <Io.h>
#include <Memory.h>
#include <Util.h>
#include <Int.h>
#include <stdio.h>
#include <stdlib.h>
@ -49,11 +50,11 @@ struct Stream
{
Io *io;
uint8_t *rBuf;
UInt8 *rBuf;
size_t rLen;
size_t rOff;
uint8_t *wBuf;
UInt8 *wBuf;
size_t wLen;
char *ugBuf;
@ -549,13 +550,13 @@ StreamSeek(Stream * stream, off_t offset, int whence)
return result;
}
bool
int
StreamEof(Stream * stream)
{
return stream && (stream->flags & STREAM_EOF);
}
bool
int
StreamError(Stream * stream)
{
return stream && (stream->flags & STREAM_ERR);
@ -625,7 +626,7 @@ StreamCopy(Stream * in, Stream * out)
}
else
{
UtilSleepMillis(STREAM_DELAY);
UtilSleepMillis(UInt64Create(0, STREAM_DELAY));
continue;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -295,7 +295,9 @@ TlsClose(void *cookie)
SSL_free(ssl->ssl);
SSL_CTX_free(ssl->ctx);
#if 0
close(ssl->fd);
#endif
Free(ssl);

265
src/UInt64.c Normal file
View File

@ -0,0 +1,265 @@
/*
* Copyright (C) 2022-2023 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.
*/
#include <UInt64.h>
#include <stddef.h>
#include <signal.h>
size_t
UInt64Str(UInt64 x, int base, char *out, size_t len)
{
static const char symbols[] = "0123456789ABCDEF";
size_t i = len - 1;
size_t j = 0;
UInt64 base64 = UInt64Create(0, base);
/* We only have symbols up to base 16 */
if (base < 2 || base > 16)
{
return 0;
}
do
{
UInt64 mod = UInt64Rem(x, base64);
UInt32 low = UInt64Low(mod);
out[i] = symbols[low];
i--;
x = UInt64Div(x, base64);
} while (UInt64Gt(x, UInt64Create(0, 0)));
while (++i < len)
{
out[j++] = out[i];
}
out[j] = '\0';
return j;
}
#ifndef UINT64_NATIVE
/* No native 64-bit support, add our own */
UInt64
UInt64Create(UInt32 high, UInt32 low)
{
UInt64 x;
x.i[0] = low;
x.i[1] = high;
return x;
}
UInt64
UInt64Add(UInt64 x, UInt64 y)
{
UInt64 z = UInt64Create(0, 0);
int carry;
z.i[0] = x.i[0] + y.i[0];
carry = z.i[0] < x.i[0];
z.i[1] = x.i[1] + y.i[1] + carry;
return z;
}
UInt64
UInt64Sub(UInt64 x, UInt64 y)
{
UInt64 twosCompl = UInt64Add(UInt64Not(y), UInt64Create(0, 1));
return UInt64Add(x, twosCompl);
}
UInt64
UInt64Mul(UInt64 x, UInt64 y)
{
UInt64 z = UInt64Create(0, 0);
/* while (y > 0) */
while (UInt64Gt(y, UInt64Create(0, 0)))
{
/* if (y & 1 != 0) */
if (UInt64Neq(UInt64And(y, UInt64Create(0, 1)), UInt64Create(0, 0)))
{
z = UInt64Add(z, x);
}
x = UInt64Sll(x, 1);
y = UInt64Srl(y, 1);
}
return z;
}
typedef struct
{
UInt64 q;
UInt64 r;
} UInt64Ldiv;
static UInt64Ldiv
UInt64LongDivision(UInt64 n, UInt64 d)
{
UInt64Ldiv o;
int i;
o.q = UInt64Create(0, 0);
o.r = UInt64Create(0, 0);
if (UInt64Eq(d, UInt64Create(0, 0)))
{
raise(SIGFPE);
return o;
}
for (i = 63; i >= 0; i--)
{
UInt64 bit = UInt64And(UInt64Srl(n, i), UInt64Create(0, 1));
o.r = UInt64Sll(o.r, 1);
o.r = UInt64Or(o.r, bit);
if (UInt64Geq(o.r, d))
{
o.r = UInt64Sub(o.r, d);
o.q = UInt64Or(o.q, UInt64Sll(UInt64Create(0, 1), i));
}
}
return o;
}
UInt64
UInt64Div(UInt64 x, UInt64 y)
{
return UInt64LongDivision(x, y).q;
}
UInt64
UInt64Rem(UInt64 x, UInt64 y)
{
return UInt64LongDivision(x, y).r;
}
UInt64
UInt64Sll(UInt64 x, int y)
{
UInt64 z;
if (!y)
{
return x;
}
z = UInt64Create(0, 0);
if (y < 32)
{
z.i[1] = (x.i[0] >> (32 - y)) | (x.i[1] << y);
z.i[0] = x.i[0] << y;
}
else
{
z.i[1] = x.i[0] << (y - 32);
}
return z;
}
UInt64
UInt64Srl(UInt64 x, int y)
{
UInt64 z;
if (!y)
{
return x;
}
z = UInt64Create(0, 0);
if (y < 32)
{
z.i[0] = (x.i[1] << (32 - y)) | (x.i[0] >> y);
z.i[1] = x.i[1] >> y;
}
else
{
z.i[0] = x.i[1] >> (y - 32);
}
return z;
}
UInt64
UInt64And(UInt64 x, UInt64 y)
{
return UInt64Create(x.i[1] & y.i[1], x.i[0] & y.i[0]);
}
UInt64
UInt64Or(UInt64 x, UInt64 y)
{
return UInt64Create(x.i[1] | y.i[1], x.i[0] | y.i[0]);
}
UInt64
UInt64Xor(UInt64 x, UInt64 y)
{
return UInt64Create(x.i[1] ^ y.i[1], x.i[0] ^ y.i[0]);
}
UInt64
UInt64Not(UInt64 x)
{
return UInt64Create(~(x.i[1]), ~(x.i[0]));
}
int
UInt64Eq(UInt64 x, UInt64 y)
{
return x.i[0] == y.i[0] && x.i[1] == y.i[1];
}
int
UInt64Lt(UInt64 x, UInt64 y)
{
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
}
int
UInt64Gt(UInt64 x, UInt64 y)
{
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
}
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -40,6 +40,8 @@
#include <limits.h>
#include <pthread.h>
#include <UInt64.h>
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
@ -48,14 +50,14 @@
#define SSIZE_MAX LONG_MAX
#endif
uint64_t
UtilTsMillis(void)
UInt64
UtilServerTs(void)
{
struct timeval tv;
uint64_t ts;
uint64_t sec;
uint64_t usec;
UInt64 ts;
UInt64 sec;
UInt64 usec;
gettimeofday(&tv, NULL);
@ -75,31 +77,54 @@ UtilTsMillis(void)
*
* The same goes for suseconds_t.
*/
if (sizeof(time_t) == sizeof(UInt64))
{
/* 64 bit time_t: convert it to a 64 bit integer */
time_t ms = tv.tv_sec * 1000;
UInt32 high = (UInt32) (ms >> 32);
UInt32 low = (UInt32) ms;
// Two separate steps because time_t might be 32-bit. In that
// case, we want the multiplication to happen after the promotion
// to uint64_t.
sec = tv.tv_sec;
sec *= 1000;
sec = UInt64Create(high, low);
}
else
{
/* Assume 32 bit time_t: promote to 64 bit, then multiply, in
* case multiplication overflows 32 bits. */
sec = UInt64Create(0, tv.tv_sec);
sec = UInt64Mul(sec, UInt64Create(0, 1000));
}
usec = tv.tv_usec / 1000;
ts = sec + usec;
usec = UInt64Create(0, tv.tv_usec / 1000);
ts = UInt64Add(sec, usec);
return ts;
}
uint64_t
UInt64
UtilLastModified(char *path)
{
struct stat st;
uint64_t ts = 0;
UInt64 ts = UInt64Create(0, 0);
if (stat(path, &st) == 0)
{
ts = st.st_mtim.tv_sec;
ts *= 1000;
ts += st.st_mtim.tv_nsec / 1000000;
if (sizeof(time_t) == sizeof(UInt64))
{
/* 64 bit time_t: convert it to a 64 bit integer */
time_t ms = st.st_mtim.tv_sec * 1000;
UInt32 high = (UInt32) (ms >> 32);
UInt32 low = (UInt32) ms;
ts = UInt64Create(high, low);
}
else
{
ts = UInt64Create(0, st.st_mtim.tv_sec);
ts = UInt64Mul(ts, UInt64Create(0, 1000));
}
/* nsec gauanteed to fit in 32 bits */
ts = UInt64Add(ts, UInt64Create(0, st.st_mtim.tv_nsec / 1000000));
}
return ts;
@ -177,13 +202,21 @@ UtilMkdir(const char *dir, const mode_t mode)
}
int
UtilSleepMillis(uint64_t ms)
UtilSleepMillis(UInt64 ms)
{
struct timespec ts;
int res;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
if (sizeof(time_t) == sizeof(UInt64))
{
ts.tv_sec = ((time_t) UInt64High(ms) << 32 | UInt64Low(ms)) / 1000;
}
else
{
ts.tv_sec = UInt64Low(ms) / 1000;
}
ts.tv_nsec = UInt64Low(UInt64Rem(ms, UInt64Create(0, 1000))) * 1000000;
res = nanosleep(&ts, &ts);
@ -279,14 +312,14 @@ ThreadNoDestructor(void *p)
free(p);
}
uint32_t
UInt32
UtilThreadNo(void)
{
static pthread_key_t key;
static int createdKey = 0;
static unsigned long count = 0;
uint32_t *no;
UInt32 *no;
if (!createdKey)
{
@ -297,7 +330,7 @@ UtilThreadNo(void)
no = pthread_getspecific(key);
if (!no)
{
no = malloc(sizeof(uint32_t));
no = malloc(sizeof(UInt32));
*no = count++;
pthread_setspecific(key, no);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -52,7 +52,6 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
/**
* The functions in this API operate on an array structure which is
@ -99,7 +98,7 @@ extern void *ArrayGet(Array *, size_t);
* This function returns a boolean value indicating whether or not it
* suceeded.
*/
extern bool ArrayInsert(Array *, size_t, void *);
extern int ArrayInsert(Array *, size_t, void *);
/**
* Set the value at the specified index in the specified array to the
@ -116,7 +115,7 @@ extern void *ArraySet(Array *, size_t, void *);
* return value as
* .Fn ArrayInsert .
*/
extern bool ArrayAdd(Array *, void *);
extern int ArrayAdd(Array *, void *);
/**
* Remove the element at the specified index from the specified array.
@ -147,7 +146,7 @@ extern void ArraySort(Array *, int (*) (void *, void *));
* .P
* This is a relatively expensive operation. The array must first be
* duplicated. Then it is sorted, then it is iterated from beginning
* to end to remove duplicate entries. Note that the comparison
* to end to remove duplicate entires. Note that the comparison
* function is executed on each element at least twice.
*/
extern Array *ArrayUnique(Array *, int (*) (void *, void *));
@ -168,7 +167,7 @@ extern Array *ArrayReverse(Array *);
* array. This function is intended to be used by functions that return
* relatively read-only arrays that will be long-lived.
*/
extern bool ArrayTrim(Array *);
extern int ArrayTrim(Array *);
/**
* Convert a variadic arguments list into an Array. In most cases, the

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -39,7 +39,6 @@
*/
#include <stddef.h>
#include <stdbool.h>
/**
* This function computes the amount of bytes needed to store a message
@ -94,7 +93,7 @@ extern void
* this means it will only fail if a bigger string is necessary, but it
* could not be automatically allocated on the heap.
*/
extern bool
extern int
Base64Pad(char **, size_t);
#endif /* CYTOPLASM_BASE64_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -24,8 +24,6 @@
#ifndef CYTOPLASM_CRON_H
#define CYTOPLASM_CRON_H
#include <stdint.h>
/***
* @Nm Cron
* @Nd Basic periodic job scheduler.
@ -58,6 +56,8 @@
* by any means.
*/
#include "Int.h"
/**
* All functions defined here operate on a structure opaque to the
* caller.
@ -82,7 +82,7 @@ typedef void (JobFunc) (void *);
* .Pp
* This function takes the tick interval in milliseconds.
*/
extern Cron * CronCreate(uint64_t);
extern Cron * CronCreate(UInt32);
/**
* Schedule a one-off job to be executed only at the next tick, and
@ -110,7 +110,7 @@ extern void
* and a pointer to pass to that function when it is executed.
*/
extern void
CronEvery(Cron *, uint64_t, JobFunc *, void *);
CronEvery(Cron *, unsigned long, JobFunc *, void *);
/**
* Start ticking the clock and executing registered jobs.

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -24,17 +24,6 @@
#ifndef CYTOPLASM_CYTOPLASM_H
#define CYTOPLASM_CYTOPLASM_H
#define CYTOPLASM_VERSION_MAJOR 0
#define CYTOPLASM_VERSION_MINOR 4
#define CYTOPLASM_VERSION_PATCH 1
#define CYTOPLASM_VERSION ((CYTOPLASM_VERSION_MAJOR * 10000) + (CYTOPLASM_VERSION_MINOR * 100) + (CYTOPLASM_VERSION_PATCH))
#define CYTOPLASM_VERSION_ALPHA 1
#define CYTOPLASM_VERSION_BETA 0
#define CYTOPLASM_VERSION_STABLE (!CYTOPLASM_VERSION_ALPHA && !CYTOPLASM_VERSION_BETA)
#define STRINGIFY(x) #x
/***
* @Nm Cytoplasm
* @Nd A simple API that provides metadata on the library itself.
@ -45,8 +34,18 @@
* currently loaded library.
*/
/** */
extern int CytoplasmGetVersion(void);
/**
* Get the name that this library was compiled with. In most cases,
* this will be hard-coded to "Cytoplasm", but it may differ if, for
* some reason, there exists another ABI-compatible library that
* wishes to report its name.
*
* This function really only exists because the information is
* available along side of the version information so for
* consistency, it made sense to include both.
*/
extern char * CytoplasmGetName(void);
/**
* Get the library version. This will be useful mostly for printing
@ -56,6 +55,6 @@ extern int CytoplasmGetVersion(void);
* This function returns a string, which should usually be able to be
* parsed using sscanf() if absolutely necessary.
*/
extern const char * CytoplasmGetVersionStr(void);
extern char * CytoplasmGetVersion(void);
#endif /* CYTOPLASM_CYTOPLASM_H */
#endif /* CYTOPLASM_CYTOPLASM_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -37,7 +37,6 @@
*/
#include <stddef.h>
#include <stdbool.h>
#include "HashMap.h"
#include "Array.h"
@ -114,7 +113,7 @@ extern DbRef * DbLock(Db *, size_t,...);
* This function assumes the object is not locked, otherwise undefined
* behavior will result.
*/
extern bool DbDelete(Db *, size_t,...);
extern int DbDelete(Db *, size_t,...);
/**
* Unlock an object and return it back to the database. This function
@ -122,7 +121,7 @@ extern bool DbDelete(Db *, size_t,...);
* read cache; writes are always immediate to ensure data integrity in
* the event of a system failure.
*/
extern bool DbUnlock(Db *, DbRef *);
extern int DbUnlock(Db *, DbRef *);
/**
* Check the existence of the given database object in a more efficient
@ -131,7 +130,7 @@ extern bool DbUnlock(Db *, DbRef *);
* This function does not lock the object, nor does it load it into
* memory if it exists.
*/
extern bool DbExists(Db *, size_t,...);
extern int DbExists(Db *, size_t,...);
/**
* List all of the objects at a given path. Unlike the other varargs
@ -165,6 +164,6 @@ extern HashMap * DbJson(DbRef *);
* replace it with new JSON. This is more efficient than duplicating
* a separate object into the database reference.
*/
extern bool DbJsonSet(DbRef *, HashMap *);
extern int DbJsonSet(DbRef *, HashMap *);
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -150,7 +150,7 @@ extern void * HashMapDelete(HashMap *, const char *);
* insertions or deletions occur during the iteration. This
* functionality has not been tested, and will likely not work.
*/
extern bool HashMapIterate(HashMap *, char **, void **);
extern int HashMapIterate(HashMap *, char **, void **);
/**
* A reentrant version of
@ -163,7 +163,7 @@ extern bool HashMapIterate(HashMap *, char **, void **);
* .Pp
* The cursor should be initialized to 0 at the start of iteration.
*/
extern bool
extern int
HashMapIterateReentrant(HashMap *, char **, void **, size_t *);
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -74,7 +74,7 @@ extern void HttpRouterFree(HttpRouter *);
* .Pa /some/path/(.*)/parts
* to work as one would expect.
*/
extern bool HttpRouterAdd(HttpRouter *, char *, HttpRouteFunc *);
extern int HttpRouterAdd(HttpRouter *, char *, HttpRouteFunc *);
/**
* Route the specified request path using the specified routing
@ -86,6 +86,6 @@ extern bool HttpRouterAdd(HttpRouter *, char *, HttpRouteFunc *);
* how to handle, and the pointer to a void pointer is where the
* route function's response will be placed.
*/
extern bool HttpRouterRoute(HttpRouter *, char *, void *, void **);
extern int HttpRouterRoute(HttpRouter *, char *, void *, void **);
#endif /* CYTOPLASM_HTTPROUTER_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -47,7 +47,6 @@
*/
#include <stdio.h>
#include <stdbool.h>
#include "Http.h"
#include "HashMap.h"
@ -134,7 +133,7 @@ extern void HttpServerFree(HttpServer *);
* caller can continue working while the HTTP server is running in a
* separate thread and managing a pool of threads to handle responses.
*/
extern bool HttpServerStart(HttpServer *);
extern int HttpServerStart(HttpServer *);
/**
* Typically, at some point after calling

122
src/include/Int.h Normal file
View File

@ -0,0 +1,122 @@
/*
* Copyright (C) 2022-2023 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_INT_H
#define CYTOPLASM_INT_H
/***
* @Nm Int
* @Nd Fixed-width integer types.
* @Dd April 27 2023
*
* This header provides cross-platform, fixed-width integer types.
* Specifically, it uses preprocessor magic to define the following
* types:
* .Bl -bullet -offset indent
* .It
* Int8 and UInt8
* .It
* Int16 and UInt16
* .It
* Int32 and UInt32
* .El
* .Pp
* Note that there is no 64-bit integer type, because the ANSI C
* standard makes no guarantee that such a type will exist, even
* though it does on most platforms.
* .Pp
* The reason Cytoplasm provides its own header for this is
* because ANSI C does not define fixed-width types, and while it
* should be safe to rely on C99 fixed-width types in most cases,
* there may be cases where even that is not possible.
*
* @ignore-typedefs
*/
#include <limits.h>
#define BIT32_MAX 4294967295UL
#define BIT16_MAX 65535UL
#define BIT8_MAX 255UL
#ifndef UCHAR_MAX
#error Size of char data type is unknown. Define UCHAR_MAX.
#endif
#ifndef USHRT_MAX
#error Size of short data type is unknown. Define USHRT_MAX.
#endif
#ifndef UINT_MAX
#error Size of int data type is unknown. Define UINT_MAX.
#endif
#ifndef ULONG_MAX
#error Size of long data type is unknown. Define ULONG_MAX.
#endif
#if UCHAR_MAX == BIT8_MAX
typedef signed char Int8;
typedef unsigned char UInt8;
#else
#error Unable to determine suitable data type for 8-bit integers.
#endif
#if UINT_MAX == BIT16_MAX
typedef signed int Int16;
typedef unsigned int UInt16;
#elif USHRT_MAX == BIT16_MAX
typedef signed short Int16;
typedef unsigned short UInt16;
#elif UCHAR_MAX == BIT16_MAX
typedef signed char Int16;
typedef unsigned char UInt16;
#else
#error Unable to determine suitable data type for 16-bit integers.
#endif
#if ULONG_MAX == BIT32_MAX
typedef signed long Int32;
typedef unsigned long UInt32;
#elif UINT_MAX == BIT32_MAX
typedef signed int Int32;
typedef unsigned int UInt32;
#elif USHRT_MAX == BIT32_MAX
typedef signed short Int32;
typedef unsigned short UInt32;
#elif UCHAR_MAX == BIT32_MAX
typedef signed char Int32;
typedef unsigned char UInt32;
#else
#error Unable to determine suitable data type for 32-bit integers.
#endif
#endif /* CYTOPLASM_INT_H */

252
src/include/Int64.h Normal file
View File

@ -0,0 +1,252 @@
/*
* Copyright (C) 2022-2023 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_INT64_H
#define CYTOPLASM_INT64_H
/***
* @Nm Int64
* @Nd Fixed-width 64 bit integers.
* @Dd August 11, 2023
*
* .Pp
* ANSI C89 (or C99 for that matter) provides no required mechanism
* for 64 bit integers. Nevertheless, many compilers provide them as
* extensions. However, since it is not a gaurantee, and to be fully
* standards-compliant and thus portable, a platform-agnostic interface
* is required. This header provides such an interface. If the platform
* has a 64 bit integer type, that is used, and native operations are
* performed by C preprocessor macro expansion. Otherwise, a
* compatibility layer is provided, which implements 64-bit
* arithmetic on an array of 2 32-bit numbers which are provided by
* .Xr Int 3 .
* .Pp
* Note that 64-bit emulation is certainly not as performant as using
* native 64-bit operations, so whenever possible, the native
* operations should be preferred. However, since C provides no required
* 64 bit integer on 32-bit and less platforms, this API can be used as
* a "good enough" fallback mechanism.
* .Pp
* Also note that this implementation, both in the native and
* non-native forms, makes some assumptions:
* .Bl -bullet -width Ds
* .It
* When a cast from a larger integer to a smaller integer is performed,
* the upper bits are truncated, not the lower bits.
* .It
* Negative numbers are represented in memory and in registers in two's
* compliment form.
* .El
* .Pp
* This API may provide unexpected output if these assumptions are
* false for a given platform.
*
* @ignore-typedefs
*/
#include "Int.h"
#include "UInt64.h"
#include <stddef.h>
#ifndef INT64_FORCE_EMULATED
#define BIT64_MAX 18446744073709551615UL
#if UINT_MAX == BIT64_MAX
typedef signed int Int64;
#define INT64_NATIVE
#elif ULONG_MAX == BIT64_MAX
typedef signed long Int64;
#define INT64_NATIVE
#endif
#endif /* ifndef INT64_FORCE_EMULATED */
#ifdef INT64_NATIVE
#define Int64Create(high, low) ((Int64) (((UInt64) (high) << 32) | (low)))
#define Int64Neg(x) (-(x))
#define Int64Low(a) ((UInt32) (a))
#define Int64High(a) ((UInt32) ((a) >> 32))
#define Int64Add(a, b) ((a) + (b))
#define Int64Sub(a, b) ((a) - (b))
#define Int64Mul(a, b) ((a) * (b))
#define Int64Div(a, b) ((a) / (b))
#define Int64Rem(a, b) ((a) % (b))
#define Int64Sll(a, b) ((a) << (b))
#define Int64Sra(a, b) ((a) >> (b))
#define Int64And(a, b) ((a) & (b))
#define Int64Or(a, b) ((a) | (b))
#define Int64Xor(a, b) ((a) ^ (b))
#define Int64Not(a) (~(a))
#define Int64Eq(a, b) ((a) == (b))
#define Int64Lt(a, b) ((a) < (b))
#define Int64Gt(a, b) ((a) > (b))
#define Int64Neq(a, b) ((a) != (b))
#define Int64Leq(a, b) ((a) <= (b))
#define Int64Geq(a, b) ((a) >= (b))
#else
#define Int64Neg(x) (Int64Add(Int64Not(x), Int64Create(0, 1)))
/**
* The internal bit representation of a signed integer is identical
* to an unsigned integer, the difference is in the algorithms and
* the way the bits are interpreted.
*/
typedef UInt64 Int64;
/**
* Create a new signed 64 bit integer using the given high and low
* bits.
*/
extern Int64 Int64Create(UInt32, UInt32);
/**
* Add two signed 64 bit integers together.
*/
extern Int64 Int64Add(Int64, Int64);
/**
* Subtract the second 64 bit integer from the first.
*/
extern Int64 Int64Sub(Int64, Int64);
/**
* Multiply two 64 bit integers together. The non-native version of
* this function uses the Russian Peasant method of multiplication,
* which should afford more performance than a naive multiplication by
* addition, but it is still rather slow and depends on the size of
* the integers being multiplied.
*/
extern Int64 Int64Mul(Int64, Int64);
/**
* Divide the first 64 bit integer by the second and return the
* quotient. The non-native version of this function uses naive binary
* long division, which is slow, but gauranteed to finish in constant
* time.
*/
extern Int64 Int64Div(Int64, Int64);
/**
* Divide the first 64 bit integer by the second and return the
* remainder. The non-native version of this function uses naive binary
* long division, which is slow, but gauranteed to finish in constant
* time.
*/
extern Int64 Int64Rem(Int64, Int64);
/**
* Perform a left logical bit shift of a 64 bit integer. The second
* parameter is how many places to shift, and is declared as a regular
* integer because anything more than 64 does not make sense.
*/
extern Int64 Int64Sll(Int64, int);
/**
* Perform a right arithmetic bit shift of a 64 bit integer. The second
* parameter is how many places to shift, and is declared as a regular
* integer because anything more than 64 does not make sense.
* .Pp
* Note that on platforms that use the native 64-bit implementation,
* this is technically implementation-defined, and may in fact be a
* logical shift instead of an arithmetic shift. Note that typically
* this operation is not performed on signed integers.
*/
extern Int64 Int64Sra(Int64, int);
/**
* Perform a bitwise AND (&) of the provided 64 bit integers.
*/
extern Int64 Int64And(Int64, Int64);
/**
* Perform a bitwise OR (|) of the provided 64 bit integers.
*/
extern Int64 Int64Or(Int64, Int64);
/**
* Perform a bitwise XOR (^) of the provided 64 bit integers.
*/
extern Int64 Int64Xor(Int64, Int64);
/**
* Perform a bitwise NOT (~) of the provided 64 bit integer.
*/
extern Int64 Int64Not(Int64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if they are equal.
*/
extern int Int64Eq(Int64, Int64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if the second operand is strictly
* less than the first.
*/
extern int Int64Lt(Int64, Int64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if the second operand is strictly
* greater than the first.
*/
extern int Int64Gt(Int64, Int64);
#define Int64Low(a) ((a).i[0])
#define Int64High(a) ((a).i[1])
#define Int64Neq(a, b) (!Int64Eq(a, b))
#define Int64Leq(a, b) (Int64Eq(a, b) || Int64Lt(a, b))
#define Int64Geq(a, b) (Int64Eq(a, b) || Int64Gt(a, b))
#endif
#define INT64_STRBUF 65 /* Base 2 representation with '\0' */
/**
* Convert a 64 bit integer to a string in an arbitrary base
* representation specified by the second parameter, using the provided
* buffer and length specified by the third and fourth parameters. To
* guarantee that the string will fit in the buffer, allocate it of
* size INT64_STRBUF or larger. Note that a buffer size smaller than
* INT64_STRBUF will invoke undefined behavior.
*/
extern size_t Int64Str(Int64, int, char *, size_t);
#endif /* CYTOPLASM_INT64_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -71,10 +71,10 @@
#include "HashMap.h"
#include "Array.h"
#include "Stream.h"
#include "Int64.h"
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#define JSON_DEFAULT -1
#define JSON_PRETTY 0
@ -152,7 +152,7 @@ extern char * JsonValueAsString(JsonValue *);
* Encode a number as a JSON value that can be added to an object or
* an array.
*/
extern JsonValue * JsonValueInteger(uint64_t);
extern JsonValue * JsonValueInteger(Int64);
/**
* Unwrap a JSON value that represents a number. This function will
@ -160,7 +160,7 @@ extern JsonValue * JsonValueInteger(uint64_t);
* misleading. Check the type of the value before making assumptions
* about its value.
*/
extern uint64_t JsonValueAsInteger(JsonValue *);
extern Int64 JsonValueAsInteger(JsonValue *);
/**
* Encode a floating point number as a JSON value that can be added
@ -181,7 +181,7 @@ extern double JsonValueAsFloat(JsonValue *);
* expressions as a JSON value that can be added to an object or an
* array.
*/
extern JsonValue * JsonValueBoolean(bool);
extern JsonValue * JsonValueBoolean(int);
/**
* Unwrap a JSON value that represents a boolean. This function will
@ -189,7 +189,7 @@ extern JsonValue * JsonValueBoolean(bool);
* misleading. Check the type of the value before making assumptions
* about its type.
*/
extern bool JsonValueAsBoolean(JsonValue *);
extern int JsonValueAsBoolean(JsonValue *);
/**
* This is a special case that represents a JSON null. Because the
@ -253,7 +253,7 @@ extern void JsonFree(HashMap *);
* or if the stream is NULL, the number of bytes that would have
* been written.
*/
extern size_t JsonEncodeString(const char *, Stream *);
extern int JsonEncodeString(const char *, Stream *);
/**
* Serialize a JSON value as it would appear in JSON output. This is
@ -277,7 +277,7 @@ extern size_t JsonEncodeString(const char *, Stream *);
* or if the stream is NULL, the number of bytes that would have
* been written.
*/
extern size_t JsonEncodeValue(JsonValue *, Stream *, int);
extern int JsonEncodeValue(JsonValue *, Stream *, int);
/**
* Encode a JSON object as it would appear in JSON output, writing it
@ -289,7 +289,7 @@ extern size_t JsonEncodeValue(JsonValue *, Stream *, int);
* or if the stream is NULL, the number of bytes that would have
* been written.
*/
extern size_t JsonEncode(HashMap *, Stream *, int);
extern int JsonEncode(HashMap *, Stream *, int);
/**
* Decode a JSON object from the given input stream and parse it into

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -76,7 +76,6 @@
* macros.
*/
#include <stddef.h>
#include <stdint.h>
/**
* These values are passed into the memory hook function to indicate

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -46,7 +46,6 @@
*/
#include <stddef.h>
#include <stdbool.h>
/**
* These functions operate on a queue structure that is opaque to the
@ -74,7 +73,7 @@ extern void QueueFree(Queue *);
* value indicating whether or not the push succeeded. Pushing items
* into the queue will fail if the queue is full.
*/
extern bool QueuePush(Queue *, void *);
extern int QueuePush(Queue *, void *);
/**
* Pop an element out of the queue. This function returns NULL if the
@ -96,11 +95,11 @@ extern void * QueuePeek(Queue *);
/**
* Determine whether or not the queue is full.
*/
extern bool QueueFull(Queue *);
extern int QueueFull(Queue *);
/**
* Determine whether or not the queue is empty.
*/
extern bool QueueEmpty(Queue *);
extern int QueueEmpty(Queue *);
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -58,13 +58,11 @@
*/
#include <stddef.h>
#include <stdint.h>
/**
* Generate a single random 32-bit integer between 0 and the
* passed value.
* Generate a single random integer between 0 and the passed value.
*/
extern uint32_t RandInt(uint32_t);
extern int RandInt(unsigned int);
/**
* Generate the number of integers specified by the second argument
@ -78,6 +76,6 @@ extern uint32_t RandInt(uint32_t);
* has to lock and unlock a mutex. It is therefore better to obtain
* multiple random numbers in one pass if multiple are needed.
*/
extern void RandIntN(uint32_t *, size_t, uint32_t);
extern void RandIntN(int *, size_t, unsigned int);
#endif /* CYTOPLASM_RAND_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -39,21 +39,21 @@
* is a standard library header.
*/
#include "Int.h"
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
/**
* Convert UTF-16 into a Unicode codepoint.
*/
extern uint32_t StrUtf16Decode(uint16_t, uint16_t);
extern UInt32 StrUtf16Decode(UInt16, UInt16);
/**
* Take a Unicode codepoint and encode it into a string buffer containing
* between 1 and 4 bytes. The string buffer is allocated on the heap,
* so it should be freed when it is no longer needed.
*/
extern char * StrUtf8Encode(uint32_t);
extern char * StrUtf8Encode(UInt32);
/**
* Duplicate a null-terminated string, returning a new string on the
@ -87,7 +87,7 @@ extern char * StrConcat(size_t,...);
* string consists only of blank characters, as determined by
* .Xr isblank 3 .
*/
extern bool StrBlank(const char *str);
extern int StrBlank(const char *str);
/**
* Generate a string of the specified length, containing random
@ -124,6 +124,6 @@ extern char * StrLower(char *);
* function returns a boolean value indicating whether or not
* strcmp() returned 0.
*/
extern bool StrEquals(const char *, const char *);
extern int StrEquals(const char *, const char *);
#endif /* CYTOPLASM_STR_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -39,8 +39,6 @@
#include "Io.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
/**
* An opaque structure analogous to C's FILE pointers.
@ -174,7 +172,7 @@ extern off_t StreamSeek(Stream *, off_t, int);
* .Xr feof 3
* function.
*/
extern bool StreamEof(Stream *);
extern int StreamEof(Stream *);
/**
* Test the stream for an error condition, returning a boolean value
@ -183,7 +181,7 @@ extern bool StreamEof(Stream *);
* .Xr ferror 3
* function.
*/
extern bool StreamError(Stream *);
extern int StreamError(Stream *);
/**
* Clear the error condition associated with the given stream, allowing

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

252
src/include/UInt64.h Normal file
View File

@ -0,0 +1,252 @@
/*
* Copyright (C) 2022-2023 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_UINT64_H
#define CYTOPLASM_UINT64_H
/***
* @Nm UInt64
* @Nd Fixed-width 64 bit integers.
* @Dd August 11, 2023
*
* .Pp
* ANSI C89 (or C99 for that matter) provides no required mechanism
* for 64 bit integers. Nevertheless, many compilers provide them as
* extensions. However, since it is not a gaurantee, and to be fully
* standards-compliant and thus portable, a platform-agnostic interface
* is required. This header provides such an interface. If the platform
* has a 64 bit integer type, that is used, and native operations are
* performed by C preprocessor macro expansion. Otherwise, a
* compatibility layer is provided, which implements 64-bit
* arithmetic on an array of 2 32-bit numbers which are provided by
* .Xr Int 3 .
* .Pp
* Note that 64-bit emulation is certainly not as performant as using
* native 64-bit operations, so whenever possible, the native
* operations should be preferred. However, since C provides no required
* 64 bit integer on 32-bit and less platforms, this API can be used as
* a "good enough" fallback mechanism.
* .Pp
* Also note that this implementation, both in the native and
* non-native forms, makes some assumptions:
* .Bl -bullet -width Ds
* .It
* When a cast from a larger integer to a smaller integer is performed,
* the upper bits are truncated, not the lower bits.
* .It
* Negative numbers are represented in memory and in registers in two's
* compliment form.
* .El
* .Pp
* This API may provide unexpected output if these assumptions are
* false for a given platform.
*
* @ignore-typedefs
*/
#include "Int.h"
#include <stddef.h>
#ifndef INT64_FORCE_EMULATED
#define BIT64_MAX 18446744073709551615UL
#if UINT_MAX == BIT64_MAX
/* typedef signed int Int64; */
typedef unsigned int UInt64;
#define UINT64_NATIVE
#elif ULONG_MAX == BIT64_MAX
/* typedef signed int Int64; */
typedef unsigned long UInt64;
#define UINT64_NATIVE
#endif
#endif /* ifndef INT64_FORCE_EMULATED */
#ifdef UINT64_NATIVE
#define UInt64Create(high, low) (((UInt64) (high) << 32) | (low))
#define UInt64Low(a) ((UInt32) ((a) & 0x00000000FFFFFFFF))
#define UInt64High(a) ((UInt32) ((a) >> 32))
#define UInt64Add(a, b) ((a) + (b))
#define UInt64Sub(a, b) ((a) - (b))
#define UInt64Mul(a, b) ((a) * (b))
#define UInt64Div(a, b) ((a) / (b))
#define UInt64Rem(a, b) ((a) % (b))
#define UInt64Sll(a, b) ((a) << (b))
#define UInt64Srl(a, b) ((a) >> (b))
#define UInt64And(a, b) ((a) & (b))
#define UInt64Or(a, b) ((a) | (b))
#define UInt64Xor(a, b) ((a) ^ (b))
#define UInt64Not(a) (~(a))
#define UInt64Eq(a, b) ((a) == (b))
#define UInt64Lt(a, b) ((a) < (b))
#define UInt64Gt(a, b) ((a) > (b))
#define UInt64Neq(a, b) ((a) != (b))
#define UInt64Leq(a, b) ((a) <= (b))
#define UInt64Geq(a, b) ((a) >= (b))
#else
/**
* For platforms that do not have a native integer large enough to
* store a 64 bit integer, this struct is used. i[0] contains the low
* bits of integer, and i[1] contains the high bits of the integer.
* .Pp
* This struct should not be accessed directly, because UInt64 may not
* actually be this struct, it might be an actual integer type. For
* maximum portability, only use the functions defined here to
* manipulate 64 bit integers.
*/
typedef struct
{
UInt32 i[2];
} UInt64;
/**
* Create a new unsigned 64 bit integer using the given high and low
* bits.
*/
extern UInt64 UInt64Create(UInt32, UInt32);
/**
* Add two unsigned 64 bit integers together.
*/
extern UInt64 UInt64Add(UInt64, UInt64);
/**
* Subtract the second 64 bit integer from the first.
*/
extern UInt64 UInt64Sub(UInt64, UInt64);
/**
* Multiply two 64 bit integers together. The non-native version of
* this function uses the Russian Peasant method of multiplication,
* which should afford more performance than a naive multiplication by
* addition, but it is still rather slow and depends on the size of
* the integers being multiplied.
*/
extern UInt64 UInt64Mul(UInt64, UInt64);
/**
* Divide the first 64 bit integer by the second and return the
* quotient. The non-native version of this function uses naive binary
* long division, which is slow, but gauranteed to finish in constant
* time.
*/
extern UInt64 UInt64Div(UInt64, UInt64);
/**
* Divide the first 64 bit integer by the second and return the
* remainder. The non-native version of this function uses naive binary
* long division, which is slow, but gauranteed to finish in constant
* time.
*/
extern UInt64 UInt64Rem(UInt64, UInt64);
/**
* Perform a left logical bit shift of a 64 bit integer. The second
* parameter is how many places to shift, and is declared as a regular
* integer because anything more than 64 does not make sense.
*/
extern UInt64 UInt64Sll(UInt64, int);
/**
* Perform a right logical bit shift of a 64 bit integer. The second
* parameter is how many places to shift, and is declared as a regular
* integer because anything more than 64 does not make sense.
*/
extern UInt64 UInt64Srl(UInt64, int);
/**
* Perform a bitwise AND (&) of the provided 64 bit integers.
*/
extern UInt64 UInt64And(UInt64, UInt64);
/**
* Perform a bitwise OR (|) of the provided 64 bit integers.
*/
extern UInt64 UInt64Or(UInt64, UInt64);
/**
* Perform a bitwise XOR (^) of the provided 64 bit integers.
*/
extern UInt64 UInt64Xor(UInt64, UInt64);
/**
* Perform a bitwise NOT (~) of the provided 64 bit integer.
*/
extern UInt64 UInt64Not(UInt64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if they are equal.
*/
extern int UInt64Eq(UInt64, UInt64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if the second operand is strictly
* less than the first.
*/
extern int UInt64Lt(UInt64, UInt64);
/**
* Perform a comparison of the provided 64 bit integers and return a C
* boolean that is true if and only if the second operand is strictly
* greater than the first.
*/
extern int UInt64Gt(UInt64, UInt64);
#define UInt64Low(a) ((a).i[0])
#define UInt64High(a) ((a).i[1])
#define UInt64Neq(a, b) (!UInt64Eq(a, b))
#define UInt64Leq(a, b) (UInt64Eq(a, b) || UInt64Lt(a, b))
#define UInt64Geq(a, b) (UInt64Eq(a, b) || UInt64Gt(a, b))
#endif
#define UINT64_STRBUF 65 /* Base 2 representation with '\0' */
/**
* Convert a 64 bit integer to a string in an arbitrary base
* representation specified by the second parameter, using the provided
* buffer and length specified by the third and fourth parameters. To
* guarantee that the string will fit in the buffer, allocate it of
* size UINT64_STRBUF or larger. Note that a buffer size smaller than
* UINT64_STRBUF will invoke undefined behavior.
*/
extern size_t UInt64Str(UInt64, int, char *, size_t);
#endif /* CYTOPLASM_UINT64_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -43,6 +43,7 @@
#include <sys/types.h>
#include "Stream.h"
#include "UInt64.h"
/**
* Get the current timestamp in milliseconds since the Unix epoch. This
@ -60,7 +61,7 @@
* overflow before it even gets to this function, which will cause this
* function to produce unexpected results.
*/
extern uint64_t UtilTsMillis(void);
extern UInt64 UtilServerTs(void);
/**
* Use
@ -69,7 +70,7 @@ extern uint64_t UtilTsMillis(void);
* was an error getting the last modified time of a file. This is
* primarily useful for caching file data.
*/
extern uint64_t UtilLastModified(char *);
extern UInt64 UtilLastModified(char *);
/**
* This function behaves just like the system call
@ -85,7 +86,7 @@ extern int UtilMkdir(const char *, const mode_t);
* .Xr nanosleep 2
* to make its usage much, much simpler.
*/
extern int UtilSleepMillis(uint64_t);
extern int UtilSleepMillis(UInt64);
/**
* This function works identically to the POSIX
@ -111,6 +112,6 @@ extern ssize_t UtilGetLine(char **, size_t *, Stream *);
* .Fn pthread_self
* to a number.
*/
extern uint32_t UtilThreadNo(void);
extern UInt32 UtilThreadNo(void);
#endif /* CYTOPLASM_UTIL_H */

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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
@ -25,7 +25,6 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <Log.h>
#include <Array.h>
@ -46,6 +45,33 @@ Trim(char c, char *str)
return str;
}
static char *
JsonTypeFunction(JsonType jsonType)
{
char *func;
switch (jsonType)
{
case JSON_STRING:
func = "String";
break;
case JSON_INTEGER:
func = "Integer";
break;
case JSON_FLOAT:
func = "Float";
break;
case JSON_BOOLEAN:
func = "Boolean";
break;
default:
/* Should not happen */
func = NULL;
break;
}
return func;
}
static JsonType
TypeToJsonType(char *type)
{
@ -310,7 +336,7 @@ Main(Array * args)
ArrayAdd(requiredTypes, StrDuplicate(type));
}
if (StrEquals(typeType, "struct"))
if (StrEquals(typeType, "struct") || StrEquals(typeType, "union"))
{
typeFieldsVal = HashMapGet(typeObj, "fields");
if (JsonValueType(typeFieldsVal) != JSON_OBJECT)
@ -324,7 +350,7 @@ Main(Array * args)
while (HashMapIterate(typeFields, &fieldName, (void **) &fieldVal))
{
char *fieldType;
bool isArrType = false;
int isArrType = 0;
JsonValue *requiredVal;
JsonValue *ignoreVal;
@ -347,7 +373,7 @@ Main(Array * args)
{
fieldType++;
fieldType[strlen(fieldType) - 1] = '\0';
isArrType = true;
isArrType = 1;
}
if (!StrEquals(fieldType, "object") &&
@ -460,11 +486,10 @@ Main(Array * args)
StreamPrintf(headerFile, "#ifndef %s\n", guard);
StreamPrintf(headerFile, "#define %s\n\n", guard);
StreamPrintf(headerFile, "#include <stdint.h>\n");
StreamPrintf(headerFile, "#include <stdbool.h>\n");
StreamPrintf(headerFile, "#include <Cytoplasm/Array.h>\n");
StreamPrintf(headerFile, "#include <Cytoplasm/HashMap.h>\n");
StreamPrintf(headerFile, "#include <Cytoplasm/Int64.h>\n");
StreamPrintf(headerFile, "#include <Cytoplasm/Json.h>\n");
StreamPutc(headerFile, '\n');
@ -484,6 +509,7 @@ Main(Array * args)
HashMap *fields;
char *field;
char *info;
HashMap *fieldDesc;
if (!type)
@ -500,9 +526,29 @@ Main(Array * args)
fields = JsonValueAsObject(JsonGet(types, 2, type, "fields"));
StreamPrintf(headerFile, "typedef %s %s\n{\n", typeType, type);
if (StrEquals(typeType, "struct"))
if (StrEquals(typeType, "union"))
{
Array *keys = HashMapKeys(fields);
size_t j;
StreamPrintf(headerFile, "typedef enum %sType\n{\n", type);
for (j = 0; j < ArraySize(keys); j++)
{
char *key = ArrayGet(keys, j);
char *comma = j == ArraySize(keys) - 1 ? "\n" : ",\n";
StreamPrintf(headerFile, " %s_AS_%s%s", type, key, comma);
}
ArrayFree(keys);
StreamPrintf(headerFile, "} %sType;\n\n", type);
}
info = StrEquals(typeType, "union") ? "Union" : "";
StreamPrintf(headerFile, "typedef %s %s%s\n{\n", typeType, type, info);
if (StrEquals(typeType, "struct") || StrEquals(typeType, "union"))
{
while (HashMapIterate(fields, &field, (void **) &fieldDesc))
{
@ -519,11 +565,11 @@ Main(Array * args)
}
else if (StrEquals(fieldType, "integer"))
{
cType = "int64_t";
cType = "Int64";
}
else if (StrEquals(fieldType, "boolean"))
{
cType = "bool";
cType = "int";
}
else if (StrEquals(fieldType, "float"))
{
@ -545,7 +591,15 @@ Main(Array * args)
StreamPrintf(headerFile, " %s %s;\n", cType, field);
}
StreamPrintf(headerFile, "} %s;\n\n", type);
StreamPrintf(headerFile, "} %s%s;\n\n", type, info);
if (StrEquals(typeType, "union"))
{
StreamPrintf(headerFile, "typedef struct %s {\n", type);
StreamPrintf(headerFile, " %sType type;\n", type);
StreamPrintf(headerFile, " %s%s value;\n", type, info);
StreamPrintf(headerFile, "} %s;\n\n", type);
}
}
else if (StrEquals(typeType, "enum"))
{
@ -625,8 +679,8 @@ Main(Array * args)
if (StrEquals(typeType, "struct"))
{
StreamPrintf(headerFile, "extern bool %sFromJson(HashMap *, %s *, char **);\n", type, type);
StreamPrintf(implFile, "bool\n%sFromJson(HashMap *json, %s *out, char **errp)\n{\n", type, type);
StreamPrintf(headerFile, "extern int %sFromJson(HashMap *, %s *, char **);\n", type, type);
StreamPrintf(implFile, "int\n%sFromJson(HashMap *json, %s *out, char **errp)\n{\n", type, type);
StreamPrintf(implFile, " JsonValue *val;\n");
StreamPrintf(implFile, " int enumParseRes;\n");
StreamPrintf(implFile, "\n");
@ -635,14 +689,14 @@ Main(Array * args)
StreamPrintf(implFile, " if (!json | !out)\n"
" {\n"
" *errp = \"Invalid pointers passed to %sFromJson()\";\n"
" return false;\n"
" return 0;\n"
" }\n\n"
,type);
for (i = 0; i < ArraySize(keys); i++)
{
char *key = ArrayGet(keys, i);
bool required = JsonValueAsBoolean(JsonGet(fields, 2, key, "required"));
bool ignore = JsonValueAsBoolean(JsonGet(fields, 2, key, "ignore"));
int required = JsonValueAsBoolean(JsonGet(fields, 2, key, "required"));
int ignore = JsonValueAsBoolean(JsonGet(fields, 2, key, "ignore"));
char *fieldType = JsonValueAsString(JsonGet(fields, 2, key, "type"));
int isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
JsonType jsonType = isEnum ? JSON_STRING : TypeToJsonType(fieldType);
@ -659,7 +713,7 @@ Main(Array * args)
StreamPrintf(implFile, " if (val)\n {\n");
StreamPrintf(implFile, " if (JsonValueType(val) != %s)\n {\n", jsonTypeStr);
StreamPrintf(implFile, " *errp = \"%s.%s must be of type %s.\";\n", type, Trim('_', key), fieldType);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n\n");
if (StrEquals(fieldType, "array"))
{
@ -684,7 +738,7 @@ Main(Array * args)
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, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " else\n");
StreamPrintf(implFile, " {\n");
@ -703,7 +757,7 @@ Main(Array * args)
if (StrEquals(fieldType, "integer"))
{
cType = "int64_t";
cType = "Int64";
}
else if (StrEquals(fieldType, "float"))
{
@ -711,7 +765,7 @@ Main(Array * args)
}
else if (StrEquals(fieldType, "boolean"))
{
cType = "bool";
cType = "int";
}
else
{
@ -725,13 +779,13 @@ Main(Array * args)
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, " return 0;\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 array value.\";\n");
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " *ref = JsonValueAs%s(v);\n", fieldType);
StreamPrintf(implFile, " ArrayAdd(out->%s, ref);\n", key);
@ -743,7 +797,7 @@ Main(Array * args)
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, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " ArrayAdd(out->%s, StrDuplicate(JsonValueAsString(v)));\n", key);
}
@ -752,7 +806,7 @@ Main(Array * args)
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, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " ArrayAdd(out->%s, JsonDuplicate(JsonValueAsObject(v)));\n", key);
}
@ -766,13 +820,13 @@ Main(Array * args)
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, " return 0;\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, " return 0;\n");
StreamPrintf(implFile, " }\n");
if (isEnum)
{
@ -790,7 +844,7 @@ Main(Array * args)
StreamPrintf(implFile, " %sFree(parsed);\n", fieldType);
}
StreamPrintf(implFile, " Free(parsed);\n");
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " ArrayAdd(out->%s, parsed);\n", key);
}
@ -803,32 +857,12 @@ Main(Array * args)
else if (jsonType == JSON_OBJECT)
{
StreamPrintf(implFile, " if (!%sFromJson(JsonValueAsObject(val), &out->%s, errp))\n {\n", fieldType, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
}
else
{
char *func;
switch (jsonType)
{
case JSON_STRING:
func = "String";
break;
case JSON_INTEGER:
func = "Integer";
break;
case JSON_FLOAT:
func = "Float";
break;
case JSON_BOOLEAN:
func = "Boolean";
break;
default:
/* Should not happen */
func = NULL;
break;
}
char *func = JsonTypeFunction(jsonType);
if (jsonType == JSON_STRING)
{
@ -838,7 +872,7 @@ Main(Array * args)
StreamPrintf(implFile, " if (enumParseRes == -1)\n", key);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"Invalid value for %s.%s.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " out->%s = enumParseRes;\n", key);
}
@ -858,13 +892,13 @@ Main(Array * args)
{
StreamPrintf(implFile, " else\n {\n");
StreamPrintf(implFile, " *errp = \"%s.%s is required.\";\n", type, key);
StreamPrintf(implFile, " return false;\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
}
StreamPutc(implFile, '\n');
}
StreamPrintf(implFile, " return true;\n");
StreamPrintf(implFile, " return 1;\n");
StreamPrintf(implFile, "}\n\n");
StreamPrintf(headerFile, "extern HashMap * %sToJson(%s *);\n", type, type);
@ -884,8 +918,8 @@ Main(Array * args)
{
char *key = ArrayGet(keys, i);
char *fieldType = JsonValueAsString(JsonGet(fields, 2, key, "type"));
bool isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
bool ignore = JsonValueAsBoolean(JsonGet(fields, 2, key, "ignore"));
int isEnum = StrEquals(JsonValueAsString(JsonGet(types, 2, fieldType, "type")), "enum");
int ignore = JsonValueAsBoolean(JsonGet(fields, 2, key, "ignore"));
if (ignore)
{
@ -953,7 +987,7 @@ Main(Array * args)
if (StrEquals(fieldType, "integer"))
{
cType = "int64_t";
cType = "Int64";
}
else if (StrEquals(fieldType, "float"))
{
@ -961,7 +995,7 @@ Main(Array * args)
}
else if (StrEquals(fieldType, "boolean"))
{
cType = "bool";
cType = "int";
}
else
{
@ -1078,6 +1112,72 @@ Main(Array * args)
StreamPrintf(implFile, "}\n\n");
StreamPutc(headerFile, '\n');
}
else if (StrEquals(typeType, "union"))
{
StreamPrintf(headerFile, "extern int %sFromValue(JsonValue *, %s *, char **);\n", type, type);
StreamPrintf(implFile, "int\n%sFromValue(JsonValue *val, %s *out, char **errp)\n{\n", type, type);
StreamPrintf(implFile, " if (!val || !out)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " *errp = \"Invalid values passed to %sFromValue\";\n", type);
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n\n");
for (i = 0; i < ArraySize(keys); i++)
{
char *key = ArrayGet(keys, i);
char *fieldType = JsonValueAsString(JsonGet(fields, 2, key, "type"));
int isSimple = StrEquals(fieldType, "string") ||
StrEquals(fieldType, "boolean") ||
StrEquals(fieldType, "integer") ||
StrEquals(fieldType, "float") ;
int ignore = JsonValueAsBoolean(JsonGet(fields, 2, key, "ignore"));
if (isSimple)
{
JsonType actualType = TypeToJsonType(fieldType);
char *func = JsonTypeFunction(actualType);
StreamPrintf(implFile, " if (JsonValueType(val) == %s)\n", JsonTypeToStr(actualType));
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " out->type = %s_AS_%s;\n", type, key);
StreamPrintf(implFile, " out->value.%s = JsonValueAs%s(val);\n\n", key, func);
StreamPrintf(implFile, " return 1;\n");
StreamPrintf(implFile, " }\n");
continue;
}
if (StrEquals(fieldType, "array"))
{
StreamPrintf(implFile, " if (JsonValueType(val) == JSON_ARRAY)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " out->type = %s_AS_%s;\n", type, key);
StreamPrintf(implFile, " out->value.%s = JsonValueAsArray(val);\n\n", key);
StreamPrintf(implFile, " return 1;\n");
StreamPrintf(implFile, " }\n");
continue;
}
if (ignore)
{
continue;
}
StreamPrintf(implFile, " if (JsonValueType(val) == JSON_OBJECT)\n");
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " HashMap *json = JsonValueAsObject(val);\n");
StreamPrintf(implFile, " if (!%sFromJson(json, &out->value.%s, errp))\n", fieldType, key, fieldType);
StreamPrintf(implFile, " {\n");
StreamPrintf(implFile, " /* errp is already set */\n");
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, " }\n");
StreamPrintf(implFile, " out->type = %s_AS_%s;\n", type, key);
StreamPrintf(implFile, " return 1;\n");
StreamPrintf(implFile, " }\n");
/* TODO */
}
StreamPrintf(implFile, " \n");
StreamPrintf(implFile, " *errp = \"Invalid type for %s\";\n", type);
StreamPrintf(implFile, " return 0;\n");
StreamPrintf(implFile, "}\n");
}
else if (StrEquals(typeType, "enum"))
{
StreamPrintf(headerFile, "extern int %sFromStr(char *);\n", type);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
* Copyright (C) 2022-2023 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