Merge branch 'main' into string_revert
This commit is contained in:
commit
ecdc9c14aa
@ -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;blockI<ar->blocks_count-1;blockI++)
|
||||
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++)
|
||||
|
||||
@ -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)({\
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -7,24 +7,24 @@ extern "C" {
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
|
||||
//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);
|
||||
|
||||
//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
|
||||
|
||||
@ -6,16 +6,16 @@
|
||||
#define STRB_BC 64
|
||||
#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 \""
|
||||
"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)
|
||||
errBuf[12]=c;
|
||||
for(uint8 i=0; i<32; i++)
|
||||
// writes 16 chars before and 15 after the wrongchar
|
||||
errBuf[i+22]=*(text - 16 + i);
|
||||
safethrow(cptr_copy(errBuf));
|
||||
}
|
||||
#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text)
|
||||
|
||||
|
||||
typedef struct DeserializeSharedData{
|
||||
@ -29,14 +29,15 @@ typedef struct DeserializeSharedData{
|
||||
#define readingList shared->sh_readingList
|
||||
#define calledRecursively shared->sh_calledRecursively
|
||||
|
||||
void __SkipComment(DeserializeSharedData* shared) {
|
||||
Maybe __SkipComment(DeserializeSharedData* shared) {
|
||||
char c;
|
||||
while ((c=*++text) != '\n')
|
||||
if (!c) throw(ERR_ENDOFSTR);
|
||||
};
|
||||
if (!c) safethrow(ERR_ENDOFSTR);
|
||||
return MaybeNull;
|
||||
}
|
||||
#define SkipComment() __SkipComment(shared)
|
||||
|
||||
string __ReadName(DeserializeSharedData* shared){
|
||||
Maybe __ReadName(DeserializeSharedData* shared){
|
||||
char c;
|
||||
string nameStr={text,0};
|
||||
text--;
|
||||
@ -44,30 +45,30 @@ string __ReadName(DeserializeSharedData* shared){
|
||||
case ' ': case '\t':
|
||||
case '\r': case '\n':
|
||||
if(nameStr.length!=0)
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
nameStr.ptr++;
|
||||
break;
|
||||
case '=': case ';':
|
||||
case '\'': case '"':
|
||||
case '[': case ']':
|
||||
case '{':
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
break;
|
||||
case '#':
|
||||
SkipComment();
|
||||
case '#': ;
|
||||
try(SkipComment(),_);
|
||||
if(nameStr.length!=0)
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
nameStr.ptr=text+1; //skips '\n'
|
||||
break;
|
||||
case '}':
|
||||
if(!calledRecursively) throw_wrongchar(c);
|
||||
if(!calledRecursively) safethrow_wrongchar(c);
|
||||
if((*++text)!=';')
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
case ':':
|
||||
return nameStr;
|
||||
return SUCCESS(UniPtr(CharPtr,string_cpToCptr(nameStr)));
|
||||
case '$':
|
||||
if(nameStr.length!=0)
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
nameStr.ptr++;
|
||||
partOfDollarList=true;
|
||||
break;
|
||||
@ -76,17 +77,17 @@ string __ReadName(DeserializeSharedData* shared){
|
||||
break;
|
||||
}
|
||||
|
||||
if(nameStr.length>0) throw(ERR_ENDOFSTR);
|
||||
return nameStr;
|
||||
};
|
||||
if(nameStr.length>0) safethrow(ERR_ENDOFSTR);
|
||||
return SUCCESS(UniPtr(CharPtr,NULL));
|
||||
}
|
||||
#define ReadName() __ReadName(shared)
|
||||
|
||||
Hashtable* __deserialize(char** _text, bool _calledRecursively);
|
||||
Unitype __ReadValue(DeserializeSharedData* shared);
|
||||
Maybe __deserialize(char** _text, bool _calledRecursively);
|
||||
Maybe __ReadValue(DeserializeSharedData* shared);
|
||||
#define ReadValue() __ReadValue(shared)
|
||||
|
||||
//returns part of <text> without quotes
|
||||
char* __ReadString(DeserializeSharedData* shared){
|
||||
Maybe __ReadString(DeserializeSharedData* shared){
|
||||
char c;
|
||||
bool prevIsBackslash=false;
|
||||
StringBuilder _b=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
@ -101,7 +102,7 @@ char* __ReadString(DeserializeSharedData* shared){
|
||||
else {
|
||||
char* str=StringBuilder_build(b);
|
||||
Autoarr_clear(b);
|
||||
return str;
|
||||
return SUCCESS(UniPtr(CharPtr,str));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -109,24 +110,24 @@ char* __ReadString(DeserializeSharedData* shared){
|
||||
StringBuilder_append_char(b,c);
|
||||
}
|
||||
}
|
||||
throw(ERR_ENDOFSTR);
|
||||
return NULL;
|
||||
};
|
||||
safethrow(ERR_ENDOFSTR);
|
||||
}
|
||||
#define ReadString() __ReadString(shared)
|
||||
|
||||
Autoarr(Unitype)* __ReadList(DeserializeSharedData* shared){
|
||||
Maybe __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());
|
||||
try(ReadValue(), val)
|
||||
Autoarr_add(list,val.value);
|
||||
if (!readingList) break;
|
||||
}
|
||||
return list;
|
||||
return SUCCESS(UniPtr(AutoarrUnitypePtr,list));
|
||||
};
|
||||
#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));
|
||||
const string nullStr={"null",4};
|
||||
const string trueStr={"true",4};
|
||||
@ -134,18 +135,18 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
|
||||
switch(*str.ptr){
|
||||
case 'n':
|
||||
if(string_compare(str,nullStr))
|
||||
return UniNull;
|
||||
else throw_wrongchar(*str.ptr);
|
||||
return SUCCESS(UniNull);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
case 't':
|
||||
if(string_compare(str,trueStr))
|
||||
return UniTrue;
|
||||
else throw_wrongchar(*str.ptr);
|
||||
return SUCCESS(UniTrue);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
case 'f':
|
||||
if(string_compare(str,falseStr))
|
||||
return UniFalse;
|
||||
else throw_wrongchar(*str.ptr);
|
||||
return SUCCESS(UniFalse);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
default:
|
||||
switch(str.ptr[str.length-1]){
|
||||
@ -153,14 +154,14 @@ Unitype __ParseValue(DeserializeSharedData* shared, string str){
|
||||
char* _c=string_cpToCptr(str);
|
||||
Unitype rez=Uni(Float64,strtod(_c,NULL));
|
||||
free(_c);
|
||||
return rez;
|
||||
return SUCCESS(rez);
|
||||
}
|
||||
case 'u': {
|
||||
uint64 lu=0;
|
||||
char* _c=string_cpToCptr(str);
|
||||
sscanf(_c,"%lu",&lu);
|
||||
free(_c);
|
||||
return Uni(UInt64,lu);
|
||||
return SUCCESS(Uni(UInt64,lu));
|
||||
}
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
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);
|
||||
if(sscanf(_c,"%li",&li)!=1){
|
||||
char err[64];
|
||||
sprintf(err,"can't parse to int: <%s>",_c);
|
||||
throw(err);
|
||||
IFWIN(
|
||||
sprintf_s(err,64,"can't parse to int: <%s>",_c),
|
||||
sprintf(err,"can't parse to int: <%s>",_c)
|
||||
);
|
||||
safethrow(err);
|
||||
}
|
||||
|
||||
free(_c);
|
||||
return Uni(Int64,li);
|
||||
return SUCCESS(Uni(Int64,li));
|
||||
}
|
||||
default:
|
||||
throw_wrongchar(str.ptr[str.length-1]);
|
||||
safethrow_wrongchar(str.ptr[str.length-1]);
|
||||
}
|
||||
}
|
||||
throw(ERR_ENDOFSTR);
|
||||
return UniNull;
|
||||
safethrow(ERR_ENDOFSTR);
|
||||
};
|
||||
#define ParseValue(str) __ParseValue(shared, str)
|
||||
|
||||
Unitype __ReadValue(DeserializeSharedData* shared){
|
||||
Maybe __ReadValue(DeserializeSharedData* shared){
|
||||
char c;
|
||||
string valueStr={text+1,0};
|
||||
Unitype value;
|
||||
@ -192,57 +194,55 @@ Unitype __ReadValue(DeserializeSharedData* shared){
|
||||
case ' ': case '\t':
|
||||
case '\r': case '\n':
|
||||
if(valueStr.length!=0)
|
||||
throw_wrongchar(c);
|
||||
safethrow_wrongchar(c);
|
||||
valueStr.ptr++;
|
||||
break;
|
||||
case '=': case ':':
|
||||
case '}': case '$':
|
||||
throw_wrongchar(c);
|
||||
case '\'':
|
||||
safethrow_wrongchar(c);
|
||||
break;
|
||||
case '#':;
|
||||
char _c=c;
|
||||
SkipComment();
|
||||
try(SkipComment(),_);
|
||||
if(valueStr.length!=0)
|
||||
throw_wrongchar(_c);
|
||||
safethrow_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;
|
||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||
try(ReadString(),maybeString)
|
||||
value=maybeString.value;
|
||||
break;
|
||||
case '[':
|
||||
if(valueStr.length!=0) throw_wrongchar(c);
|
||||
value=UniPtr(AutoarrUnitypePtr,ReadList());
|
||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||
try(ReadList(),maybeList)
|
||||
value=maybeList.value;
|
||||
case ']':
|
||||
readingList=false;
|
||||
break;
|
||||
case '{':
|
||||
if(valueStr.length!=0) throw_wrongchar(c);
|
||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||
++text; //skips '{'
|
||||
value=UniPtr(HashtablePtr, __deserialize(&text,true));
|
||||
return value;
|
||||
try(__deserialize(&text,true), val)
|
||||
return SUCCESS(val.value);
|
||||
case ';':
|
||||
case ',':
|
||||
if(valueStr.length!=0)
|
||||
value=ParseValue(valueStr);
|
||||
return value;
|
||||
if(valueStr.length!=0){
|
||||
try(ParseValue(valueStr),maybeParsed)
|
||||
value=maybeParsed.value;
|
||||
}
|
||||
return SUCCESS(value);
|
||||
default:
|
||||
valueStr.length++;
|
||||
break;
|
||||
}
|
||||
|
||||
throw(ERR_ENDOFSTR);
|
||||
return UniNull;
|
||||
};
|
||||
safethrow(ERR_ENDOFSTR);
|
||||
}
|
||||
|
||||
|
||||
Hashtable* __deserialize(char** _text, bool _calledRecursively) {
|
||||
Maybe __deserialize(char** _text, bool _calledRecursively) {
|
||||
DeserializeSharedData _shared={
|
||||
.sh_text=*_text,
|
||||
.sh_partOfDollarList=false,
|
||||
@ -255,31 +255,31 @@ Hashtable* __deserialize(char** _text, bool _calledRecursively) {
|
||||
|
||||
text--;
|
||||
while((c=*++text)){
|
||||
string name=ReadName();
|
||||
if(name.length==0) //end of file or '}' in recursive call
|
||||
try(ReadName(), maybeName)
|
||||
if(!maybeName.value.VoidPtr) //end of file or '}' in recursive call
|
||||
goto END;
|
||||
char* nameCPtr=string_cpToCptr(name);
|
||||
Unitype value=ReadValue();
|
||||
if(partOfDollarList){
|
||||
Autoarr(Unitype)* list;
|
||||
Unitype lu;
|
||||
if(Hashtable_try_get(dict,nameCPtr, &lu)){
|
||||
list=(Autoarr(Unitype)*)lu.VoidPtr;
|
||||
char* nameCPtr=maybeName.value.VoidPtr;
|
||||
try(ReadValue(), val)
|
||||
if(partOfDollarList){
|
||||
Autoarr(Unitype)* list;
|
||||
Unitype lu;
|
||||
if(Hashtable_try_get(dict,nameCPtr, &lu)){
|
||||
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{
|
||||
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);
|
||||
else Hashtable_add(dict,nameCPtr,val.value);
|
||||
}
|
||||
END:
|
||||
*_text=text;
|
||||
return dict;
|
||||
return SUCCESS(UniPtr(HashtablePtr,dict));
|
||||
}
|
||||
|
||||
Hashtable* DtsodV24_deserialize(char* _text) {
|
||||
Maybe DtsodV24_deserialize(char* _text) {
|
||||
return __deserialize(&_text, false);
|
||||
}
|
||||
|
||||
@ -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,37 +10,39 @@ extern "C" {
|
||||
|
||||
#include "DtsodV24.h"
|
||||
|
||||
//parses text to binary values
|
||||
EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output){
|
||||
*output=DtsodV24_deserialize(text);
|
||||
// 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);
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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++)
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
24
StringFragment/StringBuilder.h
Normal file
24
StringFragment/StringBuilder.h
Normal 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
|
||||
42
StringFragment/StringFragment.c
Normal file
42
StringFragment/StringFragment.c
Normal 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;
|
||||
}
|
||||
29
StringFragment/StringFragment.h
Normal file
29
StringFragment/StringFragment.h
Normal 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
|
||||
37
base/base.h
37
base/base.h
@ -10,23 +10,26 @@ extern "C" {
|
||||
#include "mystr.h"
|
||||
|
||||
// executes codeblock and prints execution time
|
||||
/*#define optime(opname,repeats,codeblock) ({\
|
||||
struct timespec start, stop;\
|
||||
clock_gettime(CLOCK_REALTIME, &start);\
|
||||
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
clock_gettime(CLOCK_REALTIME, &stop);\
|
||||
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();\
|
||||
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
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);\
|
||||
})
|
||||
#ifdef CLOCK_REALTIME // non-standard high-precision clock
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
struct timespec start, stop;\
|
||||
clock_gettime(CLOCK_REALTIME, &start);\
|
||||
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
clock_gettime(CLOCK_REALTIME, &stop);\
|
||||
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
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
clock_t start=clock();\
|
||||
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
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
|
||||
}
|
||||
|
||||
36
base/cptr.c
Normal file
36
base/cptr.c
Normal 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
23
base/cptr.h
Normal 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
|
||||
@ -5,7 +5,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -48,7 +48,7 @@ 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);
|
||||
|
||||
|
||||
@ -204,10 +204,9 @@
|
||||
<ClInclude Include="Autoarr\Autoarr.h" />
|
||||
<ClInclude Include="Autoarr\Autoarr_declare.h" />
|
||||
<ClInclude Include="Autoarr\Autoarr_define.h" />
|
||||
<ClInclude Include="Autoarr\StringBuilder.h" />
|
||||
<ClInclude Include="base\base.h" />
|
||||
<ClInclude Include="base\cptr.h" />
|
||||
<ClInclude Include="base\errors.h" />
|
||||
<ClInclude Include="base\mystr.h" />
|
||||
<ClInclude Include="base\std.h" />
|
||||
<ClInclude Include="base\types.h" />
|
||||
<ClInclude Include="DtsodParser\DtsodV24.h" />
|
||||
@ -215,14 +214,17 @@
|
||||
<ClInclude Include="Hashtable\Hashtable.h" />
|
||||
<ClInclude Include="Hashtable\KeyValuePair.h" />
|
||||
<ClInclude Include="SearchTree\SearchTree.h" />
|
||||
<ClInclude Include="StringFragment\StringBuilder.h" />
|
||||
<ClInclude Include="StringFragment\StringFragment.h" />
|
||||
<ClInclude Include="tests\tests.h" />
|
||||
<ClInclude Include="tests\test_marshalling.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Autoarr\Autoarr.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\mystr.c" />
|
||||
<ClCompile Include="base\types.c" />
|
||||
<ClCompile Include="DtsodParser\DtsodV24.c" />
|
||||
<ClCompile Include="DtsodParser\DtsodV24_deserialize.c" />
|
||||
@ -232,6 +234,8 @@
|
||||
<ClCompile Include="Hashtable\Hashtable.c" />
|
||||
<ClCompile Include="Hashtable\KeyValuePair.c" />
|
||||
<ClCompile Include="SearchTree\SearchTree.c" />
|
||||
<ClCompile Include="StringFragment\StringBuilder.c" />
|
||||
<ClCompile Include="StringFragment\StringFragment.c" />
|
||||
<ClCompile Include="tests\main.c" />
|
||||
<ClCompile Include="tests\test_autoarr.c" />
|
||||
<ClCompile Include="tests\test_dtsod.c" />
|
||||
|
||||
@ -63,6 +63,15 @@
|
||||
<ClInclude Include="tests\tests.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</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>
|
||||
<ClCompile Include="Autoarr\Autoarr.c">
|
||||
@ -125,5 +134,14 @@
|
||||
<ClCompile Include="DtsodParser\DtsodV24_exported.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</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>
|
||||
</Project>
|
||||
Loading…
Reference in New Issue
Block a user