Merge branch 'main' into string_revert

This commit is contained in:
Timerix22 2022-04-17 00:29:45 +03:00 committed by GitHub
commit ecdc9c14aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 341 additions and 160 deletions

View File

@ -12,7 +12,7 @@ define_Autoarr(float)
define_Autoarr(double) define_Autoarr(double)
define_Autoarr(Unitype) 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){ void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++) for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++) for(uint32 elemI=0;elemI<ar->max_block_length;elemI++)

View File

@ -19,7 +19,7 @@ declare_Autoarr(float)
declare_Autoarr(double) declare_Autoarr(double)
declare_Autoarr(Unitype) 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); void Autoarr_Unitype_clear(Autoarr(Unitype)* ar);
#define Autoarr_foreach(ar,elem,codeblock)({\ #define Autoarr_foreach(ar,elem,codeblock)({\

View File

@ -1,24 +1,24 @@
#include "DtsodV24.h" #include "DtsodV24.h"
//returns UniNull if key not found // returns UniNull if key not found
Unitype DtsodV24_get(Hashtable* dtsod, char* key){ Unitype DtsodV24_get(Hashtable* dtsod, char* key){
return Hashtable_get(dtsod, key); return Hashtable_get(dtsod, key);
} }
//adds or sets value // adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){ void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
Unitype* val=Hashtable_getptr(dtsod, key); Unitype* val=Hashtable_getptr(dtsod, key);
if(val) *val=value; if(val) *val=value;
else Hashtable_add(dtsod, key, 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){ bool DtsodV24_contains(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key); Unitype* val=Hashtable_getptr(dtsod, key);
return val!=NULL; 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){ bool DtsodV24_remove(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key); Unitype* val=Hashtable_getptr(dtsod, key);
if (!val) return false; if (!val) return false;
@ -26,7 +26,7 @@ bool DtsodV24_remove(Hashtable* dtsod, char* key){
return true; 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){ void DtsodV24_free(Hashtable* dtsod){
Hashtable_free(dtsod); Hashtable_free(dtsod);
} }

View File

@ -7,24 +7,24 @@ extern "C" {
#include "../Hashtable/Hashtable.h" #include "../Hashtable/Hashtable.h"
//parses text to binary values //parses text to binary values
Hashtable* DtsodV24_deserialize(char* text); Maybe DtsodV24_deserialize(char* text);
//creates text representation of dtsod // creates text representation of dtsod
char* DtsodV24_serialize(Hashtable* 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); Unitype DtsodV24_get(Hashtable* dtsod, char* key);
//adds or sets value // adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype 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); 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); 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); void DtsodV24_free(Hashtable* dtsod);
#if __cplusplus #if __cplusplus

View File

@ -6,16 +6,16 @@
#define STRB_BC 64 #define STRB_BC 64
#define STRB_BL 1024 #define STRB_BL 1024
void __throw_wrongchar(char* file, int line, char* fname, char _c, char* _text) { Maybe ERROR_WRONGCHAR(char c, char* text){
char errBuf[]="unexpected <c> at:\n \"" char errBuf[]="unexpected <c> at:\n \""
"00000000000000000000000000000000\""; "00000000000000000000000000000000\"";
errBuf[12]=_c; errBuf[12]=c;
for (uint8 i=0; i < 32; i++) for(uint8 i=0; i<32; i++)
errBuf[i + 22]=*(_text - 16 + i); // writes 16 chars before and 15 after the wrongchar
printf("\n\e[91m[%s:%d %s] throwed error: %s\n", file, line, fname, errBuf); errBuf[i+22]=*(text - 16 + i);
exit(128); safethrow(cptr_copy(errBuf));
}; }
#define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C, text) #define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text)
typedef struct DeserializeSharedData{ typedef struct DeserializeSharedData{
@ -29,14 +29,15 @@ typedef struct DeserializeSharedData{
#define readingList shared->sh_readingList #define readingList shared->sh_readingList
#define calledRecursively shared->sh_calledRecursively #define calledRecursively shared->sh_calledRecursively
void __SkipComment(DeserializeSharedData* shared) { Maybe __SkipComment(DeserializeSharedData* shared) {
char c; char c;
while ((c=*++text) != '\n') while ((c=*++text) != '\n')
if (!c) throw(ERR_ENDOFSTR); if (!c) safethrow(ERR_ENDOFSTR);
}; return MaybeNull;
}
#define SkipComment() __SkipComment(shared) #define SkipComment() __SkipComment(shared)
string __ReadName(DeserializeSharedData* shared){ Maybe __ReadName(DeserializeSharedData* shared){
char c; char c;
string nameStr={text,0}; string nameStr={text,0};
text--; text--;
@ -44,30 +45,30 @@ string __ReadName(DeserializeSharedData* shared){
case ' ': case '\t': case ' ': case '\t':
case '\r': case '\n': case '\r': case '\n':
if(nameStr.length!=0) if(nameStr.length!=0)
throw_wrongchar(c); safethrow_wrongchar(c);
nameStr.ptr++; nameStr.ptr++;
break; break;
case '=': case ';': case '=': case ';':
case '\'': case '"': case '\'': case '"':
case '[': case ']': case '[': case ']':
case '{': case '{':
throw_wrongchar(c); safethrow_wrongchar(c);
break; break;
case '#': case '#': ;
SkipComment(); try(SkipComment(),_);
if(nameStr.length!=0) if(nameStr.length!=0)
throw_wrongchar(c); safethrow_wrongchar(c);
nameStr.ptr=text+1; //skips '\n' nameStr.ptr=text+1; //skips '\n'
break; break;
case '}': case '}':
if(!calledRecursively) throw_wrongchar(c); if(!calledRecursively) safethrow_wrongchar(c);
if((*++text)!=';') if((*++text)!=';')
throw_wrongchar(c); safethrow_wrongchar(c);
case ':': case ':':
return nameStr; return SUCCESS(UniPtr(CharPtr,string_cpToCptr(nameStr)));
case '$': case '$':
if(nameStr.length!=0) if(nameStr.length!=0)
throw_wrongchar(c); safethrow_wrongchar(c);
nameStr.ptr++; nameStr.ptr++;
partOfDollarList=true; partOfDollarList=true;
break; break;
@ -76,17 +77,17 @@ string __ReadName(DeserializeSharedData* shared){
break; break;
} }
if(nameStr.length>0) throw(ERR_ENDOFSTR); if(nameStr.length>0) safethrow(ERR_ENDOFSTR);
return nameStr; return SUCCESS(UniPtr(CharPtr,NULL));
}; }
#define ReadName() __ReadName(shared) #define ReadName() __ReadName(shared)
Hashtable* __deserialize(char** _text, bool _calledRecursively); Maybe __deserialize(char** _text, bool _calledRecursively);
Unitype __ReadValue(DeserializeSharedData* shared); Maybe __ReadValue(DeserializeSharedData* shared);
#define ReadValue() __ReadValue(shared) #define ReadValue() __ReadValue(shared)
//returns part of <text> without quotes //returns part of <text> without quotes
char* __ReadString(DeserializeSharedData* shared){ Maybe __ReadString(DeserializeSharedData* shared){
char c; char c;
bool prevIsBackslash=false; bool prevIsBackslash=false;
StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL); StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL);
@ -101,7 +102,7 @@ char* __ReadString(DeserializeSharedData* shared){
else { else {
char* str=StringBuilder_build(b); char* str=StringBuilder_build(b);
Autoarr_clear(b); Autoarr_clear(b);
return str; return SUCCESS(UniPtr(CharPtr,str));
} }
} }
else { else {
@ -109,24 +110,24 @@ char* __ReadString(DeserializeSharedData* shared){
StringBuilder_append_char(b,c); StringBuilder_append_char(b,c);
} }
} }
throw(ERR_ENDOFSTR); safethrow(ERR_ENDOFSTR);
return NULL; }
};
#define ReadString() __ReadString(shared) #define ReadString() __ReadString(shared)
Autoarr(Unitype)* __ReadList(DeserializeSharedData* shared){ Maybe __ReadList(DeserializeSharedData* shared){
Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype))); Autoarr(Unitype)* list=malloc(sizeof(Autoarr(Unitype)));
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL); *list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
readingList=true; readingList=true;
while (true){ while (true){
Autoarr_add(list,ReadValue()); try(ReadValue(), val)
Autoarr_add(list,val.value);
if (!readingList) break; if (!readingList) break;
} }
return list; return SUCCESS(UniPtr(AutoarrUnitypePtr,list));
}; };
#define ReadList() __ReadList(shared) #define ReadList() __ReadList(shared)
Unitype __ParseValue(DeserializeSharedData* shared, string str){ Maybe __ParseValue(DeserializeSharedData* shared, string str){
//printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCptr(str)); //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCptr(str));
const string nullStr={"null",4}; const string nullStr={"null",4};
const string trueStr={"true",4}; const string trueStr={"true",4};
@ -134,18 +135,18 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
switch(*str.ptr){ switch(*str.ptr){
case 'n': case 'n':
if(string_compare(str,nullStr)) if(string_compare(str,nullStr))
return UniNull; return SUCCESS(UniNull);
else throw_wrongchar(*str.ptr); else safethrow_wrongchar(*str.ptr);
break; break;
case 't': case 't':
if(string_compare(str,trueStr)) if(string_compare(str,trueStr))
return UniTrue; return SUCCESS(UniTrue);
else throw_wrongchar(*str.ptr); else safethrow_wrongchar(*str.ptr);
break; break;
case 'f': case 'f':
if(string_compare(str,falseStr)) if(string_compare(str,falseStr))
return UniFalse; return SUCCESS(UniFalse);
else throw_wrongchar(*str.ptr); else safethrow_wrongchar(*str.ptr);
break; break;
default: default:
switch(str.ptr[str.length-1]){ switch(str.ptr[str.length-1]){
@ -153,14 +154,14 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
char* _c=string_cpToCptr(str); char* _c=string_cpToCptr(str);
Unitype rez=Uni(Float64,strtod(_c,NULL)); Unitype rez=Uni(Float64,strtod(_c,NULL));
free(_c); free(_c);
return rez; return SUCCESS(rez);
} }
case 'u': { case 'u': {
uint64 lu=0; uint64 lu=0;
char* _c=string_cpToCptr(str); char* _c=string_cpToCptr(str);
sscanf(_c,"%lu",&lu); sscanf(_c,"%lu",&lu);
free(_c); free(_c);
return Uni(UInt64,lu); return SUCCESS(Uni(UInt64,lu));
} }
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': { case '5': case '6': case '7': case '8': case '9': {
@ -168,23 +169,24 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
char* _c=string_cpToCptr(str); char* _c=string_cpToCptr(str);
if(sscanf(_c,"%li",&li)!=1){ if(sscanf(_c,"%li",&li)!=1){
char err[64]; char err[64];
sprintf(err,"can't parse to int: <%s>",_c); IFWIN(
throw(err); sprintf_s(err,64,"can't parse to int: <%s>",_c),
sprintf(err,"can't parse to int: <%s>",_c)
);
safethrow(err);
} }
free(_c); free(_c);
return Uni(Int64,li); return SUCCESS(Uni(Int64,li));
} }
default: default:
throw_wrongchar(str.ptr[str.length-1]); safethrow_wrongchar(str.ptr[str.length-1]);
} }
} }
throw(ERR_ENDOFSTR); safethrow(ERR_ENDOFSTR);
return UniNull;
}; };
#define ParseValue(str) __ParseValue(shared, str) #define ParseValue(str) __ParseValue(shared, str)
Unitype __ReadValue(DeserializeSharedData* shared){ Maybe __ReadValue(DeserializeSharedData* shared){
char c; char c;
string valueStr={text+1,0}; string valueStr={text+1,0};
Unitype value; Unitype value;
@ -192,57 +194,55 @@ Unitype __ReadValue(DeserializeSharedData* shared){
case ' ': case '\t': case ' ': case '\t':
case '\r': case '\n': case '\r': case '\n':
if(valueStr.length!=0) if(valueStr.length!=0)
throw_wrongchar(c); safethrow_wrongchar(c);
valueStr.ptr++; valueStr.ptr++;
break; break;
case '=': case ':': case '=': case ':':
case '}': case '$': case '}': case '$':
throw_wrongchar(c); case '\'':
safethrow_wrongchar(c);
break; break;
case '#':; case '#':;
char _c=c; char _c=c;
SkipComment(); try(SkipComment(),_);
if(valueStr.length!=0) if(valueStr.length!=0)
throw_wrongchar(_c); safethrow_wrongchar(_c);
valueStr.ptr=text+1; //skips '\n' valueStr.ptr=text+1; //skips '\n'
break; break;
case '"': case '"':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) safethrow_wrongchar(c);
value=UniPtr(CharPtr,ReadString()); try(ReadString(),maybeString)
break; value=maybeString.value;
case '\'':
if(valueStr.length!=0) throw_wrongchar(c);
char valueChar=*++text;
if (*++text != '\'') throw("after <'> should be char");
value=Uni(Char,valueChar);
break; break;
case '[': case '[':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) safethrow_wrongchar(c);
value=UniPtr(AutoarrUnitypePtr,ReadList()); try(ReadList(),maybeList)
value=maybeList.value;
case ']': case ']':
readingList=false; readingList=false;
break; break;
case '{': case '{':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) safethrow_wrongchar(c);
++text; //skips '{' ++text; //skips '{'
value=UniPtr(HashtablePtr, __deserialize(&text,true)); try(__deserialize(&text,true), val)
return value; return SUCCESS(val.value);
case ';': case ';':
case ',': case ',':
if(valueStr.length!=0) if(valueStr.length!=0){
value=ParseValue(valueStr); try(ParseValue(valueStr),maybeParsed)
return value; value=maybeParsed.value;
}
return SUCCESS(value);
default: default:
valueStr.length++; valueStr.length++;
break; break;
} }
throw(ERR_ENDOFSTR); safethrow(ERR_ENDOFSTR);
return UniNull; }
};
Hashtable* __deserialize(char** _text, bool _calledRecursively) { Maybe __deserialize(char** _text, bool _calledRecursively) {
DeserializeSharedData _shared={ DeserializeSharedData _shared={
.sh_text=*_text, .sh_text=*_text,
.sh_partOfDollarList=false, .sh_partOfDollarList=false,
@ -255,31 +255,31 @@ Hashtable* __deserialize(char** _text, bool _calledRecursively) {
text--; text--;
while((c=*++text)){ while((c=*++text)){
string name=ReadName(); try(ReadName(), maybeName)
if(name.length==0) //end of file or '}' in recursive call if(!maybeName.value.VoidPtr) //end of file or '}' in recursive call
goto END; goto END;
char* nameCPtr=string_cpToCptr(name); char* nameCPtr=maybeName.value.VoidPtr;
Unitype value=ReadValue(); try(ReadValue(), val)
if(partOfDollarList){ if(partOfDollarList){
Autoarr(Unitype)* list; Autoarr(Unitype)* list;
Unitype lu; Unitype lu;
if(Hashtable_try_get(dict,nameCPtr, &lu)){ if(Hashtable_try_get(dict,nameCPtr, &lu)){
list=(Autoarr(Unitype)*)lu.VoidPtr; list=(Autoarr(Unitype)*)lu.VoidPtr;
}
else{
list=malloc(sizeof(Autoarr(Unitype)));
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list));
}
Autoarr_add(list,val.value);
} }
else{ else Hashtable_add(dict,nameCPtr,val.value);
list=malloc(sizeof(Autoarr(Unitype)));
*list=Autoarr_create(Unitype,ARR_BC,ARR_BL);
Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list));
}
Autoarr_add(list,value);
}
else Hashtable_add(dict,nameCPtr,value);
} }
END: END:
*_text=text; *_text=text;
return dict; return SUCCESS(UniPtr(HashtablePtr,dict));
} }
Hashtable* DtsodV24_deserialize(char* _text) { Maybe DtsodV24_deserialize(char* _text) {
return __deserialize(&_text, false); return __deserialize(&_text, false);
} }

View File

@ -10,37 +10,39 @@ extern "C" {
#include "DtsodV24.h" #include "DtsodV24.h"
//parses text to binary values // parses text to binary values
EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output){ EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output, char** errmsg){
*output=DtsodV24_deserialize(text); 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){ EXPORT void CALL kerep_DtsodV24_serialize(Hashtable* dtsod, char** output){
*output=DtsodV24_serialize(dtsod); *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){ EXPORT void CALL kerep_DtsodV24_get(Hashtable* dtsod, char* key, Unitype* output){
*output=DtsodV24_get(dtsod, key); *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){ EXPORT void CALL kerep_DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
DtsodV24_addOrSet(dtsod, key, 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){ EXPORT void CALL kerep_DtsodV24_contains(Hashtable* dtsod, char* key, bool* output){
*output=DtsodV24_contains(dtsod, key); *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){ EXPORT void CALL kerep_DtsodV24_remove(Hashtable* dtsod, char* key, bool* output){
*output=DtsodV24_remove(dtsod, key); *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){ EXPORT void CALL kerep_DtsodV24_free(Hashtable* dtsod){
DtsodV24_free(dtsod); DtsodV24_free(dtsod);
} }

View File

@ -1,7 +1,7 @@
#include "DtsodV24.h" #include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h" #include "../Autoarr/StringBuilder.h"
//65536 max length! // 65536 max length!
#define STRB_BC 64 #define STRB_BC 64
#define STRB_BL 1024 #define STRB_BL 1024

View File

@ -64,7 +64,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
Hashtable_add_pair(ht,KVPair(key,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){ Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false); Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar); uint32 arlen=Autoarr_length(ar);

View File

@ -9,18 +9,18 @@ extern "C" {
#include "KeyValuePair.h" #include "KeyValuePair.h"
typedef struct Hashtable{ typedef struct Hashtable{
uint8 hein; //height=HT_HEIGHTS[hein] uint8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KeyValuePair)* rows; // Autoarr[height] Autoarr(KeyValuePair)* rows; // Autoarr[height]
} Hashtable; } Hashtable;
Hashtable* Hashtable_create(); Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht); void Hashtable_free(Hashtable* ht);
//amount of rows // amount of rows
uint32 Hashtable_height(Hashtable* ht); uint32 Hashtable_height(Hashtable* ht);
//adds charptr and value to new KeyValuePair // adds charptr and value to new KeyValuePair
//use cptr_copy() to create new string if needed // use cptr_copy() to create new string if needed
#define KVPair(key,value) (KeyValuePair){key,value} #define KVPair(key,value) (KeyValuePair){key,value}
// //
@ -30,14 +30,14 @@ uint32 Hashtable_height(Hashtable* ht);
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); void Hashtable_add_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_add(Hashtable* ht, char* key, Unitype u); 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_getptr(Hashtable* ht, char* key);
Unitype Hashtable_get(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key);
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key); KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); 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_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u); void Hashtable_set(Hashtable* ht, char* key, Unitype u);

View File

@ -3,13 +3,13 @@
define_Autoarr(KeyValuePair) define_Autoarr(KeyValuePair)
//proper way to clear a KVP // proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p){ void KeyValuePair_free(KeyValuePair p){
free(p.key); free(p.key);
Unitype_free(p.value); Unitype_free(p.value);
} }
//func for KVP array clearing // func for KVP array clearing
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){ void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++) for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++)
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++) for(uint16 elemI=0; elemI < ar->max_block_length; elemI++)

View File

@ -14,10 +14,10 @@ typedef struct KeyValuePair{
declare_Autoarr(KeyValuePair) declare_Autoarr(KeyValuePair)
//proper way to clear a KVP // proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p); void KeyValuePair_free(KeyValuePair p);
//func to clear KVP array // func to clear KVP array
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar); void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
void printkvp(KeyValuePair p); void printkvp(KeyValuePair p);

View File

@ -6,9 +6,9 @@ extern "C" {
#include "../base/base.h" #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); uint32 ihash(char *str);
//sdbm hash function // sdbm hash function
uint64 lhash(char* str); uint64 lhash(char* str);
#if __cplusplus #if __cplusplus

View File

@ -7,7 +7,7 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
typedef struct SearchTreeNode{ typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; //*STNode[8][8][4] struct SearchTreeNode**** branches; // *STNode[8][8][4]
Unitype value; Unitype value;
} STNode; } STNode;

View File

@ -0,0 +1,24 @@
#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

View File

@ -0,0 +1,42 @@
#include "StringFragment.h"
// copies <length> characters from <ptr+offset> 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; i<str.length; i++)
extr.ptr[i]=str.ptr[i];
extr.ptr[str.length]='\0';
return extr;
}
// compares two strings, NullPtr-friendly
bool StringFragment_compare(StringFragment str0, StringFragment str1){
if(str0.length!=str1.length) return false;
if(!str0.ptr) return str1.ptr ? false : true;
else if(!str1.ptr) return false;
str0.ptr+=str0.offset;
str1.ptr+=str1.offset;
while(str0.length-->0)
if(*str0.ptr++ != *str1.ptr++)
return false;
return true;
}
// creates new StringFragment which is reversed variant of <s>
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<s.length; i++)
r.ptr[i+s.offset]=s.ptr[s.length-i-1];
return r;
}

View File

@ -0,0 +1,29 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
// part of string
typedef struct StringFragment{
char* ptr; // may be without '\0' at the end
uint32 offset;
uint32 length;
} StringFragment;
static const StringFragment stringNull={NULL,0,0};
// copies <length> characters from <ptr+offset> 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 <s>
StringFragment StringFragment_reverse(StringFragment s);
#if __cplusplus
}
#endif

View File

@ -10,23 +10,26 @@ extern "C" {
#include "mystr.h" #include "mystr.h"
// executes codeblock and prints execution time // executes codeblock and prints execution time
/*#define optime(opname,repeats,codeblock) ({\ #ifdef CLOCK_REALTIME // non-standard high-precision clock
struct timespec start, stop;\ #define optime(opname,repeats,codeblock) ({\
clock_gettime(CLOCK_REALTIME, &start);\ struct timespec start, stop;\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\ clock_gettime(CLOCK_REALTIME, &start);\
(codeblock);\ for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
clock_gettime(CLOCK_REALTIME, &stop);\ (codeblock);\
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ clock_gettime(CLOCK_REALTIME, &stop);\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ 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);\
#define optime(opname,repeats,codeblock) ({\ })
clock_t start=clock();\ #else // standard clock which works worse then previous
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\ #define optime(opname,repeats,codeblock) ({\
(codeblock);\ clock_t start=clock();\
clock_t stop=clock();\ for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ (codeblock);\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ clock_t stop=clock();\
}) double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})
#endif
#if __cplusplus #if __cplusplus
} }

36
base/cptr.c Normal file
View File

@ -0,0 +1,36 @@
#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;
}

23
base/cptr.h Normal file
View File

@ -0,0 +1,23 @@
#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

View File

@ -5,7 +5,7 @@ extern "C" {
#endif #endif
typedef enum err_t { 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_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t; } err_t;

View File

@ -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){ void Unitype_free(Unitype u){
switch (u.type) { switch (u.type) {
case Null: case Null:

View File

@ -48,7 +48,7 @@ static const Unitype UniFalse={.Bool=false,.type=Bool};
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} #define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=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 Unitype_free(Unitype u);
void printuni(Unitype v); void printuni(Unitype v);

View File

@ -204,10 +204,9 @@
<ClInclude Include="Autoarr\Autoarr.h" /> <ClInclude Include="Autoarr\Autoarr.h" />
<ClInclude Include="Autoarr\Autoarr_declare.h" /> <ClInclude Include="Autoarr\Autoarr_declare.h" />
<ClInclude Include="Autoarr\Autoarr_define.h" /> <ClInclude Include="Autoarr\Autoarr_define.h" />
<ClInclude Include="Autoarr\StringBuilder.h" />
<ClInclude Include="base\base.h" /> <ClInclude Include="base\base.h" />
<ClInclude Include="base\cptr.h" />
<ClInclude Include="base\errors.h" /> <ClInclude Include="base\errors.h" />
<ClInclude Include="base\mystr.h" />
<ClInclude Include="base\std.h" /> <ClInclude Include="base\std.h" />
<ClInclude Include="base\types.h" /> <ClInclude Include="base\types.h" />
<ClInclude Include="DtsodParser\DtsodV24.h" /> <ClInclude Include="DtsodParser\DtsodV24.h" />
@ -215,14 +214,17 @@
<ClInclude Include="Hashtable\Hashtable.h" /> <ClInclude Include="Hashtable\Hashtable.h" />
<ClInclude Include="Hashtable\KeyValuePair.h" /> <ClInclude Include="Hashtable\KeyValuePair.h" />
<ClInclude Include="SearchTree\SearchTree.h" /> <ClInclude Include="SearchTree\SearchTree.h" />
<ClInclude Include="StringFragment\StringBuilder.h" />
<ClInclude Include="StringFragment\StringFragment.h" />
<ClInclude Include="tests\tests.h" /> <ClInclude Include="tests\tests.h" />
<ClInclude Include="tests\test_marshalling.h" /> <ClInclude Include="tests\test_marshalling.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c" /> <ClCompile Include="Autoarr\Autoarr.c" />
<ClCompile Include="Autoarr\StringBuilder.c" /> <ClCompile Include="Autoarr\StringBuilder.c" />
<ClCompile Include="Autoarr\Autoarr_KVPair_exported.c" />
<ClCompile Include="Autoarr\Autoarr_Unitype_exported.c" />
<ClCompile Include="base\errors.c" /> <ClCompile Include="base\errors.c" />
<ClCompile Include="base\mystr.c" />
<ClCompile Include="base\types.c" /> <ClCompile Include="base\types.c" />
<ClCompile Include="DtsodParser\DtsodV24.c" /> <ClCompile Include="DtsodParser\DtsodV24.c" />
<ClCompile Include="DtsodParser\DtsodV24_deserialize.c" /> <ClCompile Include="DtsodParser\DtsodV24_deserialize.c" />
@ -232,6 +234,8 @@
<ClCompile Include="Hashtable\Hashtable.c" /> <ClCompile Include="Hashtable\Hashtable.c" />
<ClCompile Include="Hashtable\KeyValuePair.c" /> <ClCompile Include="Hashtable\KeyValuePair.c" />
<ClCompile Include="SearchTree\SearchTree.c" /> <ClCompile Include="SearchTree\SearchTree.c" />
<ClCompile Include="StringFragment\StringBuilder.c" />
<ClCompile Include="StringFragment\StringFragment.c" />
<ClCompile Include="tests\main.c" /> <ClCompile Include="tests\main.c" />
<ClCompile Include="tests\test_autoarr.c" /> <ClCompile Include="tests\test_autoarr.c" />
<ClCompile Include="tests\test_dtsod.c" /> <ClCompile Include="tests\test_dtsod.c" />

View File

@ -63,6 +63,15 @@
<ClInclude Include="tests\tests.h"> <ClInclude Include="tests\tests.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="base\cptr.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="StringFragment\StringBuilder.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="StringFragment\StringFragment.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c"> <ClCompile Include="Autoarr\Autoarr.c">
@ -125,5 +134,14 @@
<ClCompile Include="DtsodParser\DtsodV24_exported.c"> <ClCompile Include="DtsodParser\DtsodV24_exported.c">
<Filter>Исходные файлы</Filter> <Filter>Исходные файлы</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Autoarr\Autoarr_KVPair_exported.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Autoarr\Autoarr_Unitype_exported.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="tests\test_safethrow.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>