diff --git a/.vscode/launch.json b/.vscode/launch.json index 421e47a..df30fe5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,12 +5,35 @@ "name": "(gdb) Debug", "type": "cppdbg", "request": "launch", + "program": "${workspaceFolder}/bin/kerep.com", + "preLaunchTask": "build_exec_dbg", + "stopAtEntry": false, + "cwd": "${fileDirname}", + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + }, + { + "name": "(gdb-pipe) Debug", + "type": "cppdbg", + "request": "launch", "preLaunchTask": "build_exec_dbg", "program": "${workspaceFolder}/bin/kerep.com", "stopAtEntry": false, "cwd": "${workspaceFolder}", - "externalConsole": true, - "internalConsoleOptions": "openOnSessionStart", + "externalConsole": false, "miDebuggerPath": "/usr/bin/gdb", "MIMode": "gdb", "pipeTransport": { diff --git a/src/Array/Array.c b/src/Array/Array.c new file mode 100644 index 0000000..c669459 --- /dev/null +++ b/src/Array/Array.c @@ -0,0 +1,16 @@ +#include "Array.h" + +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(Unitype) diff --git a/src/Array/Array.h b/src/Array/Array.h index ac9dabc..016d0ed 100644 --- a/src/Array/Array.h +++ b/src/Array/Array.h @@ -1,4 +1,11 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + #include "Array_declare.h" +#include "Array_define.h" Array_declare(char) Array_declare(bool) @@ -13,28 +20,8 @@ Array_declare(uint32) Array_declare(int64) Array_declare(uint64) -ktId_declare(ArrayChar); -ktId_declare(ArrayBool); -ktId_declare(ArrayFloat32); -ktId_declare(ArrayFloat64); -ktId_declare(ArrayInt8); -ktId_declare(ArrayUInt8); -ktId_declare(ArrayInt16); -ktId_declare(ArrayUInt16); -ktId_declare(ArrayInt32); -ktId_declare(ArrayUInt32); -ktId_declare(ArrayInt64); -ktId_declare(ArrayUInt64); +Array_declare(Unitype) -ktId_declare(ArrayCharPtr); -ktId_declare(ArrayBoolPtr); -ktId_declare(ArrayFloat32Ptr); -ktId_declare(ArrayFloat64Ptr); -ktId_declare(ArrayInt8Ptr); -ktId_declare(ArrayUInt8Ptr); -ktId_declare(ArrayInt16Ptr); -ktId_declare(ArrayUInt16Ptr); -ktId_declare(ArrayInt32Ptr); -ktId_declare(ArrayUInt32Ptr); -ktId_declare(ArrayInt64Ptr); -ktId_declare(ArrayUInt64Ptr); +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h index 6d12ead..e7139b3 100644 --- a/src/Array/Array_declare.h +++ b/src/Array/Array_declare.h @@ -13,16 +13,17 @@ typedef struct Array_##type{\ bool allocatedOnHeap;\ } Array_##type;\ \ +ktid_declare(Array_##type);\ \ static inline Array_##type Array_##type##_allocValues(uint32 length){\ return (Array_##type) {\ - .values=(type##*)malloc(sizeof(type)*length),\ + .values=(type*)malloc(sizeof(type)*length),\ .length=length,\ .allocatedOnHeap=true\ };\ }\ \ -static inline Array_##type Array_##type##_fromBuffer(type##* buffer, uint32 bufferLength, bool allocatedOnHeap){\ +static inline Array_##type Array_##type##_fromBuffer(type* buffer, uint32 bufferLength, bool allocatedOnHeap){\ return (Array_##type) {\ .values=buffer,\ .length=bufferLength,\ diff --git a/src/Array/Array_define.h b/src/Array/Array_define.h new file mode 100644 index 0000000..6a16f9b --- /dev/null +++ b/src/Array/Array_define.h @@ -0,0 +1,14 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" + +#define Array_define(type)\ +ktid_define(Array_##type); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Array/README.md b/src/Array/README.md index 931ff09..3027ac9 100644 --- a/src/Array/README.md +++ b/src/Array/README.md @@ -1,2 +1,2 @@ # Array struct -This struct stores array pointer and length. If you want to use `Array` of some type, it should be declared in header file by macro `Array_declare`. It uses `static inline` functions, so all definitions will be generated in header. +This struct stores array pointer and length. If you want to use `Array` of some type, it should be declared in header file by macro `Array_declare` and defined in source file by `Array_define`. diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index c72a299..274f777 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -12,40 +12,3 @@ Autoarr_define(uint32) Autoarr_define(int32) Autoarr_define(uint64) Autoarr_define(int64) - -ktId_define(AutoarrChar); -ktId_define(AutoarrBool); -ktId_define(AutoarrFloat32); -ktId_define(AutoarrFloat64); -ktId_define(AutoarrInt8); -ktId_define(AutoarrUInt8); -ktId_define(AutoarrInt16); -ktId_define(AutoarrUInt16); -ktId_define(AutoarrInt32); -ktId_define(AutoarrUInt32); -ktId_define(AutoarrInt64); -ktId_define(AutoarrUInt64); - -ktId_define(AutoarrCharPtr); -ktId_define(AutoarrBoolPtr); -ktId_define(AutoarrFloat32Ptr); -ktId_define(AutoarrFloat64Ptr); -ktId_define(AutoarrInt8Ptr); -ktId_define(AutoarrUInt8Ptr); -ktId_define(AutoarrInt16Ptr); -ktId_define(AutoarrUInt16Ptr); -ktId_define(AutoarrInt32Ptr); -ktId_define(AutoarrUInt32Ptr); -ktId_define(AutoarrInt64Ptr); -ktId_define(AutoarrUInt64Ptr); - -Autoarr_define(Unitype) -ktId_define(AutoarrUnitype); -ktId_define(AutoarrUnitypePtr); - -// 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); } \ No newline at end of file diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 3a953c0..e22059a 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -6,6 +6,7 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" +#include "Autoarr_Unitype.h" Autoarr_declare(char) Autoarr_declare(bool) @@ -20,54 +21,6 @@ Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -ktId_declare(AutoarrChar); -ktId_declare(AutoarrBool); -ktId_declare(AutoarrFloat32); -ktId_declare(AutoarrFloat64); -ktId_declare(AutoarrInt8); -ktId_declare(AutoarrUInt8); -ktId_declare(AutoarrInt16); -ktId_declare(AutoarrUInt16); -ktId_declare(AutoarrInt32); -ktId_declare(AutoarrUInt32); -ktId_declare(AutoarrInt64); -ktId_declare(AutoarrUInt64); - -ktId_declare(AutoarrCharPtr); -ktId_declare(AutoarrBoolPtr); -ktId_declare(AutoarrFloat32Ptr); -ktId_declare(AutoarrFloat64Ptr); -ktId_declare(AutoarrInt8Ptr); -ktId_declare(AutoarrUInt8Ptr); -ktId_declare(AutoarrInt16Ptr); -ktId_declare(AutoarrUInt16Ptr); -ktId_declare(AutoarrInt32Ptr); -ktId_declare(AutoarrUInt32Ptr); -ktId_declare(AutoarrInt64Ptr); -ktId_declare(AutoarrUInt64Ptr); - -Autoarr_declare(Unitype) -ktId_declare(AutoarrUnitype); -ktId_declare(AutoarrUnitypePtr); - -// 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 } diff --git a/src/Autoarr/Autoarr_Unitype.c b/src/Autoarr/Autoarr_Unitype.c new file mode 100644 index 0000000..98ece1c --- /dev/null +++ b/src/Autoarr/Autoarr_Unitype.c @@ -0,0 +1,12 @@ +#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 new file mode 100644 index 0000000..cf49c7c --- /dev/null +++ b/src/Autoarr/Autoarr_Unitype.h @@ -0,0 +1,33 @@ +#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_declare.h b/src/Autoarr/Autoarr_declare.h index 404a361..dc16829 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -28,6 +28,8 @@ typedef struct Autoarr_##type{\ __functions_list_t_##type* functions;\ } Autoarr_##type;\ \ +ktid_declare(Autoarr_##type);\ +\ 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); diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index b70ad86..32b665a 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -8,6 +8,8 @@ extern "C" { #define Autoarr_define(type)\ \ +ktid_define(Autoarr_##type);\ +\ void __Autoarr_add_##type(Autoarr_##type* ar, type element){\ if(!ar->values){\ ar->values=malloc(ar->max_blocks_count*sizeof(type*));\ diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index 20107ef..a284e29 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){ case '}': if(!calledRecursively || nameStr.length!=0) safethrow_wrongchar(c,;); - return SUCCESS(UniHeap(ktId_CharPtr,NULL)); + return SUCCESS(UniHeapPtr(char,NULL)); case ':': - return SUCCESS(UniHeap(ktId_CharPtr,string_extract(nameStr))); + return SUCCESS(UniHeapPtr(char,string_extract(nameStr))); case '$': if(nameStr.length!=0) safethrow_wrongchar(c,;); @@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ } if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;); - return SUCCESS(UniHeap(ktId_CharPtr,NULL)); + return SUCCESS(UniHeapPtr(char,NULL)); } #define ReadName() __ReadName(shared) @@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){ } else { char* str=StringBuilder_build(b).ptr; - return SUCCESS(UniHeap(ktId_CharPtr,str)); + return SUCCESS(UniHeapPtr(char,str)); } } else { @@ -157,13 +157,13 @@ Maybe __ReadList(DeserializeSharedData* shared){ try(ReadValue((&readingList)), val, Autoarr_free(list, true)) Autoarr_add(list,val.value); if (!readingList){ - if(val.value.typeId==ktId_Null) + if(val.value.typeId==ktid_Null) Autoarr_pop(list); break; } } - return SUCCESS(UniHeap(ktId_AutoarrUnitypePtr,list)); + return SUCCESS(UniHeapPtr(Autoarr_Unitype,list)); }; #define ReadList() __ReadList(shared) @@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){ case ';': case ',': if(valueStr.length!=0){ - if(value.typeId!=ktId_Null) + if(value.typeId!=ktid_Null) safethrow_wrongchar(c,Unitype_free(value)); try(ParseValue(valueStr),maybeParsed,;) value=maybeParsed.value; @@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { } else{ list=Autoarr_create(Unitype,ARR_BC,ARR_BL); - Hashtable_add(dict,nameCPtr,UniHeap(ktId_AutoarrUnitypePtr,list)); + Hashtable_add(dict,nameCPtr,UniHeapPtr(Autoarr_Unitype,list)); } Autoarr_add(list,val.value); } @@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { END: *_text=text; - return SUCCESS(UniHeap(ktId_HashtablePtr,dict)); + return SUCCESS(UniHeapPtr(Hashtable,dict)); } Maybe DtsodV24_deserialize(char* _text) { diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index cf48d34..f68e706 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -23,18 +23,18 @@ 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_Int64){ + if(u.typeId==ktid_name(int64)){ StringBuilder_append_int64(b,u.Int64); } - else if(u.typeId==ktId_UInt64){ + else if(u.typeId==ktid_name(uint64)){ StringBuilder_append_uint64(b,u.UInt64); addc('u'); } - else if(u.typeId==ktId_Float64){ + else if(u.typeId==ktid_name(float64)){ StringBuilder_append_float64(b,u.Float64); addc('f'); } - else if(u.typeId==ktId_CharPtr){ + else if(u.typeId==ktid_ptrName(char)){ addc('"'); char c; while((c=*(char*)(u.VoidPtr++))){ @@ -43,13 +43,13 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ } addc('"'); } - else if(u.typeId==ktId_Bool){ + else if(u.typeId==ktid_name(bool)){ StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); } - else if(u.typeId==ktId_Null){ + else if(u.typeId==ktid_Null){ safethrow("Null isn't supported in DtsodV24",;); } - else if(u.typeId==ktId_AutoarrUnitypePtr){ + else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){ if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){ addc('\n'); AppendTabs(); @@ -72,7 +72,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ addc(']'); } } - else if(u.typeId==ktId_HashtablePtr){ + else if(u.typeId==ktid_ptrName(Hashtable)){ // check hashtable is blank bool hashtableNotBlank=false; Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ @@ -126,5 +126,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){ StringBuilder* sb=StringBuilder_create(); try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb)); char* str=StringBuilder_build(sb).ptr; - return SUCCESS(UniHeap(ktId_CharPtr, str)); + return SUCCESS(UniHeapPtr(char, str)); } diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c new file mode 100644 index 0000000..840a3f3 --- /dev/null +++ b/src/Filesystem/dir.c @@ -0,0 +1 @@ +#include "dir.h" \ No newline at end of file diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h new file mode 100644 index 0000000..111e4ca --- /dev/null +++ b/src/Filesystem/dir.h @@ -0,0 +1,18 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" +#include "file.h" + +typedef char* DirPath; +Array_declare(DirPath); + +Array_FilePath dir_getFiles(DirPath path); +Array_FilePath dir_findFiles(DirPath path, FilePath searchPattern); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c new file mode 100644 index 0000000..08e53fb --- /dev/null +++ b/src/Filesystem/file.c @@ -0,0 +1,108 @@ +#include "file.h" +#include "../String/StringBuilder.h" + +ktid_define(File); + +char* FileOpenMode_toStr(FileOpenMode m){ + char* p; + switch(m){ + case FileOpenMode_Read: p="rb"; break; + case FileOpenMode_Write: p="wb"; break; + case FileOpenMode_Append: p="ab"; break; + case FileOpenMode_ReadWrite: p="wb+"; break; + case FileOpenMode_ReadAppend: p="ab+"; break; + default: + dbg(m); + throw(ERR_UNEXPECTEDVAL); + } + return p; +} + +Maybe file_open(FilePath path, FileOpenMode mode){ + File* file=fopen(path, FileOpenMode_toStr(mode)); + if(!file) + safethrow(cptr_concat("can't open file ", (char*)path),;); + return SUCCESS(UniHeapPtr(File,file)); +} + +Maybe file_close(File* file){ + if(!file) + safethrow(ERR_NULLPTR,;); + if(fclose(file)) + safethrow(ERR_IO,;); + return MaybeNull; +} + +#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); + ioWriteCheck(); + return MaybeNull; +} + +Maybe file_writeBuffer(File* file, char* buffer, uint64 length){ + int rezult=0; + for(uint64 i=0; i +Maybe file_open(FilePath path, FileOpenMode mode); + +/// @brief closes file descriptor +/// @return Maybe +Maybe file_close(File* file); + +/// @brief closes file descriptor +/// @param byte byte to write +/// @return Maybe +Maybe file_writeChar(File* 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); + +/// @brief writes all cstring array content to file +/// @param cptr zero-terminated cstring +/// @return Maybe +Maybe file_writeCptr(File* file, char* cptr); + + +/// @brief reads single byte from file +/// @return Maybe +Maybe file_readChar(File* 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); + +/// @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); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Filesystem/filesystem.h b/src/Filesystem/filesystem.h index 2bdd4cb..9e68038 100644 --- a/src/Filesystem/filesystem.h +++ b/src/Filesystem/filesystem.h @@ -1,22 +1,3 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "std.h" - -#if defined(_WIN32) || defined (_WIN64) -static const char path_sep='\\'; -#else -static const char path_sep='/'; -#endif - -char* __path_concat(uint16 n, char* path_start, ...); -#define path_concat(PATH_PARTS) __path_concat(count_args(PATH_PARTS), PATH_PARTS) - - - -#if __cplusplus -} -#endif \ No newline at end of file +#include "path.h" +#include "dir.h" +#include "file.h" diff --git a/src/Filesystem/filesystem.c b/src/Filesystem/path.c similarity index 65% rename from src/Filesystem/filesystem.c rename to src/Filesystem/path.c index fe535da..0a8757b 100644 --- a/src/Filesystem/filesystem.c +++ b/src/Filesystem/path.c @@ -1,13 +1,13 @@ #include "filesystem.h" -char* __path_concat(uint16 n, char* prev_part, ...){ +char* __path_concat(uint16 n, ...){ char** parts=(char**)malloc(n*sizeof(char*)); uint32* lengths=malloc(n*sizeof(uint32)); uint32 totalLength=0; // reading args from va_list va_list vl; - va_start(vl, prev_part); + va_start(vl, n); for(uint16 i=0; i uses <..>, that's not allowed"),); + return MaybeNull; +} diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h new file mode 100644 index 0000000..7c3b049 --- /dev/null +++ b/src/Filesystem/path.h @@ -0,0 +1,36 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" + +#if defined(_WIN32) || defined (_WIN64) +static const char path_sep='\\'; +static const char path_notSep='/'; +#else +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 +/// @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); + +#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); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 9a01ba4..edab896 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -1,7 +1,6 @@ #include "Hashtable.h" -ktId_define(Hashtable); -ktId_define(HashtablePtr); +ktid_define(Hashtable); // amount of rows static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; @@ -96,7 +95,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ Unitype u=Hashtable_get(ht,key); *output=u; - return u.typeId!=ktId_Null; + return u.typeId!=ktid_Null; } void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 520c2e4..f0a81ff 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -11,8 +11,7 @@ typedef struct Hashtable{ uint8 hein; // height=HT_HEIGHTS[hein] Autoarr(KVPair)** rows; // Autoarr[height] } Hashtable; -ktId_declare(Hashtable); -ktId_declare(HashtablePtr); +ktid_declare(Hashtable); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 667487e..7e80ede 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -1,11 +1,8 @@ #include "KeyValuePair.h" -ktId_define(KVPair); -ktId_define(KVPairPtr); +ktid_define(KVPair); Autoarr_define(KVPair) -ktId_define(AutoarrKVPair); -ktId_define(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index ef6a32b..280f0d6 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,12 +11,9 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; -ktId_declare(KVPair); -ktId_declare(KVPairPtr); +ktid_declare(KVPair); Autoarr_declare(KVPair) -ktId_declare(AutoarrKVPair); -ktId_declare(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 29e772d..2a0617f 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -1,12 +1,11 @@ #include "SearchTree.h" -ktId_define(STNode); -ktId_define(STNodePtr); +ktid_define(STNode); STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; - node->value.typeId=ktId_Null; + node->value.typeId=ktid_Null; node->value.UInt64=0; return node; } diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index a11de86..bc8785f 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -11,8 +11,7 @@ typedef struct SearchTreeNode{ struct SearchTreeNode**** branches; // *STNode[8][8][4] Unitype value; } STNode; -ktId_declare(STNode); -ktId_declare(STNodePtr); +ktid_declare(STNode); STNode* STNode_create(); void STNode_free(STNode* node); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 8f7cff5..e3c3244 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -1,11 +1,8 @@ #include "StringBuilder.h" Autoarr_define(string) -ktId_define(AutoarrString); -ktId_define(AutoarrStringPtr); -ktId_define(StringBuilder); -ktId_define(StringBuilderPtr); +ktid_define(StringBuilder); #define BL_C 32 #define BL_L 1024 diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index a5f13ef..8072cab 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -8,15 +8,12 @@ extern "C" { #include "string.h" Autoarr_declare(string) -ktId_declare(AutoarrString); -ktId_declare(AutoarrStringPtr); typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; -ktId_declare(StringBuilder); -ktId_declare(StringBuilderPtr); +ktid_declare(StringBuilder); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); diff --git a/src/String/string.c b/src/String/string.c index 93c0ed9..81d61de 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,7 +1,6 @@ #include "string.h" -ktId_define(string); -ktId_define(stringPtr); +ktid_define(string); // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ diff --git a/src/String/string.h b/src/String/string.h index caf5e73..0b8716e 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -10,10 +10,9 @@ extern "C" { // doesn't store '\0' at the end typedef struct string{ char* ptr; // char pointer - uint32 length; // amount of chars in ptr value + uint64 length; // amount of chars in ptr value } string; -ktId_declare(string); -ktId_declare(stringPtr); +ktid_declare(string); static const string stringNull={NULL,0}; diff --git a/src/base/cptr.c b/src/base/cptr.c index 0086edd..f098fdc 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -48,6 +48,19 @@ bool cptr_endsWith(char* ptr, char* fragment){ return true; } +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; + } + } + return -1; +} + void memcopy(void* from, void* to, uint32 size){ if(from==NULL || to==NULL) throw(ERR_NULLPTR); diff --git a/src/base/cptr.h b/src/base/cptr.h index bcf3252..255ee0a 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -22,6 +22,17 @@ bool cptr_startsWith(char* ptr, char* fragment); bool cptr_endsWith(char* ptr, char* fragment); +/// @brief search for in +/// @return index of first inclusion or -1 if not found +uint32 cptr_indexOf(char* ptr, char* fragment); + +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; +} + void memcopy(void* from, void* to, uint32 size); char* __cptr_concat(uint16 n, ...); diff --git a/src/base/errors.c b/src/base/errors.c index cf49d31..dacf45b 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -15,6 +15,8 @@ char* errname(ErrorId err){ 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"; default: return "UNKNOWN_ERROR"; } } diff --git a/src/base/errors.h b/src/base/errors.h index 6b59f5d..d3cb5ac 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -11,7 +11,8 @@ typedef enum ErrorId { SUCCESS, // not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, - ERR_KEYNOTFOUND, ERR_FORMAT, ERR_UNEXPECTEDVAL + ERR_KEYNOTFOUND, ERR_FORMAT, ERR_UNEXPECTEDVAL, + ERR_IO, ERR_IO_EOF } ErrorId; char* errname(ErrorId err); @@ -34,7 +35,7 @@ void printMaybe(Maybe e); #define SUCCESS(REZLT) (Maybe){.errmsg=NULL, .value=REZLT} -#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull} +#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); }) diff --git a/src/base/std.h b/src/base/std.h index 6e9f37e..c266406 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -7,7 +7,6 @@ extern "C" { #include #include #include -#include #include #include #include @@ -23,6 +22,15 @@ 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; +#define true 1 +#define false 0 +#endif #define dbg(N) kprintf("\e[95m%d\n",N) diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index 356cd5d..2606a85 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -4,18 +4,16 @@ 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. +Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file. Example: ```c //someStruct.h typedef struct { } someStruct; -ktId_declare(someStruct); -ktId_declare(someStructPtr); // pointer to type is another type +ktid_declare(someStruct); ``` ```c //someStruct.c -ktId_define(someStruct); -ktId_define(someStructPtr); +ktid_define(someStruct); ``` ## type descriptors diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 02be90f..61b7085 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -3,14 +3,18 @@ #include "../../kprint/kprint_format.h" char* __toString_char(void* c, uint32 fmt) { - char* cc=malloc(2); - cc[0]=*(char*)c; - cc[1]=0; - return cc; -} - -char* __toString_charPtr(void* c, uint32 fmt){ - return cptr_copy(*(char**)c); + //*c=char + if(kprint_format_dataFormat(fmt)==kprint_fmtChar){ + char* cc=malloc(2); + cc[0]=*(char*)c; + cc[1]=0; + return cc; + } + // *c=cstring + else if(kprint_format_dataFormat(fmt)==kprint_fmtString){ + return cptr_copy(*(char**)c); + } + else throw(ERR_FORMAT); } char* __toString_bool(void* c, uint32 fmt) { diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index 6fa335b..bc3f58b 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -7,8 +7,8 @@ extern "C" { #include "../errors.h" // functions for base types +// has different output for fmtChar and fmtString char* __toString_char(void* c, uint32 fmt); -char* __toString_charPtr(void* c, uint32 fmt); char* __toString_bool(void* c, uint32 fmt); char* toString_int(int64 n); diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 729da9f..037da38 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -8,127 +8,80 @@ void ktDescriptors_initKerepTypes(){ // null - kt_register(NULL, ktId_Null, NULL, NULL); + __kt_register("Null", sizeof(NULL), NULL, NULL); + ktid_Null=ktid_last; // base types - kt_register(char, ktId_Char, NULL, __toString_char); - kt_register(bool, ktId_Bool, NULL, __toString_bool); - kt_register(float32, ktId_Float32, NULL, __toString_float32); - kt_register(float64, ktId_Float64, NULL, __toString_float64); - kt_register(int8, ktId_Int8, NULL, __toString_int8); - kt_register(uint8, ktId_UInt8, NULL, __toString_uint8); - kt_register(int16, ktId_Int16, NULL, __toString_int16); - kt_register(uint16, ktId_UInt16, NULL, __toString_uint16); - kt_register(int32, ktId_Int32, NULL, __toString_int32); - kt_register(uint32, ktId_UInt32, NULL, __toString_uint32); - kt_register(int64, ktId_Int64, NULL, __toString_int64); - kt_register(uint64, ktId_UInt64, NULL, __toString_uint64); - // base type pointers - kt_register(char*, ktId_CharPtr, NULL, __toString_charPtr); - kt_register(bool*, ktId_BoolPtr, NULL, NULL); - kt_register(float32*, ktId_Float32Ptr, NULL, NULL); - kt_register(float64*, ktId_Float64Ptr, NULL, NULL); - kt_register(int8*, ktId_Int8Ptr, NULL, NULL); - kt_register(uint8*, ktId_UInt8Ptr, NULL, NULL); - kt_register(int16*, ktId_Int16Ptr, NULL, NULL); - kt_register(uint16*, ktId_UInt16Ptr, NULL, NULL); - kt_register(int32*, ktId_Int32Ptr, NULL, NULL); - kt_register(uint32*, ktId_UInt32Ptr, NULL, NULL); - kt_register(int64*, ktId_Int64Ptr, NULL, NULL); - kt_register(uint64*, ktId_UInt64Ptr, NULL, NULL); + 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); // ktDescriptor - kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); - kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL); + kt_register(ktDescriptor, NULL, NULL); // base type arrays - kt_register(Array_char, ktId_ArrayChar, Array_char_freeValues, NULL); - kt_register(Array_bool, ktId_ArrayBool, Array_bool_freeValues, NULL); - kt_register(Array_float32, ktId_ArrayFloat32, Array_float32_freeValues, NULL); - kt_register(Array_float64, ktId_ArrayFloat64, Array_float64_freeValues, NULL); - kt_register(Array_int8, ktId_ArrayInt8, Array_int8_freeValues, NULL); - kt_register(Array_uint8, ktId_ArrayUInt8, Array_uint8_freeValues, NULL); - kt_register(Array_int16, ktId_ArrayInt16, Array_int16_freeValues, NULL); - kt_register(Array_uint16, ktId_ArrayUInt16, Array_uint16_freeValues, NULL); - kt_register(Array_int32, ktId_ArrayInt32, Array_int32_freeValues, NULL); - kt_register(Array_uint32, ktId_ArrayUInt32, Array_uint32_freeValues, NULL); - kt_register(Array_int64, ktId_ArrayInt64, Array_int64_freeValues, NULL); - kt_register(Array_uint64, ktId_ArrayUInt64, Array_uint64_freeValues, NULL); - // base type array pointers - kt_register(Array_char*, ktId_ArrayCharPtr, Array_char_freeValues, NULL); - kt_register(Array_bool*, ktId_ArrayBoolPtr, Array_bool_freeValues, NULL); - kt_register(Array_float32*, ktId_ArrayFloat32Ptr, Array_float32_freeValues, NULL); - kt_register(Array_float64*, ktId_ArrayFloat64Ptr, Array_float64_freeValues, NULL); - kt_register(Array_int8*, ktId_ArrayInt8Ptr, Array_int8_freeValues, NULL); - kt_register(Array_uint8*, ktId_ArrayUInt8Ptr, Array_uint8_freeValues, NULL); - kt_register(Array_int16*, ktId_ArrayInt16Ptr, Array_int16_freeValues, NULL); - kt_register(Array_uint16*, ktId_ArrayUInt16Ptr, Array_uint16_freeValues, NULL); - kt_register(Array_int32*, ktId_ArrayInt32Ptr, Array_int32_freeValues, NULL); - kt_register(Array_uint32*, ktId_ArrayUInt32Ptr, Array_uint32_freeValues, NULL); - kt_register(Array_int64*, ktId_ArrayInt64Ptr, Array_int64_freeValues, NULL); - kt_register(Array_uint64*, ktId_ArrayUInt64Ptr, Array_uint64_freeValues, NULL); + 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); // base type autoarrs - kt_register(Autoarr_char, ktId_AutoarrChar, ____Autoarr_free_char, NULL); - kt_register(Autoarr_bool, ktId_AutoarrBool, ____Autoarr_free_bool, NULL); - kt_register(Autoarr_float32, ktId_AutoarrFloat32, ____Autoarr_free_float32, NULL); - kt_register(Autoarr_float64, ktId_AutoarrFloat64, ____Autoarr_free_float64, NULL); - kt_register(Autoarr_int8, ktId_AutoarrInt8, ____Autoarr_free_int8, NULL); - kt_register(Autoarr_uint8, ktId_AutoarrUInt8, ____Autoarr_free_uint8, NULL); - kt_register(Autoarr_int16, ktId_AutoarrInt16, ____Autoarr_free_int16, NULL); - kt_register(Autoarr_uint16, ktId_AutoarrUInt16, ____Autoarr_free_uint16, NULL); - kt_register(Autoarr_int32, ktId_AutoarrInt32, ____Autoarr_free_int32, NULL); - kt_register(Autoarr_uint32, ktId_AutoarrUInt32, ____Autoarr_free_uint32, NULL); - kt_register(Autoarr_int64, ktId_AutoarrInt64, ____Autoarr_free_int64, NULL); - kt_register(Autoarr_uint64, ktId_AutoarrUInt64, ____Autoarr_free_uint64, NULL); - // base type autoarr pointers - kt_register(Autoarr_char*, ktId_AutoarrCharPtr, ____Autoarr_free_char, NULL); - kt_register(Autoarr_bool*, ktId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL); - kt_register(Autoarr_float32*, ktId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL); - kt_register(Autoarr_float64*, ktId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL); - kt_register(Autoarr_int8*, ktId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL); - kt_register(Autoarr_uint8*, ktId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL); - kt_register(Autoarr_int16*, ktId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL); - kt_register(Autoarr_uint16*, ktId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL); - kt_register(Autoarr_int32*, ktId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL); - kt_register(Autoarr_uint32*, ktId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL); - kt_register(Autoarr_int64*, ktId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL); - kt_register(Autoarr_uint64*, ktId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL); + 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); // Unitype - kt_register(Unitype, ktId_Unitype, __UnitypePtr_free, NULL); - kt_register(Unitype*, ktId_UnitypePtr, __UnitypePtr_free, NULL); - kt_register(Autoarr_Unitype, ktId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL); - kt_register(Autoarr_Unitype*, ktId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL); + 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); // SearchTreeNode - kt_register(STNode, ktId_STNode, __STNode_free, NULL); - kt_register(STNode*, ktId_STNodePtr, __STNode_free, NULL); + kt_register(STNode, __STNode_free, NULL); // KeyValuePair - kt_register(KVPair, ktId_KVPair, __KVPair_free, NULL); - kt_register(KVPair*, ktId_KVPairPtr, __KVPair_free, NULL); - kt_register(Autoarr_KVPair, ktId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL); - kt_register(Autoarr_KVPair*, ktId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL); + 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); // Hashtable - kt_register(Hashtable, ktId_Hashtable, __Hashtable_free, NULL); - kt_register(Hashtable*, ktId_HashtablePtr, __Hashtable_free, NULL); + kt_register(Hashtable, __Hashtable_free, NULL); // string - kt_register(string, ktId_string, NULL, NULL); - kt_register(string*, ktId_stringPtr, NULL, NULL); - kt_register(string, ktId_AutoarrString, ____Autoarr_free_string, NULL); - kt_register(string*, ktId_AutoarrStringPtr, ____Autoarr_free_string, NULL); + kt_register(string, NULL, NULL); + kt_register(string, ____Autoarr_free_string, NULL); // StringBuilder - kt_register(StringBuilder, ktId_StringBuilder, __StringBuilder_free, NULL); - kt_register(StringBuilder*, ktId_StringBuilderPtr, __StringBuilder_free, NULL); + kt_register(StringBuilder, __StringBuilder_free, NULL); } diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h index a12facb..682b3ca 100644 --- a/src/base/type_system/ktDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -5,14 +5,16 @@ extern "C" { #endif #include "../std.h" -#include "ktId.h" +#include "ktid.h" +typedef void (*freeMembers_t)(void*); +typedef char* (*toString_t)(void* obj, uint32 fmt); typedef struct ktDescriptor{ char* name; - ktId id; + ktid id; uint16 size; - void (*freeMembers)(void*); // NULL or function which frees all struct members - char* (*toString)(void* obj, uint32 fmt); // NULL or function which generates string representaion of object + freeMembers_t freeMembers; // NULL or function which frees all struct members + toString_t toString; // NULL or function which generates string representaion of object } ktDescriptor; #if __cplusplus diff --git a/src/base/type_system/ktId.h b/src/base/type_system/ktId.h index 63f7847..feda10d 100644 --- a/src/base/type_system/ktId.h +++ b/src/base/type_system/ktId.h @@ -5,12 +5,18 @@ extern "C" { #endif #include "../std.h" -typedef uint16 ktId; +typedef uint16 ktid; -#define ktId_declare(TYPE_NAME)\ - extern ktId ktId_##TYPE_NAME -#define ktId_define(TYPE_NAME)\ - ktId ktId_##TYPE_NAME=-1 +#define ktid_name(TYPE) ktid_##TYPE +#define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr + +#define ktid_declare(TYPE)\ + extern ktid ktid_##TYPE;\ + extern ktid ktid_##TYPE##_Ptr; + +#define ktid_define(TYPE)\ + ktid ktid_##TYPE=-1;\ + ktid ktid_##TYPE##_Ptr=-1; #if __cplusplus } diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index fa4a26d..6ead198 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -3,42 +3,28 @@ Autoarr_declare(ktDescriptor) Autoarr_define(ktDescriptor) -ktId_define(Null); +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(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(CharPtr); -ktId_define(BoolPtr); -ktId_define(Float32Ptr); -ktId_define(Float64Ptr); -ktId_define(Int8Ptr); -ktId_define(UInt8Ptr); -ktId_define(Int16Ptr); -ktId_define(UInt16Ptr); -ktId_define(Int32Ptr); -ktId_define(UInt32Ptr); -ktId_define(Int64Ptr); -ktId_define(UInt64Ptr); - -ktId_define(ktDescriptor); -ktId_define(ktDescriptorPtr); +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; +ktid ktid_last=-1; typedef enum{ NotInitialized, Initializing, Initialized @@ -55,22 +41,22 @@ void ktDescriptors_endInit(){ typeDescriptors=Autoarr_toArray(__ktDescriptors); Autoarr_free(__ktDescriptors,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); - kprintf("\e[92minitialized %u type descriptors\n", ktId_last); + 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, + .id=++ktid_last, .freeMembers=freeMembers, .toString=toString }; Autoarr_add(__ktDescriptors, typeDesc); } -ktDescriptor ktDescriptor_get(ktId id){ - if(id>ktId_last) { +ktDescriptor ktDescriptor_get(ktid id){ + if(id>ktid_last) { kprintf("\ntype id: %u\n",id); throw("invalid type id"); } diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index cf27d3e..26bfd47 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -5,55 +5,43 @@ extern "C" { #endif #include "../std.h" -#include "ktId.h" +#include "ktid.h" #include "ktDescriptor.h" -extern ktId ktId_last; +extern ktid ktid_last; void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32)); -#define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ - __kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ - ID_VAR_NAME=ktId_last; +#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);\ + ktid_##TYPE##_Ptr=ktid_last; void ktDescriptors_beginInit(); void ktDescriptors_endInit(); /// @param id id of registered type -ktDescriptor ktDescriptor_get(ktId id); +ktDescriptor ktDescriptor_get(ktid id); // call it to free heap-allocated ktDescriptors array void ktDescriptors_free(); -ktId_declare(Null); +extern ktid ktid_Null; -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(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(CharPtr); -ktId_declare(BoolPtr); -ktId_declare(Float32Ptr); -ktId_declare(Float64Ptr); -ktId_declare(Int8Ptr); -ktId_declare(UInt8Ptr); -ktId_declare(Int16Ptr); -ktId_declare(UInt16Ptr); -ktId_declare(Int32Ptr); -ktId_declare(UInt32Ptr); -ktId_declare(Int64Ptr); -ktId_declare(UInt64Ptr); - -ktId_declare(ktDescriptor); -ktId_declare(ktDescriptorPtr); +ktid_declare(ktDescriptor); #if __cplusplus } diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h index 2c20ac0..f5e6b1a 100644 --- a/src/base/type_system/type_system.h +++ b/src/base/type_system/type_system.h @@ -1,5 +1,5 @@ #include "init.h" -#include "ktId.h" +#include "ktid.h" #include "ktDescriptor.h" #include "kt_functions.h" #include "unitype.h" diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index 59892fb..46fe530 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,7 +1,6 @@ #include "../base.h" -ktId_define(Unitype); -ktId_define(UnitypePtr); +ktid_define(Unitype); void Unitype_free(Unitype u){ ktDescriptor type=ktDescriptor_get(u.typeId); @@ -29,15 +28,15 @@ char* toString_Unitype(void* _u, uint32 fmt){ char* sprintuni(Unitype v){ char* buf=malloc(BUFSIZE); ktDescriptor type=ktDescriptor_get(v.typeId); - if(v.typeId==ktId_Null) + if(v.typeId==ktid_Null) sprintf_s(buf, BUFSIZE, "{Null}"); - else if(v.typeId==ktId_Float64) + else if(v.typeId==ktid_name(float64)) sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64); - else if(v.typeId==ktId_Bool || v.typeId==ktId_UInt64) + 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_Int64) + else if(v.typeId==ktid_name(int64)) sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64); - else if(v.typeId==ktId_CharPtr){ + 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); diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index 666ee3e..e86eedc 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "ktId.h" +#include "ktid.h" typedef struct Unitype{ union { @@ -15,27 +15,26 @@ typedef struct Unitype{ void* VoidPtr; char Bytes[8]; }; - ktId typeId; + ktid typeId; bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* } Unitype; -ktId_declare(Unitype); -ktId_declare(UnitypePtr); +ktid_declare(Unitype); -#define __UniDef(TYPE, VAL) (Unitype){\ - .TYPE=VAL, .typeId=ktId_##TYPE, .allocatedInHeap=false} +#define __UniDef(FIELD, TYPE, VAL) (Unitype){\ + .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false} -#define UniInt64(VAL) __UniDef(Int64, VAL) -#define UniUInt64(VAL) __UniDef(UInt64, VAL) -#define UniFloat64(VAL) __UniDef(Float64, VAL) -#define UniBool(VAL) __UniDef(Bool, VAL) +#define UniInt64(VAL) __UniDef(Int64, int64, VAL) +#define UniUInt64(VAL) __UniDef(UInt64, uint64, VAL) +#define UniFloat64(VAL) __UniDef(Float64, float64, VAL) +#define UniBool(VAL) __UniDef(Bool, bool, VAL) -#define UniStack(ID_VAR_NAME, VAL) (Unitype){\ - .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=false} -#define UniHeap(ID_VAR_NAME, VAL) (Unitype){\ - .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true} +#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 UniStack(ktId_Null, NULL) +#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false} #define UniTrue UniBool(true) #define UniFalse UniBool(false) diff --git a/src/kprint/README.md b/src/kprint/README.md index c59a66e..ec7e124 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -13,11 +13,11 @@ I don't really like printf function (and its variants), so i made safer and more ## how to use it: + **format construction:** ``` - kprint_format fmt= kprint_fgColor | kprint_bgColor | kprint_fdataFmt | flags | ktId; + kprint_format fmt= kprint_fgColor | kprint_bgColor | kprint_fdataFmt | flags | ktid; ``` [more about `kprint_format`](kprint_format.md) + fgColor and bgColor can be set to change console output color - + you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and ktId for other types + + you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and ktid for other types + flags can be set to modify TypeDescriptor.toString() behavior + don't forget to set TypeDescriptor.toString when registering type, or kprint will crash @@ -31,6 +31,6 @@ I don't really like printf function (and its variants), so i made safer and more should be sent as pointers ``` Maybe m=MaybeNull; - kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | ktId_MaybePtr, &m); + kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | ktid_MaybePtr, &m); ``` - output: Maybe: {value={0, ktId_Null}} \ No newline at end of file + output: Maybe: {value={0, ktid_Null}} \ No newline at end of file diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 97186fb..bc3421f 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -1,23 +1,23 @@ #include "../String/StringBuilder.h" #include "kprint.h" -ktId __typeFromFormat(kprint_format f){ - ktId typeId=kprint_format_ktId(f); +ktid __typeFromFormat(kprint_format f){ + ktid typeId=kprint_format_ktid(f); if(typeId) return typeId; switch(kprint_format_dataFormat(f)){ case kprint_fmtInt: case kprint_fmtHex: case kprint_fmtBin: - return ktId_Int64; + return ktid_name(int64); case kprint_fmtUInt: - return ktId_UInt64; + return ktid_name(uint64); case kprint_fmtFloat: - return ktId_Float64; + return ktid_name(float64); case kprint_fmtChar: - return ktId_Char; + return ktid_char; case kprint_fmtString: - return ktId_CharPtr; + return ktid_ptrName(char); default: return -1; } @@ -25,13 +25,13 @@ ktId __typeFromFormat(kprint_format f){ Maybe __next_toString(kprint_format f, __kprint_value_union* object){ // detecting type - ktId typeId=__typeFromFormat(f); + ktid typeId=__typeFromFormat(f); if(typeId==-1) safethrow("typeId is not set, can't autodetect type",;); ktDescriptor typeDesc=ktDescriptor_get(typeId); if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); - return SUCCESS(UniHeap(ktId_CharPtr, typeDesc.toString(object, f))); + return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f))); } Maybe __ksprint(uint8 n, kprint_format* formats, __kprint_value_union* objects){ @@ -43,7 +43,7 @@ Maybe __ksprint(uint8 n, kprint_format* formats, __kprint_value_union* objects){ Unitype_free(mStr.value); } char* rezult=StringBuilder_build(strb).ptr; - return SUCCESS(UniHeap(ktId_CharPtr, rezult)); + return SUCCESS(UniHeapPtr(char, rezult)); } Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, __kprint_value_union* objects){ @@ -150,7 +150,7 @@ void kprint_setColor(kprint_format f){ } #endif -/* Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){ +/* Maybe ksprint_ar(uint32 count, kprint_format format, ktid typeId, void* array){ ktDescriptor typeDesc=ktDescriptor_get(format.typeId); if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h index 0a275ea..a9c5db8 100644 --- a/src/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "../base/std.h" -#include "../base/type_system/ktId.h" +#include "../base/type_system/ktid.h" typedef enum kprint_dataFormat{ // 00000000 00000000 00000000 00000000 @@ -41,7 +41,7 @@ typedef uint32 kprint_format; #define kprint_format_fgColor(FMT) (kprint_fgColor)(FMT&0x8f000000) #define kprint_format_bgColor(FMT) (kprint_bgColor)(FMT&0x40f00000) #define kprint_format_dataFormat(FMT) (kprint_dataFormat)(FMT&0x000f0000) -#define kprint_format_ktId(FMT) (kprint_dataFormat)(FMT&0x0000ffff) +#define kprint_format_ktid(FMT) (kprint_dataFormat)(FMT&0x0000ffff) #if __cplusplus } diff --git a/src/kprint/kprint_format.md b/src/kprint/kprint_format.md index d47cfd1..14edb48 100644 --- a/src/kprint/kprint_format.md +++ b/src/kprint/kprint_format.md @@ -2,7 +2,7 @@ ``` 00000000 00000000 00000000 00000000 -fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktId +fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktid bgColorSet─┘││ │ bgColor └data format prefix┬────┘│ └fgColor postfix └uppercase diff --git a/tests/main.cpp b/tests/main.cpp index 6338d3b..1cd39e0 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -20,9 +20,8 @@ int main(){ ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); kprintf("\e[97mkerep tests are starting!\n"); - //optime("test_all",1,test_all()); + optime("test_all",1,test_all()); ktDescriptors_free(); - kprintf("ъъъъ"); kprintf("\e[0m\n"); return 0; } diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index cc34f74..3fc6747 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -18,7 +18,7 @@ void print_dtsod(Hashtable* dtsod){ kprintf("\e[92m"); Hashtable_foreach(dtsod, p,({ printkvp(p); - if(p.value.typeId==ktId_HashtablePtr){ + if(p.value.typeId==ktid_ptrName(Hashtable)){ kprintf(": {\n"); Hashtable* sub=p.value.VoidPtr; Hashtable_foreach(sub, _p,({ diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c index a3646e6..e29835c 100644 --- a/tests/test_marshalling.c +++ b/tests/test_marshalling.c @@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ KVPair* k=malloc(sizeof(KVPair)); k->key="message"; char* tc=cptr_copy(text); - Unitype u=UniHeap(ktId_CharPtr,tc); + Unitype u=UniHeapPtr(char,tc); k->value=u; *kptr=k; } diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index c7312e8..656a96a 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -44,11 +44,11 @@ void test_searchtree(){ printuni(u); ST_push(node,"channel_id", u); kprintf(" -> channel_id\n "); - u=UniHeap(ktId_CharPtr, cptr_copy("32.2004")); + u=UniHeapPtr(char, cptr_copy("32.2004")); printuni(u); ST_push(node,"message_id", u); kprintf(" -> message_id\n "); - u=UniStack(ktId_CharPtr,"some text UwU"); + u=UniStackPtr(char,"some text UwU"); printuni(u); ST_push(node,"text", u); kprintf(" -> text\n");