diff --git a/Autoarr/Autoarr.c b/Autoarr/Autoarr.c index d6ee333..e22daaf 100644 --- a/Autoarr/Autoarr.c +++ b/Autoarr/Autoarr.c @@ -12,7 +12,7 @@ define_Autoarr(float) define_Autoarr(double) define_Autoarr(Unitype) -// right func to clear array of unitype values +//right func to clear array of unitype values void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){ for(uint32 blockI=0;blockIblocks_count-1;blockI++) for(uint32 elemI=0;elemImax_block_length;elemI++) diff --git a/Autoarr/Autoarr.h b/Autoarr/Autoarr.h index 1ecacd3..5f12633 100644 --- a/Autoarr/Autoarr.h +++ b/Autoarr/Autoarr.h @@ -19,7 +19,7 @@ declare_Autoarr(float) declare_Autoarr(double) declare_Autoarr(Unitype) -// right func to clear array of unitype values +//right func to clear array of unitype values void Autoarr_Unitype_clear(Autoarr(Unitype)* ar); #define Autoarr_foreach(ar,elem,codeblock)({\ diff --git a/Autoarr/StringBuilder.c b/Autoarr/StringBuilder.c index 1eb729e..6a12265 100644 --- a/Autoarr/StringBuilder.c +++ b/Autoarr/StringBuilder.c @@ -25,6 +25,7 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){ uint8 i=0; if(a==0){ Autoarr_add(b,'0'); + return; } else if(a<0){ Autoarr_add(b,'-'); @@ -44,6 +45,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ uint8 i=0; if(a==0){ Autoarr_add(b,'0'); + return; } char buf[24]; while(a!=0){ @@ -51,13 +53,17 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ a/=10; } string rev=string_reverse((string){buf,i}); + printf("\e[95mrev:%s\n",string_cpToCptr(rev)); StringBuilder_append_string(b,rev); free(rev.ptr); } void StringBuilder_append_double(StringBuilder* b, double a){ char buf[32]; - sprintf(buf,"%lf",a); + IFWIN( + sprintf_s(buf,32,"%lf",a), + sprintf(buf,"%lf",a) + ); StringBuilder_append_cptr(b,buf); } diff --git a/DtsodParser/DtsodV24.c b/DtsodParser/DtsodV24.c index f5ce5b4..21b0984 100644 --- a/DtsodParser/DtsodV24.c +++ b/DtsodParser/DtsodV24.c @@ -1,24 +1,24 @@ #include "DtsodV24.h" -// returns UniNull if key not found +//returns UniNull if key not found Unitype DtsodV24_get(Hashtable* dtsod, char* key){ return Hashtable_get(dtsod, key); } -// adds or sets value +//adds or sets value void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){ Unitype* val=Hashtable_getptr(dtsod, key); if(val) *val=value; else Hashtable_add(dtsod, key, value); } -// checks for dtsod contains value or dont +//checks for dtsod contains value or dont bool DtsodV24_contains(Hashtable* dtsod, char* key){ Unitype* val=Hashtable_getptr(dtsod, key); return val!=NULL; } -// replaces value with UniNull if key exists in dtsod +//replaces value with UniNull if key exists in dtsod bool DtsodV24_remove(Hashtable* dtsod, char* key){ Unitype* val=Hashtable_getptr(dtsod, key); if (!val) return false; @@ -26,7 +26,7 @@ bool DtsodV24_remove(Hashtable* dtsod, char* key){ return true; } -// frees memory including memory of elements (hashtables, autoarrs, etc.) +//frees memory including memory of elements (hashtables, autoarrs, etc.) void DtsodV24_free(Hashtable* dtsod){ Hashtable_free(dtsod); } \ No newline at end of file diff --git a/DtsodParser/DtsodV24.h b/DtsodParser/DtsodV24.h index 1328f93..26a116c 100644 --- a/DtsodParser/DtsodV24.h +++ b/DtsodParser/DtsodV24.h @@ -9,22 +9,22 @@ extern "C" { //parses text to binary values Maybe DtsodV24_deserialize(char* text); -// creates text representation of dtsod +//creates text representation of dtsod char* DtsodV24_serialize(Hashtable* dtsod); -// returns value or UniNull if key not found +//returns value or UniNull if key not found Unitype DtsodV24_get(Hashtable* dtsod, char* key); -// adds or sets value +//adds or sets value void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value); -// checks for dtsod contains value or dont +//checks for dtsod contains value or dont bool DtsodV24_contains(Hashtable* dtsod, char* key); -// replaces value with UniNull if key exists in dtsod +//replaces value with UniNull if key exists in dtsod bool DtsodV24_remove(Hashtable* dtsod, char* key); -// frees memory including memory of elements (hashtables, autoarrs, etc.) +//frees memory including memory of elements (hashtables, autoarrs, etc.) void DtsodV24_free(Hashtable* dtsod); #if __cplusplus diff --git a/DtsodParser/DtsodV24_exported.c b/DtsodParser/DtsodV24_exported.c index 1ffb67b..3ba9f31 100644 --- a/DtsodParser/DtsodV24_exported.c +++ b/DtsodParser/DtsodV24_exported.c @@ -1,8 +1,8 @@ -// +// // 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" { @@ -10,39 +10,39 @@ extern "C" { #include "DtsodV24.h" -// parses text to binary values +//parses text to binary values EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output, char** errmsg){ Maybe r=DtsodV24_deserialize(text); *errmsg= r.errmsg ? r.errmsg : NULL; *output=r.value.VoidPtr; } -// creates text representation of dtsod +//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 +//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 the value +//adds or sets the 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 +//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 +//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); } -// replaces value with UniNull if key exists in dtsod +//replaces value with UniNull if key exists in dtsod EXPORT void CALL kerep_DtsodV24_free(Hashtable* dtsod){ DtsodV24_free(dtsod); } diff --git a/DtsodParser/DtsodV24_serialize.c b/DtsodParser/DtsodV24_serialize.c index d9f0fde..5b7c455 100644 --- a/DtsodParser/DtsodV24_serialize.c +++ b/DtsodParser/DtsodV24_serialize.c @@ -1,7 +1,7 @@ #include "DtsodV24.h" #include "../Autoarr/StringBuilder.h" -// 65536 max length! +//65536 max length! #define STRB_BC 64 #define STRB_BL 1024 @@ -47,11 +47,6 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){ } addc('"'); break; - case Char: - addc('\''); - addc(u.Char); - addc('\''); - break; case Bool: StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); break; diff --git a/Hashtable/Hashtable.c b/Hashtable/Hashtable.c index 7bddbb6..9f66450 100644 --- a/Hashtable/Hashtable.c +++ b/Hashtable/Hashtable.c @@ -64,7 +64,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){ Hashtable_add_pair(ht,KVPair(key,u)); } -// returns null or pointer to value in hashtable +//returns null or pointer to value in hashtable Unitype* Hashtable_getptr(Hashtable* ht, char* key){ Autoarr(KeyValuePair)* ar=getrow(ht,key,false); uint32 arlen=Autoarr_length(ar); diff --git a/Hashtable/Hashtable.h b/Hashtable/Hashtable.h index 90ab0ac..14714de 100644 --- a/Hashtable/Hashtable.h +++ b/Hashtable/Hashtable.h @@ -9,35 +9,35 @@ extern "C" { #include "KeyValuePair.h" typedef struct Hashtable{ - uint8 hein; // height=HT_HEIGHTS[hein] + uint8 hein; //height=HT_HEIGHTS[hein] Autoarr(KeyValuePair)* rows; // Autoarr[height] } Hashtable; Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); -// amount of rows +//amount of rows uint32 Hashtable_height(Hashtable* ht); -// adds charptr and value to new KeyValuePair -// use cptr_copy() to create new string if needed +//adds charptr and value to new KeyValuePair +//use cptr_copy() to create new string if needed #define KVPair(key,value) (KeyValuePair){key,value} -// +// // don't add pairs with the same keys, // or something weird will happen -// +// void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); void Hashtable_add(Hashtable* ht, char* key, Unitype u); -// returns null or pointer to value in hashtable +//returns null or pointer to value in hashtable Unitype* Hashtable_getptr(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key); KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); -// not implemented yet +//not implemented yet void Hashtable_set_pair(Hashtable* ht, KeyValuePair p); void Hashtable_set(Hashtable* ht, char* key, Unitype u); diff --git a/Hashtable/KeyValuePair.c b/Hashtable/KeyValuePair.c index d07216e..c11ae9f 100644 --- a/Hashtable/KeyValuePair.c +++ b/Hashtable/KeyValuePair.c @@ -3,13 +3,13 @@ define_Autoarr(KeyValuePair) -// proper way to clear a KVP +//proper way to clear a KVP void KeyValuePair_free(KeyValuePair p){ free(p.key); Unitype_free(p.value); } -// func for KVP array clearing +//func for KVP array clearing void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){ for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++) for(uint16 elemI=0; elemI < ar->max_block_length; elemI++) diff --git a/Hashtable/KeyValuePair.h b/Hashtable/KeyValuePair.h index afbc803..7f99dfb 100644 --- a/Hashtable/KeyValuePair.h +++ b/Hashtable/KeyValuePair.h @@ -14,10 +14,10 @@ typedef struct KeyValuePair{ declare_Autoarr(KeyValuePair) -// proper way to clear a KVP +//proper way to clear a KVP void KeyValuePair_free(KeyValuePair p); -// func to clear KVP array +//func to clear KVP array void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar); void printkvp(KeyValuePair p); diff --git a/Hashtable/hash.h b/Hashtable/hash.h index 2f575c5..b13db28 100644 --- a/Hashtable/hash.h +++ b/Hashtable/hash.h @@ -6,9 +6,9 @@ extern "C" { #include "../base/base.h" -// djb2 hash function from http:// www.cse.yorku.ca/~oz/hash.html +//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html uint32 ihash(char *str); -// sdbm hash function +//sdbm hash function uint64 lhash(char* str); #if __cplusplus diff --git a/Makefile b/Makefile index dfc7810..bdaefa9 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c) OUTDIR=bin CMP=gcc -all: clear_c clear_bin build_test build_lib +all: build_test clear_c: clear @@ -19,11 +19,11 @@ clang: all TEST_FILE=$(OUTDIR)/kerep_test.com TEST_ARGS= -Wall -Wno-discarded-qualifiers $(SRC) $(TESTS) -o $(TEST_FILE) OPT_ARGS= -O1 -flto -build_test: +build_test: clear_bin @echo -e '\n\e[96m----------------[build_test]----------------\e[0m' $(CMP) $(OPT_ARGS) $(TEST_ARGS) -build_test_dbg: +build_test_dbg: clear_bin @echo -e '\n\e[96m--------------[build_test_dbg]--------------\e[0m' $(CMP) -O0 -g $(TEST_ARGS).dbg @@ -40,6 +40,6 @@ LIB_FILE=kerep.so LIB_ARGS= -Wall -Wno-discarded-qualifiers \ -O1 -fPIC -shared -Wl,-soname,$(LIB_FILE) \ $(SRC) $(TESTS) -o $(OUTDIR)/$(LIB_FILE) -build_lib: +build_lib: clear_bin @echo -e '\n\e[96m-------------[build_lib]---------------\e[0m' $(CMP) $(OPT_ARGS) $(LIB_ARGS) diff --git a/SearchTree/SearchTree.h b/SearchTree/SearchTree.h index 4d83bb1..d7ae5a9 100644 --- a/SearchTree/SearchTree.h +++ b/SearchTree/SearchTree.h @@ -7,7 +7,7 @@ extern "C" { #include "../base/base.h" typedef struct SearchTreeNode{ - struct SearchTreeNode**** branches; // *STNode[8][8][4] + struct SearchTreeNode**** branches; //*STNode[8][8][4] Unitype value; } STNode; diff --git a/StringFragment/StringBuilder.h b/StringFragment/StringBuilder.h deleted file mode 100644 index 9369973..0000000 --- a/StringFragment/StringBuilder.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../Autoarr/Autoarr.h" -#include "StringFragment.h" - -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, StringFragment 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); -// returns StringFragment with '\0' at the end -StringFragment StringBuilder_build(StringBuilder* b); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/StringFragment/StringFragment.c b/StringFragment/StringFragment.c deleted file mode 100644 index b3c0ca7..0000000 --- a/StringFragment/StringFragment.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "StringFragment.h" - -// copies characters from to new StringFragment (adding '\0' at the end) -StringFragment StringFragment_extract(StringFragment str){ - if(str.length==0) return stringNull; - StringFragment extr={ - .offset=0, - .length=str.length, - .ptr=malloc(str.length+1) - }; - str.ptr+=str.offset; - for(uint32 i=0; i0) - if(*str0.ptr++ != *str1.ptr++) - return false; - return true; -} - -// creates new StringFragment which is reversed variant of -StringFragment StringFragment_reverse(StringFragment s){ - if(s.length==0) return s; - StringFragment r={ - .offset=0, - .length=s.length, - .ptr=malloc(s.length) - }; - for(uint32 i=0; i characters from to new StringFragment (adding '\0' at the end) -StringFragment StringFragment_extract(StringFragment str); - -// compares two strings, NullPtr-friendly -bool StringFragment_compare(StringFragment str0, StringFragment str1); - -// creates new StringFragment which is reversed variant of -StringFragment StringFragment_reverse(StringFragment s); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/base/base.h b/base/base.h index ff07fdd..ad651e6 100644 --- a/base/base.h +++ b/base/base.h @@ -10,7 +10,7 @@ extern "C" { #include "mystr.h" // executes codeblock and prints execution time -#ifdef CLOCK_REALTIME // non-standard high-precision clock +#ifdef CLOCK_REALTIME //non-standard high-precision clock #define optime(opname,repeats,codeblock) ({\ struct timespec start, stop;\ clock_gettime(CLOCK_REALTIME, &start);\ @@ -20,7 +20,7 @@ extern "C" { double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ }) -#else // standard clock which works worse then previous +#else // #define optime(opname,repeats,codeblock) ({\ clock_t start=clock();\ for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ diff --git a/base/cptr.c b/base/cptr.c deleted file mode 100644 index 50dbcdf..0000000 --- a/base/cptr.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "base.h" - -// returns length of string (without \0) -uint32 cptr_length(char* str){ - uint32 len=0; - while(*(str++)) len++; - return len; -} - -// allocates new char[] and copies src there -char* cptr_copy(char* src){ - uint32 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* rez=malloc(n+1); - rez[n]=0; - while(n-->0) - rez[n]=c; - return rez; -} \ No newline at end of file diff --git a/base/cptr.h b/base/cptr.h deleted file mode 100644 index 93829fc..0000000 --- a/base/cptr.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "types.h" - -// returns length of string (without \0) -uint32 cptr_length(char* str); - -// allocates new char[] and copies src there -char* cptr_copy(char* src); - -// compares two char buffers, NullPtr-friendly -bool cptr_compare(char* key0, char* key1); - -// multiplies char n times -char* char_multiply(char c, uint32 n); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/base/errors.c b/base/errors.c index 13f6528..d10d5d1 100644 --- a/base/errors.c +++ b/base/errors.c @@ -1,7 +1,8 @@ #include "std.h" #include "errors.h" +#include "mystr.h" -const char* errname(err_t err){ +char* errname(err_t err){ switch(err){ case SUCCESS: return "SUCCESS"; case ERR_MAXLENGTH: return "ERR_MAXLENGTH"; @@ -14,14 +15,37 @@ const char* errname(err_t err){ } } -void _throwint(int err, const char* srcfile, int line, const char* funcname){ - if(err){ // SUCCESS=0 is not an error - printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err)); - exit(err); - } - else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname); +#define ERRMSG_MAXLENGTH 1024 + +char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){ + size_t bufsize=ERRMSG_MAXLENGTH; + char* rezult=malloc(bufsize); + IFWIN( + sprintf_s(rezult,bufsize,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg), + sprintf(rezult,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg) + ); + return rezult; } -void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){ - printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg); - exit(255); -} \ No newline at end of file + +char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){ + size_t bufsize=cptr_length(errmsg)+ERRMSG_MAXLENGTH; + char* rezult=malloc(bufsize); + IFWIN( + sprintf_s(rezult,bufsize,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname), + sprintf(rezult,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname) + ); + free(errmsg); + return rezult; +} + +void Maybe_free(Maybe e){ + free(e.errmsg); + Unitype_free(e.value); +} +void printMaybe(Maybe e){ + if(e.errmsg) printf("%s\n",e.errmsg); + else printuni(e.value); +} + +char* __doNothing(char* a) {return a;} +char* __unknownErr() {return "UNKNOWN ERROR";} \ No newline at end of file diff --git a/base/errors.h b/base/errors.h index f1de875..88f63b0 100644 --- a/base/errors.h +++ b/base/errors.h @@ -4,20 +4,58 @@ extern "C" { #endif +#include "std.h" +#include "types.h" + typedef enum err_t { - SUCCESS, // not an error + SUCCESS, //not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR } err_t; -const char* errname(err_t err); +char* errname(err_t err); -void _throwint(int err, const char* srcfile, int line, const char* funcname) ; -void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname); -#pragma GCC diagnostic ignored "-Wint-conversion" -#define throw(E) \ - CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \ - CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \ - printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__))) +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); + +typedef struct Maybe{ + Unitype value; + char* errmsg; +} Maybe; +static const Maybe MaybeNull={.value.type=Null, .value.VoidPtr=NULL,.errmsg=NULL}; + +void Maybe_free(Maybe e); +void printMaybe(Maybe e); + + +#define SUCCESS(REZLT) (Maybe){.errmsg=NULL, .value=REZLT} + +#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull} + +#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); exit(128); }) + +char* __doNothing(char* a); +char* __unknownErr( ); + +#define __stringify_err(E) _Generic(\ + (E),\ + char*: __doNothing,\ + int: errname,\ + default: __unknownErr\ +)(E) + +#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) +#define safethrow(E) __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) + + +#define try(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + return _rezult;\ + }else + +#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + __EXIT(_rezult.errmsg);\ + }else #if __cplusplus } diff --git a/base/mystr.c b/base/mystr.c index ad498a7..5503b46 100644 --- a/base/mystr.c +++ b/base/mystr.c @@ -37,8 +37,8 @@ char* char_multiply(char c, uint32 n){ //copies str content to new char pointer value (adding '\0' at the end) char* string_cpToCptr(string str){ - char* cptr=malloc(str.length*sizeof(char)+1); if(str.length==0) return NULL; + char* cptr=malloc(str.length*sizeof(char)+1); cptr[str.length]=0; while(str.length-->0) cptr[str.length]=str.ptr[str.length]; @@ -69,6 +69,7 @@ bool string_compare(string str0, string str1){ //creates new string which is reversed variant of string string_reverse(string s){ + if(s.length==0) return s; string r={malloc(s.length), s.length}; for(uint32 i=0; i #include #include +#include #define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N) diff --git a/base/types.c b/base/types.c index bcd2bf2..f0197c7 100644 --- a/base/types.c +++ b/base/types.c @@ -45,7 +45,7 @@ const char* my_type_name(my_type t){ } } -// frees VoidPtr value or does nothing if type isn't pointer +//frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u){ switch (u.type) { case Null: @@ -115,16 +115,33 @@ void Unitype_free(Unitype u){ } } +#define SPRINT_BUFSIZE 64 +void sprintuni(char* buf,Unitype v){ + IFWIN( + switch (v.type) { + case Null: sprintf_s(buf, SPRINT_BUFSIZE, "{Null}");break; + case Float64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break; + case Bool: + case UInt64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; + case Int64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break; + case CharPtr: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);break; + default: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; + }, + switch (v.type) { + case Null: sprintf(buf, "{Null}");break; + case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64);break; + case Bool: + case UInt64: sprintf(buf, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; + case Int64: sprintf(buf, "{%s : %ld}", my_type_name(v.type),v.Int64);break; + case CharPtr: sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);break; + default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; + } + ); +} void printuni(Unitype v){ - switch (v.type) { - case Null: printf("{Null}");break; - case Float64: printf("{%s : %lf}",my_type_name(v.type),v.Float64);break; - case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break; - case Bool: - 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; - } -} + char* s=malloc(SPRINT_BUFSIZE); + sprintuni(s,v); + fputs(s, stdout); + free(s); +} \ No newline at end of file diff --git a/base/types.h b/base/types.h index 58418e8..bbbf2f6 100644 --- a/base/types.h +++ b/base/types.h @@ -34,7 +34,6 @@ typedef struct Unitype{ int64 Int64; uint64 UInt64; double Float64; - char Char; bool Bool; void* VoidPtr; }; @@ -48,9 +47,10 @@ static const Unitype UniFalse={.Bool=false,.type=Bool}; #define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} #define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL} -// frees VoidPtr value or does nothing if type isn't pointer +//frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); void printuni(Unitype v); +void sprintuni(char* s, Unitype v); #if __cplusplus } diff --git a/kerep.vcxproj b/kerep.vcxproj index 95978ba..5ed9303 100644 --- a/kerep.vcxproj +++ b/kerep.vcxproj @@ -75,27 +75,27 @@ - bin-$(Platform)\$(Configuration)\ - obj-$(Platform)\$(Configuration)\ + bin\$(Configuration)-$(Platform)\ + obj\$(Configuration)-$(Platform)\ true false - bin-$(Platform)\$(Configuration)\ - obj-$(Platform)\$(Configuration)\ + bin\$(Configuration)-$(Platform)\ + obj\$(Configuration)-$(Platform)\ true false true - bin-$(Platform)\$(Configuration)\ - obj-$(Platform)\$(Configuration)\ + bin\$(Configuration)-$(Platform)\ + obj\$(Configuration)-$(Platform)\ false true - bin-$(Platform)\$(Configuration)\ - obj-$(Platform)\$(Configuration)\ + bin\$(Configuration)-$(Platform)\ + obj\$(Configuration)-$(Platform)\ false @@ -204,9 +204,10 @@ + - + @@ -214,17 +215,16 @@ - - - + + @@ -234,13 +234,12 @@ - - + diff --git a/kerep.vcxproj.filters b/kerep.vcxproj.filters index 12a6e5c..802eb88 100644 --- a/kerep.vcxproj.filters +++ b/kerep.vcxproj.filters @@ -63,15 +63,6 @@ Файлы заголовков - - Файлы заголовков - - - Файлы заголовков - - - Файлы заголовков - diff --git a/tests/main.c b/tests/main.c index 47445cc..4b3149d 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,10 +1,11 @@ #include "tests.h" void test_all(){ - test_searchtree(); test_autoarr(); - test_hashtable(); test_string(); + test_safethrow(); + test_searchtree(); + test_hashtable(); test_dtsod(); printf("\e[96m---------------------------------------\e[0m\n"); } diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index 6b4ae69..5b82206 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -4,12 +4,11 @@ const char text[]= "message: {\n" " bool: false;" -" char: 'v';" " int: -2515;" " uint:#comment!\n 0u;" " double: 965.557f;#another comment!\n" " text: \"_$\\\"\\\\'''a ыыы000;2;=:%d;```\";\n" -"}; "; +"};"; void print_dtsod(Hashtable* dtsod){ printf("\e[92m"); @@ -33,18 +32,21 @@ void test_dtsod(){ optime(__func__,1,({ printf("\e[96m-------------[test_dtsod]-------------\n"); Hashtable* dtsod; - char* s=cptr_copy(text); - optime("deserialize",1,(dtsod=DtsodV24_deserialize(s))); - free(s); + optime("deserialize",1,({ + tryLast(DtsodV24_deserialize(text),r) + dtsod=r.value.VoidPtr; + })); print_dtsod(dtsod); + char* s; optime("serialize",1,(s=DtsodV24_serialize(dtsod))); Hashtable_free(dtsod); printf("\e[92m%s",s); optime("reserialize",10,({ - dtsod=DtsodV24_deserialize(s); + tryLast(DtsodV24_deserialize(s),r) + dtsod=r.value.VoidPtr; free(s); s=DtsodV24_serialize(dtsod); Hashtable_free(dtsod); diff --git a/tests/test_hashtable.c b/tests/test_hashtable.c index 6a6f5c3..70e7944 100644 --- a/tests/test_hashtable.c +++ b/tests/test_hashtable.c @@ -39,7 +39,10 @@ void printrowgraph(Hashtable* ht){ void fill(Hashtable* ht){ for(uint32 i=0;i<100000;i++){ char* key=malloc(12); - sprintf(key,"key__%u",i); + IFWIN( + sprintf_s(key,12,"key_%u",i), + sprintf(key,"key_%u",i) + ); Hashtable_add(ht,key,Uni(UInt64,i)); } } @@ -48,7 +51,10 @@ Unitype gett(Hashtable* ht){ char* key=malloc(12); Unitype u; for(uint32 i=0;i<100000;i++){ - sprintf(key,"key__%u",i); + IFWIN( + sprintf_s(key,12,"key_%u",i), + sprintf(key,"key_%u",i) + ); u=Hashtable_get(ht,key); } free(key); diff --git a/tests/test_safethrow.c b/tests/test_safethrow.c new file mode 100644 index 0000000..78c6a1c --- /dev/null +++ b/tests/test_safethrow.c @@ -0,0 +1,43 @@ +#include "tests.h" + +Maybe dont_throw(){ + return SUCCESS(Uni(UInt64, 9/2)); +} + +Maybe throw_error(){ + safethrow("test exception"); +} + +Maybe throw_errcode(){ + safethrow(ERR_NULLPTR); +} + +Maybe test_maybe(){ + printf("\e[94mdont_throw returns \e[92m"); + tryLast(dont_throw(),rez0) + printMaybe(rez0); + printf("\n"); + try(throw_error(),rez1) + printMaybe(rez1); + throw("test_maybe failed"); + return MaybeNull; +} + + +Maybe c(){ try(throw_errcode(),_) return MaybeNull; } +Maybe b(){ try(c(),_) return MaybeNull; } +Maybe a(){ try(b(),_) return MaybeNull; } + +void test_safethrow(){ + printf("\e[96m-----------[test_safethrow]-----------\n"); + optime("test_safethrow", 1, ({ + Maybe e=test_maybe(); + printf("\e[94mthrow_error:\n\e[92m"); + printMaybe(e); + Maybe_free(e); + printf("\e[94mthrow_errcode:\n\e[92m"); + e=a(); + printMaybe(e); + Maybe_free(e); + })); +} diff --git a/tests/test_string.c b/tests/test_string.c index 6eef4c2..e9e249f 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -6,10 +6,10 @@ void test_string(){ printf("\e[96m-------------[test_string]-------------\n"); char c[]="0123456789abcdef"; string s=string_cpFromCharPtr(c); - printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c); + printf("\e[92m\"%s\" \e[94m-> string_cpFromCharPtr()\n",c); if(s.length!=16) throw("string created with incorrect length"); char* p=string_cpToCptr(s); - printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p); + printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p); free(p); free(s.ptr); })); diff --git a/tests/tests.h b/tests/tests.h index 220ac56..edaa49f 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -8,4 +8,5 @@ void test_searchtree(); void test_autoarr(); void test_hashtable(); void test_string(); -void test_dtsod(); \ No newline at end of file +void test_dtsod(); +void test_safethrow(); \ No newline at end of file