From c1d004f411c5c2963bbbcb6172f08ad55188ed88 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 28 Mar 2022 00:32:46 +0300 Subject: [PATCH] visual studio project --- .gitignore | 17 +- Autoarr/Autoarr.h | 34 ++- Autoarr/Autoarr_declare.h | 10 +- Autoarr/Autoarr_define.h | 8 + Autoarr/StringBuilder.h | 26 +- DtsodParser/DtsodV24.h | 4 +- DtsodParser/DtsodV24_deserialize.c | 468 +++++++++++++++-------------- DtsodParser/DtsodV24_exported.c | 45 +++ DtsodParser/DtsodV24_serialize.c | 160 +++++----- Hashtable/Hashtable.h | 8 + Hashtable/KeyValuePair.h | 10 + Hashtable/hash.h | 8 + Makefile | 20 +- README.md | 2 +- SearchTree/SearchTree.h | 8 + base/base.h | 18 +- base/errors.h | 8 + base/mystr.c | 2 +- base/mystr.h | 10 +- base/std.h | 36 +++ base/types.c | 14 +- base/types.h | 19 +- kerep.sln | 31 ++ kerep.vcxproj | 246 +++++++++++++++ kerep.vcxproj.filters | 129 ++++++++ tests/main.c | 10 - tests/test_autoarr.c | 5 +- tests/test_hashtable.c | 5 +- tests/test_marshalling.c | 14 + tests/test_marshalling.h | 13 - tests/test_searchtree.c | 4 +- tests/test_string.c | 4 +- 32 files changed, 1010 insertions(+), 386 deletions(-) create mode 100644 DtsodParser/DtsodV24_exported.c create mode 100644 kerep.sln create mode 100644 kerep.vcxproj create mode 100644 kerep.vcxproj.filters create mode 100644 tests/test_marshalling.c delete mode 100644 tests/test_marshalling.h diff --git a/.gitignore b/.gitignore index 1e6cd00..7df5964 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,12 @@ # Build results -[Bb]in/ +bin/ +bin-*/ .bin/ -[Dd]ebug/ -[Rr]elease/ -[Rr]eleases/ -[Oo]bj/ -[Oo]ut/ -[Ll]og/ -[Ll]ogs/ +obj/ +obj-*/ +log/ +logs/ +logs-*/ # IDE files .vs/ @@ -18,4 +17,4 @@ *.user #backups -.old*/ \ No newline at end of file +.old*/ diff --git a/Autoarr/Autoarr.h b/Autoarr/Autoarr.h index aefaf0d..9589db9 100644 --- a/Autoarr/Autoarr.h +++ b/Autoarr/Autoarr.h @@ -1,22 +1,26 @@ #pragma once +#if __cplusplus +extern "C" { +#endif + #include "Autoarr_declare.h" #include "Autoarr_define.h" -declare_Autoarr(uint8) -declare_Autoarr(int8) -declare_Autoarr(uint16) -declare_Autoarr(int16) -declare_Autoarr(uint32) -declare_Autoarr(int32) -declare_Autoarr(uint64) -declare_Autoarr(int64) -declare_Autoarr(float) -declare_Autoarr(double) -declare_Autoarr(Unitype) + declare_Autoarr(uint8) + declare_Autoarr(int8) + declare_Autoarr(uint16) + declare_Autoarr(int16) + declare_Autoarr(uint32) + declare_Autoarr(int32) + declare_Autoarr(uint64) + declare_Autoarr(int64) + declare_Autoarr(float) + declare_Autoarr(double) + declare_Autoarr(Unitype) -//right func to clear array of unitype values -void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); + //right func to clear array of unitype values + void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); #define Autoarr_foreach(ar,elem,codeblock)({\ if(ar->blocks_count>0) {\ @@ -32,3 +36,7 @@ void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); }\ }\ }) + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/Autoarr/Autoarr_declare.h b/Autoarr/Autoarr_declare.h index 9686a4a..144b190 100644 --- a/Autoarr/Autoarr_declare.h +++ b/Autoarr/Autoarr_declare.h @@ -1,5 +1,9 @@ #pragma once +#if __cplusplus +extern "C" { +#endif + #include "../base/base.h" #define declare_Autoarr(type)\ @@ -50,7 +54,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block 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_remove(AR){\ if(AR->block_length==1){\ AR->blocks_count--;\ @@ -59,3 +63,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block }\ else AR->block_length--;\ } + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/Autoarr/Autoarr_define.h b/Autoarr/Autoarr_define.h index a02d2ff..dc3b5c1 100644 --- a/Autoarr/Autoarr_define.h +++ b/Autoarr/Autoarr_define.h @@ -1,5 +1,9 @@ #pragma once +#if __cplusplus +extern "C" { +#endif + #include "../base/base.h" #define define_Autoarr(type)\ @@ -62,3 +66,7 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block .functions=&__functions_list_##type\ };\ } + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/Autoarr/StringBuilder.h b/Autoarr/StringBuilder.h index 2a62558..0b52429 100644 --- a/Autoarr/StringBuilder.h +++ b/Autoarr/StringBuilder.h @@ -1,14 +1,22 @@ #pragma once +#if __cplusplus +extern "C" { +#endif + #include "Autoarr.h" -typedef Autoarr(int8) StringBuilder; + typedef Autoarr(int8) StringBuilder; -StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length); -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_double(StringBuilder* b, double a); -char* StringBuilder_build(StringBuilder* b); + StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length); + 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_double(StringBuilder* b, double a); + char* StringBuilder_build(StringBuilder* b); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/DtsodParser/DtsodV24.h b/DtsodParser/DtsodV24.h index 7500c60..5b0fa6c 100644 --- a/DtsodParser/DtsodV24.h +++ b/DtsodParser/DtsodV24.h @@ -1,9 +1,9 @@ +#pragma once + #if __cplusplus extern "C" { #endif -#pragma once -#include "../base/base.h" #include "../Hashtable/Hashtable.h" //parses text to binary values diff --git a/DtsodParser/DtsodV24_deserialize.c b/DtsodParser/DtsodV24_deserialize.c index 3a6f109..ee8d3a5 100644 --- a/DtsodParser/DtsodV24_deserialize.c +++ b/DtsodParser/DtsodV24_deserialize.c @@ -6,234 +6,259 @@ #define STRB_BC 64 #define STRB_BL 1024 -Hashtable* __deserialize(char** _text, bool calledRecursively){ - Hashtable* dict=Hashtable_create(); - char* text=*_text; +void __throw_wrongchar(char* file, int line, char* fname, char _c, char* _text) { + char errBuf[]="unexpected at:\n \"" + "00000000000000000000000000000000\""; + errBuf[12]=_c; + for (uint8 i=0; i < 32; i++) + errBuf[i + 22]=*(_text - 16 + i); + printf("\n\e[91m[%s:%d %s] throwed error: %s\n", file, line, fname, errBuf); + exit(128); +}; +#define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C, text) + + +typedef struct DeserializeSharedData{ + char* sh_text; + bool sh_partOfDollarList; + bool sh_readingList; + bool sh_calledRecursively; +} DeserializeSharedData; +#define text shared->sh_text +#define partOfDollarList shared->sh_partOfDollarList +#define readingList shared->sh_readingList +#define calledRecursively shared->sh_calledRecursively + +void __SkipComment(DeserializeSharedData* shared) { char c; - bool partOfDollarList=false; - bool readingList=false; + while ((c=*++text) != '\n') + if (!c) throw(ERR_ENDOFSTR); +}; +#define SkipComment() __SkipComment(shared) - void __throw_wrongchar(char* file, int line, char* fname,char _c){ - char errBuf[]="unexpected at:\n \"" - "00000000000000000000000000000000\""; - errBuf[12]=_c; - for(uint8 i=0;i<32;i++) - errBuf[i+22]=*(text-16+i); - printf("\n\e[91m[%s:%d %s] throwed error: %s\n",file,line,fname,errBuf); - exit(128); +string __ReadName(DeserializeSharedData* shared){ + char c; + string nameStr={text,0}; + text--; + while ((c=*++text)) switch (c){ + case ' ': case '\t': + case '\r': case '\n': + if(nameStr.length!=0) + throw_wrongchar(c); + nameStr.ptr++; + break; + case '=': case ';': + case '\'': case '"': + case '[': case ']': + case '{': + throw_wrongchar(c); + break; + case '#': + SkipComment(); + if(nameStr.length!=0) + throw_wrongchar(c); + nameStr.ptr=text+1; //skips '\n' + break; + case '}': + if(!calledRecursively) throw_wrongchar(c); + if((*++text)!=';') + throw_wrongchar(c); + case ':': + return nameStr; + case '$': + if(nameStr.length!=0) + throw_wrongchar(c); + nameStr.ptr++; + partOfDollarList=true; + break; + default: + nameStr.length++; + break; + } + + if(nameStr.length>0) throw(ERR_ENDOFSTR); + return nameStr; +}; +#define ReadName() __ReadName(shared) + +Hashtable* __deserialize(char** _text, bool _calledRecursively); +Unitype __ReadValue(DeserializeSharedData* shared); +#define ReadValue() __ReadValue(shared) + +//returns part of without quotes +char* __ReadString(DeserializeSharedData* shared){ + char c; + bool prevIsBackslash=false; + StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL); + StringBuilder* b=&_b; + while ((c=*++text)){ + if(c=='"') { + if(prevIsBackslash) { + //replacing <\"> with <"> + Autoarr_remove(b); + StringBuilder_append_char(b,c); + } + else { + char* str=StringBuilder_build(b); + Autoarr_clear(b); + return str; + } + } + else { + prevIsBackslash= c=='\\' && !prevIsBackslash; + StringBuilder_append_char(b,c); + } + } + throw(ERR_ENDOFSTR); + return NULL; +}; +#define ReadString() __ReadString(shared) + +Autoarr(Unitype)* __ReadList(DeserializeSharedData* shared){ + Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype))); + *list=Autoarr_create(Unitype,ARR_BC,ARR_BL); + readingList=true; + while (true){ + Autoarr_add(list,ReadValue()); + if (!readingList) break; + } + return list; +}; +#define ReadList() __ReadList(shared) + +Unitype __ParseValue(DeserializeSharedData* shared, string str){ + //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCptr(str)); + const string nullStr={"null",4}; + const string trueStr={"true",4}; + const string falseStr={"false",5}; + switch(*str.ptr){ + case 'n': + if(string_compare(str,nullStr)) + return UniNull; + else throw_wrongchar(*str.ptr); + break; + case 't': + if(string_compare(str,trueStr)) + return UniTrue; + else throw_wrongchar(*str.ptr); + break; + case 'f': + if(string_compare(str,falseStr)) + return UniFalse; + else throw_wrongchar(*str.ptr); + break; + default: + switch(str.ptr[str.length-1]){ + case 'f': { + char* _c=string_cpToCptr(str); + Unitype rez=Uni(Double,strtod(_c,NULL)); + free(_c); + return rez; + } + case 'u': { + uint64 lu=0; + char* _c=string_cpToCptr(str); + sscanf(_c,"%lu",&lu); + free(_c); + return Uni(UInt64,lu); + } + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': { + int64 li=0; + char* _c=string_cpToCptr(str); + if(sscanf(_c,"%li",&li)!=1){ + char err[64]; + sprintf(err,"can't parse to int: <%s>",_c); + throw(err); + } + + free(_c); + return Uni(Int64,li); + } + default: + throw_wrongchar(str.ptr[str.length-1]); + } + } + throw(ERR_ENDOFSTR); + return UniNull; +}; +#define ParseValue(str) __ParseValue(shared, str) + +Unitype __ReadValue(DeserializeSharedData* shared){ + char c; + string valueStr={text+1,0}; + Unitype value; + while ((c=*++text)) switch (c){ + case ' ': case '\t': + case '\r': case '\n': + if(valueStr.length!=0) + throw_wrongchar(c); + valueStr.ptr++; + break; + case '=': case ':': + case '}': case '$': + throw_wrongchar(c); + break; + case '#':; + char _c=c; + SkipComment(); + if(valueStr.length!=0) + throw_wrongchar(_c); + valueStr.ptr=text+1; //skips '\n' + break; + case '"': + if(valueStr.length!=0) throw_wrongchar(c); + value=UniPtr(CharPtr,ReadString()); + break; + case '\'': + if(valueStr.length!=0) throw_wrongchar(c); + char valueChar=*++text; + if (*++text != '\'') throw("after <'> should be char"); + value=Uni(Char,valueChar); + break; + case '[': + if(valueStr.length!=0) throw_wrongchar(c); + value=UniPtr(AutoarrUnitypePtr,ReadList()); + case ']': + readingList=false; + break; + case '{': + if(valueStr.length!=0) throw_wrongchar(c); + ++text; //skips '{' + value=UniPtr(HashtablePtr, __deserialize(&text,true)); + return value; + case ';': + case ',': + if(valueStr.length!=0) + value=ParseValue(valueStr); + return value; + default: + valueStr.length++; + break; + } + + throw(ERR_ENDOFSTR); + return UniNull; +}; + + +Hashtable* __deserialize(char** _text, bool _calledRecursively) { + DeserializeSharedData _shared={ + .sh_text=*_text, + .sh_partOfDollarList=false, + .sh_readingList=false, + .sh_calledRecursively=_calledRecursively }; - #define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C) + DeserializeSharedData* shared=&_shared; + Hashtable* dict=Hashtable_create(); + char c; - - void SkipComment(){ - while((c=*++text)!='\n') - if(!c) throw(ERR_ENDOFSTR); - }; - - string ReadName(){ - string nameStr={text,0}; - text--; - while ((c=*++text)) switch (c){ - case ' ': case '\t': - case '\r': case '\n': - if(nameStr.length!=0) - throw_wrongchar(c); - nameStr.ptr++; - break; - case '=': case ';': - case '\'': case '"': - case '[': case ']': - case '{': - throw_wrongchar(c); - break; - case '#': - SkipComment(); - if(nameStr.length!=0) - throw_wrongchar(c); - nameStr.ptr=text+1; //skips '\n' - break; - case '}': - if(!calledRecursively) throw_wrongchar(c); - if((*++text)!=';') - throw_wrongchar(c); - case ':': - return nameStr; - case '$': - if(nameStr.length!=0) - throw_wrongchar(c); - nameStr.ptr++; - partOfDollarList=true; - break; - default: - nameStr.length++; - break; - } - - if(nameStr.length>0) throw(ERR_ENDOFSTR); - return nameStr; - }; - - Unitype ReadValue(){ - //returns part of without quotes - char* ReadString(){ - bool prevIsBackslash=false; - StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL); - StringBuilder* b=&_b; - while ((c=*++text)){ - if(c=='"') { - if(prevIsBackslash) { - //replacing <\"> with <"> - Autoarr_remove(b); - StringBuilder_append_char(b,c); - } - else { - char* str=StringBuilder_build(b); - Autoarr_clear(b); - return str; - } - } - else { - prevIsBackslash= c=='\\' && !prevIsBackslash; - StringBuilder_append_char(b,c); - } - } - throw(ERR_ENDOFSTR); - return NULL; - }; - - Autoarr(Unitype)* ReadList(){ - Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype))); - *list=Autoarr_create(Unitype,ARR_BC,ARR_BL); - readingList=true; - while (true){ - Autoarr_add(list,ReadValue()); - if (!readingList) break; - } - return list; - }; - - Hashtable* ReadDtsod(){ - ++text; //skips '{' - return __deserialize(&text,true); - } - - Unitype ParseValue(string str){ - //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCharPtr(str)); - const string nullStr={"null",4}; - const string trueStr={"true",4}; - const string falseStr={"false",5}; - switch(*str.ptr){ - case 'n': - if(string_compare(str,nullStr)) - return UniNull; - else throw_wrongchar(*str.ptr); - break; - case 't': - if(string_compare(str,trueStr)) - return UniTrue; - else throw_wrongchar(*str.ptr); - break; - case 'f': - if(string_compare(str,falseStr)) - return UniFalse; - else throw_wrongchar(*str.ptr); - break; - default: - switch(str.ptr[str.length-1]){ - case 'f': { - char* _c=string_cpToCharPtr(str); - Unitype rez=Uni(Double,strtod(_c,NULL)); - free(_c); - return rez; - } - case 'u': { - uint64 lu=0; - char* _c=string_cpToCharPtr(str); - sscanf(_c,"%lu",&lu); - free(_c); - return Uni(UInt64,lu); - } - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': { - int64 li=0; - char* _c=string_cpToCharPtr(str); - if(sscanf(_c,"%li",&li)!=1){ - char err[64]; - sprintf(err,"can't parse to int: <%s>",_c); - throw(err); - } - - free(_c); - return Uni(Int64,li); - } - default: - throw_wrongchar(str.ptr[str.length-1]); - } - } - throw(ERR_ENDOFSTR); - return UniNull; - }; - - string valueStr={text+1,0}; - Unitype value; - while ((c=*++text)) switch (c){ - case ' ': case '\t': - case '\r': case '\n': - if(valueStr.length!=0) - throw_wrongchar(c); - valueStr.ptr++; - break; - case '=': case ':': - case '}': case '$': - throw_wrongchar(c); - break; - case '#':; - char _c=c; - SkipComment(); - if(valueStr.length!=0) - throw_wrongchar(_c); - valueStr.ptr=text+1; //skips '\n' - break; - case '"': - if(valueStr.length!=0) throw_wrongchar(c); - value=UniPtr(CharPtr,ReadString()); - break; - case '\'': - if(valueStr.length!=0) throw_wrongchar(c); - char valueChar=*++text; - if (*++text != '\'') throw("after <'> should be char"); - value=Uni(Char,valueChar); - break; - case '[': - if(valueStr.length!=0) throw_wrongchar(c); - value=UniPtr(AutoarrUnitypePtr,ReadList()); - case ']': - readingList=false; - break; - case '{': - if(valueStr.length!=0) throw_wrongchar(c); - value=UniPtr(HashtablePtr,ReadDtsod()); - return value; - case ';': - case ',': - if(valueStr.length!=0) - value=ParseValue(valueStr); - return value; - default: - valueStr.length++; - break; - } - - throw(ERR_ENDOFSTR); - return UniNull; - }; - text--; while((c=*++text)){ string name=ReadName(); if(name.length==0) //end of file or '}' in recursive call goto END; - char* nameCPtr=string_cpToCharPtr(name); + char* nameCPtr=string_cpToCptr(name); Unitype value=ReadValue(); if(partOfDollarList){ Autoarr(Unitype)* list; @@ -255,7 +280,6 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){ return dict; } -Hashtable* DtsodV24_deserialize(char* text) { - Hashtable* r=__deserialize(&text,false); - return r; +Hashtable* DtsodV24_deserialize(char* _text) { + return __deserialize(&_text, false); } diff --git a/DtsodParser/DtsodV24_exported.c b/DtsodParser/DtsodV24_exported.c new file mode 100644 index 0000000..b1867ae --- /dev/null +++ b/DtsodParser/DtsodV24_exported.c @@ -0,0 +1,45 @@ +// +// I planned to export functions from DtsodV24.h, +// but C# P/Invoke can't get returned values for some reason. +// Following functions return values by pointer, which looks in C# like out parameter +// + +#if __cplusplus +extern "C" { +#endif + +#include "DtsodV24.h" + +//parses text to binary values +EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output){ + *output=DtsodV24_deserialize(text); +} + +//creates text representation of dtsod +EXPORT void CALL kerep_DtsodV24_serialize(Hashtable* dtsod, char** output){ + *output=DtsodV24_serialize(dtsod); +} + +//returns value or UniNull if key not found +EXPORT void CALL kerep_DtsodV24_get(Hashtable* dtsod, char* key, Unitype* output){ + *output=DtsodV24_get(dtsod, key); +} + +//adds or sets value +EXPORT void CALL kerep_DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){ + DtsodV24_addOrSet(dtsod, key, value); +} + +//checks for dtsod contains value or dont +EXPORT void CALL kerep_DtsodV24_contains(Hashtable* dtsod, char* key, bool* output){ + *output=DtsodV24_contains(dtsod, key); +} + +//replaces value with UniNull if key exists in dtsod +EXPORT void CALL kerep_DtsodV24_remove(Hashtable* dtsod, char* key, bool* output){ + *output=DtsodV24_remove(dtsod, key); +} + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index eb71929..b2bb9f9 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -5,86 +5,104 @@ #define STRB_BC 64 #define STRB_BL 1024 -#define addc(B,C) StringBuilder_append_char(B,C) +typedef struct SerializeSharedData{ + StringBuilder* sh_builder; + uint8 sh_tabs; +} SerializeSharedData; +#define b shared->sh_builder +#define tabs shared->sh_tabs -void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){ - - void AppendTabs(){ - for(uint8 t=0; t string string_reverse(string s); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/base/std.h b/base/std.h index 25b7fc5..dc5641f 100644 --- a/base/std.h +++ b/base/std.h @@ -1,4 +1,9 @@ #pragma once + +#if __cplusplus +extern "C" { +#endif + #include #include #include @@ -11,3 +16,34 @@ #define IFTYPE(X, T) __builtin_types_compatible_p(typeof(X), T) #define dbg(N) printf("\e[95m%d\n",N) + + +#ifdef _MSC_VER + #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK + #define _CRT_SECURE_NO_WARNINGS 1 + #define EXPORT __declspec(dllexport) + #define CALL __cdecl +#elif defined(__GNUC__) + #define EXPORT __attribute__((visibility("default"))) + #if __SIZEOF_POINTER__ == 4 + #define CALL __attribute__((__cdecl__)) + #else + #define CALL + #endif +#else + #pragma GCC error "unknown compiler" +#endif + +#ifdef _MSC_VER + #define IFWIN(YES, NO) YES +#elif defined(__GNUC__) + #define IFWIN(YES, NO) NO +#else + #pragma GCC error "unknown compiler" +#endif + + + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/base/types.c b/base/types.c index 8ccb309..b4f6ba1 100644 --- a/base/types.c +++ b/base/types.c @@ -4,7 +4,7 @@ #include "../Hashtable/Hashtable.h" #include "../SearchTree/SearchTree.h" -const char* typename(my_type t){ +const char* my_type_name(my_type t){ switch (t) { case Null: return "Null"; case Double: return "Double"; @@ -119,12 +119,12 @@ void Unitype_free(Unitype u){ void printuni(Unitype v){ switch (v.type) { case Null: printf("{Null}");break; - case Double: printf("{%s : %lf}",typename(v.type),v.Double);break; - case Char: printf("{%s : '%c'}",typename(v.type),v.Char);break; + case Double: printf("{%s : %lf}",my_type_name(v.type),v.Double);break; + case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break; case Bool: - case UInt64: printf("{%s : %lu}",typename(v.type),v.UInt64);break; - case Int64: printf("{%s : %ld}",typename(v.type),v.Int64);break; - case CharPtr: printf("{%s : \"%s\"}",typename(v.type),(char*)v.VoidPtr);break; - default: printf("{%s : %p}",typename(v.type),v.VoidPtr);break; + case UInt64: printf("{%s : %lu}",my_type_name(v.type),v.UInt64);break; + case Int64: printf("{%s : %ld}",my_type_name(v.type),v.Int64);break; + case CharPtr: printf("{%s : \"%s\"}",my_type_name(v.type),(char*)v.VoidPtr);break; + default: printf("{%s : %p}",my_type_name(v.type),v.VoidPtr);break; } } diff --git a/base/types.h b/base/types.h index 3405a07..7956931 100644 --- a/base/types.h +++ b/base/types.h @@ -1,5 +1,9 @@ #pragma once +#if __cplusplus +extern "C" { +#endif + #include "std.h" typedef int8_t int8; @@ -10,7 +14,7 @@ typedef int32_t int32; typedef uint32_t uint32; typedef int64_t int64; typedef uint64_t uint64; -typedef enum my_type{ +typedef enum __attribute__((__packed__)) my_type { Null, Float, Double, Char, Bool, UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr, @@ -19,15 +23,10 @@ typedef enum my_type{ AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr, AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr, AutoarrUnitypePtr, AutoarrKVPairPtr -} __attribute__ ((__packed__)) my_type; +} my_type; -//returns type name -const char* typename(my_type t); +const char* my_type_name(my_type t); -// returns size of type in bytes -int8 typesize(my_type type); - -// can store any base type typedef struct Unitype{ union { int64 Int64; @@ -50,3 +49,7 @@ static const Unitype UniFalse={.Bool=false,.type=Bool}; //frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); void printuni(Unitype v); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/kerep.sln b/kerep.sln new file mode 100644 index 0000000..8ecc4f3 --- /dev/null +++ b/kerep.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32317.152 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "kerep", "kerep.vcxproj", "{52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Debug|x64.ActiveCfg = Debug|x64 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Debug|x64.Build.0 = Debug|x64 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Debug|x86.ActiveCfg = Debug|Win32 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Debug|x86.Build.0 = Debug|Win32 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Release|x64.ActiveCfg = Release|x64 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Release|x64.Build.0 = Release|x64 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Release|x86.ActiveCfg = Release|Win32 + {52F0BD29-A3CB-47CE-B25D-CEAF5DFB2D73}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {22F8C351-9A96-4F37-94E6-37E6AB5BA058} + EndGlobalSection +EndGlobal diff --git a/kerep.vcxproj b/kerep.vcxproj new file mode 100644 index 0000000..cfcdc93 --- /dev/null +++ b/kerep.vcxproj @@ -0,0 +1,246 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {52f0bd29-a3cb-47ce-b25d-ceaf5dfb2d73} + kerep + 10.0.18362.0 + + + + DynamicLibrary + true + ClangCL + MultiByte + false + + + DynamicLibrary + false + ClangCL + true + MultiByte + false + + + DynamicLibrary + true + ClangCL + MultiByte + false + + + DynamicLibrary + false + ClangCL + true + MultiByte + false + + + + + + + + + + + + + + + + + + + + + bin-$(Platform)\$(Configuration)\ + obj-$(Platform)\$(Configuration)\ + true + false + + + bin-$(Platform)\$(Configuration)\ + obj-$(Platform)\$(Configuration)\ + true + false + + + true + bin-$(Platform)\$(Configuration)\ + obj-$(Platform)\$(Configuration)\ + false + + + true + bin-$(Platform)\$(Configuration)\ + obj-$(Platform)\$(Configuration)\ + false + + + + TurnOffAllWarnings + true + WIN32;_DEBUG;KEREP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + + + stdc17 + Speed + + /Zc:twoPhase- /MP %(AdditionalOptions) + Default + Default + None + + + Windows + true + false + + + + + TurnOffAllWarnings + true + true + true + WIN32;NDEBUG;KEREP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + + + stdc17 + Speed + + /Zc:twoPhase- /MP %(AdditionalOptions) + MultiThreadedDLL + Default + Default + None + + + Windows + true + true + true + false + + + + + TurnOffAllWarnings + true + _DEBUG;KEREP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + + + stdc17 + Speed + + /Zc:twoPhase- /MP %(AdditionalOptions) + Default + Default + None + + + Windows + true + false + + + + + TurnOffAllWarnings + true + true + true + NDEBUG;KEREP_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + + + stdc11 + Speed + + /Zc:twoPhase- /MP %(AdditionalOptions) + MultiThreadedDLL + Default + Default + None + + + Windows + true + true + true + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/kerep.vcxproj.filters b/kerep.vcxproj.filters new file mode 100644 index 0000000..a4008fb --- /dev/null +++ b/kerep.vcxproj.filters @@ -0,0 +1,129 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index d120d77..47445cc 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,15 +1,5 @@ -#include "../base/base.h" #include "tests.h" -#include "../Hashtable/KeyValuePair.h" - -KeyValuePair test_marshalling(char* text){ - //printf("<%s>\n", text); - Unitype u={.VoidPtr=text,.type=CharPtr}; - KeyValuePair msg={"message",u}; - return msg; -} - void test_all(){ test_searchtree(); test_autoarr(); diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 7faf28d..e407bfd 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -2,8 +2,9 @@ #include "../Autoarr/Autoarr.h" static void printautoarr(Autoarr(uint16)* ar){ - printf("\e[94mAutoarr(uint16): %lu\n" - " max_blocks_count: %u\n" + printf("\e[94mAutoarr(uint16): " + IFWIN("%llu", "%lu") + "\n max_blocks_count: %u\n" " blocks_count: %u\n" " max_block_length: %u\n" " block_length: %u\n" diff --git a/tests/test_hashtable.c b/tests/test_hashtable.c index 2b5689c..6a6f5c3 100644 --- a/tests/test_hashtable.c +++ b/tests/test_hashtable.c @@ -2,8 +2,9 @@ #include "../Hashtable/Hashtable.h" void print_hashtable(Hashtable* ht){ - printf("\e[94mHashtable:%lu\n" - " hein: %u\n" + printf("\e[94mHashtable: " + IFWIN("%llu", "%lu") + "\n hein: %u\n" " height: %u\n" " rows: %p\n", sizeof(Hashtable), diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c new file mode 100644 index 0000000..0619f03 --- /dev/null +++ b/tests/test_marshalling.c @@ -0,0 +1,14 @@ +#include "../Hashtable/KeyValuePair.h" + +EXPORT void CALL test_marshalling(char* text, KeyValuePair** kptr){ + KeyValuePair* k=malloc(sizeof(KeyValuePair)); + k->key="message"; + char* tc=cptr_copy(text); + Unitype u={.VoidPtr=tc, .type=CharPtr}; + k->value=u; + *kptr=k; +} + +EXPORT void CALL pinvoke_print(char* msg) { + printf("printed from unmanaged code: %s\n", msg); +} diff --git a/tests/test_marshalling.h b/tests/test_marshalling.h deleted file mode 100644 index f1b3582..0000000 --- a/tests/test_marshalling.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../Hashtable/KeyValuePair.h" - -KeyValuePair test_marshalling(char* text); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index eab380b..1dc4ce3 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -2,7 +2,9 @@ #include "../SearchTree/SearchTree.h" void printstnode(STNode* node){ - printf("\e[94mSTNode: %lu\n address: %p\n value: ",sizeof(STNode),node); + printf("\e[94mSTNode: " + IFWIN("%llu", "%lu") + "\n address: %p\n value: ",sizeof(STNode),node); printuni(node->value); // prints pointers to all existing branches printf("\n branches: %p\n", node->branches); diff --git a/tests/test_string.c b/tests/test_string.c index d6202ef..6eef4c2 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -8,8 +8,8 @@ void test_string(){ string s=string_cpFromCharPtr(c); printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c); if(s.length!=16) throw("string created with incorrect length"); - char* p=string_cpToCharPtr(s); - printf("\e[92mstring_cpToCharPtr() -> \"%s\"\n",p); + char* p=string_cpToCptr(s); + printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p); free(p); free(s.ptr); }));