diff --git a/.gitignore b/.gitignore
index cab1d1a..a577b9f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,12 @@ obj/
.old*/
.vs/
.vshistory/
+tmp/
+temp/
+*.tmp
+*.temp
.editorconfig
*.user
*.vcxproj.filters
+*.log
current.config
diff --git a/.gitmodules b/.gitmodules
index dd060e8..c0d4562 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,4 @@
[submodule "cbuild"]
path = cbuild
url = https://github.com/Timerix22/cbuild.git
+ branch = main
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 87f37ed..e53e8e5 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -5,10 +5,10 @@
"name": "(gdb) Debug",
"type": "cppdbg",
"request": "launch",
- "program": "${workspaceFolder}/bin/kerep.com",
+ "program": "${workspaceFolder}/bin/kerep",
+ "cwd": "${workspaceFolder}/bin",
"preLaunchTask": "build_exec_dbg",
"stopAtEntry": false,
- "cwd": "${fileDirname}",
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb"
@@ -18,9 +18,9 @@
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "build_exec_dbg",
- "program": "${workspaceFolder}/bin/kerep.com",
+ "program": "${workspaceFolder}/bin/kerep",
+ "cwd": "${workspaceFolder}/bin",
"stopAtEntry": false,
- "cwd": "${workspaceFolder}",
"externalConsole": false,
"miDebuggerPath": "/usr/bin/gdb",
"MIMode": "gdb",
@@ -36,8 +36,8 @@
"type": "cppvsdbg",
"request": "launch",
"preLaunchTask": "build_dbg",
+ "program": "${workspaceFolder}\\bin\\kerep",
"cwd": "${workspaceFolder}\\bin",
- "program": "${workspaceFolder}\\bin\\kerep.com",
"stopAtEntry": false,
"console": "integratedTerminal"
}
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
index 7fb803b..4494a57 100644
--- a/.vscode/tasks.json
+++ b/.vscode/tasks.json
@@ -3,32 +3,6 @@
"version": "2.0.0",
"tasks": [
- {
- "label": "build_exec",
- "detail": "build project",
- "type": "cppbuild",
- "command": "make",
- "args": [
- "build_exec"
- ],
- "options": {
- "cwd": "${workspaceFolder}"
- },
- "problemMatcher": ["$gcc"],
- "group": {
- "kind": "build",
- "isDefault": true
- },
- "presentation": {
- "echo": true,
- "reveal": "always",
- "focus": true,
- "panel": "shared",
- "showReuseMessage": false,
- "clear": true
- }
- },
-
{
"label": "build_exec_dbg",
"detail": "build project with debug symbols",
@@ -50,7 +24,7 @@
"focus": true,
"panel": "shared",
"showReuseMessage": false,
- "clear": true
+ "clean": true
}
}
]
diff --git a/Makefile b/Makefile
index 82fdff2..3f0f054 100644
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,85 @@
-###### Build cbuild/default_tasks #######
-all: build_exec
+######################################
+###### Build tasks #######
+######################################
-build_exec:
- @cbuild/call_task.sh build_exec
-build_exec_dbg:
- @cbuild/call_task.sh build_exec_dbg
+all: build_exec_dbg
-build_shared_lib:
- @cbuild/call_task.sh build_shared_lib
-build_shared_lib_dbg:
- @cbuild/call_task.sh build_shared_lib_dbg
+# creates executable using profiling info generated by profile
+build_exec: profile
+ @cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log
-build_static_lib:
- @cbuild/call_task.sh build_static_lib
-build_static_lib_dbg:
- @cbuild/call_task.sh build_static_lib_dbg
+# creates executable with debug info and no optimizations
+build_exec_dbg:
+ @cbuild/call_task.sh build_exec_dbg 2>&1 | tee make_raw.log
-###### Launch cbuild/default_tasks #######
+# creates shared library
+build_shared_lib:
+ @cbuild/call_task.sh build_shared_lib 2>&1 | tee make_raw.log
+
+# creates shared library with debug symbols and no optimizations
+build_shared_lib_dbg:
+ @cbuild/call_task.sh build_shared_lib_dbg 2>&1 | tee make_raw.log
+
+# creates static library
+build_static_lib:
+ @cbuild/call_task.sh build_static_lib 2>&1 | tee make_raw.log
+
+# creates static library with debug symbols and no optimizations
+build_static_lib_dbg:
+ @cbuild/call_task.sh build_static_lib_dbg 2>&1 | tee make_raw.log
+
+######################################
+###### Launch tasks #######
+######################################
+
+# executes $EXEC_FILE
exec: build_exec
- @cbuild/call_task.sh exec
+ @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log
+# executes $EXEC_FILE
+exec_dbg: build_exec_dbg
+ @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log
+
+# executes $EXEC_FILE with valgrind memory checker
valgrind: build_exec_dbg
- @cbuild/call_task.sh valgrind
+ @cbuild/call_task.sh valgrind 2>&1 | tee -a make_raw.log
+# generates profiling info
+profile:
+ @cbuild/call_task.sh profile 2>&1 | tee make_raw.log
+
+# compiles program with -pg and runs it with gprof
+# uses gprof2dot python script to generate function call tree (pip install gprof2dot)
+# requires graphviz (https://www.graphviz.org/download/source/)
+gprof:
+ @cbuild/call_task.sh gprof 2>&1 | tee make_raw.log
+
+# compiles program and runs it with callgrind (part of valgrind)
+# uses gprof2dot python script to generate function call tree (pip install gprof2dot)
+# requires graphviz (https://www.graphviz.org/download/source/)
+# P.S. detailed rezults can be viewed in KCacheGrind
+callgrind:
+ @cbuild/call_task.sh callgrind 2>&1 | tee make_raw.log
+
+# compiles executable with sanitizers and executes it to find errors and warnings
+sanitize:
+ @cbuild/call_task.sh sanitize 2>&1 | tee make_raw.log
+
+######################################
+###### Other tasks #######
+######################################
+
+# deletes generated files
+clean:
+ @cbuild/call_task.sh clean 2>&1 | tee make_raw.log
+
+# removes all unreadable characters copied from stdio
+fix_log:
+ sed 's/[^[:blank:][:print:]]//g' make_raw.log \
+ | sed 's/\[0;[0-9][0-9]m//g' \
+ | sed 's/\[0;[0-9]m//g' \
+ | sed 's/\[[0-9][0-9]m//g' \
+ | sed 's/\[[0-9]m//g' \
+ | sed 's/ H //g' \
+ | sed 's/\[3gH //g' \
+ > make_fixed.log
diff --git a/cbuild b/cbuild
index 15a9661..60fa8c1 160000
--- a/cbuild
+++ b/cbuild
@@ -1 +1 @@
-Subproject commit 15a9661c467facf65b2155eab3fad1ecf5f0c945
+Subproject commit 60fa8c11c2673a10f8afbe37bcea13c9be3317b8
diff --git a/default.config b/default.config
index 2018f93..11f00cb 100644
--- a/default.config
+++ b/default.config
@@ -1,81 +1,184 @@
#!/bin/bash
-CBUILD_VERSION=4
-CONFIG_VERSION=5
+CBUILD_VERSION=7
+CONFIG_VERSION=7
-PROJECT=kerep
-CMP_C=gcc
-CMP_CPP=g++
-STD_C=c11
-STD_CPP=c++17
-WARN_C="-Wall -Wno-discarded-qualifiers"
-WARN_CPP="-Wall"
+PROJECT="kerep"
+CMP_C="gcc"
+CMP_CPP="g++"
+STD_C="c11"
+STD_CPP="c++11"
+WARN_C="-Wall -Wno-discarded-qualifiers -Wextra -Wno-unused-parameter"
+WARN_CPP="-Wall -Wextra -Wno-unused-parameter"
SRC_C="$( find src -name '*.c')"
SRC_CPP="$( find src -name '*.cpp')"
TESTS_C="$( find tests -name '*.c')"
TESTS_CPP="$(find tests -name '*.cpp')"
-OUTDIR=bin
-OBJDIR=obj
-EXEC_FILE=$PROJECT.com
-SHARED_LIB_FILE=$PROJECT.so
-STATIC_LIB_FILE=$PROJECT.a
+# dir with dependeicy dirs
+DEPS_BASEDIR="."
+# EXAMPLE: "dependency_dir='build_task out_dir lib_file'
+# other_depndency_dir=..."
+# Dependencies must be declared on separate lines
+# Values can be override by resetting one of dependencies:
+# DEPS="$DEPS
+# dependency_dir='...'"
+DEPS=""
-#can be used in tasks
-#PRE_TASK_SCRIPT=
-#POST_TASK_SCRIPT=
+# OBJDIR structure:
+# ├── objects/ - dir where compiled *.o files are stored. cleans every call of build task
+# ├── profile/ - dir where gcc *.gcda profiling info files stored
+# └── libs/ - there you can put static libs and linker will find them
+OBJDIR="obj"
+OUTDIR="bin"
+STATIC_LIB_FILE="lib$PROJECT.a"
-case $TASK in
+# TASKS
+case "$TASK" in
+ # creates executable using profiling info if it exists
build_exec)
- C_ARGS="-O2"
+ # -flto applies more optimizations across object files
+ # -flto=auto is needed to multithreaded copilation
+ # -fuse-linker-plugin is required to use static libs with lto
+ # -fprofile-use enables compiler to use profiling info files to optimize executable
+ # -fprofile-prefix-path sets path where profiling info about objects are be saved
+ # -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
+ C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
CPP_ARGS="$C_ARGS"
- LINKER_ARGS=""
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ POST_TASK_SCRIPT=
;;
+ # creates executable with debug info and no optimizations
build_exec_dbg)
- C_ARGS="-O0 -g"
+ C_ARGS="-O0 -g3"
CPP_ARGS="$C_ARGS"
- LINKER_ARGS=""
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ POST_TASK_SCRIPT=
;;
+ # creates shared library
build_shared_lib)
C_ARGS="-O2 -fpic -flto -shared"
CPP_ARGS="$C_ARGS"
- LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE"
+ LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
+ POST_TASK_SCRIPT=
;;
+ # creates shared library with debug symbols and no optimizations
build_shared_lib_dbg)
- C_ARGS="-O0 -g -fpic -shared"
+ C_ARGS="-O0 -g3 -fpic -shared"
CPP_ARGS="$C_ARGS"
- LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE"
+ LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
+ POST_TASK_SCRIPT=
;;
+ # creates static library
build_static_lib)
- C_ARGS="-O2 -fpic"
+ C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections"
CPP_ARGS="$C_ARGS"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh
+ POST_TASK_SCRIPT=
;;
+ # creates static library with debug symbols and no optimizations
build_static_lib_dbg)
- C_ARGS="-O0 -g"
+ C_ARGS="-O0 -g3"
CPP_ARGS="$C_ARGS"
+ PRE_TASK_SCRIPT=
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh
+ POST_TASK_SCRIPT=
;;
+ # executes $EXEC_FILE
exec)
TASK_SCRIPT=cbuild/default_tasks/exec.sh
;;
+ # executes $EXEC_FILE with valgrind memory checker
valgrind)
- VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all"
+ VALGRIND_ARGS="-s --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all"
TASK_SCRIPT=cbuild/default_tasks/valgrind.sh
;;
+ # generates profiling info
+ profile)
+ OUTDIR="$OUTDIR/profile"
+ # -flto applies more optimizations across object files
+ # -flto=auto is needed to multithreaded copilation
+ # -fuse-linker-plugin is required to use static libs with lto
+ # -pg adds code to executable, that generates file containing function call info (gmon.out)
+ # -fprofile-generate generates executable with profiling code
+ # -fprofile-prefix-path sets path where profiling info about objects will be saved
+ C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
+ CPP_ARGS="$C_ARGS"
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ TASK_SCRIPT=cbuild/default_tasks/profile.sh
+ POST_TASK_SCRIPT=
+ ;;
+ # compiles program with -pg and runs it with gprof
+ # uses gprof2dot python script to generate function call tree (pip install gprof2dot)
+ # requires graphviz (https://www.graphviz.org/download/source/)
+ gprof)
+ OUTDIR="$OUTDIR/gprof"
+ # -pg adds code to executable, that generates file containing function call info (gmon.out)
+ C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg"
+ CPP_ARGS="$C_ARGS"
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ TASK_SCRIPT=cbuild/default_tasks/gprof.sh
+ POST_TASK_SCRIPT=
+ ;;
+ # compiles program and runs it with callgrind (part of valgrind)
+ # uses gprof2dot python script to generate function call tree (pip install gprof2dot)
+ # requires graphviz (https://www.graphviz.org/download/source/)
+ # P.S. detailed rezults can be viewed in KCacheGrind
+ callgrind)
+ OUTDIR="$OUTDIR/callgrind"
+ # -pg adds code to executable, that generates file containing function call info (gmon.out)
+ C_ARGS="-O2 -flto=auto -fuse-linker-plugin"
+ CPP_ARGS="$C_ARGS"
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=tasks/pre_build.sh
+ TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ POST_TASK_SCRIPT=cbuild/default_tasks/callgrind.sh
+ ;;
+ # compiles executable with sanitizers and executes it to find errors and warnings
+ sanitize)
+ OUTDIR="$OUTDIR/sanitize"
+ C_ARGS="-O0 -g3 -fsanitize=undefined,address"
+ CPP_ARGS="$C_ARGS"
+ LINKER_ARGS="$CPP_ARGS"
+ PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
+ TASK_SCRIPT=cbuild/default_tasks/exec.sh
+ POST_TASK_SCRIPT=
+ ;;
+ # deletes generated files
+ clean)
+ TASK_SCRIPT=cbuild/default_tasks/clean.sh
+ ;;
+ # nothing to do
+ no_task)
+ ;;
+ # unknown task
+ *)
+ error "task <$TASK> not found"
+ ;;
esac
-case $OS in
+# OS-specific options
+case "$OS" in
WINDOWS)
+ EXEC_FILE="$PROJECT.exe"
+ SHARED_LIB_FILE="$PROJECT.dll"
LINKER_ARGS="$LINKER_ARGS -lws2_32"
;;
LINUX)
+ EXEC_FILE="$PROJECT"
+ SHARED_LIB_FILE="$PROJECT.so"
;;
*)
- printf "${RED}operating system $OS has no configuration variants\n"
- exit 1
+ error "operating system $OS has no configuration variants"
;;
-esac
+esac
\ No newline at end of file
diff --git a/kerep.vcxproj b/kerep.vcxproj
index 33807a4..2a8ffb6 100644
--- a/kerep.vcxproj
+++ b/kerep.vcxproj
@@ -19,46 +19,88 @@
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -156,7 +198,7 @@
stdc17
Speed
- /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions)
+ /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions)
Default
stdcpp17
None
@@ -182,7 +224,7 @@
stdc17
Speed
- /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions)
+ /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions)
MultiThreadedDLL
Default
stdcpp17
@@ -208,7 +250,7 @@
stdc17
Speed
- /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions)
+ /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions)
Default
stdcpp17
None
@@ -235,7 +277,7 @@
stdc17
Speed
- /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions)
+ /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions)
MultiThreadedDLL
Default
stdcpp17
@@ -252,4 +294,4 @@
-
+
\ No newline at end of file
diff --git a/src/Array/Array.c b/src/Array/Array.c
index c669459..63d1ead 100644
--- a/src/Array/Array.c
+++ b/src/Array/Array.c
@@ -2,15 +2,26 @@
Array_define(char)
Array_define(bool)
-Array_define(float32)
-Array_define(float64)
-Array_define(int8)
-Array_define(uint8)
-Array_define(int16)
-Array_define(uint16)
-Array_define(int32)
-Array_define(uint32)
-Array_define(int64)
-Array_define(uint64)
+Array_define(f32)
+Array_define(f64)
+Array_define(i8)
+Array_define(u8)
+Array_define(i16)
+Array_define(u16)
+Array_define(i32)
+Array_define(u32)
+Array_define(i64)
+Array_define(u64)
+Array_define(Pointer)
Array_define(Unitype)
+
+void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){
+ if(freeMembers) for (u32 i=0; ilength; i++)
+ Unitype_free(array->values[i]);
+ if(array->allocatedOnHeap)
+ free(array->values);
+}
+
+void __Array_Unitype_free_(void* ar)
+{ Array_Unitype_free_(ar, true); }
diff --git a/src/Array/Array.h b/src/Array/Array.h
index 016d0ed..0f16588 100644
--- a/src/Array/Array.h
+++ b/src/Array/Array.h
@@ -9,19 +9,24 @@ extern "C" {
Array_declare(char)
Array_declare(bool)
-Array_declare(float32)
-Array_declare(float64)
-Array_declare(int8)
-Array_declare(uint8)
-Array_declare(int16)
-Array_declare(uint16)
-Array_declare(int32)
-Array_declare(uint32)
-Array_declare(int64)
-Array_declare(uint64)
+Array_declare(f32)
+Array_declare(f64)
+Array_declare(i8)
+Array_declare(u8)
+Array_declare(i16)
+Array_declare(u16)
+Array_declare(i32)
+Array_declare(u32)
+Array_declare(i64)
+Array_declare(u64)
+Array_declare(Pointer)
Array_declare(Unitype)
+/// use this function instead of auto generated
+void Array_Unitype_free_(Array_Unitype* array, bool freeMembers);
+void __Array_Unitype_free_(void* ar);
+
#if __cplusplus
}
#endif
\ No newline at end of file
diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h
index e7139b3..0c18b52 100644
--- a/src/Array/Array_declare.h
+++ b/src/Array/Array_declare.h
@@ -6,34 +6,32 @@ extern "C" {
#include "../base/base.h"
-#define Array_declare(type)\
-typedef struct Array_##type{\
- type* values;\
- uint32 length;\
- bool allocatedOnHeap;\
-} Array_##type;\
+#define Array_declare(type) \
+STRUCT(Array_##type, \
+ type* values; \
+ u32 length; \
+ bool allocatedOnHeap; \
+) \
\
-ktid_declare(Array_##type);\
+static inline Array_##type Array_##type##_allocValues(u32 length){ \
+ return (Array_##type) { \
+ .values=(type*)malloc(sizeof(type)*length), \
+ .length=length, \
+ .allocatedOnHeap=true \
+ }; \
+} \
\
-static inline Array_##type Array_##type##_allocValues(uint32 length){\
- return (Array_##type) {\
- .values=(type*)malloc(sizeof(type)*length),\
- .length=length,\
- .allocatedOnHeap=true\
- };\
-}\
+static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){ \
+ return (Array_##type) { \
+ .values=buffer, \
+ .length=bufferLength, \
+ .allocatedOnHeap=allocatedOnHeap \
+ }; \
+} \
\
-static inline Array_##type Array_##type##_fromBuffer(type* buffer, uint32 bufferLength, bool allocatedOnHeap){\
- return (Array_##type) {\
- .values=buffer,\
- .length=bufferLength,\
- .allocatedOnHeap=allocatedOnHeap\
- };\
-}\
-\
-static inline void Array_##type##_freeValues(Array_##type* array){\
- if(array->allocatedOnHeap)\
- free(array->values);\
+static inline void Array_##type##_free(Array_##type* array){ \
+ if(array->allocatedOnHeap) \
+ free(array->values); \
}
#if __cplusplus
diff --git a/src/Array/Array_define.h b/src/Array/Array_define.h
index 6a16f9b..f15533c 100644
--- a/src/Array/Array_define.h
+++ b/src/Array/Array_define.h
@@ -6,8 +6,8 @@ extern "C" {
#include "../base/base.h"
-#define Array_define(type)\
-ktid_define(Array_##type);
+#define Array_define(type) \
+kt_define(Array_##type, (freeMembers_t)Array_##type##_free, NULL);
#if __cplusplus
}
diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c
index 274f777..f2159c8 100644
--- a/src/Autoarr/Autoarr.c
+++ b/src/Autoarr/Autoarr.c
@@ -1,14 +1,17 @@
#include "Autoarr.h"
-Autoarr_define(char)
-Autoarr_define(bool)
-Autoarr_define(float32)
-Autoarr_define(float64)
-Autoarr_define(uint8)
-Autoarr_define(int8)
-Autoarr_define(uint16)
-Autoarr_define(int16)
-Autoarr_define(uint32)
-Autoarr_define(int32)
-Autoarr_define(uint64)
-Autoarr_define(int64)
+Autoarr_define(Pointer, true)
+Autoarr_define(char, false)
+Autoarr_define(bool, false)
+Autoarr_define(f32, false)
+Autoarr_define(f64, false)
+Autoarr_define(u8, false)
+Autoarr_define(i8, false)
+Autoarr_define(u16, false)
+Autoarr_define(i16, false)
+Autoarr_define(u32, false)
+Autoarr_define(i32, false)
+Autoarr_define(u64, false)
+Autoarr_define(i64, false)
+
+Autoarr_define(Unitype, false)
diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h
index e22059a..b059d00 100644
--- a/src/Autoarr/Autoarr.h
+++ b/src/Autoarr/Autoarr.h
@@ -6,21 +6,37 @@ extern "C" {
#include "Autoarr_declare.h"
#include "Autoarr_define.h"
-#include "Autoarr_Unitype.h"
+Autoarr_declare(Pointer)
Autoarr_declare(char)
Autoarr_declare(bool)
-Autoarr_declare(float32)
-Autoarr_declare(float64)
-Autoarr_declare(int8)
-Autoarr_declare(uint8)
-Autoarr_declare(int16)
-Autoarr_declare(uint16)
-Autoarr_declare(int32)
-Autoarr_declare(uint32)
-Autoarr_declare(int64)
-Autoarr_declare(uint64)
+Autoarr_declare(f32)
+Autoarr_declare(f64)
+Autoarr_declare(i8)
+Autoarr_declare(u8)
+Autoarr_declare(i16)
+Autoarr_declare(u16)
+Autoarr_declare(i32)
+Autoarr_declare(u32)
+Autoarr_declare(i64)
+Autoarr_declare(u64)
+Autoarr_declare(Unitype)
+
+#define Autoarr_foreach(ar, elem, codeblock...) { \
+ if(ar->blocks_count>0) { \
+ typeof(**ar->values) elem; \
+ for(u16 blockI=0;blockIblocks_count-1;blockI++) \
+ for(u32 elemI=0;elemImax_block_length;elemI++){ \
+ elem=ar->values[blockI][elemI]; \
+ { codeblock; } \
+ } \
+ for(u16 elemI=0;elemIblock_length;elemI++){ \
+ elem=ar->values[ar->blocks_count-1][elemI]; \
+ { codeblock; } \
+ } \
+ } \
+}
#if __cplusplus
}
diff --git a/src/Autoarr/Autoarr_KVPair_exported.c b/src/Autoarr/Autoarr_KVPair_exported.c
index 8bfbd56..77a6f69 100644
--- a/src/Autoarr/Autoarr_KVPair_exported.c
+++ b/src/Autoarr/Autoarr_KVPair_exported.c
@@ -5,7 +5,7 @@ extern "C" {
#include "Autoarr.h"
#include "../Hashtable/KeyValuePair.h"
-EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KVPair** output){
+EXPORT void CALL kerep_Autoarr_KVPair_create(u16 max_blocks_count, u16 max_block_length, Autoarr_KVPair** output){
*output=Autoarr_create(KVPair, max_blocks_count, max_block_length);
}
@@ -13,7 +13,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){
Autoarr_free(ar, true);
}
-EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){
+EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, u32 index, KVPair* output){
*output=Autoarr_get(ar, index);
}
@@ -21,15 +21,15 @@ EXPORT void CALL kerep_Autoarr_KVPair_add(Autoarr_KVPair* ar, KVPair element){
Autoarr_add(ar, element);
}
-EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, uint32 index, KVPair element){
+EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, u32 index, KVPair element){
Autoarr_set(ar, index, element);
}
-EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, uint32* output){
+EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, u32* output){
*output=Autoarr_length(ar);
}
-EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, uint32* output){
+EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, u32* output){
*output=Autoarr_max_length(ar);
}
diff --git a/src/Autoarr/Autoarr_Unitype.c b/src/Autoarr/Autoarr_Unitype.c
deleted file mode 100644
index 98ece1c..0000000
--- a/src/Autoarr/Autoarr_Unitype.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "Autoarr.h"
-
-Autoarr_define(Unitype);
-
-// right func to clear array of unitype values
-void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){
- Autoarr_foreach(ar, u,Unitype_free(u));
- __Autoarr_free_Unitype(ar, freePtr);
-}
-void ____Autoarr_free_Unitype_(void* ar) {
- __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false);
-}
diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h
deleted file mode 100644
index cf49c7c..0000000
--- a/src/Autoarr/Autoarr_Unitype.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#pragma once
-
-#if __cplusplus
-extern "C" {
-#endif
-
-#include "Autoarr_declare.h"
-#include "Autoarr_define.h"
-
-Autoarr_declare(Unitype)
-
-// this function is injected in kerep_init()
-void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr);
-void ____Autoarr_free_Unitype_(void* ar);
-
-#define Autoarr_foreach(ar,elem,codeblock)({\
- if(ar->blocks_count>0) {\
- typeof(**ar->values) elem;\
- for(uint32 blockI=0;blockIblocks_count-1;blockI++)\
- for(uint32 elemI=0;elemImax_block_length;elemI++){\
- elem=ar->values[blockI][elemI];\
- (codeblock);\
- }\
- for(uint32 elemI=0;elemIblock_length;elemI++){\
- elem=ar->values[ar->blocks_count-1][elemI];\
- (codeblock);\
- }\
- }\
-})
-
-#if __cplusplus
-}
-#endif
\ No newline at end of file
diff --git a/src/Autoarr/Autoarr_Unitype_exported.c b/src/Autoarr/Autoarr_Unitype_exported.c
index 2aded2e..6db8a6d 100644
--- a/src/Autoarr/Autoarr_Unitype_exported.c
+++ b/src/Autoarr/Autoarr_Unitype_exported.c
@@ -4,7 +4,7 @@ extern "C" {
#include "Autoarr.h"
-EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_Unitype** output){
+EXPORT void CALL kerep_Autoarr_Unitype_create(u16 max_blocks_count, u16 max_block_length, Autoarr_Unitype** output){
*output=Autoarr_create(Unitype, max_blocks_count, max_block_length);
}
@@ -12,7 +12,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){
Autoarr_free(ar, true);
}
-EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){
+EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, u32 index, Unitype* output){
*output=Autoarr_get(ar, index);
}
@@ -20,15 +20,15 @@ EXPORT void CALL kerep_Autoarr_Unitype_add(Autoarr_Unitype* ar, Unitype element)
Autoarr_add(ar, element);
}
-EXPORT void CALL kerep_Autoarr_Unitype_set(Autoarr_Unitype* ar, uint32 index, Unitype element){
+EXPORT void CALL kerep_Autoarr_Unitype_set(Autoarr_Unitype* ar, u32 index, Unitype element){
Autoarr_set(ar, index, element);
}
-EXPORT void CALL kerep_Autoarr_Unitype_length(Autoarr_Unitype* ar, uint32* output){
+EXPORT void CALL kerep_Autoarr_Unitype_length(Autoarr_Unitype* ar, u32* output){
*output=Autoarr_length(ar);
}
-EXPORT void CALL kerep_Autoarr_Unitype_max_length(Autoarr_Unitype* ar, uint32* output){
+EXPORT void CALL kerep_Autoarr_Unitype_max_length(Autoarr_Unitype* ar, u32* output){
*output=Autoarr_max_length(ar);
}
diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h
index dc16829..d996063 100644
--- a/src/Autoarr/Autoarr_declare.h
+++ b/src/Autoarr/Autoarr_declare.h
@@ -6,64 +6,67 @@ extern "C" {
#include "../base/base.h"
-#define Autoarr_declare(type)\
+#define Autoarr_declare(type) \
\
-struct Autoarr_##type;\
+struct Autoarr_##type; \
\
-typedef struct {\
- void (*add)(struct Autoarr_##type* ar, type element);\
- type (*get)(struct Autoarr_##type* ar, uint32 index);\
- type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\
- void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\
- void (*freear)(struct Autoarr_##type* ar, bool freePtr);\
- type* (*toArray)(struct Autoarr_##type* ar);\
-} __functions_list_t_##type;\
+typedef struct __Autoarr_##type##_functions_list_t { \
+ void (*add)(struct Autoarr_##type* ar, type element); \
+ type (*get)(struct Autoarr_##type* ar, u32 index); \
+ type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \
+ void (*set)(struct Autoarr_##type* ar, u32 index, type element); \
+ void (*freeWithMembers)(struct Autoarr_##type* ar, bool freePtr); \
+ void (*freeWithoutMembers)(struct Autoarr_##type* ar, bool freePtr); \
+ type* (*toArray)(struct Autoarr_##type* ar); \
+} __Autoarr_##type##_functions_list_t; \
\
-typedef struct Autoarr_##type{\
- uint16 blocks_count;\
- uint16 max_blocks_count;\
- uint16 block_length;\
- uint16 max_block_length;\
- type** values;\
- __functions_list_t_##type* functions;\
-} Autoarr_##type;\
+extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \
\
-ktid_declare(Autoarr_##type);\
+STRUCT(Autoarr_##type, \
+ u16 blocks_count; \
+ u16 max_blocks_count; \
+ u16 block_length; \
+ u16 max_block_length; \
+ type** values; \
+ __Autoarr_##type##_functions_list_t* functions; \
+) \
\
-Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\
-void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\
-void ____Autoarr_free_##type(void* ar);
+Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length); \
+void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr); \
+void ____Autoarr_##type##_freeWithMembers(void* ar);
#define Autoarr(type) Autoarr_##type
-#define Autoarr_create(type, max_blocks_count, max_block_length)\
- __Autoarr_create_##type(max_blocks_count, max_block_length)
-#define Autoarr_add(autoarr, element)\
+#define Autoarr_create(type, max_blocks_count, max_block_length) \
+ __Autoarr_##type##_create(max_blocks_count, max_block_length)
+#define Autoarr_add(autoarr, element) \
autoarr->functions->add(autoarr, element)
-#define Autoarr_get(autoarr, index)\
+#define Autoarr_get(autoarr, index) \
autoarr->functions->get(autoarr,index)
-#define Autoarr_getptr(autoarr, index)\
- autoarr->functions->getptr(autoarr,index)
-#define Autoarr_set(autoarr, index, element)\
+#define Autoarr_getPtr(autoarr, index) \
+ autoarr->functions->getPtr(autoarr,index)
+#define Autoarr_set(autoarr, index, element) \
autoarr->functions->set(autoarr, index, element)
-#define Autoarr_free(autoarr, freePtr)\
- autoarr->functions->freear(autoarr, freePtr)
-#define Autoarr_toArray(autoarr)\
+#define Autoarr_free(autoarr, freePtr) \
+ autoarr->functions->freeWithMembers(autoarr, freePtr)
+#define Autoarr_freeWithoutMembers(autoarr, freePtr) \
+ autoarr->functions->freeWithoutMembers(autoarr, freePtr)
+#define Autoarr_toArray(autoarr) \
autoarr->functions->toArray(autoarr)
#define Autoarr_length(autoarr) \
- (uint32)(!autoarr->blocks_count ? 0 : \
+ (u32)(!autoarr->blocks_count ? 0 : \
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
-#define Autoarr_max_length(autoarr)\
- (uint32)(autoarr->max_block_length*autoarr->max_blocks_count)
+#define Autoarr_max_length(autoarr) \
+ (u32)(autoarr->max_block_length*autoarr->max_blocks_count)
-#define Autoarr_pop(AR){\
- if(AR->block_length==1){\
- AR->blocks_count--;\
- AR->block_length=AR->max_block_length;\
- free(AR->values[AR->blocks_count]);\
- }\
- else AR->block_length--;\
+#define Autoarr_pop(AR){ \
+ if(AR->block_length==1){ \
+ AR->blocks_count--; \
+ AR->block_length=AR->max_block_length; \
+ free(AR->values[AR->blocks_count]); \
+ } \
+ else AR->block_length--; \
}
#if __cplusplus
diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h
index 32b665a..a7badc4 100644
--- a/src/Autoarr/Autoarr_define.h
+++ b/src/Autoarr/Autoarr_define.h
@@ -6,79 +6,93 @@ extern "C" {
#include "../base/base.h"
-#define Autoarr_define(type)\
+#define Autoarr_define(type, TYPE_IS_PTR) \
\
-ktid_define(Autoarr_##type);\
+kt_define(Autoarr_##type, ____Autoarr_##type##_freeWithMembers, NULL); \
\
-void __Autoarr_add_##type(Autoarr_##type* ar, type element){\
- if(!ar->values){\
- ar->values=malloc(ar->max_blocks_count*sizeof(type*));\
- goto create_block;\
- }\
- if(ar->block_length==ar->max_block_length){\
- if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH);\
- ar->block_length=0;\
-create_block:\
- ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type));\
- ar->blocks_count++;\
- }\
- ar->values[ar->blocks_count-1][ar->block_length]=element;\
- ar->block_length++;\
-}\
+void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \
+ if(!ar->values){ \
+ ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \
+ goto create_block; \
+ } \
+ if(ar->block_length==ar->max_block_length){ \
+ if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH); \
+ ar->block_length=0; \
+create_block: \
+ ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type)); \
+ ar->blocks_count++; \
+ } \
+ ar->values[ar->blocks_count-1][ar->block_length]=element; \
+ ar->block_length++; \
+} \
\
-type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index){\
- if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
- return ar->values[index/ar->max_block_length][index%ar->max_block_length];\
-}\
+type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \
+ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
+ return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \
+} \
\
-type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index){\
- if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
- return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\
-}\
+type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \
+ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
+ return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \
+} \
\
-void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\
- if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\
- ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\
-}\
+void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \
+ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \
+ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \
+} \
\
-void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\
- for(uint16 i=0; iblocks_count;i++)\
+void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \
+ for(u16 i=0; iblocks_count;i++) \
free(ar->values[i]); \
- free(ar->values);\
- if(freePtr) free(ar);\
-}\
-void ____Autoarr_free_##type(void* ar){\
- __Autoarr_free_##type((Autoarr_##type*)ar, false);\
-}\
+ free(ar->values); \
+ if(freePtr) free(ar); \
+} \
\
-type* __Autoarr_toArray_##type(Autoarr_##type* ar){\
- uint32 length=Autoarr_length(ar);\
- type* array=malloc(length * sizeof(type));\
- for(uint32 i=0; irows[h];
}
diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c
index f68e706..1c81feb 100644
--- a/src/DtsodParser/DtsodV24_serialize.c
+++ b/src/DtsodParser/DtsodV24_serialize.c
@@ -2,20 +2,20 @@
#include "../String/StringBuilder.h"
-typedef struct SerializeSharedData{
+STRUCT(SerializeSharedData,
StringBuilder* sh_builder;
- uint8 sh_tabs;
-} SerializeSharedData;
+ u8 sh_tabs;
+)
#define b shared->sh_builder
#define tabs shared->sh_tabs
-Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod);
+Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod);
#define addc(C) StringBuilder_append_char(b,C)
void __AppendTabs(SerializeSharedData* shared) {
- for (uint8 t = 0; t < tabs; t++)
+ for (u8 t = 0; t < tabs; t++)
addc( '\t');
};
#define AppendTabs() __AppendTabs(shared)
@@ -23,15 +23,15 @@ void __AppendTabs(SerializeSharedData* shared) {
Maybe __AppendValue(SerializeSharedData* shared, Unitype u);
#define AppendValue(UNI) __AppendValue(shared, UNI)
Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
- if(u.typeId==ktid_name(int64)){
- StringBuilder_append_int64(b,u.Int64);
+ if(u.typeId==ktid_name(i64)){
+ StringBuilder_append_i64(b,u.Int64);
}
- else if(u.typeId==ktid_name(uint64)){
- StringBuilder_append_uint64(b,u.UInt64);
+ else if(u.typeId==ktid_name(u64)){
+ StringBuilder_append_u64(b,u.UInt64);
addc('u');
}
- else if(u.typeId==ktid_name(float64)){
- StringBuilder_append_float64(b,u.Float64);
+ else if(u.typeId==ktid_name(f64)){
+ StringBuilder_append_f64(b,u.Float64);
addc('f');
}
else if(u.typeId==ktid_ptrName(char)){
@@ -46,7 +46,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
else if(u.typeId==ktid_name(bool)){
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
}
- else if(u.typeId==ktid_Null){
+ else if(Unitype_isUniNull(u)){
safethrow("Null isn't supported in DtsodV24",;);
}
else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){
@@ -55,12 +55,12 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
AppendTabs();
addc('[');
tabs++;
- Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
+ Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e,
addc('\n');
AppendTabs();
try(AppendValue(e),__,;);
addc(',');
- }));
+ );
StringBuilder_rmchar(b);
addc('\n');
tabs--;
@@ -75,11 +75,11 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
else if(u.typeId==ktid_ptrName(Hashtable)){
// check hashtable is blank
bool hashtableNotBlank=false;
- Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({
+ Hashtable_foreach(((Hashtable*)u.VoidPtr), __,
hashtableNotBlank=true;
- if(__.key); // weird way to disable warning
+ if(__.key) {} // weird way to disable warning
break;
- }));
+ );
if(hashtableNotBlank){
addc('\n');
@@ -102,14 +102,14 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){
return MaybeNull;
};
-Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){
+Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){
SerializeSharedData _shared={
.sh_builder=_b,
.sh_tabs=_tabs
};
SerializeSharedData* shared=&_shared;
- Hashtable_foreach(dtsod, p, ({
+ Hashtable_foreach(dtsod, p,
AppendTabs();
StringBuilder_append_cptr(b,p.key);
addc(':');
@@ -117,7 +117,7 @@ Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){
try(AppendValue(p.value),__,;);
addc(';');
addc('\n');
- }));
+ );
return MaybeNull;
}
diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c
index 840a3f3..812c755 100644
--- a/src/Filesystem/dir.c
+++ b/src/Filesystem/dir.c
@@ -1 +1,69 @@
-#include "dir.h"
\ No newline at end of file
+#include "filesystem.h"
+#include "io_includes.h"
+#include "../kprint/kprint.h"
+
+bool dir_exists(const char* path){
+ if(path[0]=='.'){
+ if(path[1]==0 || (path[1]==path_sep && path[1]==0))
+ return true; // dir . or ./ always exists
+ // else if(path[1]=='.' && path[2]==path_sep)
+ //TODO path_resolve because windows doesnt recognize .\ pattern
+ }
+#if KFS_USE_WINDOWS_H
+ DWORD dwAttrib = GetFileAttributes(path);
+ return (bool)(
+ (dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
+ (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory
+#else
+ struct stat stats;
+ i32 rez=stat(path, &stats);
+ return (bool)(
+ (rez!=-1) && // file exists
+ (S_ISDIR(stats.st_mode))); // file is a directory
+#endif
+}
+
+Maybe dir_create(const char* path){
+ if (dir_exists(path))
+ return MaybeNull;
+ char* parentDir=path_parentDir(path);
+ dir_create(parentDir);
+ free(parentDir);
+#if KFS_USE_WINDOWS_H
+ if(!CreateDirectory(path, NULL))
+#else
+ if(mkdir(path, 0777) == -1)
+#endif
+ {
+ char err[512];
+ IFWIN(
+ sprintf_s(err, 512, "can't create dicectory <%s>", path),
+ sprintf(err, "can't create dicectory <%s>", path));
+ safethrow(err,;);
+ }
+
+ return MaybeNull;
+}
+
+Maybe dir_delete(const char* path){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
+
+Maybe dir_getFiles(const char* path, bool recursive){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
+Maybe dir_getDirs(const char* path, bool recursive){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
+
+Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
+Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h
index 111e4ca..3821229 100644
--- a/src/Filesystem/dir.h
+++ b/src/Filesystem/dir.h
@@ -7,11 +7,21 @@ extern "C" {
#include "../base/base.h"
#include "file.h"
-typedef char* DirPath;
-Array_declare(DirPath);
+bool dir_exists(const char* path);
+///@return Maybe
+Maybe dir_create(const char* path);
+///@return Maybe
+Maybe dir_delete(const char* path);
-Array_FilePath dir_getFiles(DirPath path);
-Array_FilePath dir_findFiles(DirPath path, FilePath searchPattern);
+///@return Maybe
+Maybe dir_getFiles(const char* path, bool recursive);
+///@return Maybe
+Maybe dir_getDirs(const char* path, bool recursive);
+
+///@return Maybe
+Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive);
+///@return Maybe
+Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive);
#if __cplusplus
}
diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c
index 08e53fb..5c38523 100644
--- a/src/Filesystem/file.c
+++ b/src/Filesystem/file.c
@@ -1,7 +1,37 @@
-#include "file.h"
+#include "filesystem.h"
#include "../String/StringBuilder.h"
+#include "io_includes.h"
-ktid_define(File);
+void __file_freeMembers(void* _f){ fclose((FileHandle)_f); }
+
+kt_define(FileHandle, __file_freeMembers, NULL)
+
+bool file_exists(const char* path){
+ if(path[0]=='.'){
+ if(path[1]==0 || (path[1]==path_sep && path[2]==0))
+ return false; // . or ./ is not a file
+ // else if(path[1]=='.' && path[2]==path_sep)
+ //TODO path_resolve because windows doesnt recognize .\ pattern
+ }
+
+#if KFS_USE_WINDOWS_H
+ DWORD dwAttrib = GetFileAttributes(path);
+ return (bool)(
+ (dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
+ !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory
+#else
+ struct stat stats;
+ i32 rez=stat(path, &stats);
+ return (bool)(
+ (rez!=-1) && // file exists
+ !(S_ISDIR(stats.st_mode))); // file is not directory
+#endif
+}
+
+Maybe file_delete(const char* path, bool recursive){
+ throw(ERR_NOTIMPLEMENTED);
+ return MaybeNull;
+}
char* FileOpenMode_toStr(FileOpenMode m){
char* p;
@@ -18,14 +48,14 @@ char* FileOpenMode_toStr(FileOpenMode m){
return p;
}
-Maybe file_open(FilePath path, FileOpenMode mode){
- File* file=fopen(path, FileOpenMode_toStr(mode));
+Maybe file_open(const char* path, FileOpenMode mode){
+ FileHandle file=fopen(path, FileOpenMode_toStr(mode));
if(!file)
safethrow(cptr_concat("can't open file ", (char*)path),;);
- return SUCCESS(UniHeapPtr(File,file));
+ return SUCCESS(UniHeapPtr(FileHandle,file));
}
-Maybe file_close(File* file){
+Maybe file_close(FileHandle file){
if(!file)
safethrow(ERR_NULLPTR,;);
if(fclose(file))
@@ -33,43 +63,43 @@ Maybe file_close(File* file){
return MaybeNull;
}
-#define ioWriteCheck()\
- if(rezult==EOF)\
- safethrow(ERR_IO_EOF,;);\
- if(rezult!=0)\
+#define ioWriteCheck() \
+ if(rezult==EOF) \
+ safethrow(ERR_IO_EOF,;); \
+ if(rezult!=0) \
safethrow(ERR_IO,;);
-Maybe file_writeChar(File* file, char byte){
- int rezult=fputc(byte, file);
+Maybe file_writeChar(FileHandle file, char byte){
+ i32 rezult=fputc(byte, file);
ioWriteCheck();
return MaybeNull;
}
-Maybe file_writeBuffer(File* file, char* buffer, uint64 length){
- int rezult=0;
- for(uint64 i=0; i
+Maybe file_delete(const char* path, bool recursive);
+
+PACKED_ENUM(FileOpenMode,
// open a file for reading
FileOpenMode_Read=1,
// (re)create a file for writing
@@ -24,50 +27,49 @@ PACK_ENUM(FileOpenMode,
FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write,
// opens file for readng/writing additional data to the end / creates new file
FileOpenMode_ReadAppend=FileOpenMode_Read|FileOpenMode_Append
- )
-
+)
/// @brief opens file
/// @param path path to file
/// @param mode Read/Write/Append/ReadWrite/ReadAppend
-/// @return Maybe
-Maybe file_open(FilePath path, FileOpenMode mode);
+/// @return Maybe
+Maybe file_open(const char* path, FileOpenMode mode);
/// @brief closes file descriptor
/// @return Maybe
-Maybe file_close(File* file);
+Maybe file_close(FileHandle file);
/// @brief closes file descriptor
/// @param byte byte to write
/// @return Maybe
-Maybe file_writeChar(File* file, char byte);
+Maybe file_writeChar(FileHandle file, char byte);
/// @brief closes file descriptor
/// @param buffer bytes to write
/// @param length buffer length
/// @return Maybe
-Maybe file_writeBuffer(File* file, char* buffer, uint64 length);
+Maybe file_writeBuffer(FileHandle file, char* buffer, u64 length);
/// @brief writes all cstring array content to file
/// @param cptr zero-terminated cstring
/// @return Maybe
-Maybe file_writeCptr(File* file, char* cptr);
+Maybe file_writeCptr(FileHandle file, char* cptr);
/// @brief reads single byte from file
/// @return Maybe
-Maybe file_readChar(File* file);
+Maybe file_readChar(FileHandle file);
/// @brief reads byte array of specofied length
/// @param buffer buffer that will be filled with file bytes
/// @param length buffer length
-/// @return Maybe total number of successfully read bytes (<=length)
-Maybe file_readBuffer(File* file, char* buffer, uint64 length);
+/// @return Maybe total number of successfully read bytes (<=length)
+Maybe file_readBuffer(FileHandle file, char* buffer, u64 length);
/// @brief reads all bytes from file
/// @param allBytes ptr to the file's content will be pushed there
-/// @return Maybe total number of successfully read bytes
-Maybe file_readAll(File* file, char** allBytes);
+/// @return Maybe total number of successfully read bytes
+Maybe file_readAll(FileHandle file, char** allBytes);
#if __cplusplus
}
diff --git a/src/Filesystem/io_includes.h b/src/Filesystem/io_includes.h
new file mode 100644
index 0000000..033adf6
--- /dev/null
+++ b/src/Filesystem/io_includes.h
@@ -0,0 +1,15 @@
+#include "../base/std.h"
+
+#if defined(_WIN64) || defined(_WIN32)
+#define KFS_USE_WINDOWS_H 1
+#else
+#define KFS_USE_WINDOWS_H 0
+#endif
+
+#if KFS_USE_WINDOWS_H
+#include
+#else
+#include
+#include
+#include
+#endif
diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c
index 0a8757b..cc5cb6e 100644
--- a/src/Filesystem/path.c
+++ b/src/Filesystem/path.c
@@ -1,16 +1,16 @@
#include "filesystem.h"
-char* __path_concat(uint16 n, ...){
+char* __path_concat(u32 n, ...){
char** parts=(char**)malloc(n*sizeof(char*));
- uint32* lengths=malloc(n*sizeof(uint32));
- uint32 totalLength=0;
+ u32* lengths=malloc(n*sizeof(u32));
+ u32 totalLength=0;
// reading args from va_list
va_list vl;
va_start(vl, n);
- for(uint16 i=0; i uses <..>, that's not allowed"),);
return MaybeNull;
}
+
+char* path_parentDir(char* dir){
+ char* copy=cptr_copy(dir);
+ i32 length=cptr_length(copy);
+ i32 i=cptr_lastIndexOfChar(copy,path_sep);
+ if(i!=-1 && i==length-1){
+ copy[length-1]=0;
+ i=cptr_lastIndexOfChar(copy,path_sep);
+ }
+ if(i==-1){
+ free(copy);
+ copy=malloc(2);
+ copy[0]='.';
+ copy[1]=0;
+ }
+ return copy;
+}
+
+
+char* path_basename(char* path, bool with_extension){
+ i32 nameIndex=cptr_lastIndexOfChar(path, path_sep)+1;
+ string rezult=string_fromCptr(path+nameIndex);
+ if(!with_extension){
+ i32 extIndex=cptr_lastIndexOfChar(rezult.ptr, '.');
+ if(extIndex!=0 && extIndex!=-1)
+ rezult.length=extIndex;
+ }
+ return string_extract(rezult);
+}
diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h
index 7c3b049..65e0f31 100644
--- a/src/Filesystem/path.h
+++ b/src/Filesystem/path.h
@@ -14,22 +14,28 @@ static const char path_sep='/';
static const char path_notSep='\\';
#endif
-char* __path_concat(uint16 n, ...);
-/// @brief merges path parts together and places between them
+char* __path_concat(u32 n, ...);
+/// @brief merges path parts together and puts between them
/// @return new cstr
#define path_concat(PATH_PARTS...) __path_concat(count_args(PATH_PARTS), PATH_PARTS)
/// @brief fixes path separators
/// @param cstr where can be
/// @return new cstr with correct separators
-char* path_fixSeparators(char* path);
+char* path_fixSeparators(const char* path);
#define path_resolve(PATH_PARTS...) path_fixSeparators(path_concat(PATH_PARTS))
/// @brief calls safethrow() if finds escape sequense in path
/// @param path cstr where can be <..>
/// @return Maybe
-Maybe path_throwIfEscapes(char* path);
+Maybe path_throwIfEscapes(const char* path);
+
+///@return path of parent dir
+char* path_parentDir(char* path);
+
+///@return file name
+char* path_basename(char* path, bool with_extension);
#if __cplusplus
}
diff --git a/src/HashFunctions/hash.c b/src/HashFunctions/hash.c
index 9035ef0..3f2761b 100644
--- a/src/HashFunctions/hash.c
+++ b/src/HashFunctions/hash.c
@@ -1,14 +1,14 @@
#include "hash.h"
-uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len){
- uint8* ubuf=(uint8*)buf;
- register uint32 hash=oldhash;
+u32 hash_sdbm32(u32 oldhash, void* buf, u32 len){
+ u8* ubuf=(u8*)buf;
+ register u32 hash=oldhash;
for (; len ; len--, ubuf++)
hash=(hash<<6)+(hash<<16)-hash+*ubuf;
return hash;
}
-static const uint32 crc_32_tab[]={
+static const u32 crc_32_tab[]={
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
@@ -75,19 +75,19 @@ static const uint32 crc_32_tab[]={
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
-uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len){
- uint8* ubuf=(uint8*)buf;
- register uint32 crc=oldhash;
+u32 hash_crc32(u32 oldhash, void* buf, u32 len){
+ u8* ubuf=(u8*)buf;
+ register u32 crc=oldhash;
for (; len; --len, ++ubuf)
crc=crc_32_tab[(crc^(*ubuf)) & 0xff] ^ (crc>>8);
return ~crc;
}
-// bool hashf_crc32c(char *name, uint32 *crc, long *charcnt) {
+// bool hashf_crc32c(char *name, u32 *crc, long *charcnt) {
// register FILE *fin;
-// register uint32 oldcrc32;
-// register int c;
+// register u32 oldcrc32;
+// register i32 c;
// oldcrc32 = 0xFFFFFFFF; *charcnt = 0;
// if ((fin=fopen(name, "r"))==NULL) {
diff --git a/src/HashFunctions/hash.h b/src/HashFunctions/hash.h
index 753ed11..c4b71ef 100644
--- a/src/HashFunctions/hash.h
+++ b/src/HashFunctions/hash.h
@@ -9,8 +9,8 @@ extern "C" {
#define hashb(FUNC, BUF, LEN) FUNC(0xFFFFFFFF, BUF, LEN)
#define hashs(FUNC, STR) FUNC(0xFFFFFFFF, STR, cptr_length(STR))
-uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len);
-uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len);
+u32 hash_sdbm32(u32 oldhash, void* buf, u32 len);
+u32 hash_crc32(u32 oldhash, void* buf, u32 len);
#if __cplusplus
}
diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c
index edab896..9c8fe15 100644
--- a/src/Hashtable/Hashtable.c
+++ b/src/Hashtable/Hashtable.c
@@ -1,9 +1,9 @@
#include "Hashtable.h"
-ktid_define(Hashtable);
+kt_define(Hashtable, __Hashtable_free, NULL);
// amount of rows
-static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
+static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
#define HT_HEIN_MIN 0
#define HT_HEIN_MAX 6
@@ -14,14 +14,14 @@ Hashtable* Hashtable_create(){
Hashtable* ht=malloc(sizeof(Hashtable));
ht->hein=HT_HEIN_MIN;
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*));
- for(uint16 i=0;irows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
return ht;
}
void __Hashtable_free(void* _ht){
Hashtable* ht=_ht;
- for(uint16 i=0;ihein];i++)
+ for(u16 i=0;ihein];i++)
Autoarr_free(ht->rows[i], true);
free(ht->rows);
}
@@ -30,27 +30,28 @@ void Hashtable_free(Hashtable* ht){
free(ht);
}
-uint16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; }
+u16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; }
void Hashtable_expand(Hashtable* ht){
if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH);
Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*));
- for(uint16 i=0;ihein];i++)
+ for(u16 i=0;ihein];i++)
newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
- for(uint16 i=0;ihein-1];i++){
+ for(u16 i=0;ihein-1];i++){
Autoarr(KVPair)* ar=ht->rows[i];
- uint32 arlen=Autoarr_length(ar);
- for(uint32 k=0;khein];
+ u16 newrown=hashs(hash_sdbm32, p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KVPair)* newar=newrows[newrown];
Autoarr_add(newar,p);
}
// there is no need to free array values, because they are copied into new array
- __Autoarr_free_KVPair(ar, true);
+ // so dont replace this incorrect auto-generated function
+ Autoarr_freeWithoutMembers(ar, true);
}
free(ht->rows);
@@ -58,7 +59,7 @@ void Hashtable_expand(Hashtable* ht){
}
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
- uint32 hash=hashs(hash_sdbm32, key);
+ u32 hash=hashs(hash_sdbm32, key);
Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
Hashtable_expand(ht);
@@ -66,40 +67,67 @@ Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
return ar;
}
+/// @param key must be heap allocated
+/// Hashtable_free will free this pointer
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
KVPair p={ .key=key, .value=u };
Autoarr_add(getrow(ht,key,true),p);
}
+void Hashtable_addMany(Hashtable* ht, KVPair* pair_array, u32 count){
+ for(u32 i=0; ikey)) return &p->value;
+ u32 arlen=Autoarr_length(ar);
+ for(u32 i=0;ikey)) return &p->value;
}
return NULL;
}
Unitype Hashtable_get(Hashtable* ht, char* key){
Autoarr(KVPair)* ar=getrow(ht,key,false);
- uint32 arlen=Autoarr_length(ar);
- for(uint32 i=0;irows[h];\
- Autoarr_foreach(AR, EL, codeblock);\
- }\
-})
+#define Hashtable_foreach(HT, EL, codeblock...) { \
+ u16 hmax=Hashtable_height(HT); \
+ for(u16 h=0; hrows[h]; \
+ Autoarr_foreach(AR, EL, codeblock); \
+ } \
+}
#if __cplusplus
}
diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c
index 7e80ede..952ebe8 100644
--- a/src/Hashtable/KeyValuePair.c
+++ b/src/Hashtable/KeyValuePair.c
@@ -1,25 +1,16 @@
#include "KeyValuePair.h"
-ktid_define(KVPair);
+kt_define(KVPair, __KVPair_free, NULL);
-Autoarr_define(KVPair)
+Autoarr_define(KVPair, false)
-// proper way to clear a KVP
+// proper way to clean a KVP
void KVPair_free(KVPair p){
free(p.key);
Unitype_free(p.value);
}
void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); }
-// func for KVP array clearing
-void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){
- Autoarr_foreach(ar,k,KVPair_free(k));
- __Autoarr_free_KVPair(ar, freePtr);
-}
-void ____Autoarr_free_KVPair_(void* ar){
- __Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false);
-}
-
void printkvp(KVPair p){
kprintf("{\"%s\", ",p.key);
printuni(p.value);
diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h
index 280f0d6..99d8117 100644
--- a/src/Hashtable/KeyValuePair.h
+++ b/src/Hashtable/KeyValuePair.h
@@ -7,22 +7,17 @@ extern "C" {
#include "../base/base.h"
#include "../Autoarr/Autoarr.h"
-typedef struct KVPair{
+STRUCT(KVPair,
char* key;
Unitype value;
-} KVPair;
-ktid_declare(KVPair);
+)
Autoarr_declare(KVPair)
-// proper way to clear a KVP
+// proper way to clean a KVP
void KVPair_free(KVPair p);
void __KVPair_free(void* p);
-// func to clear KVP array
-void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr);
-void ____Autoarr_free_KVPair_(void* ar);
-
void printkvp(KVPair p);
#if __cplusplus
diff --git a/src/LinkedList/LinkedList.c b/src/LinkedList/LinkedList.c
new file mode 100644
index 0000000..26732c6
--- /dev/null
+++ b/src/LinkedList/LinkedList.c
@@ -0,0 +1,68 @@
+#include "LinkedList.h"
+
+LinkedList_define(Pointer, true)
+
+void LinkedList_addToBeginning(void* _llist, void* _new_node) {
+ LinkedList(Pointer)* llist=_llist;
+ LLNode(Pointer)* new_node=_new_node;
+ llist->count++;
+ if(llist->last_node==NULL){
+ if(llist->first_node!=NULL)
+ throw(ERR_NULLPTR); // last_node can't be null if first_node != null
+ llist->last_node=new_node;
+ }
+ else {
+ llist->first_node->prev=new_node;
+ new_node->next=llist->first_node;
+ }
+ llist->first_node=new_node;
+}
+
+void LinkedList_addToEnd(void* _llist, void* _new_node) {
+ LinkedList(Pointer)* llist=_llist;
+ LLNode(Pointer)* new_node=_new_node;
+ llist->count++;
+ if(llist->first_node==NULL) {
+ if(llist->last_node!=NULL)
+ throw(ERR_NULLPTR); // first_node can't be null if last_node != null
+ llist->first_node=new_node;
+ }
+ else {
+ llist->last_node->next=new_node;
+ new_node->prev=llist->last_node;
+ }
+ llist->last_node=new_node;
+}
+
+static inline void _insertNode(LinkedList(Pointer)* llist,
+ LLNode(Pointer)* prev_node, LLNode(Pointer)* new_node, LLNode(Pointer)* next_node){
+ if(prev_node==NULL){
+ if(next_node==llist->first_node)
+ LinkedList_addToBeginning(llist, new_node);
+ else throw(ERR_NULLPTR); // prev_node is null, but it isn't insertion before first_node
+ }
+ else if(next_node==NULL){
+ if(prev_node==llist->last_node)
+ LinkedList_addToEnd(llist, new_node);
+ else throw(ERR_NULLPTR); // next_node is null, but it isn't insertion after last_node
+ }
+ else {
+ prev_node->next=new_node;
+ new_node->prev=prev_node;
+ new_node->next=next_node;
+ next_node->prev=new_node;
+ llist->count++;
+ }
+}
+
+void LinkedList_insertPrev(void* _llist, void* _new_node, void* _next_node){
+ LLNode(Pointer)* next_node=_next_node;
+ LLNode(Pointer)* prev_node=next_node->prev;
+ _insertNode(_llist, prev_node, _new_node, next_node);
+}
+
+void LinkedList_insertNext(void* _llist, void* _new_node, void* _prev_node){
+ LLNode(Pointer)* prev_node=_prev_node;
+ LLNode(Pointer)* next_node=prev_node->next;
+ _insertNode(_llist, prev_node, _new_node, next_node);
+}
diff --git a/src/LinkedList/LinkedList.h b/src/LinkedList/LinkedList.h
new file mode 100644
index 0000000..f8435d6
--- /dev/null
+++ b/src/LinkedList/LinkedList.h
@@ -0,0 +1,83 @@
+#pragma once
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#include "../base/base.h"
+#include "LinkedList_declare.h"
+#include "LinkedList_define.h"
+
+// LinkedListNode
+
+#define LLNode(TYPE) LLNode_##TYPE
+#define LinkedList(TYPE) LinkedList_##TYPE
+
+#define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE)
+
+#define LinkedList_create(TYPE) LinkedList_##TYPE##_create()
+#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); })
+
+
+void LinkedList_addToBeginning(void* _llist, void* _new_node);
+void LinkedList_addToEnd(void* _llist, void* _new_node);
+
+/// inserts NEW_NODE before NEXT_NODE in LLIST
+void LinkedList_insertPrev(void* _llist, void* _new_node, void* _next_node);
+
+/// inserts NEW_NODE after PREV_NODE in LLIST
+void LinkedList_insertNext(void* _llist, void* _new_node, void* _prev_node);
+
+/// removes node before NEXT_NODE in LLIST
+/// if FREE_REMOVED then frees removed node
+#define LinkedList_removePrev(LLIST, NEXT_NODE, FREE_REMOVED) LLIST->_functions->removePrev(LLIST, NEXT_NODE, FREE_REMOVED)
+
+/// removes node after PREV_NODE in LLIST
+/// if FREE_REMOVED then frees removed node
+#define LinkedList_removeNext(LLIST, PREV_NODE, FREE_REMOVED) LLIST->_functions->removeNext(LLIST, PREV_NODE, FREE_REMOVED)
+
+///@param FIRST_N first node in enumeration
+///@param CURR_N name of iteration variable
+///@param CODE code todo in every iteration
+#define LLNode_foreach(FIRST_N, CURR_N, CODE...) { \
+ typeof(FIRST_N) CURR_N=FIRST_N; \
+ typeof(FIRST_N) NEXT_N=FIRST_N; \
+ while(CURR_N!=NULL) { \
+ NEXT_N=CURR_N->next; \
+ CODE; \
+ CURR_N=NEXT_N; \
+ } \
+}
+
+///@param FIRST_N first node in enumeration
+///@param CURR_N name of iteration variable
+///@param CODE code todo in every iteration
+#define LLNode_foreachReverse(FIRST_N, CURR_N, CODE...) { \
+ typeof(FIRST_N) CURR_N=FIRST_N; \
+ typeof(FIRST_N) PREV_N=FIRST_N; \
+ while(CURR_N!=NULL) { \
+ PREV_N=CURR_N->prev; \
+ CODE; \
+ CURR_N=PREV_N; \
+ } \
+}
+
+///@param LLIST LinkedList
+///@param CURR_N name of iteration variable
+///@param CODE code todo in every iteration
+#define LinkedList_foreach(LLIST, CURR_N, CODE...) \
+ LLNode_foreach(LLIST->first_node, CURR_N, CODE)
+
+///@param LLIST LinkedList
+///@param CURR_N name of iteration variable
+///@param CODE code todo in every iteration
+#define LinkedList_foreachReverse(LLIST, CURR_N, CODE...) \
+ LLNode_foreachReverse(LLIST->last_node, CURR_N, CODE)
+
+
+
+LinkedList_declare(Pointer)
+
+#if __cplusplus
+}
+#endif
diff --git a/src/LinkedList/LinkedList_declare.h b/src/LinkedList/LinkedList_declare.h
new file mode 100644
index 0000000..a0f4c9e
--- /dev/null
+++ b/src/LinkedList/LinkedList_declare.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define LLNode_declare(TYPE)\
+STRUCT(LLNode(TYPE), \
+ LLNode(TYPE)* prev; \
+ LLNode(TYPE)* next; \
+ TYPE value; \
+) \
+\
+LLNode(TYPE)* LLNode_##TYPE##_create(TYPE value); \
+void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value);
+
+
+#define LinkedList_declare(TYPE)\
+LLNode_declare(TYPE) \
+typedef struct LinkedList_##TYPE##_functions_t LinkedList_##TYPE##_functions_t; \
+\
+STRUCT(LinkedList(TYPE), \
+ LinkedList_##TYPE##_functions_t* _functions; \
+ LLNode(TYPE)* first_node; \
+ LLNode(TYPE)* last_node; \
+ u32 count; \
+) \
+\
+typedef struct LinkedList_##TYPE##_functions_t { \
+ freeMembers_t freeMembers; \
+ void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \
+ void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \
+} LinkedList_##TYPE##_functions_t; \
+\
+extern LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions; \
+\
+LinkedList(TYPE)* LinkedList_##TYPE##_create(); \
+
+
+#if __cplusplus
+}
+#endif
diff --git a/src/LinkedList/LinkedList_define.h b/src/LinkedList/LinkedList_define.h
new file mode 100644
index 0000000..f978638
--- /dev/null
+++ b/src/LinkedList/LinkedList_define.h
@@ -0,0 +1,82 @@
+#pragma once
+
+#if __cplusplus
+extern "C" {
+#endif
+
+#define LLNode_define(TYPE, TYPE_IS_PTR)\
+\
+LLNode(TYPE)* LLNode_##TYPE##_create(TYPE value){ \
+ LLNode(TYPE)* node= (LLNode(TYPE)*)malloc(sizeof(*node)); \
+ node->prev=NULL; \
+ node->next=NULL; \
+ node->value=value; \
+ return node; \
+} \
+\
+void LLNode_##TYPE##_freeMembers(void* _node){ \
+ LLNode(TYPE)* node=(LLNode(TYPE)*)_node; \
+ void* value_ptr=&node->value; \
+ if(TYPE_IS_PTR) value_ptr=*(TYPE**)value_ptr; \
+ ktDescriptor_##TYPE.freeMembers(value_ptr); \
+} \
+\
+void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value){ \
+ if(free_value) LLNode_##TYPE##_freeMembers(node); \
+ free(node); \
+} \
+\
+kt_define(LLNode_##TYPE, LLNode_##TYPE##_freeMembers, NULL)
+
+
+#define LinkedList_define(TYPE, VALUE_IS_PTR)\
+LLNode_define(TYPE, VALUE_IS_PTR) \
+\
+LinkedList(TYPE)* LinkedList_##TYPE##_create(){ \
+ LinkedList(TYPE)* l=malloc(sizeof(*l)); \
+ l->_functions=&_LinkedList_##TYPE##_functions; \
+ l->first_node=NULL; \
+ l->last_node=NULL; \
+ l->count=0; \
+ return l; \
+} \
+\
+void LinkedList_##TYPE##_freeMembers(void* _l){ \
+ LinkedList(TYPE)* l=(LinkedList(TYPE)*)_l; \
+ if(l->first_node!=NULL) \
+ LinkedList_foreach(l, node, LLNode_##TYPE##_free(node, true)); \
+ l->first_node=NULL; l->last_node=NULL; l->count=0; \
+} \
+\
+void LinkedList_##TYPE##_removePrev(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved){ \
+ llist->count--; \
+ LLNode(TYPE)* removedNode=nextNode->prev; \
+ LLNode(TYPE)* prevNode=removedNode->prev; \
+ nextNode->prev=prevNode; \
+ prevNode->next=nextNode; \
+ if(freeRemoved) \
+ LLNode_##TYPE##_free(removedNode, true); \
+} \
+\
+void LinkedList_##TYPE##_removeNext(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved){ \
+ llist->count--; \
+ LLNode(TYPE)* removedNode=prevNode->next; \
+ LLNode(TYPE)* nextNode=removedNode->next; \
+ prevNode->next=nextNode; \
+ nextNode->prev=prevNode; \
+ if(freeRemoved) \
+ LLNode_##TYPE##_free(removedNode, true); \
+} \
+\
+LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions={ \
+ .freeMembers=LinkedList_##TYPE##_freeMembers, \
+ .removePrev=LinkedList_##TYPE##_removePrev, \
+ .removeNext=LinkedList_##TYPE##_removeNext \
+}; \
+\
+kt_define(LinkedList_##TYPE, LinkedList_##TYPE##_freeMembers, NULL)
+
+
+#if __cplusplus
+}
+#endif
diff --git a/src/Network/knAddress.c b/src/Network/knAddress.c
index 06b0f9e..5f02151 100644
--- a/src/Network/knAddress.c
+++ b/src/Network/knAddress.c
@@ -6,11 +6,11 @@ ktid_define(knIPV4Endpoint);
Maybe knIPV4Address_fromStr(char* addrStr){
char* addrStr_src=addrStr;
char* errmsg_extra="wrong char";
- uint8 c;
+ u8 c;
knIPV4Address addr;
addr.u32=0;
- uint16 n=0;
- for(uint8 i=0; i<4; ){
+ u16 n=0;
+ for(u8 i=0; i<4; ){
c=*addrStr++;
switch (c){
case '\0':
@@ -18,6 +18,10 @@ Maybe knIPV4Address_fromStr(char* addrStr){
errmsg_extra="end of string";
goto default_case;
}
+
+ addr.bytes[i++]=n;
+ n=0;
+ break;
case '.':
addr.bytes[i++]=n;
n=0;
@@ -32,7 +36,7 @@ Maybe knIPV4Address_fromStr(char* addrStr){
break;
default_case:
default:
- uint32 errmsgL=cptr_length(addrStr) + 80;
+ u32 errmsgL=cptr_length(addrStr) + 80;
char* errmsg=malloc(errmsgL);
IFMSC(sprintf_s(errmsg, errmsgL, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra),
sprintf( errmsg, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra));
diff --git a/src/Network/knAddress.h b/src/Network/knAddress.h
index ce95a39..721ae88 100644
--- a/src/Network/knAddress.h
+++ b/src/Network/knAddress.h
@@ -7,10 +7,10 @@ extern "C" {
#include "../base/base.h"
-typedef uint16 knPort;
+typedef u16 knPort;
typedef union knIPV4Address {
- uint32 u32;
+ u32 u32;
char bytes[4];
} knIPV4Address;
ktid_declare(knIPV4Address);
diff --git a/src/Network/sockets/knSocketChanneled.c b/src/Network/sockets/knSocketChanneled.c
index 55e576c..2bf4443 100644
--- a/src/Network/sockets/knSocketChanneled.c
+++ b/src/Network/sockets/knSocketChanneled.c
@@ -38,7 +38,7 @@ knChannel* __createChannel(){
Maybe knSocketChanneled_createChannel(knSocketChanneled* socket){
if(socket->channelsAmount == 65535)
safethrow("max amount of channels",;);
- uint16 channelsAmountPrev=socket->channelsAmount;
+ u16 channelsAmountPrev=socket->channelsAmount;
socket->channelsAmount++;
if(channelsAmountPrev==0)
socket->channels=malloc(sizeof(knChannel*));
@@ -54,6 +54,6 @@ Maybe knSocketChanneled_connect(knSocketChanneled* socket, knIPV4Endpoint remote
Maybe knSocketChanneled_accept(knSocketChanneled* socket);
-Maybe knSocketChanneled_send(knSocketChanneled* socket, uint16 destinationIndex, uint8* data, uint32 dataLength);
+Maybe knSocketChanneled_send(knSocketChanneled* socket, u16 destinationIndex, u8* data, u32 dataLength);
-Maybe knSocketChanneled_recieve(knSocketChanneled* socket, uint16 destinationIndex, uint8* buffer, uint32 bufferLength);
\ No newline at end of file
+Maybe knSocketChanneled_recieve(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
\ No newline at end of file
diff --git a/src/Network/sockets/knSocketChanneled.h b/src/Network/sockets/knSocketChanneled.h
index 3ba8d39..e5e3018 100644
--- a/src/Network/sockets/knSocketChanneled.h
+++ b/src/Network/sockets/knSocketChanneled.h
@@ -7,7 +7,7 @@ extern "C" {
#include "../../base/base.h"
#include "../knAddress.h"
-#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*))
+#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(u8*))
typedef enum __attribute__((__packed__)) knPacVersion {
@@ -19,11 +19,11 @@ static const char knPacHeader[5]={'k','n','p','a','c'};
typedef struct knPackage {
char header[5]; // knpac
knPacVersion version; // protocol version
- uint16 data_size; // size of data block in bytes (1-KNPAC_MAX_DATA_SIZE)
- uint32 channel_id; // id of knChannel in socket
- uint32 package_num; // number in sequence of sent packages
- uint64 data_hash; // hash64 of data
- uint8* data; // ptr to data
+ u16 data_size; // size of data block in bytes (1-KNPAC_MAX_DATA_SIZE)
+ u32 channel_id; // id of knChannel in socket
+ u32 package_num; // number in sequence of sent packages
+ u64 data_hash; // hash64 of data
+ u8* data; // ptr to data
} knPackage;
ktid_declare(knPackage);
@@ -41,10 +41,10 @@ typedef struct knChannel {
ktid_declare(knChannel);
typedef struct knSocketChanneled{
- int64 socketfd;
+ i64 socketfd;
knIPV4Endpoint localEndpoint;
knIPV4Endpoint remoteEndpoint;
- uint16 channelsAmount;
+ u16 channelsAmount;
knChannel** channels;
} knSocketChanneled;
ktid_declare(knSocketChanneled);
@@ -56,7 +56,7 @@ Maybe knSocketChanneled_open();
///@return Maybe error or nothing
Maybe knSocketChanneled_close(knSocketChanneled* socket);
-///@return Maybe channel index
+///@return Maybe channel index
Maybe knSocketChanneled_createChannel(knSocketChanneled* socket);
///start listening at local endpoint
@@ -72,12 +72,12 @@ Maybe knSocketChanneled_accept(knSocketChanneled* socket);
///@param dataLength 0-4294967295
///@return Maybe
-Maybe knSocketChanneled_send(knSocketChanneled* socket, uint16 destinationIndex, uint8* data, uint32 dataLength);
+Maybe knSocketChanneled_send(knSocketChanneled* socket, u16 destinationIndex, u8* data, u32 dataLength);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
-///@return Maybe recieved bytes amount
-Maybe knSocketChanneled_recieve(knSocketChanneled* socket, uint16 destinationIndex, uint8* buffer, uint32 bufferLength);
+///@return Maybe recieved bytes amount
+Maybe knSocketChanneled_recieve(knSocketChanneled* socket, u16 destinationIndex, u8* buffer, u32 bufferLength);
#if __cplusplus
}
diff --git a/src/Network/sockets/knSocketTCP.c b/src/Network/sockets/knSocketTCP.c
index 4e55d27..41902e8 100644
--- a/src/Network/sockets/knSocketTCP.c
+++ b/src/Network/sockets/knSocketTCP.c
@@ -65,9 +65,9 @@ Maybe knSocketTCP_accept(knSocketTCP* socket);
///@param dataLength 0-4294967295
///@return Maybe
-Maybe knSocketTCP_send(knSocketTCP* socket, char* data, uint32 dataLength);
+Maybe knSocketTCP_send(knSocketTCP* socket, char* data, u32 dataLength);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
-///@return Maybe recieved bytes amount
-Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, uint32 bufferLength);
+///@return Maybe recieved bytes amount
+Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, u32 bufferLength);
diff --git a/src/Network/sockets/knSocketTCP.h b/src/Network/sockets/knSocketTCP.h
index 8635e9e..d0cef79 100644
--- a/src/Network/sockets/knSocketTCP.h
+++ b/src/Network/sockets/knSocketTCP.h
@@ -8,7 +8,7 @@ extern "C" {
#include "../knAddress.h"
typedef struct knSocketTCP {
- int64 socketfd;
+ i64 socketfd;
knIPV4Endpoint localEndpoint;
knIPV4Endpoint remoteEndpoint;
} knSocketTCP;
@@ -33,12 +33,12 @@ Maybe knSocketTCP_accept(knSocketTCP* socket);
///@param dataLength 0-4294967295
///@return Maybe
-Maybe knSocketTCP_send(knSocketTCP* socket, char* data, uint32 dataLength);
+Maybe knSocketTCP_send(knSocketTCP* socket, char* data, u32 dataLength);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
-///@return Maybe recieved bytes amount
-Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, uint32 bufferLength);
+///@return Maybe recieved bytes amount
+Maybe knSocketTCP_recieve(knSocketTCP* socket, char* buffer, u32 bufferLength);
#if __cplusplus
}
diff --git a/src/Network/sockets/knSocketUDP.c b/src/Network/sockets/knSocketUDP.c
index 93a34d8..03fb1a4 100644
--- a/src/Network/sockets/knSocketUDP.c
+++ b/src/Network/sockets/knSocketUDP.c
@@ -21,9 +21,9 @@ Maybe knSocketUDP_accept(knSocketUDP* socket);
///@param dataLength 0-4294967295
///@return Maybe
-Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, uint32 dataLength, knIPV4Endpoint destination);
+Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, u32 dataLength, knIPV4Endpoint destination);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
-///@return Maybe recieved bytes amount
-Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, uint32 bufferLength);
+///@return Maybe recieved bytes amount
+Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, u32 bufferLength);
diff --git a/src/Network/sockets/knSocketUDP.h b/src/Network/sockets/knSocketUDP.h
index eb3bfe1..1336b1a 100644
--- a/src/Network/sockets/knSocketUDP.h
+++ b/src/Network/sockets/knSocketUDP.h
@@ -7,7 +7,7 @@ extern "C" {
#include "knSocketUDP.h"
typedef struct knSocketUDP {
- int64 socketfd;
+ i64 socketfd;
knIPV4Endpoint localEndpoint;
} knSocketUDP;
ktid_declare(knSocketUDP);
@@ -27,12 +27,12 @@ Maybe knSocketUDP_accept(knSocketUDP* socket);
///@param dataLength 0-4294967295
///@return Maybe
-Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, uint32 dataLength, knIPV4Endpoint destination);
+Maybe knSocketUDP_sendto(knSocketUDP* socket, char* data, u32 dataLength, knIPV4Endpoint destination);
///@param buffer buffer for recieving data
///@param bufferLength 0-4294967295
-///@return Maybe recieved bytes amount
-Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, uint32 bufferLength);
+///@return Maybe recieved bytes amount
+Maybe knSocketUDP_recieve(knSocketUDP* socket, char* buffer, u32 bufferLength);
#if __cplusplus
diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c
index 2a0617f..5433696 100644
--- a/src/SearchTree/SearchTree.c
+++ b/src/SearchTree/SearchTree.c
@@ -1,12 +1,11 @@
#include "SearchTree.h"
-ktid_define(STNode);
+kt_define(STNode, __STNode_free, NULL);
STNode* STNode_create(){
STNode* node=malloc(sizeof(STNode));
node->branches=NULL;
- node->value.typeId=ktid_Null;
- node->value.UInt64=0;
+ node->value=UniNull;
return node;
}
@@ -14,13 +13,13 @@ void __STNode_free(void* _node){
STNode* node=_node;
if (!node) throw(ERR_NULLPTR);
if(node->branches){
- for(uint8 n32 = 0;n32<8;n32++){
+ for(u8 n32 = 0;n32<8;n32++){
STNode*** ptrn32=(STNode***)node->branches[n32];
if(ptrn32){
- for(uint8 n4 = 0;n4<8;n4++){
+ for(u8 n4 = 0;n4<8;n4++){
STNode** ptrn4=ptrn32[n4];
if (ptrn4){
- for(uint8 rem=0;rem<4;rem++){
+ for(u8 rem=0;rem<4;rem++){
STNode* ptrrem=ptrn4[rem];
if(ptrrem)
STNode_free(ptrrem);
@@ -41,9 +40,9 @@ void STNode_free(STNode* node){
free(node);
}
-typedef struct {uint8 n32, n4, rem;} indexes3;
+typedef struct {u8 n32, n4, rem;} indexes3;
-indexes3 splitindex(uint8 i){
+indexes3 splitindex(u8 i){
return (indexes3){
.n32=i/32,
.n4=i%32/4,
@@ -60,20 +59,20 @@ void ST_pushString(STNode* node_first, string key, Unitype value){
if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first;
while(key.length--){
- indexes3 i3=splitindex((uint8)*key.ptr);
+ indexes3 i3=splitindex((u8)*key.ptr);
if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode***));
- for(uint8 i=0;i<8;i++)
+ for(u8 i=0;i<8;i++)
node_last->branches[i]=(STNode***)NULL;
}
if(!node_last->branches[i3.n32]){
node_last->branches[i3.n32]=(STNode***)malloc(8*sizeof(STNode**));
- for(uint8 i=0;i<8;i++)
+ for(u8 i=0;i<8;i++)
node_last->branches[i3.n32][i]=(STNode**)NULL;
}
if(!node_last->branches[i3.n32][i3.n4]){
node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*));
- for(uint8 i=0;i<4;i++)
+ for(u8 i=0;i<4;i++)
node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL;
}
if(!node_last->branches[i3.n32][i3.n4][i3.rem])
@@ -93,7 +92,7 @@ Unitype ST_pullString(STNode* node_first, string key){
if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first;
while (key.length--){
- indexes3 i3=splitindex((uint8)*key.ptr);
+ indexes3 i3=splitindex((u8)*key.ptr);
if(!node_last->branches) return UniNull;
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32) return UniNull;
diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h
index bc8785f..ba72486 100644
--- a/src/SearchTree/SearchTree.h
+++ b/src/SearchTree/SearchTree.h
@@ -7,11 +7,10 @@ extern "C" {
#include "../base/base.h"
#include "../String/string.h"
-typedef struct SearchTreeNode{
- struct SearchTreeNode**** branches; // *STNode[8][8][4]
+STRUCT(STNode,
+ struct STNode**** branches; // *STNode[8][8][4]
Unitype value;
-} STNode;
-ktid_declare(STNode);
+)
STNode* STNode_create();
void STNode_free(STNode* node);
diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c
index e3c3244..91139e8 100644
--- a/src/String/StringBuilder.c
+++ b/src/String/StringBuilder.c
@@ -1,8 +1,6 @@
#include "StringBuilder.h"
-Autoarr_define(string)
-
-ktid_define(StringBuilder);
+kt_define(StringBuilder, __StringBuilder_free, NULL);
#define BL_C 32
#define BL_L 1024
@@ -11,16 +9,16 @@ ktid_define(StringBuilder);
void complete_buf(StringBuilder* b){
if(!b->compl_bufs)
b->compl_bufs=Autoarr_create(string,BL_C,BL_L);
- uint32 len=Autoarr_length(b->curr_buf);
+ u32 len=Autoarr_length(b->curr_buf);
if(!len) return;
string str={.length=len, .ptr=malloc(len)};
- uint32 i=0;
- Autoarr_foreach(b->curr_buf, c, ({
+ u32 i=0;
+ Autoarr_foreach(b->curr_buf, c,
str.ptr[i++]=c;
- }));
+ );
Autoarr_add(b->compl_bufs,str);
Autoarr_free(b->curr_buf, true);
- b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
+ b->curr_buf=Autoarr_create(i8,BL_C,BL_L);
}
void try_complete_buf(StringBuilder* b){
@@ -32,7 +30,7 @@ void try_complete_buf(StringBuilder* b){
StringBuilder* StringBuilder_create(){
StringBuilder* b=malloc(sizeof(StringBuilder));
b->compl_bufs=NULL;
- b->curr_buf=Autoarr_create(int8,BL_C,BL_L);
+ b->curr_buf=Autoarr_create(i8,BL_C,BL_L);
return b;
}
@@ -48,18 +46,18 @@ void StringBuilder_free(StringBuilder* b){
string StringBuilder_build(StringBuilder* b){
complete_buf(b);
- uint32 len=0;
- Autoarr_foreach(b->compl_bufs, cs, ({
+ u32 len=0;
+ Autoarr_foreach(b->compl_bufs, cs,
len+=cs.length;
- }));
+ );
string str= { .length=len, .ptr=malloc(len+1) };
str.ptr[len]='\0';
- uint32 i=0;
- Autoarr_foreach(b->compl_bufs, cs, ({
- for(uint32 n=0;ncompl_bufs, cs,
+ for(u32 n=0;ncurr_buf)
else {
if(!b->compl_bufs) throw(ERR_NULLPTR);
- string* lastcb=Autoarr_getptr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1));
+ string* lastcb=Autoarr_getPtr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1));
lastcb->length--;
}
}
@@ -96,13 +94,13 @@ void StringBuilder_append_cptr(StringBuilder* b, char* s){
}
void curr_buf_add_string(StringBuilder* b, string s){
- for(uint32 i=0; icurr_buf,s.ptr[i]);
}
-void StringBuilder_append_int64(StringBuilder* b, int64 a){
+void StringBuilder_append_i64(StringBuilder* b, i64 a){
try_complete_buf(b);
- uint8 i=0;
+ u8 i=0;
if(a==0){
Autoarr_add(b->curr_buf,'0');
return;
@@ -121,9 +119,9 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){
free(rev.ptr);
}
-void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
+void StringBuilder_append_u64(StringBuilder* b, u64 a){
try_complete_buf(b);
- uint8 i=0;
+ u8 i=0;
if(a==0){
Autoarr_add(b->curr_buf,'0');
return;
@@ -138,7 +136,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
free(rev.ptr);
}
-void StringBuilder_append_float64(StringBuilder* b, double a){
+void StringBuilder_append_f64(StringBuilder* b, f64 a){
try_complete_buf(b);
char buf[32];
IFMSC(
diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h
index 8072cab..e860f8e 100644
--- a/src/String/StringBuilder.h
+++ b/src/String/StringBuilder.h
@@ -7,13 +7,10 @@ extern "C" {
#include "../Autoarr/Autoarr.h"
#include "string.h"
-Autoarr_declare(string)
-
-typedef struct StringBuilder{
+STRUCT(StringBuilder,
Autoarr(string)* compl_bufs;
- Autoarr(int8)* curr_buf;
-} StringBuilder;
-ktid_declare(StringBuilder);
+ Autoarr(i8)* curr_buf;
+)
StringBuilder* StringBuilder_create(void);
void StringBuilder_free(StringBuilder* b);
@@ -28,9 +25,9 @@ void StringBuilder_rmchar(StringBuilder* b);
void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cptr(StringBuilder* b, char* s);
void StringBuilder_append_string(StringBuilder* b, string s);
-void StringBuilder_append_int64(StringBuilder* b, int64 a);
-void StringBuilder_append_uint64(StringBuilder* b, uint64 a);
-void StringBuilder_append_float64(StringBuilder* b, double a);
+void StringBuilder_append_i64(StringBuilder* b, i64 a);
+void StringBuilder_append_u64(StringBuilder* b, u64 a);
+void StringBuilder_append_f64(StringBuilder* b, f64 a);
#if __cplusplus
}
diff --git a/src/String/string.c b/src/String/string.c
index 81d61de..2099468 100644
--- a/src/String/string.c
+++ b/src/String/string.c
@@ -1,6 +1,8 @@
#include "string.h"
-ktid_define(string);
+kt_define(string, NULL, NULL);
+Array_define(string)
+Autoarr_define(string, false)
// copies str content to new char pointer value (adding '\0' at the end)
char* string_extract(string str){
@@ -12,14 +14,16 @@ char* string_extract(string str){
return cptr;
}
-// copies src.ptr content to new string
+// copies src.ptr content to new string and adds \0 at the end
string string_copy(string src){
- if(!src.ptr) return src;
+ if(!src.ptr)
+ return src;
string nstr;
nstr.length=src.length;
- nstr.ptr=malloc(nstr.length);
- for(uint32 i=0;i
// returns length of char buffer (without \0)
-uint32 cptr_length(char* str){
- uint32 len=0;
- while(*(str++)) len++;
- return len;
+u32 cptr_length(const char* str){
+ const char *const str_first=str;
+ while(*str)
+ str++;
+ return str-str_first;
}
// allocates new char[] and copies src there
-char* cptr_copy(char* src){
- uint32 len=cptr_length(src)+1;
+char* cptr_copy(const char* src){
+ u32 len=cptr_length(src)+1;
char* dst=malloc(len);
while(len--!=0)
dst[len]=src[len];
return dst;
}
-// compares two char buffers, NullPtr-friendly
-bool cptr_compare(char* key0, char* key1){
- if(!key0) return key1 ? false : true;
- if(!key1) return false;
- while(*key0&&*key1)
- if(*key0++ != *key1++)
- return false;
- return true;
-}
-
// multiplies char n times
-char* char_multiply(char c, uint32 n){
+char* char_multiply(char c, u32 n){
char* rez=malloc(n+1);
rez[n]=0;
while(n--!=0)
@@ -35,50 +28,127 @@ char* char_multiply(char c, uint32 n){
return rez;
}
-bool cptr_startsWith(char* ptr, char* fragment){
- for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment)
- if(cs!=cf) return false;
- return true;
+bool cptr_equals(const char* key0, const char* key1){
+ char c0=*key0;
+ char c1=*key1;
+ bool eq=c0==c1;
+ while(c0 && c1 && eq) {
+ c0=*++key0;
+ c1=*++key1;
+ eq=c0==c1;
+ }
+ return eq;
}
-bool cptr_endsWith(char* ptr, char* fragment){
- ptr+=cptr_length(ptr)-cptr_length(fragment);
- for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment)
- if(cs!=cf) return false;
- return true;
+bool cptr_startsWith(const char* src, const char* fragment){
+ char c0=*src;
+ char c1=*fragment;
+ bool eq=c0==c1 && c0 !=0 && c1!=0;
+ while(c0 && c1 && eq) {
+ c0=*++src;
+ c1=*++fragment;
+ eq=c0==c1;
+ if(c1==0)
+ return true;
+ }
+ return eq;
}
-uint32 cptr_indexOf(char* ptr, char* fragment){
- char sc=*ptr;
- for(int si=0, fi=0; sc!=0; si++){
- sc=ptr[si];
- if(sc==fragment[fi]){
- fi++;
- if(fragment[fi]==0)
- return si-fi+1;
+bool cptr_endsWith(const char* src, const char* fragment){
+ u32 src_len=cptr_length(src);
+ u32 fr_len=cptr_length(fragment);
+ if(src_len0)
+ StringBuilder_append_string(sb, (string){.ptr=(char*)src, .length=src_remains_len});
+ string rezult=StringBuilder_build(sb);
+ return rezult.ptr;
+}
diff --git a/src/base/cptr.h b/src/base/cptr.h
index 255ee0a..509577c 100644
--- a/src/base/cptr.h
+++ b/src/base/cptr.h
@@ -7,37 +7,88 @@ extern "C" {
#include "std.h"
// returns length of char buffer (without \0)
-uint32 cptr_length(char* str);
+u32 cptr_length(const char* str);
// allocates new char[] and copies src there
-char* cptr_copy(char* src);
+char* cptr_copy(const char* src);
-// compares two char buffers, NullPtr-friendly
-bool cptr_compare(char* key0, char* key1);
+bool cptr_equals(const char* key0, const char* key1);
+
+bool cptr_startsWith(const char* src, const char* fragment);
+
+bool cptr_endsWith(const char* src, const char* fragment);
// multiplies char n times
-char* char_multiply(char c, uint32 n);
+char* char_multiply(char c, u32 n);
-bool cptr_startsWith(char* ptr, char* fragment);
+/// @param startIndex 0 ... src length
+/// @param seekLength 0 ... -1
+/// @return pos of first inclusion in or -1 if not found
+i32 cptr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
-bool cptr_endsWith(char* ptr, char* fragment);
+/// @param startIndex -1 ... src length
+/// @param seekLength 0 ... -1
+/// @return pos of first inclusion in or -1 if not found
+i32 cptr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
+
+/// @param startIndex 0 ... src length
+/// @param seekLength 0 ... -1
+/// @return pos of first inclusion in or -1 if not found
+i32 cptr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength);
+
+/// @param startIndex -1 ... src length
+/// @param seekLength 0 ... -1
+/// @return pos of first inclusion in or -1 if not found
+i32 cptr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength);
/// @brief search for in
/// @return index of first inclusion or -1 if not found
-uint32 cptr_indexOf(char* ptr, char* fragment);
+static inline i32 cptr_indexOf(const char* src, const char* fragment)
+{ return cptr_seek(src, fragment, 0, -1); }
-static inline bool cptr_contains(char* ptr, char* fragment){
- // if(cptr_indexOf(ptr, fragment)==-1)
- // return false;
- // return true;
- return cptr_indexOf(ptr, fragment) +1;
+/// @brief search for in
+/// @return index of first inclusion or -1 if not found
+static inline i32 cptr_indexOfChar(const char* src, char fragment)
+{ return cptr_seekChar(src, fragment, 0, -1); }
+
+/// @brief search for in
+/// @return index of last inclusion or -1 if not found
+static inline i32 cptr_lastIndexOf(const char* src, const char* fragment)
+{ return cptr_seekReverse(src, fragment, -1, -1); }
+
+/// @brief search for in
+/// @return index of last inclusion or -1 if not found
+static inline i32 cptr_lastIndexOfChar(const char* src, char fragment)
+{ return cptr_seekCharReverse(src, fragment, -1, -1); }
+
+
+static inline bool cptr_contains(const char* src, const char* fragment){
+ return cptr_seek(src, fragment, 0, -1) +1;
}
-void memcopy(void* from, void* to, uint32 size);
+void memcopy(void* from, void* to, u32 size);
-char* __cptr_concat(uint16 n, ...);
+char* __cptr_concat(u32 n, ...);
#define cptr_concat(STR...) __cptr_concat(count_args(STR), STR)
+char* cptr_toLower(const char* src);
+char* cptr_toUpper(const char* src);
+
+/// @param startIndex 0 ... src length
+/// @param seekLength 0 ... -1
+/// @return with replaced by or empty cstring if not found
+char* cptr_replaceIn(const char* src, const char* str_old, const char* str_new, u32 startIndex, u32 seekLength);
+/// @param startIndex 0 ... src length
+/// @param seekLength 0 ... -1
+/// @return with replaced by or empty cstring if not found
+char* cptr_replaceCharIn(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength);
+
+static inline char* cptr_replace(const char* src, const char* str_old, const char* str_new)
+{ return cptr_replaceIn(src, str_old, str_new, 0, -1); }
+
+static inline char* cptr_replaceChar(const char* src, char c_old, char c_new)
+{ return cptr_replaceCharIn(src, c_old, c_new, 0, -1); }
+
#if __cplusplus
}
#endif
\ No newline at end of file
diff --git a/src/base/endian.c b/src/base/endian.c
index b6e25f6..282a53b 100644
--- a/src/base/endian.c
+++ b/src/base/endian.c
@@ -2,7 +2,7 @@
static const union
{
- uint16 number;
+ u16 number;
Endian bytes[2];
} _endian_union={ .number=0x0102 };
diff --git a/src/base/endian.h b/src/base/endian.h
index 9d9d1e7..5863309 100644
--- a/src/base/endian.h
+++ b/src/base/endian.h
@@ -1,9 +1,20 @@
-#include "std.h"
+#pragma once
-PACK_ENUM(Endian,
+#if __cplusplus
+extern "C" {
+#endif
+
+#include "std.h"
+#include "type_system/typedef_macros.h"
+
+PACKED_ENUM(Endian,
UnknownEndian=0,
LittleEndian=1,
BigEndian=2
-);
+)
Endian getEndian();
+
+#if __cplusplus
+}
+#endif
diff --git a/src/base/errors.c b/src/base/errors.c
index dacf45b..6f093f1 100644
--- a/src/base/errors.c
+++ b/src/base/errors.c
@@ -5,25 +5,25 @@
char* errname(ErrorId err){
switch(err){
- case SUCCESS: return "SUCCESS";
- case ERR_MAXLENGTH: return "ERR_MAXLENGTH";
- case ERR_WRONGTYPE: return "ERR_WRONGTYPE";
- case ERR_WRONGINDEX: return "ERR_WRONGINDEX";
- case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
- case ERR_NULLPTR: return "ERR_NULLPTR";
- case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
- case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND";
- case ERR_FORMAT: return "ERR_FORMAT";
- case ERR_UNEXPECTEDVAL: return "ERR_UNEXPECTEDVAL";
- case ERR_IO: return "ERR_IO";
- case ERR_IO_EOF: return "ERR_IO_EOF";
+ case SUCCESS: return nameof(SUCCESS);
+ case ERR_MAXLENGTH: return nameof(ERR_MAXLENGTH);
+ case ERR_WRONGTYPE: return nameof(ERR_WRONGTYPE);
+ case ERR_WRONGINDEX: return nameof(ERR_WRONGINDEX);
+ case ERR_NOTIMPLEMENTED: return nameof(ERR_NOTIMPLEMENTED);
+ case ERR_NULLPTR: return nameof(ERR_NULLPTR);
+ case ERR_ENDOFSTR: return nameof(ERR_ENDOFSTR);
+ case ERR_KEYNOTFOUND: return nameof(ERR_KEYNOTFOUND);
+ case ERR_FORMAT: return nameof(ERR_FORMAT);
+ case ERR_UNEXPECTEDVAL: return nameof(ERR_UNEXPECTEDVAL);
+ case ERR_IO: return nameof(ERR_IO);
+ case ERR_IO_EOF: return nameof(ERR_IO_EOF);
default: return "UNKNOWN_ERROR";
}
}
#define ERRMSG_MAXLENGTH 1024
-char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
+char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname){
size_t bufsize=ERRMSG_MAXLENGTH;
char* rezult=malloc(bufsize);
IFMSC(
@@ -33,7 +33,7 @@ char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char*
return rezult;
}
-char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
+char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname){
size_t bufsize=cptr_length(errmsg)+ERRMSG_MAXLENGTH;
char* rezult=malloc(bufsize);
IFMSC(
diff --git a/src/base/errors.h b/src/base/errors.h
index 7c46733..a0e228a 100644
--- a/src/base/errors.h
+++ b/src/base/errors.h
@@ -5,9 +5,9 @@ extern "C" {
#endif
#include "std.h"
-#include "type_system/unitype.h"
+#include "type_system/type_system.h"
-PACK_ENUM(ErrorId,
+PACKED_ENUM(ErrorId,
SUCCESS, // not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX,
ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR,
@@ -17,13 +17,13 @@ PACK_ENUM(ErrorId,
char* errname(ErrorId err);
-char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
-char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
+char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
+char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname);
-typedef struct Maybe{
+STRUCT(Maybe,
Unitype value;
char* errmsg;
-} Maybe;
+)
// return it if func doesn't return anything
// .value .errmsg
@@ -37,38 +37,54 @@ void printMaybe(Maybe e);
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.value=UniNull, .errmsg=ERRMSG}
-#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
+#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
char* __doNothing(char* a);
char* __unknownErr( );
-#define __stringify_err(E) _Generic(\
- (E),\
- char*: __doNothing,\
- int: errname,\
- default: __unknownErr\
+#define __stringify_err(E) _Generic( \
+ (E), \
+ char*: __doNothing, \
+ int: errname, \
+ default: __unknownErr \
)(E)
#if __cplusplus
- #define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)))
- #define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)))
+#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)))
+#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)))
+
+#define safethrow_id(E, FREEMEM) { FREEMEM; \
+ __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))); \
+}
+#define safethrow_msg(E, FREEMEM) { FREEMEM; \
+ __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))); \
+}
+
+#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
+ freeMem; \
+ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
+ return _rezult; \
+}
+
#else
- #define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
- #define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); }
+#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
-#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
- freeMem;\
- _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
- return _rezult;\
- }
+#define safethrow(E, FREEMEM) { FREEMEM; \
+ __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); \
+}
-#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
- _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
- __EXIT(_rezult.errmsg);\
- }
-
+#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
+ freeMem; \
+ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
+ return _rezult; \
+}
#endif
+#define tryLast(_funcCall, _rezult, ON_EXIT) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \
+ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \
+ __EXIT(_rezult.errmsg); \
+}
+
#if __cplusplus
}
#endif
\ No newline at end of file
diff --git a/src/base/optime.h b/src/base/optime.h
index be45fe4..d6a65ae 100644
--- a/src/base/optime.h
+++ b/src/base/optime.h
@@ -2,40 +2,40 @@
#include "std.h"
-#define __optime_print(opname, t)\
- char tnames[3][3]={"s\0","ms","us"};\
- int tni=0;\
- if(t>1000000){\
- t/=1000000;\
- tni=0;\
- } else if(t>1000){\
- t/=1000;\
- tni=1;\
- } else tni=2;\
- kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\
+#define __optime_print(opname, t) \
+ char tnames[3][3]={"s\0","ms","us"}; \
+ i32 tni=0; \
+ if(t>1000000){ \
+ t/=1000000; \
+ tni=0; \
+ } else if(t>1000){ \
+ t/=1000; \
+ tni=1; \
+ } else tni=2; \
+ kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n", \
opname, t, tnames[tni]);
#ifdef CLOCK_REALTIME
/// executes codeblock and prints execution time
-/// uint64 op_i is counter of the internal loop
+/// u64 op_i is counter of the internal loop
/// uses non-standard high-precision clock
-#define optime(opname,repeats,codeblock) ({\
- struct timespec start, stop;\
- clock_gettime(CLOCK_REALTIME, &start);\
- for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\
- (codeblock);\
- clock_gettime(CLOCK_REALTIME, &stop);\
- double t=(double)(stop.tv_sec-start.tv_sec)*1000000+(double)(stop.tv_nsec-start.tv_nsec)/1000;\
- __optime_print(opname,t)\
-})
+#define optime(opname, repeats, codeblock...) { \
+ struct timespec start, stop; \
+ clock_gettime(CLOCK_REALTIME, &start); \
+ for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
+ { codeblock; } \
+ clock_gettime(CLOCK_REALTIME, &stop); \
+ f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000; \
+ __optime_print(opname,t); \
+}
#else
/// uses standard low precision clock
-#define optime(opname,repeats,codeblock) ({\
- clock_t start=clock();\
- for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\
- (codeblock);\
- clock_t stop=clock();\
- double t=(double)(stop-start)/CLOCKS_PER_SEC*1000000;\
- __optime_print(opname,t)\
-})
+#define optime(opname, repeats, codeblock...) { \
+ clock_t start=clock(); \
+ for(u64 op_i=0;op_i<(u64)repeats;op_i++) \
+ { codeblock; } \
+ clock_t stop=clock(); \
+ f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000; \
+ __optime_print(opname,t); \
+}
#endif
diff --git a/src/base/std.h b/src/base/std.h
index bc45b55..816d328 100644
--- a/src/base/std.h
+++ b/src/base/std.h
@@ -8,32 +8,40 @@ extern "C" {
#include
#include
#include
+#include
#include
#include
#include
+#include
+#include
+
+typedef int8_t i8;
+typedef uint8_t u8;
+typedef int16_t i16;
+typedef uint16_t u16;
+typedef int32_t i32;
+typedef uint32_t u32;
+typedef int64_t i64;
+typedef uint64_t u64;
+typedef float f32;
+typedef double f64;
+/// anonymous pointer without specified freeMembers() func
+typedef void* Pointer;
-typedef int8_t int8;
-typedef uint8_t uint8;
-typedef int16_t int16;
-typedef uint16_t uint16;
-typedef int32_t int32;
-typedef uint32_t uint32;
-typedef int64_t int64;
-typedef uint64_t uint64;
-typedef float float32;
-typedef double float64;
// Usually bool from stdbool.h is defined as macro,
// so in other macros like ktid_##TYPE it will be replaced by _Bool.
// ktid__Bool will be created instead of ktid_bool
// In C++ bool is a keyword, so there is no need to redefine it.
#if !__cplusplus
-typedef uint8 bool;
+typedef u8 bool;
#define true 1
#define false 0
#endif
#define dbg(N) kprintf("\e[95m%d\n",N)
+#define nameof(V) #V
+
#ifdef _MSC_VER
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
#define _CRT_SECURE_NO_WARNINGS 1
@@ -71,27 +79,27 @@ typedef uint8 bool;
#endif
-#define __count_args(\
- a0, a1, a2, a3, a4, a5, a6, a7 ,\
- a8, a9, a10,a11,a12,a13,a14,a15,\
- a16,a17,a18,a19,a20,a21,a22,a23,\
- a24,a25,a26,a27,a28,a29,a30,a31,\
- a32,a33,a34,a35,a36,a37,a38,a39,\
- a40,a41,a42,a43,a44,a45,a46,a47,\
- a48,a49,a50,a51,a52,a53,a54,a55,\
- a56,a57,a58,a59,a60,a61,a62,a63,\
+#define __count_args( \
+ a0, a1, a2, a3, a4, a5, a6, a7 , \
+ a8, a9, a10,a11,a12,a13,a14,a15, \
+ a16,a17,a18,a19,a20,a21,a22,a23, \
+ a24,a25,a26,a27,a28,a29,a30,a31, \
+ a32,a33,a34,a35,a36,a37,a38,a39, \
+ a40,a41,a42,a43,a44,a45,a46,a47, \
+ a48,a49,a50,a51,a52,a53,a54,a55, \
+ a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h)
-#define count_args(ARGS...) __count_args(\
- ARGS,\
- 64,63,62,61,60,59,58,57,\
- 56,55,54,53,52,51,50,49,\
- 48,47,46,45,44,43,42,41,\
- 40,39,38,37,36,35,34,33,\
- 32,31,30,29,28,27,26,25,\
- 24,23,22,21,20,19,18,17,\
- 16,15,14,13,12,11,10,9,\
+#define count_args(ARGS...) __count_args( \
+ ARGS, \
+ 64,63,62,61,60,59,58,57, \
+ 56,55,54,53,52,51,50,49, \
+ 48,47,46,45,44,43,42,41, \
+ 40,39,38,37,36,35,34,33, \
+ 32,31,30,29,28,27,26,25, \
+ 24,23,22,21,20,19,18,17, \
+ 16,15,14,13,12,11,10,9, \
8, 7, 6, 5, 4, 3, 2, 1, 0)
/*
@@ -111,16 +119,16 @@ You can even embed it into macro in header (see kprint.h)
#define PRAGMA_WARNING_DISABLE(wName) _PRAGMA(GCC diagnostic ignored wName)
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
#define W_INT_CONVERSION "-Wint-conversion"
+ #define W_IMPLICIT_FALLTHROUGH "-Wimplicit-fallthrough"
#endif
-#define WARNING_DISABLE(WARNING, CODE)\
- PRAGMA_WARNING_PUSH\
- PRAGMA_WARNING_DISABLE(WARNING)\
- CODE;\
+#define WARNING_DISABLE(WARNING, CODE...) \
+ PRAGMA_WARNING_PUSH \
+ PRAGMA_WARNING_DISABLE(WARNING) \
+ CODE; \
PRAGMA_WARNING_POP
-#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\
- ENUM_MEMBERS\
-} __attribute__((__packed__)) ENUM_NAME;
+/// gcc throws warning on unused function return value
+#define WARN_UNUSED_REZULT __attribute__((warn_unused_result))
#if __cplusplus
}
diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md
index 2606a85..c0066e9 100644
--- a/src/base/type_system/README.md
+++ b/src/base/type_system/README.md
@@ -4,17 +4,21 @@ For using some kerep capabilities, such as generic structs, unitype, and kprint,
## type id
-Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file.
-Example:
+Every registered type has its own `ktDescriptor` and `ktid` is an index of the descriptor in descriptors array.
+Descriptor should be declared in header file.
+Following macro declares `typedef struct` and `ktDescriptor`
```c
//someStruct.h
-typedef struct { } someStruct;
-ktid_declare(someStruct);
+STRUCT(someStruct,
+ i32 i; i32 j; i32 k;
+);
```
+then you need to define descriptor in a source file
```c
//someStruct.c
-ktid_define(someStruct);
+kt_define(someStruct);
```
+and register it.
## type descriptors
@@ -22,9 +26,9 @@ Every registered type should have it's own descriptor (`ktDescriptor`). It's a s
## type registration
-To finally register a type, you should call macro `kt_register()` between `ktDescriptors_beginInit()` and `ktDescriptors_endInit()`. Better do it at the start of your program. To register all types from kerep, call `ktDescriptors_initKerepTypes()`.
+To finally register a type, you should call macro `kt_register()` between `kt_beginInit()` and `kt_endInit()`. Better do it at the start of your program. To register all types from kerep, call `kt_initKerepTypes()`.
-You can free internal ktDescriptors storage by calling `ktDescriptors_free()` at exit, if your debugger (valgrind in my case) sees a memory leak.
+You can free internal ktDescriptors storage by calling `kt_free()` at exit, if your debugger (valgrind in my case) sees a memory leak.
Examples:
-+ [ktDescriptors_initKerepTypes()](src/base/type_system/init.c)
-+ [kerep types registration](tests/main.cpp)
++ [kerep types registration](src/base/type_system/init.c)
++ [kt_initKerepTypes()](tests/main.cpp)
diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c
index 0e33986..b63d82d 100644
--- a/src/base/type_system/base_toString.c
+++ b/src/base/type_system/base_toString.c
@@ -2,24 +2,27 @@
#include "../base.h"
#include "../../kprint/kprint_format.h"
-char* __toString_char(void* c, uint32 fmt) {
- //*c=char
+
+// accepts char* (ptr to char) and char* (ptr to string)
+// uses format kp_s and kp_c to determine what type is argument
+char* __toString_char(void* c, u32 fmt) {
+ // *c=char*
+ if(kp_fmt_dataFormat(fmt)==kp_s){
+ return cptr_copy((char*)c); // to avoid segmentation fault on free() when *c allocalet on stack
+ }
+ // *c=char
if(kp_fmt_dataFormat(fmt)==kp_c){
char* cc=malloc(2);
cc[0]=*(char*)c;
cc[1]=0;
return cc;
}
- // *c=cstring
- else if(kp_fmt_dataFormat(fmt)==kp_s){
- return cptr_copy(*(char**)c);
- }
else throw(ERR_FORMAT);
}
-char* __toString_bool(void* c, uint32 fmt) {
+char* __toString_bool(void* c, u32 fmt) {
static const char _strbool[4][6]={ "false", "true\0", "False", "True\0" };
- uint8 strind=*(bool*)c==1 + kp_fmt_isUpper(fmt)*2;
+ u8 strind=*(bool*)c==1 + kp_fmt_isUpper(fmt)*2;
char* rez=malloc(6);
rez[0]=_strbool[strind][0];
rez[1]=_strbool[strind][1];
@@ -30,10 +33,10 @@ char* __toString_bool(void* c, uint32 fmt) {
return rez;
}
-char* toString_int(int64 n){
- int64 d=n<0 ? -1*n : n;
+char* toString_i64(i64 n){
+ i64 d=n<0 ? -1*n : n;
char str[32];
- uint8 i=sizeof(str);
+ u8 i=sizeof(str);
str[--i]=0;
if(d==0)
str[--i]='0';
@@ -46,9 +49,9 @@ char* toString_int(int64 n){
return cptr_copy((char*)str+i);
}
-char* toString_uint(uint64 n, bool withPostfix, bool uppercase){
+char* toString_u64(u64 n, bool withPostfix, bool uppercase){
char str[32];
- uint8 i=sizeof(str);
+ u8 i=sizeof(str);
str[--i]=0;
if(withPostfix)
str[--i]= uppercase ? 'U' : 'u';
@@ -61,53 +64,56 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){
return cptr_copy((char*)str+i);
}
-#define _toString_float_impl(bufsize, maxPrecision) {\
- char str[bufsize];\
- if(precision>maxPrecision)\
- throw("too big precision");\
- if(precision==0)\
- precision=toString_float_default_precision;\
- int cn=sprintf(str, "%.*f", precision, n);\
- /* remove trailing zeroes except .0*/\
- while(str[cn-1]=='0' && str[cn-2]!='.')\
- cn--;\
- if(withPostfix)\
- str[cn++]= uppercase ? 'F' : 'f';\
- str[cn]='\0';\
- return cptr_copy(str);\
+#define _toString_float_impl(bufsize, maxPrecision) { \
+ char str[bufsize]; \
+ if(precision>maxPrecision) \
+ throw("too big precision"); \
+ if(precision==0) \
+ precision=toString_float_default_precision; \
+ i32 cn=IFMSC( \
+ sprintf_s(str, bufsize, "%.*f", precision, n), \
+ sprintf(str, "%.*f", precision, n) \
+ ); \
+ /* remove trailing zeroes except .0*/ \
+ while(str[cn-1]=='0' && str[cn-2]!='.') \
+ cn--; \
+ if(withPostfix) \
+ str[cn++]= uppercase ? 'F' : 'f'; \
+ str[cn]='\0'; \
+ return cptr_copy(str); \
}
-char* toString_float32(float32 n, uint8 precision, bool withPostfix, bool uppercase)
- _toString_float_impl(48, toString_float32_max_precision)
+char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase)
+ _toString_float_impl(48, toString_f32_max_precision)
-char* toString_float64(float64 n, uint8 precision, bool withPostfix, bool uppercase)
- _toString_float_impl(512, toString_float64_max_precision)
+char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase)
+ _toString_float_impl(512, toString_f64_max_precision)
-#define byte_to_bits(byte) {\
- str[cn++]='0' + (uint8)((byte>>7)&1); /* 8th bit */\
- str[cn++]='0' + (uint8)((byte>>6)&1); /* 7th bit */\
- str[cn++]='0' + (uint8)((byte>>5)&1); /* 6th bit */\
- str[cn++]='0' + (uint8)((byte>>4)&1); /* 5th bit */\
- str[cn++]='0' + (uint8)((byte>>3)&1); /* 4th bit */\
- str[cn++]='0' + (uint8)((byte>>2)&1); /* 3th bit */\
- str[cn++]='0' + (uint8)((byte>>1)&1); /* 2th bit */\
- str[cn++]='0' + (uint8)((byte>>0)&1); /* 1th bit */\
+#define byte_to_bits(byte) { \
+ str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */ \
+ str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */ \
+ str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */ \
+ str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */ \
+ str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */ \
+ str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */ \
+ str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */ \
+ str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */ \
}
-char* toString_bin(void* _bytes, uint32 size, bool inverse, bool withPrefix){
+char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){
char* bytes=_bytes;
char* str=malloc(size*8 + (withPrefix?2:0) +1);
- uint32 cn=0; // char number
+ u32 cn=0; // char number
if(withPrefix){
str[cn++]='0';
str[cn++]='b';
}
if(inverse){
// byte number
- for(int32 bn=size-1; bn>=0; bn--)
+ for(i32 bn=size-1; bn>=0; bn--)
byte_to_bits(bytes[bn])
} else {
- for(int32 bn=0; bn=0; bn--){
+ for(i32 bn=size-1; bn>=0; bn--){
unsigned char byte=bytes[bn];
str[cn++]=_4bitsHex(byte/16, uppercase);
str[cn++]=_4bitsHex(byte%16, uppercase);
@@ -149,7 +155,7 @@ char* toString_hex(void* _bytes, uint32 size, bool inverse, bool withPrefix, boo
}
// right to left
else {
- for(int32 bn=0; bn argument
+char* __toString_char(void* c, u32 fmt);
// bool
-char* __toString_bool(void* c, uint32 fmt);
+char* __toString_bool(void* c, u32 fmt);
// signed int
-char* toString_int(int64 n);
-char* __toString_int8(void* n, uint32 fmt);
-char* __toString_int16(void* n, uint32 fmt);
-char* __toString_int32(void* n, uint32 fmt);
-char* __toString_int64(void* n, uint32 fmt);
+char* toString_i64(i64 n);
+char* __toString_i8(void* n, u32 fmt);
+char* __toString_i16(void* n, u32 fmt);
+char* __toString_i32(void* n, u32 fmt);
+char* __toString_i64(void* n, u32 fmt);
// unsigned int
-char* toString_uint(uint64 n, bool withPostfix, bool uppercase);
-char* __toString_uint8(void* n, uint32 fmt);
-char* __toString_uint16(void* n, uint32 fmt);
-char* __toString_uint32(void* n, uint32 fmt);
-char* __toString_uint64(void* n, uint32 fmt);
+char* toString_u64(u64 n, bool withPostfix, bool uppercase);
+char* __toString_u8(void* n, u32 fmt);
+char* __toString_u16(void* n, u32 fmt);
+char* __toString_u32(void* n, u32 fmt);
+char* __toString_u64(void* n, u32 fmt);
// float
-#define toString_float32_max_precision 6
-#define toString_float64_max_precision 15
+#define toString_f32_max_precision 6
+#define toString_f64_max_precision 15
#define toString_float_default_precision 6
-char* toString_float32(float32 n, uint8 precision, bool withPostfix, bool uppercase); // uses sprintf
-char* toString_float64(float64 n, uint8 precision, bool withPostfix, bool uppercase); // uses sprintf
-char* __toString_float32(void* n, uint32 fmt);
-char* __toString_float64(void* n, uint32 fmt);
+char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase); // uses sprintf
+char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase); // uses sprintf
+char* __toString_f32(void* n, u32 fmt);
+char* __toString_f64(void* n, u32 fmt);
///@param inverse set to true for little endian numbers (their bytes are in reverse order)
-char* toString_bin(void* bytes, uint32 size, bool inverse, bool withPrefix);
+char* toString_bin(void* bytes, u32 size, bool inverse, bool withPrefix);
///@param inverse set to true for little endian numbers (their bytes are in reverse order)
-char* toString_hex(void* bytes, uint32 size, bool inverse, bool withPrefix, bool uppercase);
+char* toString_hex(void* bytes, u32 size, bool inverse, bool withPrefix, bool uppercase);
#if __cplusplus
}
diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c
index 4f1a2fd..424b759 100644
--- a/src/base/type_system/init.c
+++ b/src/base/type_system/init.c
@@ -4,84 +4,84 @@
#include "../../SearchTree/SearchTree.h"
#include "../../Hashtable/Hashtable.h"
#include "../../String/StringBuilder.h"
+#include "../../Filesystem/filesystem.h"
#include "base_toString.h"
-void ktDescriptors_initKerepTypes(){
- // null
- __kt_register("Null", sizeof(NULL), NULL, NULL);
- ktid_Null=ktid_last;
+void kt_initKerepTypes(){
// base types
- kt_register(char, NULL, __toString_char);
- kt_register(bool, NULL, __toString_bool);
- kt_register(float32, NULL, __toString_float32);
- kt_register(float64, NULL, __toString_float64);
- kt_register(int8, NULL, __toString_int8);
- kt_register(uint8, NULL, __toString_uint8);
- kt_register(int16, NULL, __toString_int16);
- kt_register(uint16, NULL, __toString_uint16);
- kt_register(int32, NULL, __toString_int32);
- kt_register(uint32, NULL, __toString_uint32);
- kt_register(int64, NULL, __toString_int64);
- kt_register(uint64, NULL, __toString_uint64);
+ kt_register(Pointer);
+ if(ktid_Pointer!=0) // this can break UnitypeNull
+ throw("ktid_Pointer!=0, you must init kerep types before any other types");
+
+ kt_register(char);
+ kt_register(bool);
+ kt_register(f32);
+ kt_register(f64);
+ kt_register(i8);
+ kt_register(u8);
+ kt_register(i16);
+ kt_register(u16);
+ kt_register(i32);
+ kt_register(u32);
+ kt_register(i64);
+ kt_register(u64);
// ktDescriptor
- kt_register(ktDescriptor, NULL, NULL);
-
+ kt_register(ktDescriptor);
// base type arrays
- kt_register(Array_char, (freeMembers_t)Array_char_freeValues, NULL);
- kt_register(Array_bool, (freeMembers_t)Array_bool_freeValues, NULL);
- kt_register(Array_float32, (freeMembers_t)Array_float32_freeValues, NULL);
- kt_register(Array_float64, (freeMembers_t)Array_float64_freeValues, NULL);
- kt_register(Array_int8, (freeMembers_t)Array_int8_freeValues, NULL);
- kt_register(Array_uint8, (freeMembers_t)Array_uint8_freeValues, NULL);
- kt_register(Array_int16, (freeMembers_t)Array_int16_freeValues, NULL);
- kt_register(Array_uint16, (freeMembers_t)Array_uint16_freeValues, NULL);
- kt_register(Array_int32, (freeMembers_t)Array_int32_freeValues, NULL);
- kt_register(Array_uint32, (freeMembers_t)Array_uint32_freeValues, NULL);
- kt_register(Array_int64, (freeMembers_t)Array_int64_freeValues, NULL);
- kt_register(Array_uint64, (freeMembers_t)Array_uint64_freeValues, NULL);
+ kt_register(Array_char);
+ kt_register(Array_bool);
+ kt_register(Array_f32);
+ kt_register(Array_f64);
+ kt_register(Array_i8);
+ kt_register(Array_u8);
+ kt_register(Array_i16);
+ kt_register(Array_u16);
+ kt_register(Array_i32);
+ kt_register(Array_u32);
+ kt_register(Array_i64);
+ kt_register(Array_u64);
+ kt_register(Array_Pointer);
// base type autoarrs
- kt_register(Autoarr_char, ____Autoarr_free_char, NULL);
- kt_register(Autoarr_bool, ____Autoarr_free_bool, NULL);
- kt_register(Autoarr_float32, ____Autoarr_free_float32, NULL);
- kt_register(Autoarr_float64, ____Autoarr_free_float64, NULL);
- kt_register(Autoarr_int8, ____Autoarr_free_int8, NULL);
- kt_register(Autoarr_uint8, ____Autoarr_free_uint8, NULL);
- kt_register(Autoarr_int16, ____Autoarr_free_int16, NULL);
- kt_register(Autoarr_uint16, ____Autoarr_free_uint16, NULL);
- kt_register(Autoarr_int32, ____Autoarr_free_int32, NULL);
- kt_register(Autoarr_uint32, ____Autoarr_free_uint32, NULL);
- kt_register(Autoarr_int64, ____Autoarr_free_int64, NULL);
- kt_register(Autoarr_uint64, ____Autoarr_free_uint64, NULL);
+ kt_register(Autoarr_Pointer);
+ kt_register(Autoarr_char);
+ kt_register(Autoarr_bool);
+ kt_register(Autoarr_f32);
+ kt_register(Autoarr_f64);
+ kt_register(Autoarr_i8);
+ kt_register(Autoarr_u8);
+ kt_register(Autoarr_i16);
+ kt_register(Autoarr_u16);
+ kt_register(Autoarr_i32);
+ kt_register(Autoarr_u32);
+ kt_register(Autoarr_i64);
+ kt_register(Autoarr_u64);
// Unitype
- kt_register(Unitype, __UnitypePtr_free, NULL);
- kt_register(Array_Unitype, (freeMembers_t)Array_Unitype_freeValues, NULL);
- kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL);
- // replacing autogenerated freear() function to custom
- Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1);
- _uar->functions->freear=__Autoarr_free_Unitype_;
- Autoarr_free(_uar, true);
+ kt_register(Unitype);
+ kt_register(Array_Unitype);
+ kt_register(Autoarr_Unitype);
- // SearchTreeNode
- kt_register(STNode, __STNode_free, NULL);
+ // STNode
+ kt_register(STNode);
// KeyValuePair
- kt_register(KVPair, __KVPair_free, NULL);
- kt_register(Autoarr_KVPair, ____Autoarr_free_KVPair_, NULL);
- // replacing autogenerated freear() function to custom
- Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1);
- _kvpar->functions->freear=__Autoarr_free_KVPair_;
- Autoarr_free(_kvpar, true);
+ kt_register(KVPair);
+ kt_register(Autoarr_KVPair);
// Hashtable
- kt_register(Hashtable, __Hashtable_free, NULL);
+ kt_register(Hashtable);
// string
- kt_register(string, NULL, NULL);
- kt_register(Autoarr_string, ____Autoarr_free_string, NULL);
+ kt_register(string);
+ kt_register(Array_string);
+ kt_register(Autoarr_string);
+
// StringBuilder
- kt_register(StringBuilder, __StringBuilder_free, NULL);
+ kt_register(StringBuilder);
+
+ //File
+ kt_register(FileHandle);
}
diff --git a/src/base/type_system/init.h b/src/base/type_system/init.h
index d08ef9d..df787ca 100644
--- a/src/base/type_system/init.h
+++ b/src/base/type_system/init.h
@@ -4,8 +4,8 @@
extern "C" {
#endif
-// call this between ktDescriptors_beginInit() and ktDescriptors_endInit()
-void ktDescriptors_initKerepTypes();
+// call this between kt_beginInit() and kt_endInit()
+void kt_initKerepTypes();
#if __cplusplus
}
diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h
index 682b3ca..189160a 100644
--- a/src/base/type_system/ktDescriptor.h
+++ b/src/base/type_system/ktDescriptor.h
@@ -6,16 +6,45 @@ extern "C" {
#include "../std.h"
#include "ktid.h"
+#include "typedef_macros.h"
+
+#define kt_declare(TYPE)\
+ ktid_declare(TYPE);\
+ extern ktDescriptor ktDescriptor_##TYPE; \
+ extern ktDescriptor ktDescriptor_##TYPE##_Ptr;
+
+#define kt_define(TYPE, FREE_MEMBERS_F, TOSTRING_F)\
+ ktid_define(TYPE); \
+ ktDescriptor ktDescriptor_##TYPE={ \
+ .name=#TYPE, \
+ .id=ktid_undefined, \
+ .size=sizeof(TYPE), \
+ .freeMembers=FREE_MEMBERS_F, \
+ .toString=TOSTRING_F \
+ }; \
+ ktDescriptor ktDescriptor_##TYPE##_Ptr={\
+ .name=#TYPE "_Ptr", \
+ .id=ktid_undefined, \
+ .size=sizeof(TYPE), \
+ .freeMembers=FREE_MEMBERS_F, \
+ .toString=TOSTRING_F \
+ };
typedef void (*freeMembers_t)(void*);
-typedef char* (*toString_t)(void* obj, uint32 fmt);
-typedef struct ktDescriptor{
+typedef char* (*toString_t)(void* obj, u32 fmt);
+
+STRUCT(ktDescriptor,
char* name;
ktid id;
- uint16 size;
+ u16 size;
freeMembers_t freeMembers; // NULL or function which frees all struct members
toString_t toString; // NULL or function which generates string representaion of object
-} ktDescriptor;
+)
+
+/// gets descriptor for TYPE
+#define ktDescriptor_name(TYPE) ktDescriptor_##TYPE
+/// gets descriptor for pointer to TYPE
+#define ktDescriptor_namePtr(TYPE) ktDescriptor_##TYPE##_Ptr
#if __cplusplus
}
diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c
index 6ead198..f066c75 100644
--- a/src/base/type_system/kt_functions.c
+++ b/src/base/type_system/kt_functions.c
@@ -1,68 +1,86 @@
#include "../../Autoarr/Autoarr.h"
+#include "type_system.h"
+#include "base_toString.h"
-Autoarr_declare(ktDescriptor)
-Autoarr_define(ktDescriptor)
+kt_define(Pointer, NULL, __toString_u64);
+kt_define(char,NULL, __toString_char);
+kt_define(bool,NULL, __toString_bool);
+kt_define(f32, NULL, __toString_f32);
+kt_define(f64, NULL, __toString_f64);
+kt_define(i8, NULL, __toString_i8);
+kt_define(u8, NULL, __toString_u8);
+kt_define(i16, NULL, __toString_i16);
+kt_define(u16, NULL, __toString_u16);
+kt_define(i32, NULL, __toString_i32);
+kt_define(u32, NULL, __toString_u32);
+kt_define(i64, NULL, __toString_i64);
+kt_define(u64, NULL, __toString_u64);
-ktid ktid_Null=-1;
-ktid_define(char);
-ktid_define(bool);
-ktid_define(float32);
-ktid_define(float64);
-ktid_define(int8);
-ktid_define(uint8);
-ktid_define(int16);
-ktid_define(uint16);
-ktid_define(int32);
-ktid_define(uint32);
-ktid_define(int64);
-ktid_define(uint64);
-
-ktid_define(ktDescriptor);
-
-// type descriptors are stored here during initialization
-Autoarr(ktDescriptor)* __ktDescriptors=NULL;
-// here type descriptors are stored when initialization is complited
-ktDescriptor* typeDescriptors=NULL;
-ktid ktid_last=-1;
-
-typedef enum{
- NotInitialized, Initializing, Initialized
-} ktDescriptorsState;
-ktDescriptorsState initState=NotInitialized;
-
-void ktDescriptors_beginInit(){
- kprintf("\e[94mtype descriptors initializing...\n");
- __ktDescriptors=Autoarr_create(ktDescriptor, 256, 256);
- if(__ktDescriptors==NULL) throw(ERR_NULLPTR);
+char* ktDescriptor_toString(ktDescriptor* d){
+ const char* n="null";
+ char *s0 = toString_u64(d->id, 0,0);
+ char *s1 = toString_u64(d->size, 0,0);
+ char *s2 = d->toString ? toString_hex(d->toString, sizeof(void*), 0,1,0) : n;
+ char *s3 = d->freeMembers ? toString_hex(d->freeMembers, sizeof(void*), 0,1,0) : n;
+ char *rez=cptr_concat("ktDescriptor {"
+ " name:", d->name,
+ " id:",s0,
+ " size:",s1,
+ " toString:",s2,
+ " freeMembers:",s3,
+ " }");
+ free(s0);
+ free(s1);
+ if(s2!=n) free(s2);
+ if(s3!=n) free(s3);
+ return rez;
}
-void ktDescriptors_endInit(){
- typeDescriptors=Autoarr_toArray(__ktDescriptors);
- Autoarr_free(__ktDescriptors,true);
+char* _ktDescriptor_toString(void* _d, u32 fmt) { return ktDescriptor_toString(_d); }
+
+kt_define(ktDescriptor, NULL, _ktDescriptor_toString);
+
+typedef ktDescriptor* ktDescriptor_Ptr;
+
+// type descriptors are stored here during initialization
+Autoarr(Pointer)* __descriptorPointers=NULL;
+// here type descriptors are stored when initialization is complited
+ktDescriptor** typeDescriptors=NULL;
+ktid ktid_last=-1;
+
+ENUM(ktDescriptorsState,
+ NotInitialized, Initializing, Initialized
+)
+ktDescriptorsState initState=NotInitialized;
+
+void kt_beginInit(){
+ kprintf("\e[94mtype descriptors initializing...\n");
+ __descriptorPointers=Autoarr_create(Pointer, 256, 256);
+}
+
+void kt_endInit(){
+ if(__descriptorPointers==NULL)
+ throw(ERR_NULLPTR);
+ typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers);
+ Autoarr_free(__descriptorPointers,true);
if(typeDescriptors==NULL) throw(ERR_NULLPTR);
kprintf("\e[92minitialized %u type descriptors\n", ktid_last);
}
-void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32)){
- ktDescriptor typeDesc={
- .name=name,
- .size=size,
- .id=++ktid_last,
- .freeMembers=freeMembers,
- .toString=toString
- };
- Autoarr_add(__ktDescriptors, typeDesc);
+void __kt_register(ktDescriptor* descriptor){
+ descriptor->id=++ktid_last;
+ Autoarr_add(__descriptorPointers, descriptor);
}
-ktDescriptor ktDescriptor_get(ktid id){
- if(id>ktid_last) {
+ktDescriptor* ktDescriptor_get(ktid id){
+ if(id>ktid_last || id==ktid_undefined) {
kprintf("\ntype id: %u\n",id);
throw("invalid type id");
}
return typeDescriptors[id];
}
-void ktDescriptors_free(){
+void kt_free(){
free(typeDescriptors);
}
diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h
index 26bfd47..435e28f 100644
--- a/src/base/type_system/kt_functions.h
+++ b/src/base/type_system/kt_functions.h
@@ -9,39 +9,40 @@ extern "C" {
#include "ktDescriptor.h"
extern ktid ktid_last;
-void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32));
+void __kt_register(ktDescriptor* descriptor);
-#define kt_register(TYPE, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\
- __kt_register(#TYPE, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
- ktid_##TYPE=ktid_last;\
- __kt_register(#TYPE "*", sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\
+#define kt_register(TYPE) \
+ __kt_register(&ktDescriptor_##TYPE); \
+ ktid_##TYPE=ktid_last; \
+ __kt_register(&ktDescriptor_##TYPE##_Ptr); \
ktid_##TYPE##_Ptr=ktid_last;
-void ktDescriptors_beginInit();
-void ktDescriptors_endInit();
+void kt_beginInit();
+void kt_endInit();
/// @param id id of registered type
-ktDescriptor ktDescriptor_get(ktid id);
+ktDescriptor* ktDescriptor_get(ktid id);
+
+char* ktDescriptor_toString(ktDescriptor* d);
// call it to free heap-allocated ktDescriptors array
-void ktDescriptors_free();
+void kt_free();
-extern ktid ktid_Null;
+kt_declare(Pointer);
+kt_declare(char);
+kt_declare(bool);
+kt_declare(f32);
+kt_declare(f64);
+kt_declare(i8);
+kt_declare(u8);
+kt_declare(i16);
+kt_declare(u16);
+kt_declare(i32);
+kt_declare(u32);
+kt_declare(i64);
+kt_declare(u64);
-ktid_declare(char);
-ktid_declare(bool);
-ktid_declare(float32);
-ktid_declare(float64);
-ktid_declare(int8);
-ktid_declare(uint8);
-ktid_declare(int16);
-ktid_declare(uint16);
-ktid_declare(int32);
-ktid_declare(uint32);
-ktid_declare(int64);
-ktid_declare(uint64);
-
-ktid_declare(ktDescriptor);
+kt_declare(ktDescriptor);
#if __cplusplus
}
diff --git a/src/base/type_system/ktid.h b/src/base/type_system/ktid.h
index feda10d..be08484 100644
--- a/src/base/type_system/ktid.h
+++ b/src/base/type_system/ktid.h
@@ -5,17 +5,22 @@ extern "C" {
#endif
#include "../std.h"
-typedef uint16 ktid;
+#include "typedef_macros.h"
+typedef u16 ktid;
+static const ktid ktid_undefined=-1;
+
+/// gets descriptor id for TYPE
#define ktid_name(TYPE) ktid_##TYPE
+/// gets descriptor id for pointer to TYPE
#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr
-#define ktid_declare(TYPE)\
- extern ktid ktid_##TYPE;\
+#define ktid_declare(TYPE) \
+ extern ktid ktid_##TYPE; \
extern ktid ktid_##TYPE##_Ptr;
-#define ktid_define(TYPE)\
- ktid ktid_##TYPE=-1;\
+#define ktid_define(TYPE) \
+ ktid ktid_##TYPE=-1; \
ktid ktid_##TYPE##_Ptr=-1;
#if __cplusplus
diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h
index f5e6b1a..28d80e4 100644
--- a/src/base/type_system/type_system.h
+++ b/src/base/type_system/type_system.h
@@ -1,5 +1,8 @@
+#pragma once
+
#include "init.h"
#include "ktid.h"
#include "ktDescriptor.h"
#include "kt_functions.h"
#include "unitype.h"
+#include "typedef_macros.h"
diff --git a/src/base/type_system/typedef_macros.h b/src/base/type_system/typedef_macros.h
new file mode 100644
index 0000000..ccaab8d
--- /dev/null
+++ b/src/base/type_system/typedef_macros.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#define ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
+ ENUM_MEMBERS \
+} ENUM_NAME;
+
+#define PACKED_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \
+ ENUM_MEMBERS \
+} __attribute__((__packed__)) ENUM_NAME;
+
+#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME STRUCT_NAME; \
+typedef struct STRUCT_NAME { \
+ STRUCT_MEMBERS \
+} STRUCT_NAME; \
+kt_declare(STRUCT_NAME);
diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c
index 46fe530..a85c05e 100644
--- a/src/base/type_system/unitype.c
+++ b/src/base/type_system/unitype.c
@@ -1,52 +1,100 @@
+#include "../../kprint/kprint_format.h"
#include "../base.h"
-ktid_define(Unitype);
+char *__Unitype_toString(void *_u, u32 fmt)
+{
+ return Unitype_toString(*(Unitype *)_u, fmt);
+}
-void Unitype_free(Unitype u){
- ktDescriptor type=ktDescriptor_get(u.typeId);
- if(type.freeMembers)
- type.freeMembers(u.VoidPtr);
- if(u.allocatedInHeap)
+kt_define(Unitype, __UnitypePtr_free, __Unitype_toString);
+
+void Unitype_free(Unitype u)
+{
+ if (u.typeId == ktid_undefined)
+ {
+ if (u.VoidPtr != NULL)
+ throw("unitype with undefined typeId has value");
+ return;
+ }
+
+ ktDescriptor *type = ktDescriptor_get(u.typeId);
+ if (type->freeMembers)
+ type->freeMembers(u.VoidPtr);
+ if (u.allocatedInHeap)
free(u.VoidPtr);
}
-void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); }
+void __UnitypePtr_free(void *u)
+{
+ Unitype_free(*(Unitype *)u);
+}
-char* toString_Unitype(void* _u, uint32 fmt){
- Unitype* u=_u;
- ktDescriptor type=ktDescriptor_get(u->typeId);
- char* valuestr=type.toString(_u, fmt);
- char* rezult=cptr_concat("{ type: ", type.name,
- ", allocated on heap: ", (u->allocatedInHeap ? "true" : "false"),
- ", value:", valuestr, " }");
- free(valuestr);
+char *Unitype_toString(Unitype u, u32 fmt)
+{
+ if (u.typeId == ktid_undefined)
+ {
+ if (u.VoidPtr != NULL)
+ throw("unitype with undefined typeId has value");
+ return cptr_copy("{ERROR_TYPE}");
+ }
+
+ if (fmt == 0)
+ {
+ if (u.typeId == ktid_name(bool) || u.typeId == ktid_name(i8) || u.typeId == ktid_name(i16) ||
+ u.typeId == ktid_name(i32) || u.typeId == ktid_name(i64))
+ {
+ // auto format set
+ fmt = kp_i;
+ // replaces value with pointer to value to pass into toString_i64(void*, u32)
+ i64 value = u.Int64;
+ u.VoidPtr = &value;
+ }
+ else if (u.typeId == ktid_name(u8) || u.typeId == ktid_name(u16) || u.typeId == ktid_name(u32) ||
+ u.typeId == ktid_name(u64))
+ {
+ fmt = kp_u;
+ u64 value = u.UInt64;
+ u.VoidPtr = &value;
+ }
+ else if (u.typeId == ktid_name(f32) || u.typeId == ktid_name(f64))
+ {
+ fmt = kp_f;
+ f64 value = u.Float64;
+ u.VoidPtr = &value;
+ }
+ else if (u.typeId == ktid_name(char))
+ {
+ fmt = kp_c;
+ i64 value = u.Int64;
+ u.VoidPtr = &value;
+ }
+ else if (u.typeId == ktid_ptrName(char))
+ {
+ fmt = kp_s;
+ }
+ else if (u.typeId == ktid_name(Pointer))
+ {
+ if (u.VoidPtr == NULL)
+ return cptr_copy("{ UniNull }");
+ fmt = kp_h;
+ }
+ }
+
+ ktDescriptor *type = ktDescriptor_get(u.typeId);
+ char *valuestr;
+ if (type->toString)
+ valuestr = type->toString(u.VoidPtr, fmt);
+ else
+ valuestr = "ERR_NO_TOSTRING_FUNC";
+ char *rezult = cptr_concat("{ type: ", type->name, ", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"),
+ ", value:", valuestr, " }");
+ if (type->toString)
+ free(valuestr);
return rezult;
}
-
-
-#define BUFSIZE 64
-char* sprintuni(Unitype v){
- char* buf=malloc(BUFSIZE);
- ktDescriptor type=ktDescriptor_get(v.typeId);
- if(v.typeId==ktid_Null)
- sprintf_s(buf, BUFSIZE, "{Null}");
- else if(v.typeId==ktid_name(float64))
- sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64);
- else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(uint64))
- sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64);
- else if(v.typeId==ktid_name(int64))
- sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64);
- else if(v.typeId==ktid_ptrName(char)){
- size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
- buf=realloc(buf, newBUFSIZE);
- sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr);
- }
- else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr);
- return buf;
-}
-
-void printuni(Unitype v){
- char* s=sprintuni(v);
+void printuni(Unitype v)
+{
+ char *s = Unitype_toString(v, 0);
fputs(s, stdout);
free(s);
}
\ No newline at end of file
diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h
index e86eedc..1fef31f 100644
--- a/src/base/type_system/unitype.h
+++ b/src/base/type_system/unitype.h
@@ -5,44 +5,50 @@ extern "C" {
#endif
#include "ktid.h"
+#include "typedef_macros.h"
-typedef struct Unitype{
+STRUCT(Unitype,
union {
- int64 Int64;
- uint64 UInt64;
- double Float64;
+ i64 Int64;
+ u64 UInt64;
+ f64 Float64;
bool Bool;
void* VoidPtr;
char Bytes[8];
};
ktid typeId;
bool allocatedInHeap; // should Unitype_free call free() to VoidPtr*
-} Unitype;
-ktid_declare(Unitype);
+)
-#define __UniDef(FIELD, TYPE, VAL) (Unitype){\
- .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false}
+#define __UniDef(FIELD, TYPE, VAL) ((Unitype){ \
+ .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false})
-#define UniInt64(VAL) __UniDef(Int64, int64, VAL)
-#define UniUInt64(VAL) __UniDef(UInt64, uint64, VAL)
-#define UniFloat64(VAL) __UniDef(Float64, float64, VAL)
+#define UniInt64(VAL) __UniDef(Int64, i64, VAL)
+#define UniUInt64(VAL) __UniDef(UInt64, u64, VAL)
+#define UniFloat64(VAL) __UniDef(Float64, f64, VAL)
#define UniBool(VAL) __UniDef(Bool, bool, VAL)
-#define UniStackPtr(TYPE, VAL) (Unitype){\
- .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false}
-#define UniHeapPtr(TYPE, VAL) (Unitype){\
- .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true}
-
-#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false}
+#define UniPtr(TYPE_ID, VAL, ALLOCATED_ON_HEAP)((Unitype){ \
+ .VoidPtr=VAL, .typeId=TYPE_ID, .allocatedInHeap=ALLOCATED_ON_HEAP })
+#define UniStackPtr(TYPE, VAL) UniPtr(ktid_ptrName(TYPE), VAL, false)
+#define UniHeapPtr(TYPE, VAL) UniPtr(ktid_ptrName(TYPE), VAL, true)
+ // 0==ktid_Pointer
+#define UniNull ((Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false})
#define UniTrue UniBool(true)
#define UniFalse UniBool(false)
+#define Unitype_isUniNull(UNI) (UNI.typeId==0 && UNI.Int64==0)
+
+#define UniCheckTypeId(UNI, TYPE_ID) (UNI.typeId==TYPE_ID)
+#define UniCheckType(UNI, TYPE) UniCheckTypeId(UNI, ktid_name(TYPE))
+#define UniCheckTypePtr(UNI, TYPE) UniCheckTypeId(UNI, ktid_ptrName(TYPE))
+
// frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void __UnitypePtr_free(void* u);
+char* Unitype_toString(Unitype v, u32 fmt);
void printuni(Unitype v);
-char* sprintuni(Unitype v);
#if __cplusplus
}
diff --git a/src/kprint/README.md b/src/kprint/README.md
index cb78373..0cba7db 100644
--- a/src/kprint/README.md
+++ b/src/kprint/README.md
@@ -2,16 +2,18 @@
It is just my cross-plaform variant of printf.
Unlike in standard printf, `%l...` and `%ll...` placeholders dont depend on size of `long int` and `long long int`. And you can change terminal colors by unix codes (`\e[92m`) even on Windows.
-| type | placeholder |
-|-------------------------|-------------------------|
-| int8 / int16 / int32 | %i / %d |
-| int64 | %li / %ld / %lld / %lli |
-| uint8 / uint16 / uint32 | %u |
-| uint64 | %lu / %llu |
-| float32 / float64 | %f |
-| char | %c |
-| char[] | %s |
-| void\* | %p / %x |
+| type | placeholder |
+|----------------|-------------------------|
+| i8 / i16 / i32 | %i / %d |
+| i64 | %li / %ld / %lld / %lli |
+| u8 / u16 / u32 | %u |
+| u64 | %lu / %llu |
+| f32 / f64 | %f |
+| char | %c |
+| char[] | %s |
+| void\* | %p |
+| 32bit or less | %x |
+| 64bit | %lx |
@@ -27,7 +29,7 @@ I don't really like printf function (and its variants), so i made safer and more
## how to use it:
+ **format construction:**
```
- kp_fmt fmt= kp_fgColor | kp_bgColor | kprint_fdataFmt | flags | ktid;
+ kp_fmt fmt= kp_fgColor | kp_bgColor | kp_dataFmt | flags | ktid;
```
[more about `kp_fmt`](kp_fmt.md)
+ fgColor and bgColor can be set to change console output color
@@ -47,4 +49,4 @@ I don't really like printf function (and its variants), so i made safer and more
Maybe m=MaybeNull;
kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m);
```
- output: Maybe: {value={0, ktid_Null}}
+ output: Maybe: {value: { Pointer, 0x0 }}
diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c
index 2fda00f..046cf29 100644
--- a/src/kprint/kprint.c
+++ b/src/kprint/kprint.c
@@ -9,42 +9,46 @@ ktid __typeFromFormat(kp_fmt f){
case kp_i:
case kp_h:
case kp_b:
- return ktid_name(int64);
+ return ktid_name(i64);
case kp_u:
- return ktid_name(uint64);
+ return ktid_name(u64);
case kp_f:
- return ktid_name(float64);
+ return ktid_name(f64);
case kp_c:
- return ktid_char;
+ return ktid_name(char);
case kp_s:
return ktid_ptrName(char);
default:
- return -1;
+ return ktid_undefined;
}
}
-Maybe __next_toString(kp_fmt f, __kp_value_union* object){
+Maybe __next_toString(kp_fmt f, void* object){
// detecting type
ktid typeId=__typeFromFormat(f);
- if(typeId==-1)
- safethrow("typeId is not set, can't autodetect type",;);
- ktDescriptor typeDesc=ktDescriptor_get(typeId);
- if(!typeDesc.toString)
+ if(typeId==ktid_undefined)
+ safethrow("typeId is undefined, can't autodetect type",;);
+
+ if(typeId==ktid_ptrName(char))
+ object=*(char**)object; // dereferencing char** to char*
+
+ ktDescriptor* type=ktDescriptor_get(typeId);
+ if(!type->toString)
safethrow("type descriptor doesnt have toString() func",;);
- return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f)));
+ return SUCCESS(UniHeapPtr(char, type->toString(object, f)));
}
-Maybe check_argsN(uint8 n){
+Maybe check_argsN(u8 n){
if(n%2 != 0) safethrow("kprint recieved non-even number of arguments",;);
if(n > 32) safethrow("kprint recieved >32 number of arguments",;);
return MaybeNull;
}
-Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){
+Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects){
try(check_argsN(n), _,;);
n/=2;
StringBuilder* strb=StringBuilder_create();
- for(uint8 i=0; i>24;
+ u8 fg=(f&0x0f000000)>>24;
if(fg<8) fg+=30;
else fg+=90-8;
printf("\e[%um", fg);
}
if(kp_fmt_bgColorSet(f)){
- uint8 bg=(f&0x00f00000)>>20;
+ u8 bg=(f&0x00f00000)>>20;
if(bg<8) bg+=40;
else bg+=100-8;
printf("\e[%um", bg);
@@ -159,15 +163,15 @@ void kprint_setColor(kp_fmt f){
}
#endif
-/* Maybe ksprint_ar(uint32 count, kp_fmt format, ktid typeId, void* array){
- ktDescriptor typeDesc=ktDescriptor_get(format.typeId);
- if(!typeDesc.toString)
+/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){
+ ktDescriptor* type=ktDescriptor_get(format.typeId);
+ if(!type->toString)
safethrow("type descriptor doesnt have toString() func",;);
StringBuilder* strb=StringBuilder_create();
StringBuilder_append_char(strb, '[');
- for (uint16 e=1; etoString(array+type->size*e, &format);
StringBuilder_append_cptr(strb, elStr);
StringBuilder_append_char(strb, ',');
}
@@ -175,3 +179,33 @@ void kprint_setColor(kp_fmt f){
StringBuilder_append_char(strb, ' ');
StringBuilder_append_char(strb, ']');
} */
+
+static const char* _kp_colorNames[16]={
+ "black",
+ "dark_red",
+ "dark_green",
+ "dark_yellow",
+ "dark_blue",
+ "dark_magenta",
+ "dark_cyan",
+ "gray",
+ "dark_gray",
+ "red",
+ "green",
+ "yellow",
+ "blue",
+ "magenta",
+ "cyan",
+ "white"
+};
+
+char* kp_bgColor_toString(kp_bgColor c){
+ u32 color_index=(c&0x00f00000)>>20;
+ if(color_index>15) throw(ERR_WRONGINDEX);
+ return _kp_colorNames[color_index];
+}
+char* kp_fgColor_toString(kp_fgColor c){
+ u32 color_index=(c&0x00f00000)>>24;
+ if(color_index>15) throw(ERR_WRONGINDEX);
+ return _kp_colorNames[color_index];
+}
diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h
index 60a7ab0..69cc63c 100644
--- a/src/kprint/kprint.h
+++ b/src/kprint/kprint.h
@@ -5,7 +5,6 @@ extern "C" {
#endif
#include "../base/errors.h"
-#include "kprint_colors.h"
#include "kprint_format.h"
/*
@@ -15,91 +14,88 @@ This file looks like a mess, but all cotnent here just solves the problem of put
*/
typedef union {
- int64 i64;
- uint64 u64;
- float64 f64;
+ i64 i64;
+ u64 u64;
+ f64 f64;
void* ptr;
} __kp_value_union;
-static inline __kp_value_union __kpVU_f(float64 f) { return (__kp_value_union){ .f64=f }; }
-inline __kp_value_union __kpVU_i(int64 f) { return (__kp_value_union){ .i64=f }; }
+static inline __kp_value_union __kpVU_f(f64 f) { return (__kp_value_union){ .f64=f }; }
+static inline __kp_value_union __kpVU_i(i64 f) { return (__kp_value_union){ .i64=f }; }
-#define __kpVU_selectType(V) _Generic(V, float: __kpVU_f, double: __kpVU_f, default: __kpVU_i)(V)
+#define __kpVU_selectType(V) _Generic(V, float: __kpVU_f, f64: __kpVU_f, default: __kpVU_i)(V)
#define __kpVU(V) __kpVU_selectType(V)
-#define __kp_argsToFmts8(\
- a0, a1, a2, a3, a4, a5, a6, a7,...)\
- ((int32[]){ a0,a2,a4,a6 })
-#define __kp_argsToObjs8(\
- a0, a1, a2, a3, a4, a5, a6, a7,...)\
+#define __kp_argsToFmts8( \
+ a0, a1, a2, a3, a4, a5, a6, a7,...) \
+ ((i32[]){ a0,a2,a4,a6 })
+#define __kp_argsToObjs8( \
+ a0, a1, a2, a3, a4, a5, a6, a7,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) })
-#define __kp_argsToFmts16(\
- a0, a1, a2, a3, a4, a5, a6, a7,\
- a8, a9, a10,a11,a12,a13,a14,a15,...)\
- ((int32[]){ a0,a2,a4,a6,a8,a10,a12,a14 })
-#define __kp_argsToObjs16(\
- a0, a1, a2, a3, a4, a5, a6, a7,\
- a8, a9, a10,a11,a12,a13,a14,a15,...)\
+#define __kp_argsToFmts16( \
+ a0, a1, a2, a3, a4, a5, a6, a7, \
+ a8, a9, a10,a11,a12,a13,a14,a15,...) \
+ ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 })
+#define __kp_argsToObjs16( \
+ a0, a1, a2, a3, a4, a5, a6, a7, \
+ a8, a9, a10,a11,a12,a13,a14,a15,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) })
-#define __kp_argsToFmts32(\
- a0, a1, a2, a3, a4, a5, a6, a7,\
- a8, a9, a10,a11,a12,a13,a14,a15,\
- a16,a17,a18,a19,a20,a21,a22,a23,\
- a24,a25,a26,a27,a28,a29,a30,a31,...)\
- ((int32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 })
-#define __kp_argsToObjs32(\
- a0, a1, a2, a3, a4, a5, a6, a7,\
- a8, a9, a10,a11,a12,a13,a14,a15,\
- a16,a17,a18,a19,a20,a21,a22,a23,\
- a24,a25,a26,a27,a28,a29,a30,a31,...)\
+#define __kp_argsToFmts32( \
+ a0, a1, a2, a3, a4, a5, a6, a7, \
+ a8, a9, a10,a11,a12,a13,a14,a15, \
+ a16,a17,a18,a19,a20,a21,a22,a23, \
+ a24,a25,a26,a27,a28,a29,a30,a31,...) \
+ ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 })
+#define __kp_argsToObjs32( \
+ a0, a1, a2, a3, a4, a5, a6, a7, \
+ a8, a9, a10,a11,a12,a13,a14,a15, \
+ a16,a17,a18,a19,a20,a21,a22,a23, \
+ a24,a25,a26,a27,a28,a29,a30,a31,...) \
((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) })
#define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
-#define __kp_argsToArrs(COUNT,ARGS...)\
- (kp_fmt*)(\
- COUNT<=8 ? __kp_argsToFmts8(ARGS) :\
- COUNT<=16 ? __kp_argsToFmts16(ARGS) :\
- __kp_argsToFmts32(ARGS)),\
- (__kp_value_union*)(\
- COUNT<=8 ? __kp_argsToObjs8(ARGS) :\
- COUNT<=16 ? __kp_argsToObjs16(ARGS) :\
+#define __kp_argsToArrs(COUNT,ARGS...) \
+ (kp_fmt*)( \
+ COUNT<=8 ? __kp_argsToFmts8(ARGS) : \
+ COUNT<=16 ? __kp_argsToFmts16(ARGS) : \
+ __kp_argsToFmts32(ARGS)), \
+ (__kp_value_union*)( \
+ COUNT<=8 ? __kp_argsToObjs8(ARGS) : \
+ COUNT<=16 ? __kp_argsToObjs16(ARGS) : \
__kp_argsToObjs32(ARGS))
-Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects);
+Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe
-#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
- __ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
+#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
+ __ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
/*-Wint-conversion warning was produced during value to __kp_value_union conversion*/
-Maybe __kfprint(FILE* fd, uint8 n, kp_fmt* formats, __kp_value_union* objects);
+Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects);
/// @param FD FILE*
/// @param ARGS kp_fmt, value, kp_fmt, value...
///@returns Maybe
-#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
- __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
+#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
+ __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
-void __kprint(uint8 n, kp_fmt* formats, __kp_value_union* objects);
+void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects);
///can use non-catchable throw !!!
///@param ARGS kp_fmt, value, kp_fmt, value...
///@returns void
-#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\
- __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\
+#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \
+ __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \
)
-
-///@param f bgColor | fgColor
-void kprint_setColor(kp_fmt f);
#if __cplusplus
}
diff --git a/src/kprint/kprint_colors.h b/src/kprint/kprint_colors.h
index 5fc26f3..5131651 100644
--- a/src/kprint/kprint_colors.h
+++ b/src/kprint/kprint_colors.h
@@ -8,7 +8,7 @@ extern "C" {
// ^ ^^^^
// | color num
// fgColorSet flag
-PACK_ENUM(kp_fgColor,
+PACKED_ENUM(kp_fgColor,
/// black foreground
kp_fgBlack = 0x80000000,
/// dark red foreground
@@ -46,7 +46,7 @@ PACK_ENUM(kp_fgColor,
// 01000000 00000000 00000000 00000000
// ^ ^^^^
// bgColorSet flag color num
-PACK_ENUM(kp_bgColor,
+PACKED_ENUM(kp_bgColor,
/// black background
kp_bgBlack = 0x40000000,
/// dark red background
@@ -81,6 +81,9 @@ PACK_ENUM(kp_bgColor,
kp_bgWhite = 0x40f00000
)
+char* kp_bgColor_toString(kp_bgColor c);
+char* kp_fgColor_toString(kp_fgColor c);
+
#if __cplusplus
}
#endif
\ No newline at end of file
diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h
index 9efceae..33e8aea 100644
--- a/src/kprint/kprint_format.h
+++ b/src/kprint/kprint_format.h
@@ -6,11 +6,12 @@ extern "C" {
#include "../base/std.h"
#include "../base/type_system/ktid.h"
+#include "kprint_colors.h"
/// kprint_format
-typedef uint32 kp_fmt;
+typedef u32 kp_fmt;
-PACK_ENUM(kp_dataFmt,
+PACKED_ENUM(kp_dataFmt,
// 00000000 00000000 00000000 00000000
// ^^^^
// type
@@ -44,6 +45,9 @@ PACK_ENUM(kp_dataFmt,
#define kp_fmt_dataFormat(FMT) (kp_dataFmt)(FMT&0x000f0000)
#define kp_fmt_ktid(FMT) (ktid)(FMT&0x0000ffff)
+///@param f bgColor | fgColor
+void kprint_setColor(kp_fmt f);
+
#if __cplusplus
}
#endif
\ No newline at end of file
diff --git a/src/kprint/kprint_format.md b/src/kprint/kprint_format.md
index f0b2f7d..e4d811b 100644
--- a/src/kprint/kprint_format.md
+++ b/src/kprint/kprint_format.md
@@ -54,15 +54,17 @@ bgColorSet─┘││ │ bgColor └data format
## Data format
-| format | possible flags | data types | hex value | bin value |
-|-----------|----------------|------------|-----------|-----------|
-| kp_i | | int8... int64 | 0x00000000 | 00000000 00000000... |
-| kp_u | Postfix, Upper | uint8... uint64 | 0x00010000 | 00000000 00000001... |
-| kp_h | Prefix, Upper | any | 0x00020000 | 00000000 00000010... |
-| kp_b | Prefix | any | 0x00030000 | 00000000 00000011... |
-| kp_f | Postfix, Upper | float32, float64 | 0x00040000 | 00000000 00000100... |
-| kp_c | | char | 0x00050000 | 00000000 00000101... |
-| kp_sing | | char* | 0x00060000 | 00000000 00000110... |
+| format | possible flags | data types | hex value | bin value |
+|-----------|----------------|------------|------------|-----------|
+| kp_i | | i8... i64 | 0x00000000 | 00000000 00000000... |
+| kp_u | Postfix, Upper | u8... u64 | 0x00010000 | 00000000 00000001... |
+| kp_h | Prefix, Upper | any | 0x00020000 | 00000000 00000010... |
+| kp_b | Prefix | any | 0x00030000 | 00000000 00000011... |
+| kp_f | Postfix, Upper | f32, f64 | 0x00040000 | 00000000 00000100... |
+| kp_c | | char | 0x00050000 | 00000000 00000101... |
+| kp_string | | char* | 0x00060000 | 00000000 00000110... |
+
+P.S. `any` means you must add `kpid` to `kp_fmt` if data type is not base type
### *Flags*
| flag | hex value | bin value |
diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c
index baba98e..0f9e0a6 100644
--- a/src/kprint/kprintf.c
+++ b/src/kprint/kprintf.c
@@ -1,11 +1,10 @@
#include "kprintf.h"
#include "../base/base.h"
-#include "../base/type_system/base_toString.h"
#if defined(_WIN64) || defined(_WIN32)
#include
-WORD unixColorToWin(uint8 c){
+WORD unixColorToWin(u8 c){
switch(c){
//foreground
case 30: return 0;
@@ -49,8 +48,9 @@ WORD unixColorToWin(uint8 c){
void kprintf(const char* format, ...){
va_list vl;
va_start(vl, format);
- uint32 i=0;
+ u32 i=0;
for(char c=format[i++]; c!=0; c=format[i++]){
+ // value format specifiers
if(c=='%'){
char* argstr=NULL;
bool l=false;
@@ -58,28 +58,37 @@ void kprintf(const char* format, ...){
format_escape_seq:
switch (c) {
case 'u':
- argstr=toString_uint(
- l ? va_arg(vl, uint64) : va_arg(vl, uint32)
+ argstr=toString_u64(
+ l ? va_arg(vl, u64) : va_arg(vl, u32)
,0,0);
break;
case 'i': case 'd':
- argstr=toString_int(
- l ? va_arg(vl, int64) : va_arg(vl, int32)
+ argstr=toString_i64(
+ l ? va_arg(vl, i64) : va_arg(vl, i32)
);
break;
case 'f':
- // float32 is promoted to float64 when passed through '...'
- argstr=toString_float64(va_arg(vl, float64), toString_float_default_precision,0,0);
+ // f32 is promoted to f64 when passed through '...'
+ argstr=toString_f64(va_arg(vl, f64), toString_float_default_precision,0,0);
break;
case 'l':
l=true;
if((c=format[i++]))
goto format_escape_seq;
break;
- case 'p':
+ case 'p': ;
+ void* phex=va_arg(vl, void*);
+ argstr=toString_hex(&phex,getEndian()==LittleEndian,sizeof(phex),1,0);
+ break;
case 'x': ;
- uint64 px=va_arg(vl, uint64);
- argstr=toString_hex(&px,getEndian()==LittleEndian,sizeof(px),1,0);
+ if(l){
+ u64 xhex=va_arg(vl, u64);
+ argstr=toString_hex(&xhex,getEndian()==LittleEndian,sizeof(xhex),0,1);
+ }
+ else {
+ u32 xhex=va_arg(vl, u32);
+ argstr=toString_hex(&xhex,getEndian()==LittleEndian,sizeof(xhex),0,1);
+ }
break;
case 's': ;
char* cptr=va_arg(vl,char*);
@@ -104,19 +113,22 @@ void kprintf(const char* format, ...){
fputs(argstr, stdout);
free(argstr);
}
- } else if(c=='\e'){
+ }
+ // escape sequences
+ else if(c=='\e'){
IFWIN(
+ /* WINDOWS */
({
if((c=format[i++])=='['){
- uint8 colorUnix=0;
- for(int8 n=0; n<6 && c!=0; n++){
+ u8 colorUnix=0;
+ for(i8 n=0; n<6 && c!=0; n++){
c=format[i++];
switch (c){
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
colorUnix=colorUnix*10+c-'0';
break;
- case 'm':
+ case 'm': ;
WORD colorWin=unixColorToWin(colorUnix);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, colorWin);
@@ -127,13 +139,16 @@ void kprintf(const char* format, ...){
}
}
}),
+ /* UNIX */
putc(c,stdout);
);
- } else {
+ }
+ // common characters
+ else {
putc(c,stdout);
}
#if defined(_WIN64) || defined(_WIN32)
- end_iteration:
+ end_iteration:;
#endif
}
va_end(vl);
diff --git a/src/kprint/kprintf.h b/src/kprint/kprintf.h
index 5bb4fa9..d4df677 100644
--- a/src/kprint/kprintf.h
+++ b/src/kprint/kprintf.h
@@ -4,6 +4,8 @@
extern "C" {
#endif
+#include "../base/type_system/base_toString.h"
+
// cross-platform printf analog
void kprintf(const char* format, ...);
diff --git a/src/random/krandom.h b/src/random/krandom.h
index d78b7d6..2072260 100644
--- a/src/random/krandom.h
+++ b/src/random/krandom.h
@@ -12,8 +12,8 @@ extern "C" {
/*
You can choose any algorithm that has required functions:
- some_alg32_statePtr some_alg32_init(uint32 seed);
- uint32 some_alg32_next(some_alg32_statePtr);
+ some_alg32_statePtr some_alg32_init(u32 seed);
+ u32 some_alg32_next(some_alg32_statePtr);
void some_alg32_free(some_alg32_statePtr);
#define KRAND_ALG32_init some_alg32_init
@@ -51,19 +51,19 @@ typedef void* krand_statePtr;
#define __krand_next_definition(VALUE_SIZE) { return from+KRAND_ALG##VALUE_SIZE##_next(state)%(to-from); }
// ready-to-use functions
-static inline int8 krand_next8 (krand_statePtr state, int8 from, int8 to) __krand_next_definition(32)
-static inline int16 krand_next16(krand_statePtr state, int16 from, int16 to) __krand_next_definition(32)
-static inline int32 krand_next32(krand_statePtr state, int32 from, int32 to) __krand_next_definition(32)
-static inline int64 krand_next64(krand_statePtr state, int64 from, int64 to) __krand_next_definition(64)
+static inline i8 krand_next8 (krand_statePtr state, i8 from, i8 to) __krand_next_definition(32)
+static inline i16 krand_next16(krand_statePtr state, i16 from, i16 to) __krand_next_definition(32)
+static inline i32 krand_next32(krand_statePtr state, i32 from, i32 to) __krand_next_definition(32)
+static inline i64 krand_next64(krand_statePtr state, i64 from, i64 to) __krand_next_definition(64)
// divides random number by 2^64 to return a value between 0 and 1
-static inline float32 krand_nextFloat32(krand_statePtr state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; }
-static inline float64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; }
+static inline f32 krand_nextFloat32(krand_statePtr state) {return (u32)KRAND_ALG32_next(state)/0xffffffff; }
+static inline f64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; }
///@param chance (0-1.0) is probability of success
static inline bool fate(krand_statePtr state,float chance){
- int limit=1/chance + 0.01f;
+ i32 limit=1/chance + 0.01f;
return KRAND_ALG32_next(state)%limit == 0;
}
diff --git a/src/random/splitmix64/splitmix64.c b/src/random/splitmix64/splitmix64.c
index 591167c..c267b9e 100644
--- a/src/random/splitmix64/splitmix64.c
+++ b/src/random/splitmix64/splitmix64.c
@@ -13,18 +13,18 @@ generator.
// The state can be seeded with any (upto) 64 bit integer value.
-void* splitmix64_init(uint64 seed){
+void* splitmix64_init(u64 seed){
splitmix64_state* state=malloc(sizeof(splitmix64_state));
*state=seed;
return state;
}
-uint64 splitmix64_next(void* _state) {
+u64 splitmix64_next(void* _state) {
splitmix64_state* state=_state;
// increment the state variable
*state += 0x9e3779b97f4a7c15;
// copy the state to a working variable
- uint64 z = *state;
+ u64 z = *state;
// xor the variable with the variable right bit shifted 30 then multiply by a constant
z = (z ^ (z>>30)) * 0xbf58476d1ce4e5b9;
// xor the variable with the variable right bit shifted 27 then multiply by a constant
diff --git a/src/random/splitmix64/splitmix64.h b/src/random/splitmix64/splitmix64.h
index 8cf8104..3ffeb1a 100644
--- a/src/random/splitmix64/splitmix64.h
+++ b/src/random/splitmix64/splitmix64.h
@@ -6,13 +6,13 @@ extern "C" {
#include "../../base/base.h"
-typedef uint64 splitmix64_state;
+typedef u64 splitmix64_state;
typedef void* splitmix64_statePtr;
-splitmix64_statePtr splitmix64_init(uint64 seed);
+splitmix64_statePtr splitmix64_init(u64 seed);
static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); }
-uint64 splitmix64_next(splitmix64_statePtr);
+u64 splitmix64_next(splitmix64_statePtr);
static inline void splitmix64_free(splitmix64_statePtr state) {
free(state);
}
diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64.h b/src/random/xoroshiro/32bitValue/xoroshiro64.h
index 6377bba..680a926 100644
--- a/src/random/xoroshiro/32bitValue/xoroshiro64.h
+++ b/src/random/xoroshiro/32bitValue/xoroshiro64.h
@@ -8,12 +8,12 @@ extern "C" {
#include "../../splitmix64/splitmix64.h"
typedef union {
- uint64 merged;
- uint32 s[2];
+ u64 merged;
+ u32 s[2];
} xoroshiro64_state;
typedef void* xoroshiro64_statePtr;
-xoroshiro64_statePtr xoroshiro64_init(uint64 seed);
+xoroshiro64_statePtr xoroshiro64_init(u64 seed);
#define xoroshiro64star_init xoroshiro64_init
#define xoroshiro64starstar_init xoroshiro64_init
@@ -21,8 +21,8 @@ static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoros
#define xoroshiro64star_initFromTime xoroshiro64_initFromTime
#define xoroshiro64starstar_initFromTime xoroshiro64_initFromTime
-uint32 xoroshiro64star_next(xoroshiro64_statePtr);
-uint32 xoroshiro64starstar_next(xoroshiro64_statePtr);
+u32 xoroshiro64star_next(xoroshiro64_statePtr);
+u32 xoroshiro64starstar_next(xoroshiro64_statePtr);
static inline void xoroshiro64_free(xoroshiro64_statePtr state) {
free(state);
diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64star.c b/src/random/xoroshiro/32bitValue/xoroshiro64star.c
index 7c2fd3c..ca57d35 100644
--- a/src/random/xoroshiro/32bitValue/xoroshiro64star.c
+++ b/src/random/xoroshiro/32bitValue/xoroshiro64star.c
@@ -10,8 +10,8 @@ See . */
/*
This is xoroshiro64* 1.0, our best and fastest 32-bit small-state
-generator for 32-bit floating-point numbers. We suggest to use its
-upper bits for floating-point generation, as it is slightly faster than
+generator for 32-bit floating-poi32 numbers. We suggest to use its
+upper bits for floating-poi32 generation, as it is slightly faster than
xoroshiro64**. It passes all tests we are aware of except for linearity
tests, as the lowest six bits have low linear complexity, so if low
linear complexity is not considered an issue (as it is usually the
@@ -23,15 +23,15 @@ right shifts to extract subsets of bits.
The state must be seeded so that it is not everywhere zero.
*/
-static inline uint32 rotl(const uint32 x, int k) {
+static inline u32 rotl(const u32 x, i32 k) {
return (x << k) | (x >> (32 - k));
}
-uint32 xoroshiro64star_next(void* _state) {
+u32 xoroshiro64star_next(void* _state) {
xoroshiro64_state* state=_state;
- const uint32 s0 = state->s[0];
- uint32 s1 = state->s[1];
- const uint32 result = s0 * 0x9E3779BB;
+ const u32 s0 = state->s[0];
+ u32 s1 = state->s[1];
+ const u32 result = s0 * 0x9E3779BB;
s1 ^= s0;
state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b
@@ -40,7 +40,7 @@ uint32 xoroshiro64star_next(void* _state) {
return result;
}
-void* xoroshiro64_init(uint64 seed){
+void* xoroshiro64_init(u64 seed){
xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state));
splitmix64_state* splitmix=splitmix64_init(seed);
state->merged=splitmix64_next(splitmix);
diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c
index df4432e..40d93fc 100644
--- a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c
+++ b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c
@@ -19,15 +19,15 @@ See . */
The state must be seeded so that it is not everywhere zero. */
-static inline uint32 rotl(const uint32 x, int k) {
+static inline u32 rotl(const u32 x, i32 k) {
return (x << k) | (x >> (32 - k));
}
-uint32 xoroshiro64starstar_next(void* _state) {
+u32 xoroshiro64starstar_next(void* _state) {
xoroshiro64_state* state=_state;
- const uint32 s0 = state->s[0];
- uint32 s1 = state->s[1];
- const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5;
+ const u32 s0 = state->s[0];
+ u32 s1 = state->s[1];
+ const u32 result = rotl(s0 * 0x9E3779BB, 5) * 5;
s1 ^= s0;
state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b
diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128.h b/src/random/xoroshiro/64bitValue/xoroshiro128.h
index 63ed537..33b7116 100644
--- a/src/random/xoroshiro/64bitValue/xoroshiro128.h
+++ b/src/random/xoroshiro/64bitValue/xoroshiro128.h
@@ -9,11 +9,11 @@ extern "C" {
typedef union {
- uint32 s[2];
+ u32 s[2];
} xoroshiro128_state;
typedef void* xoroshiro128_statePtr;
-xoroshiro128_statePtr xoroshiro128_init(uint64 seed);
+xoroshiro128_statePtr xoroshiro128_init(u64 seed);
#define xoroshiro128plus_init xoroshiro128_init
#define xoroshiro128plusplus_init xoroshiro128_init
#define xoroshiro128starstar_init xoroshiro128_init
@@ -23,9 +23,9 @@ static inline xoroshiro128_statePtr xoroshiro128_initFromTime(void) { return xor
#define xoroshiro128plusplus_initFromTime xoroshiro128_initFromTime
#define xoroshiro128starstar_initFromTime xoroshiro128_initFromTime
-uint64 xoroshiro128plus_next(xoroshiro128_statePtr);
-uint64 xoroshiro128plusplus_next(xoroshiro128_statePtr);
-uint64 xoroshiro128starstar_next(xoroshiro128_statePtr);
+u64 xoroshiro128plus_next(xoroshiro128_statePtr);
+u64 xoroshiro128plusplus_next(xoroshiro128_statePtr);
+u64 xoroshiro128starstar_next(xoroshiro128_statePtr);
static inline void xoroshiro128_free(xoroshiro128_statePtr state) {
free(state);
diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c
index e01e0b5..05760ec 100644
--- a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c
+++ b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c
@@ -9,9 +9,9 @@ See . */
#include "xoroshiro128.h"
/* This is xoroshiro128+ 1.0, our best and fastest small-state generator
- for floating-point numbers, but its state space is large enough only
+ for floating-poi32 numbers, but its state space is large enough only
for mild parallelism. We suggest to use its upper bits for
- floating-point generation, as it is slightly faster than
+ floating-poi32 generation, as it is slightly faster than
xoroshiro128++/xoroshiro128**. It passes all tests we are aware of
except for the four lower bits, which might fail linearity tests (and
just those), so if low linear complexity is not considered an issue (as
@@ -32,15 +32,15 @@ See . */
better results in our test than the 2016 version (a=55, b=14, c=36).
*/
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x >> (64 - k));
}
-uint64 xoroshiro128plus_next(void* _state){
+u64 xoroshiro128plus_next(void* _state){
xoroshiro128_state* state=_state;
- const uint64 s0 = state->s[0];
- uint64 s1 = state->s[1];
- const uint64 result = s0 + s1;
+ const u64 s0 = state->s[0];
+ u64 s1 = state->s[1];
+ const u64 result = s0 + s1;
s1 ^= s0;
state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
@@ -49,7 +49,7 @@ uint64 xoroshiro128plus_next(void* _state){
return result;
}
-void* xoroshiro128_init(uint64 seed){
+void* xoroshiro128_init(u64 seed){
xoroshiro128_state* state=malloc(sizeof(xoroshiro128_state));
splitmix64_state* splitmix=splitmix64_init(seed);
state->s[0]=splitmix64_next(splitmix);
diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c
index c01fb30..43e110e 100644
--- a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c
+++ b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c
@@ -13,7 +13,7 @@ See . */
tests we are aware of, but its state space is large enough only for
mild parallelism.
- For generating just floating-point numbers, xoroshiro128+ is even
+ For generating just floating-poi32 numbers, xoroshiro128+ is even
faster (but it has a very mild bias, see notes in the comments).
The state must be seeded so that it is not everywhere zero. If you have
@@ -21,15 +21,15 @@ See . */
output to fill s. */
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x >> (64 - k));
}
-uint64 xoroshiro128plusplus_next(void* _state){
+u64 xoroshiro128plusplus_next(void* _state){
xoroshiro128_state* state=_state;
- const uint64 s0 = state->s[0];
- uint64 s1 = state->s[1];
- const uint64 result = rotl(s0 + s1, 17) + s0;
+ const u64 s0 = state->s[0];
+ u64 s1 = state->s[1];
+ const u64 result = rotl(s0 + s1, 17) + s0;
s1 ^= s0;
state->s[0] = rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b
diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c
index 3e9ac72..5e25851 100644
--- a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c
+++ b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c
@@ -13,7 +13,7 @@ See . */
tests we are aware of, but its state space is large enough only for
mild parallelism.
- For generating just floating-point numbers, xoroshiro128+ is even
+ For generating just floating-poi32 numbers, xoroshiro128+ is even
faster (but it has a very mild bias, see notes in the comments).
The state must be seeded so that it is not everywhere zero. If you have
@@ -21,15 +21,15 @@ See . */
output to fill s. */
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x >> (64 - k));
}
-uint64 xoroshiro128starstar_next(void* _state){
+u64 xoroshiro128starstar_next(void* _state){
xoroshiro128_state* state=_state;
- const uint64 s0 = state->s[0];
- uint64 s1 = state->s[1];
- const uint64 result = rotl(s0 * 5, 7) * 9;
+ const u64 s0 = state->s[0];
+ u64 s1 = state->s[1];
+ const u64 result = rotl(s0 * 5, 7) * 9;
s1 ^= s0;
state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
diff --git a/src/random/xoshiro/32bitValue/xoshiro128.h b/src/random/xoshiro/32bitValue/xoshiro128.h
index 02d52e8..daf2333 100644
--- a/src/random/xoshiro/32bitValue/xoshiro128.h
+++ b/src/random/xoshiro/32bitValue/xoshiro128.h
@@ -9,12 +9,12 @@ extern "C" {
typedef union {
- uint64 merged[2];
- uint32 s[4];
+ u64 merged[2];
+ u32 s[4];
} xoshiro128_state;
typedef void* xoshiro128_statePtr;
-xoshiro128_statePtr xoshiro128_init(uint64 seed);
+xoshiro128_statePtr xoshiro128_init(u64 seed);
#define xoshiro128plus_init xoshiro128_init
#define xoshiro128plusplus_init xoshiro128_init
#define xoshiro128starstar_init xoshiro128_init
@@ -24,9 +24,9 @@ static inline xoshiro128_statePtr xoshiro128_initFromTime(void) { return xoshiro
#define xoshiro128plusplus_initFromTime xoshiro128_initFromTime
#define xoshiro128starstar_initFromTime xoshiro128_initFromTime
-uint32 xoshiro128plus_next(xoshiro128_statePtr);
-uint32 xoshiro128plusplus_next(xoshiro128_statePtr);
-uint32 xoshiro128starstar_next(xoshiro128_statePtr);
+u32 xoshiro128plus_next(xoshiro128_statePtr);
+u32 xoshiro128plusplus_next(xoshiro128_statePtr);
+u32 xoshiro128starstar_next(xoshiro128_statePtr);
static inline void xoshiro128_free(xoshiro128_statePtr state) {
free(state);
diff --git a/src/random/xoshiro/32bitValue/xoshiro128plus.c b/src/random/xoshiro/32bitValue/xoshiro128plus.c
index 8cafc8b..f68b3d5 100644
--- a/src/random/xoshiro/32bitValue/xoshiro128plus.c
+++ b/src/random/xoshiro/32bitValue/xoshiro128plus.c
@@ -9,8 +9,8 @@ See . */
#include "xoshiro128.h"
/* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit
- floating-point numbers. We suggest to use its upper bits for
- floating-point generation, as it is slightly faster than xoshiro128**.
+ floating-poi32 numbers. We suggest to use its upper bits for
+ floating-poi32 generation, as it is slightly faster than xoshiro128**.
It passes all tests we are aware of except for
linearity tests, as the lowest four bits have low linear complexity, so
if low linear complexity is not considered an issue (as it is usually
@@ -22,15 +22,15 @@ See . */
The state must be seeded so that it is not everywhere zero. */
-static inline uint32 rotl(const uint32 x, int k) {
+static inline u32 rotl(const u32 x, i32 k) {
return (x << k) | (x >> (32 - k));
}
-uint32 xoshiro128plus_next(void* _state){
+u32 xoshiro128plus_next(void* _state){
xoshiro128_state* state=_state;
- const uint32 result = state->s[0] + state->s[3];
+ const u32 result = state->s[0] + state->s[3];
- const uint32 t = state->s[1] << 9;
+ const u32 t = state->s[1] << 9;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
@@ -44,7 +44,7 @@ uint32 xoshiro128plus_next(void* _state){
return result;
}
-void* xoshiro128_init(uint64 seed){
+void* xoshiro128_init(u64 seed){
xoshiro128_state* state=malloc(sizeof(xoshiro128_state));
splitmix64_state* splitmix=splitmix64_init(seed);
state->merged[0]=splitmix64_next(splitmix);
diff --git a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c
index e9be19d..8fea492 100644
--- a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c
+++ b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c
@@ -19,15 +19,15 @@ See . */
The state must be seeded so that it is not everywhere zero. */
-static inline uint32 rotl(const uint32 x, int k) {
+static inline u32 rotl(const u32 x, i32 k) {
return (x << k) | (x >> (32 - k));
}
-uint32 xoshiro128plusplus_next(void* _state){
+u32 xoshiro128plusplus_next(void* _state){
xoshiro128_state* state=_state;
- const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0];
+ const u32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0];
- const uint32 t = state->s[1] << 9;
+ const u32 t = state->s[1] << 9;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
diff --git a/src/random/xoshiro/32bitValue/xoshiro128starstar.c b/src/random/xoshiro/32bitValue/xoshiro128starstar.c
index 40a53d2..87a20bc 100644
--- a/src/random/xoshiro/32bitValue/xoshiro128starstar.c
+++ b/src/random/xoshiro/32bitValue/xoshiro128starstar.c
@@ -22,15 +22,15 @@ See . */
The state must be seeded so that it is not everywhere zero. */
-static inline uint32 rotl(const uint32 x, int k) {
+static inline u32 rotl(const u32 x, i32 k) {
return (x << k) | (x >> (32 - k));
}
-uint32 xoshiro128starstar_next(void* _state){
+u32 xoshiro128starstar_next(void* _state){
xoshiro128_state* state=_state;
- const uint32 result = rotl(state->s[1] * 5, 7) * 9;
+ const u32 result = rotl(state->s[1] * 5, 7) * 9;
- const uint32 t = state->s[1] << 9;
+ const u32 t = state->s[1] << 9;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
diff --git a/src/random/xoshiro/64bitValue/xoshiro256.h b/src/random/xoshiro/64bitValue/xoshiro256.h
index 479c6aa..263d75e 100644
--- a/src/random/xoshiro/64bitValue/xoshiro256.h
+++ b/src/random/xoshiro/64bitValue/xoshiro256.h
@@ -9,11 +9,11 @@ extern "C" {
typedef union {
- uint64 s[4];
+ u64 s[4];
} xoshiro256_state;
typedef void* xoshiro256_statePtr;
-xoshiro256_statePtr xoshiro256_init(uint64 seed);
+xoshiro256_statePtr xoshiro256_init(u64 seed);
#define xoshiro256plus_init xoshiro256_init
#define xoshiro256plusplus_init xoshiro256_init
#define xoshiro256starstar_init xoshiro256_init
@@ -23,9 +23,9 @@ static inline xoshiro256_statePtr xoshiro256_initFromTime(void) { return xoshiro
#define xoshiro256plusplus_initFromTime xoshiro256_initFromTime
#define xoshiro256starstar_initFromTime xoshiro256_initFromTime
-uint64 xoshiro256plus_next(xoshiro256_statePtr);
-uint64 xoshiro256plusplus_next(xoshiro256_statePtr);
-uint64 xoshiro256starstar_next(xoshiro256_statePtr);
+u64 xoshiro256plus_next(xoshiro256_statePtr);
+u64 xoshiro256plusplus_next(xoshiro256_statePtr);
+u64 xoshiro256starstar_next(xoshiro256_statePtr);
static inline void xoshiro256_free(xoshiro256_statePtr state) {
free(state);
diff --git a/src/random/xoshiro/64bitValue/xoshiro256plus.c b/src/random/xoshiro/64bitValue/xoshiro256plus.c
index 066ef8a..ee7ae32 100644
--- a/src/random/xoshiro/64bitValue/xoshiro256plus.c
+++ b/src/random/xoshiro/64bitValue/xoshiro256plus.c
@@ -24,15 +24,15 @@ See . */
output to fill s. */
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x >> (64 - k));
}
-uint64 xoshiro256plus_next(void* _state){
+u64 xoshiro256plus_next(void* _state){
xoshiro256_state* state=_state;
- const uint64 result = state->s[0] + state->s[3];
+ const u64 result = state->s[0] + state->s[3];
- const uint64 t = state->s[1] << 17;
+ const u64 t = state->s[1] << 17;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
@@ -46,7 +46,7 @@ uint64 xoshiro256plus_next(void* _state){
return result;
}
-void* xoshiro256_init(uint64 seed){
+void* xoshiro256_init(u64 seed){
xoshiro256_state* state=malloc(sizeof(xoshiro256_state));
splitmix64_state* splitmix=splitmix64_init(seed);
state->s[0]=splitmix64_next(splitmix);
diff --git a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c
index 77ca943..d28eb7c 100644
--- a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c
+++ b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c
@@ -13,20 +13,20 @@ See . */
enough for any parallel application, and it passes all tests we are
aware of.
- For generating just floating-point numbers, xoshiro256+ is even faster.
+ For generating just floating-poi32 numbers, xoshiro256+ is even faster.
The state must be seeded so that it is not everywhere zero. If you have
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
output to fill s. */
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x>>(64 - k));
}
-uint64 xoshiro256plusplus_next(void* _state) {
+u64 xoshiro256plusplus_next(void* _state) {
xoshiro256_state* state=_state;
- const uint64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0];
- const uint64 t=state->s[1] << 17;
+ const u64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0];
+ const u64 t=state->s[1] << 17;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
state->s[1] ^= state->s[2];
diff --git a/src/random/xoshiro/64bitValue/xoshiro256starstar.c b/src/random/xoshiro/64bitValue/xoshiro256starstar.c
index 7829096..804007c 100644
--- a/src/random/xoshiro/64bitValue/xoshiro256starstar.c
+++ b/src/random/xoshiro/64bitValue/xoshiro256starstar.c
@@ -13,21 +13,21 @@ See . */
large enough for any parallel application, and it passes all tests we
are aware of.
- For generating just floating-point numbers, xoshiro256+ is even faster.
+ For generating just floating-poi32 numbers, xoshiro256+ is even faster.
The state must be seeded so that it is not everywhere zero. If you have
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
output to fill s. */
-static inline uint64 rotl(const uint64 x, int k) {
+static inline u64 rotl(const u64 x, i32 k) {
return (x << k) | (x >> (64 - k));
}
-uint64 xoshiro256starstar_next(void* _state){
+u64 xoshiro256starstar_next(void* _state){
xoshiro256_state* state=_state;
- const uint64 result = rotl(state->s[1] * 5, 7) * 9;
+ const u64 result = rotl(state->s[1] * 5, 7) * 9;
- const uint64 t = state->s[1] << 17;
+ const u64 t = state->s[1] << 17;
state->s[2] ^= state->s[0];
state->s[3] ^= state->s[1];
diff --git a/tests/main.cpp b/tests/main.cpp
index 9c43add..c6876e2 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,13 +1,13 @@
#include "tests.h"
-int main(){
- if(!setlocale(LC_ALL, "C.UTF8"))
+i32 main(){
+ if(setlocale(LC_CTYPE, "C.UTF-8")!=0)
kprintf("\e[93msetlocale failed\n");
- ktDescriptors_beginInit();
- ktDescriptors_initKerepTypes();
- ktDescriptors_endInit();
+ kt_beginInit();
+ kt_initKerepTypes();
+ kt_endInit();
test_all();
- ktDescriptors_free();
+ kt_free();
kprintf("\e[0m\n");
return 0;
}
diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp
index 1ea747d..306f87a 100644
--- a/tests/test_autoarr-vs-vector.cpp
+++ b/tests/test_autoarr-vs-vector.cpp
@@ -2,16 +2,16 @@
#include "../src/Autoarr/Autoarr.h"
#include
-int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){
- uint32 count=blockLength*blockCount;
- kprintf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (uint64)count);
- Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength);
- std::vector vec=std::vector();
+i64 _autoarrVsVector(u16 blockCount, u16 blockLength){
+ u32 count=blockLength*blockCount;
+ kprintf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (u64)count);
+ Autoarr_i64* ar=Autoarr_create(i64, blockCount, blockLength);
+ std::vector vec=std::vector();
optime("Autoarr_add", count,
Autoarr_add(ar, op_i));
optime("vector_push_back", count,
vec.push_back(op_i));
- int64 t=0;
+ i64 t=0;
optime("Autoarr_get", count,
t=Autoarr_get(ar, op_i));
optime("vector_get", count,
@@ -21,7 +21,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){
}
void test_autoarrVsVector(){
- optime(__func__, 1, ({
+ optime(__func__, 1,
kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n");
_autoarrVsVector(4, 16);
_autoarrVsVector(16, 64);
@@ -30,5 +30,5 @@ void test_autoarrVsVector(){
_autoarrVsVector(32, 1024);
_autoarrVsVector(256, 256);
_autoarrVsVector(1024, 1024);
- }));
+ );
}
diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c
index 2b3d24d..07f8fb7 100644
--- a/tests/test_autoarr.c
+++ b/tests/test_autoarr.c
@@ -1,8 +1,8 @@
#include "tests.h"
#include "../src/Autoarr/Autoarr.h"
-static void printautoarr(Autoarr(uint16)* ar){
- kprintf("\e[94mAutoarr(uint16): "
+static void printautoarr(Autoarr(u16)* ar){
+ kprintf("\e[94mAutoarr(u16): "
IFWIN("%llu", "%lu")
"\n max_blocks_count: %u\n"
" blocks_count: %u\n"
@@ -10,7 +10,7 @@ static void printautoarr(Autoarr(uint16)* ar){
" block_length: %u\n"
" max_length: %u\n"
" length: %u\n",
- sizeof(Autoarr(uint16)),
+ sizeof(Autoarr(u16)),
ar->max_blocks_count,
ar->blocks_count,
ar->max_block_length,
@@ -19,26 +19,26 @@ static void printautoarr(Autoarr(uint16)* ar){
Autoarr_length(ar));
}
-static void fillar(Autoarr(uint16)* ar){
- for (uint16 i=0;irows[h];
- uint32 l=Autoarr_length(ar);
+ u32 l=Autoarr_length(ar);
lgs[l]++;
}
- for(uint32 i=0; i0) {
char* str0=char_multiply(' ',i>=100?0:(i>=10?1:2));
char* str1=char_multiply(' ',lgs[i]>=100?0:(lgs[i]>=10?1:2));
@@ -36,7 +36,7 @@ void printrowgraph(Hashtable* ht){
}
}
-char* genkey(uint32 i){
+char* genkey(u32 i){
char* key=malloc(12);
IFMSC(
sprintf_s(key,12,"key_%u",i),
@@ -46,13 +46,13 @@ char* genkey(uint32 i){
}
void fill(Hashtable* ht){
- for(uint32 i=0;i<100000;i++)
+ for(u32 i=0;i<100000;i++)
Hashtable_add(ht,genkey(i),UniUInt64(i));
}
Unitype gett(Hashtable* ht){
Unitype u;
- for(uint32 i=0;i<100000;i++){
+ for(u32 i=0;i<100000;i++){
char* key=genkey(i);
u=Hashtable_get(ht,key);
free(key);
@@ -62,7 +62,7 @@ Unitype gett(Hashtable* ht){
void test_hashtable(){
- optime("test_hashtable",1,({
+ optime("test_hashtable",1,
kprintf("\e[96m-----------[test_hashtable]-----------\n");
Hashtable* ht=Hashtable_create();
kprintf("\e[92mhashtable created\n");
@@ -73,5 +73,5 @@ void test_hashtable(){
print_hashtable(ht);
Hashtable_free(ht);
kprintf("\e[92mhashtable freed\n");
- }));
+ );
}
diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c
index b397e21..39aa250 100644
--- a/tests/test_kprint_colors.c
+++ b/tests/test_kprint_colors.c
@@ -5,24 +5,24 @@
#endif
#define testColor(COLOR) \
- kprint_setColor(kp_bgBlack|kp_fg##COLOR);\
- kprintf(#COLOR " ");\
- kprint_setColor(kp_bg##COLOR|kp_fgGray);\
- kprintf(#COLOR);\
- kprint_setColor(kp_bgBlack|kp_fgBlack);\
+ kprint_setColor(kp_bgBlack|kp_fg##COLOR); \
+ kprintf(#COLOR " "); \
+ kprint_setColor(kp_bg##COLOR|kp_fgGray); \
+ kprintf(#COLOR); \
+ kprint_setColor(kp_bgBlack|kp_fgBlack); \
kprintf("\n");
void test_kprint_colors(){
/* IFWIN(
({
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- for(uint8 col=0; col<255; col++){
+ for(u8 col=0; col<255; col++){
SetConsoleTextAttribute(hConsole, col);
kprintf("%u ",col);
}
}),
({
- for(uint8 col=0; col<255; col++)
+ for(u8 col=0; col<255; col++)
kprintf("\e[%um%u ", col, col);
})
);
diff --git a/tests/test_network.c b/tests/test_network.c
index a59f0e0..8190e43 100644
--- a/tests/test_network.c
+++ b/tests/test_network.c
@@ -1,19 +1,21 @@
#include "tests.h"
#include "../src/Network/network.h"
-void __test_knIPV4Address_fromStr(char* addrStr, uint8 a, uint8 b, uint8 c, uint8 d){
- tryLast(knIPV4Address_fromStr(addrStr), maybeAddr){
- knIPV4Address addr;
- addr.u32=(uint32)maybeAddr.value.UInt64;
- printf("\e[94mknIPV4Address_fromStr(\e[96m%s\e[94m) -> ", addrStr);
- if(maybeAddr.value.UInt64!=knIPV4Address_fromBytes(a,b,c,d).u32){
- printf("\e[91m%u.%u.%u.%u\n",
- (uint8)addr.bytes[0], (uint8)addr.bytes[1], (uint8)addr.bytes[2], (uint8)addr.bytes[3]);
- throw("knIPV4Address_fromStr returned wrong value");
- }
- else printf("\e[92m%u.%u.%u.%u\n",
- (uint8)addr.bytes[0], (uint8)addr.bytes[1], (uint8)addr.bytes[2], (uint8)addr.bytes[3]);
+void __test_knIPV4Address_fromStr(char* addrStr, u8 a, u8 b, u8 c, u8 d){
+ tryLast(knIPV4Address_fromStr(addrStr), maybeAddr, ;)
+ knIPV4Address addr;
+ addr.u32=(u32)maybeAddr.value.UInt64;
+ printf("\e[94mknIPV4Address_fromStr(\e[96m%s\e[94m) -> ", addrStr);
+ if(maybeAddr.value.UInt64!=knIPV4Address_fromBytes(a,b,c,d).u32){
+ printf("\e[91m%u.%u.%u.%u\n",
+ (u8)addr.bytes[0], (u8)addr.bytes[1], (u8)addr.bytes[2], (u8)addr.bytes[3]);
+ throw("knIPV4Address_fromStr returned wrong value");
}
+ else {
+ printf("\e[92m%u.%u.%u.%u\n",
+ (u8)addr.bytes[0], (u8)addr.bytes[1], (u8)addr.bytes[2], (u8)addr.bytes[3]);
+ }
+
}
#define test_knIPV4Address_fromStr(a,b,c,d) __test_knIPV4Address_fromStr(#a"."#b"."#c"."#d, a,b,c,d)
diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c
index 1af5ef5..1ae83c7 100644
--- a/tests/test_rng_algorithms.c
+++ b/tests/test_rng_algorithms.c
@@ -2,25 +2,25 @@
#include "../src/random/krandom.h"
-#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\
- kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n");\
- void* s= ALG##_init(0);\
- uint##VALUE_SIZE r=ALG##_next(s);\
- kprintf("\e[97m next from zero seed:");\
- if(r!=EXPECTED_FROM_ZERO){\
- kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
- throw(ERR_UNEXPECTEDVAL);\
- }\
- kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
- ALG##_free(s);\
- s= ALG##_initFromTime();\
- r=ALG##_next(s);\
- ALG##_free(s);\
- kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\
+#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO) { \
+ kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n"); \
+ void* s= ALG##_init(0); \
+ u##VALUE_SIZE r=ALG##_next(s); \
+ kprintf("\e[97m next from zero seed:"); \
+ if(r!=EXPECTED_FROM_ZERO){ \
+ kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r); \
+ throw(ERR_UNEXPECTEDVAL); \
+ } \
+ kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
+ ALG##_free(s); \
+ s= ALG##_initFromTime(); \
+ r=ALG##_next(s); \
+ ALG##_free(s); \
+ kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
}
void test_rng_algorithms(){
- optime("test_rng_algorithms",1,({
+ optime("test_rng_algorithms",1,
kprintf("\e[96m--------[test_rng_algorithms]---------\n");
// for ALG32
// xoroshiro64
@@ -41,5 +41,5 @@ void test_rng_algorithms(){
test_alg(xoshiro256starstar, 64, 11091344671253066420ULL)
// splitmix64
test_alg(splitmix64, 64, 16294208416658607535ULL)
- }));
+ );
}
\ No newline at end of file
diff --git a/tests/test_safethrow.c b/tests/test_safethrow.c
index 14e4e1e..f283c3d 100644
--- a/tests/test_safethrow.c
+++ b/tests/test_safethrow.c
@@ -15,7 +15,7 @@ Maybe throw_errcode(){
Maybe test_maybe(){
kprintf("\e[94mdont_throw returns \e[92m");
- tryLast(dont_throw(),rez0)
+ tryLast(dont_throw(),rez0,;)
printMaybe(rez0);
kprintf("\n");
try(throw_error(),rez1,;)
@@ -30,7 +30,7 @@ Maybe a(){ try(b(),_,;) return MaybeNull; }
void test_safethrow(){
kprintf("\e[96m-----------[test_safethrow]-----------\n");
- optime("test_safethrow", 1, ({
+ optime("test_safethrow", 1,
Maybe e=test_maybe();
kprintf("\e[94mthrow_error:\n\e[92m");
printMaybe(e);
@@ -39,5 +39,5 @@ void test_safethrow(){
e=a();
printMaybe(e);
Maybe_free(e);
- }));
+ );
}
diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c
index 656a96a..4682e0b 100644
--- a/tests/test_searchtree.c
+++ b/tests/test_searchtree.c
@@ -9,13 +9,13 @@ void printstnode(STNode* node){
kprintf("\n");
// prints pointers to all existing branches
/* kprintf(" branches: %p\n", node->branches);
- if(node->branches) for(uint8 i=0;i<8;i++){
+ if(node->branches) for(u8 i=0;i<8;i++){
kprintf(" \e[90m[%u]=%p\n",i,node->branches[i]);
if(node->branches[i])
- for (uint8 ii = 0; ii < 8; ii++){
+ for (u8 ii = 0; ii < 8; ii++){
kprintf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]);
if(node->branches[i][ii])
- for (uint8 iii = 0; iii < 4; iii++)
+ for (u8 iii = 0; iii < 4; iii++)
kprintf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]);
}
@@ -23,7 +23,7 @@ void printstnode(STNode* node){
}
void test_searchtree(){
- optime("test_searchtree",1,({
+ optime("test_searchtree",1,
kprintf("\e[96m-----------[test_searchtree]----------\n");
STNode* node=STNode_create();
kprintf("\e[92mnode created\n");
@@ -70,5 +70,5 @@ void test_searchtree(){
printstnode(node);
STNode_free(node);
kprintf("\e[92mnode deleted\n");
- }));
+ );
}
diff --git a/tests/test_string.c b/tests/test_string.c
index ff861f9..6bb9743 100644
--- a/tests/test_string.c
+++ b/tests/test_string.c
@@ -2,7 +2,7 @@
#include "../src/String/string.h"
void test_string(){
- optime(__func__,1,({
+ optime(__func__,1,
kprintf("\e[96m-------------[test_string]------------\n");
char c[]="0123456789abcdef";
string s={.ptr=c, .length=cptr_length(c)};
@@ -10,5 +10,5 @@ void test_string(){
char* p=string_extract(s);
kprintf("\e[94mstring_extract() -> \e[92m\"%s\"\n",p);
free(p);
- }));
+ );
}
\ No newline at end of file
diff --git a/tests/test_type_system.c b/tests/test_type_system.c
index 553bb1f..58ed2e5 100644
--- a/tests/test_type_system.c
+++ b/tests/test_type_system.c
@@ -2,8 +2,8 @@
void test_type_system(){
for(ktid id=0; idid, type->name, type->size, type->freeMembers, type->toString);
}
}
diff --git a/tests/tests.h b/tests/tests.h
index c65a4ba..859c5cc 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -6,6 +6,7 @@
extern "C" {
#endif
+void test_cptr();
void test_string();
void test_safethrow();
void test_searchtree();
@@ -18,24 +19,27 @@ void test_rng_algorithms();
void test_kprint_colors();
void test_kprint();
void test_type_system();
+void test_network();
inline void test_all(){
kprintf("\e[97mkerep tests are starting!\n");
- optime(__func__, 1, ({
+ optime(__func__, 1,
+ test_cptr();
test_type_system();
test_string();
test_safethrow();
test_searchtree();
test_autoarr();
test_autoarrVsVector();
- test_hash_functions();
- test_hashtable();
- test_dtsod();
test_rng_algorithms();
test_kprint_colors();
test_kprint();
+ test_hash_functions();
+ test_hashtable();
+ test_dtsod();
+ test_network();
kprintf("\e[96m--------------------------------------\e[0m\n");
- }));
+ );
}
#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))