updated throw_wrongchar(), removoved null parsing, commits cleared
This commit is contained in:
parent
b4f2ca92c7
commit
394fbffc12
@ -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++)
|
||||||
|
|||||||
@ -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)({\
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
@ -6,25 +6,25 @@ extern "C" {
|
|||||||
|
|
||||||
#include "../Hashtable/Hashtable.h"
|
#include "../Hashtable/Hashtable.h"
|
||||||
|
|
||||||
//parses text to binary values
|
// parses text to binary values
|
||||||
Maybe 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
|
||||||
|
|||||||
@ -6,19 +6,36 @@
|
|||||||
#define STRB_BC 64
|
#define STRB_BC 64
|
||||||
#define STRB_BL 1024
|
#define STRB_BL 1024
|
||||||
|
|
||||||
Maybe ERROR_WRONGCHAR(char c, char* text){
|
// special func for throwing error messages about wrong characters in deserializing text
|
||||||
char errBuf[]="unexpected <c> at:\n \""
|
Maybe ERROR_WRONGCHAR(const char c, char* text, char* text_first, const char* srcfile, int line, const char* funcname){
|
||||||
"00000000000000000000000000000000\"";
|
char errBuf[33];
|
||||||
errBuf[12]=c;
|
errBuf[32]='\0';
|
||||||
for(uint8 i=0; i<32; i++)
|
char* errText=text-16;
|
||||||
|
if(errText<text_first) errText=text_first;
|
||||||
|
for(uint8 i=0; i<32; i++){
|
||||||
// writes 16 chars before and 15 after the wrongchar
|
// writes 16 chars before and 15 after the wrongchar
|
||||||
errBuf[i+22]=*(text - 16 + i);
|
char _c=errText[i];
|
||||||
safethrow(cptr_copy(errBuf));
|
errBuf[i]=_c;
|
||||||
|
if(!_c) break;
|
||||||
|
}
|
||||||
|
char* errmsg=malloc(256);
|
||||||
|
IFWIN(
|
||||||
|
sprintf_s(errmsg,256, "unexpected <%c> at:\n"
|
||||||
|
" \"%s\"\n"
|
||||||
|
"\\___[%s:%d] %s()",
|
||||||
|
c,errBuf, srcfile,line,funcname),
|
||||||
|
sprintf(errmsg, "unexpected <%c> at:\n"
|
||||||
|
" \"%s\"\n"
|
||||||
|
" \\___[%s:%d] %s()",
|
||||||
|
c,errBuf, srcfile,line,funcname)
|
||||||
|
);
|
||||||
|
safethrow(cptr_copy(errmsg));
|
||||||
}
|
}
|
||||||
#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text)
|
#define safethrow_wrongchar(C) return ERROR_WRONGCHAR(C, text, shared->sh_text_first, __FILE__,__LINE__,__func__)
|
||||||
|
|
||||||
|
|
||||||
typedef struct DeserializeSharedData{
|
typedef struct DeserializeSharedData{
|
||||||
|
const char* sh_text_first;
|
||||||
char* sh_text;
|
char* sh_text;
|
||||||
bool sh_partOfDollarList;
|
bool sh_partOfDollarList;
|
||||||
bool sh_readingList;
|
bool sh_readingList;
|
||||||
@ -58,7 +75,7 @@ Maybe __ReadName(DeserializeSharedData* shared){
|
|||||||
try(SkipComment(),_);
|
try(SkipComment(),_);
|
||||||
if(nameStr.length!=0)
|
if(nameStr.length!=0)
|
||||||
safethrow_wrongchar(c);
|
safethrow_wrongchar(c);
|
||||||
nameStr.ptr=text+1; //skips '\n'
|
nameStr.ptr=text+1; // skips '\n'
|
||||||
break;
|
break;
|
||||||
case '}':
|
case '}':
|
||||||
if(!calledRecursively) safethrow_wrongchar(c);
|
if(!calledRecursively) safethrow_wrongchar(c);
|
||||||
@ -86,7 +103,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively);
|
|||||||
Maybe __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
|
||||||
Maybe __ReadString(DeserializeSharedData* shared){
|
Maybe __ReadString(DeserializeSharedData* shared){
|
||||||
char c;
|
char c;
|
||||||
bool prevIsBackslash=false;
|
bool prevIsBackslash=false;
|
||||||
@ -95,7 +112,7 @@ Maybe __ReadString(DeserializeSharedData* shared){
|
|||||||
while ((c=*++text)){
|
while ((c=*++text)){
|
||||||
if(c=='"') {
|
if(c=='"') {
|
||||||
if(prevIsBackslash) {
|
if(prevIsBackslash) {
|
||||||
//replacing <\"> with <">
|
// replacing <\"> with <">
|
||||||
Autoarr_remove(b);
|
Autoarr_remove(b);
|
||||||
StringBuilder_append_char(b,c);
|
StringBuilder_append_char(b,c);
|
||||||
}
|
}
|
||||||
@ -128,59 +145,51 @@ Maybe __ReadList(DeserializeSharedData* shared){
|
|||||||
#define ReadList() __ReadList(shared)
|
#define ReadList() __ReadList(shared)
|
||||||
|
|
||||||
Maybe __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};
|
const string trueStr={"true",4};
|
||||||
const string falseStr={"false",5};
|
const string falseStr={"false",5};
|
||||||
switch(*str.ptr){
|
switch(str.ptr[str.length-1]){
|
||||||
case 'n':
|
// Bool
|
||||||
if(string_compare(str,nullStr))
|
case 'e':
|
||||||
return SUCCESS(UniNull);
|
|
||||||
else safethrow_wrongchar(*str.ptr);
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
if(string_compare(str,trueStr))
|
if(string_compare(str,trueStr))
|
||||||
return SUCCESS(UniTrue);
|
return SUCCESS(UniTrue);
|
||||||
else safethrow_wrongchar(*str.ptr);
|
else if(string_compare(str,falseStr))
|
||||||
break;
|
|
||||||
case 'f':
|
|
||||||
if(string_compare(str,falseStr))
|
|
||||||
return SUCCESS(UniFalse);
|
return SUCCESS(UniFalse);
|
||||||
else safethrow_wrongchar(*str.ptr);
|
else safethrow_wrongchar(*str.ptr);
|
||||||
break;
|
break;
|
||||||
default:
|
// Float64
|
||||||
switch(str.ptr[str.length-1]){
|
case 'f': {
|
||||||
case 'f': {
|
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 SUCCESS(rez);
|
||||||
return SUCCESS(rez);
|
}
|
||||||
}
|
// UInt64
|
||||||
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 SUCCESS(Uni(UInt64,lu));
|
return SUCCESS(Uni(UInt64,lu));
|
||||||
}
|
}
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
// Int64
|
||||||
case '5': case '6': case '7': case '8': case '9': {
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
int64 li=0;
|
case '5': case '6': case '7': case '8': case '9': {
|
||||||
char* _c=string_cpToCptr(str);
|
int64 li=0;
|
||||||
if(sscanf(_c,"%li",&li)!=1){
|
char* _c=string_cpToCptr(str);
|
||||||
char err[64];
|
if(sscanf(_c,"%li",&li)!=1){
|
||||||
IFWIN(
|
char err[64];
|
||||||
sprintf_s(err,64,"can't parse to int: <%s>",_c),
|
IFWIN(
|
||||||
sprintf(err,"can't parse to int: <%s>",_c)
|
sprintf_s(err,64,"can't parse to int: <%s>",_c),
|
||||||
);
|
sprintf(err,"can't parse to int: <%s>",_c)
|
||||||
safethrow(err);
|
);
|
||||||
}
|
safethrow(err);
|
||||||
free(_c);
|
|
||||||
return SUCCESS(Uni(Int64,li));
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
safethrow_wrongchar(str.ptr[str.length-1]);
|
|
||||||
}
|
}
|
||||||
|
free(_c);
|
||||||
|
return SUCCESS(Uni(Int64,li));
|
||||||
|
}
|
||||||
|
// wrong type
|
||||||
|
default:
|
||||||
|
safethrow_wrongchar(str.ptr[str.length-1]);
|
||||||
}
|
}
|
||||||
safethrow(ERR_ENDOFSTR);
|
safethrow(ERR_ENDOFSTR);
|
||||||
};
|
};
|
||||||
@ -207,7 +216,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
|||||||
try(SkipComment(),_);
|
try(SkipComment(),_);
|
||||||
if(valueStr.length!=0)
|
if(valueStr.length!=0)
|
||||||
safethrow_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) safethrow_wrongchar(c);
|
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||||
@ -223,7 +232,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
|||||||
break;
|
break;
|
||||||
case '{':
|
case '{':
|
||||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||||
++text; //skips '{'
|
++text; // skips '{'
|
||||||
try(__deserialize(&text,true), val)
|
try(__deserialize(&text,true), val)
|
||||||
return SUCCESS(val.value);
|
return SUCCESS(val.value);
|
||||||
case ';':
|
case ';':
|
||||||
@ -244,6 +253,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
|||||||
|
|
||||||
Maybe __deserialize(char** _text, bool _calledRecursively) {
|
Maybe __deserialize(char** _text, bool _calledRecursively) {
|
||||||
DeserializeSharedData _shared={
|
DeserializeSharedData _shared={
|
||||||
|
.sh_text_first=*_text,
|
||||||
.sh_text=*_text,
|
.sh_text=*_text,
|
||||||
.sh_partOfDollarList=false,
|
.sh_partOfDollarList=false,
|
||||||
.sh_readingList=false,
|
.sh_readingList=false,
|
||||||
@ -256,7 +266,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
|
|||||||
text--;
|
text--;
|
||||||
while((c=*++text)){
|
while((c=*++text)){
|
||||||
try(ReadName(), maybeName)
|
try(ReadName(), maybeName)
|
||||||
if(!maybeName.value.VoidPtr) //end of file or '}' in recursive call
|
if(!maybeName.value.VoidPtr) // end of file or '}' in recursive call
|
||||||
goto END;
|
goto END;
|
||||||
char* nameCPtr=maybeName.value.VoidPtr;
|
char* nameCPtr=maybeName.value.VoidPtr;
|
||||||
try(ReadValue(), val)
|
try(ReadValue(), val)
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
//
|
//
|
||||||
// I planned to export functions from DtsodV24.h,
|
// I planned to export functions from DtsodV24.h,
|
||||||
// but C# P/Invoke can't get returned values for some reason.
|
// 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
|
// Following functions return values by pointer, which looks in C# like out parameter
|
||||||
//
|
//
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -10,39 +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, char** errmsg){
|
EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output, char** errmsg){
|
||||||
Maybe r=DtsodV24_deserialize(text);
|
Maybe r=DtsodV24_deserialize(text);
|
||||||
*errmsg= r.errmsg ? r.errmsg : NULL;
|
*errmsg= r.errmsg ? r.errmsg : NULL;
|
||||||
*output=r.value.VoidPtr;
|
*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);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
@ -51,8 +51,7 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){
|
|||||||
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
||||||
break;
|
break;
|
||||||
case Null:
|
case Null:
|
||||||
if(!u.VoidPtr) StringBuilder_append_cptr(b, "null");
|
throw("Null isn't supported in DtsodV24");
|
||||||
else throw("Null-type pointer is not 0");
|
|
||||||
break;
|
break;
|
||||||
case AutoarrUnitypePtr:
|
case AutoarrUnitypePtr:
|
||||||
addc('[');
|
addc('[');
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -9,35 +9,35 @@ 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}
|
||||||
|
|
||||||
//
|
//
|
||||||
// don't add pairs with the same keys,
|
// don't add pairs with the same keys,
|
||||||
// or something weird will happen
|
// or something weird will happen
|
||||||
//
|
//
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
@ -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++)
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
2
Makefile
2
Makefile
@ -3,7 +3,7 @@ TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c)
|
|||||||
OUTDIR=bin
|
OUTDIR=bin
|
||||||
CMP=gcc
|
CMP=gcc
|
||||||
|
|
||||||
all: build_test
|
all: clear_c build_lib
|
||||||
|
|
||||||
clear_c:
|
clear_c:
|
||||||
clear
|
clear
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ extern "C" {
|
|||||||
#include "mystr.h"
|
#include "mystr.h"
|
||||||
|
|
||||||
// executes codeblock and prints execution time
|
// 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) ({\
|
#define optime(opname,repeats,codeblock) ({\
|
||||||
struct timespec start, stop;\
|
struct timespec start, stop;\
|
||||||
clock_gettime(CLOCK_REALTIME, &start);\
|
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;\
|
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);\
|
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
|
||||||
})
|
})
|
||||||
#else //
|
#else //
|
||||||
#define optime(opname,repeats,codeblock) ({\
|
#define optime(opname,repeats,codeblock) ({\
|
||||||
clock_t start=clock();\
|
clock_t start=clock();\
|
||||||
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
|
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
|
||||||
|
|||||||
@ -8,7 +8,7 @@ extern "C" {
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
|||||||
16
base/mystr.c
16
base/mystr.c
@ -1,13 +1,13 @@
|
|||||||
#include "base.h"
|
#include "base.h"
|
||||||
|
|
||||||
//returns length of string (including \0)
|
// returns length of string (including \0)
|
||||||
uint32 cptr_length(char* str){
|
uint32 cptr_length(char* str){
|
||||||
uint32 len=0;
|
uint32 len=0;
|
||||||
while(*(str++)) len++;
|
while(*(str++)) len++;
|
||||||
return ++len;
|
return ++len;
|
||||||
}
|
}
|
||||||
|
|
||||||
//allocates new char[] and copies src there
|
// allocates new char[] and copies src there
|
||||||
char* cptr_copy(char* src){
|
char* cptr_copy(char* src){
|
||||||
uint32 len=cptr_length(src);
|
uint32 len=cptr_length(src);
|
||||||
char* dst=malloc(len*sizeof(char));
|
char* dst=malloc(len*sizeof(char));
|
||||||
@ -16,7 +16,7 @@ char* cptr_copy(char* src){
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
//compares two char buffers, NullPtr-friendly
|
// compares two char buffers, NullPtr-friendly
|
||||||
bool cptr_compare(char* key0, char* key1){
|
bool cptr_compare(char* key0, char* key1){
|
||||||
if(!key0) return key1 ? false : true;
|
if(!key0) return key1 ? false : true;
|
||||||
if(!key1) return false;
|
if(!key1) return false;
|
||||||
@ -26,7 +26,7 @@ bool cptr_compare(char* key0, char* key1){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//multiplies char n times
|
// multiplies char n times
|
||||||
char* char_multiply(char c, uint32 n){
|
char* char_multiply(char c, uint32 n){
|
||||||
char* rez=malloc(n+1);
|
char* rez=malloc(n+1);
|
||||||
rez[n]=0;
|
rez[n]=0;
|
||||||
@ -35,7 +35,7 @@ char* char_multiply(char c, uint32 n){
|
|||||||
return rez;
|
return rez;
|
||||||
}
|
}
|
||||||
|
|
||||||
//copies str content to new char pointer value (adding '\0' at the end)
|
// copies str content to new char pointer value (adding '\0' at the end)
|
||||||
char* string_cpToCptr(string str){
|
char* string_cpToCptr(string str){
|
||||||
if(str.length==0) return NULL;
|
if(str.length==0) return NULL;
|
||||||
char* cptr=malloc(str.length*sizeof(char)+1);
|
char* cptr=malloc(str.length*sizeof(char)+1);
|
||||||
@ -45,7 +45,7 @@ char* string_cpToCptr(string str){
|
|||||||
return cptr;
|
return cptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
//copies cptr content (excluding '\0' at the end) to new string
|
// copies cptr content (excluding '\0' at the end) to new string
|
||||||
string string_cpFromCharPtr(char* cptr){
|
string string_cpFromCharPtr(char* cptr){
|
||||||
if(!cptr) return stringNull;
|
if(!cptr) return stringNull;
|
||||||
string str;
|
string str;
|
||||||
@ -56,7 +56,7 @@ string string_cpFromCharPtr(char* cptr){
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
//compares two strings, NullPtr-friendly
|
// compares two strings, NullPtr-friendly
|
||||||
bool string_compare(string str0, string str1){
|
bool string_compare(string str0, string str1){
|
||||||
if(str0.length!=str1.length) return false;
|
if(str0.length!=str1.length) return false;
|
||||||
if(!str0.ptr) return str1.ptr ? false : true;
|
if(!str0.ptr) return str1.ptr ? false : true;
|
||||||
@ -67,7 +67,7 @@ bool string_compare(string str0, string str1){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//creates new string which is reversed variant of <s>
|
// creates new string which is reversed variant of <s>
|
||||||
string string_reverse(string s){
|
string string_reverse(string s){
|
||||||
if(s.length==0) return s;
|
if(s.length==0) return s;
|
||||||
string r={malloc(s.length), s.length};
|
string r={malloc(s.length), s.length};
|
||||||
|
|||||||
24
base/mystr.h
24
base/mystr.h
@ -6,37 +6,37 @@ extern "C" {
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
//returns length of string (including \0)
|
// returns length of string (including \0)
|
||||||
uint32 cptr_length(char* str);
|
uint32 cptr_length(char* str);
|
||||||
|
|
||||||
//allocates new char[] and copies src there
|
// allocates new char[] and copies src there
|
||||||
char* cptr_copy(char* src);
|
char* cptr_copy(char* src);
|
||||||
|
|
||||||
//compares two char buffers, NullPtr-friendly
|
// compares two char buffers, NullPtr-friendly
|
||||||
bool cptr_compare(char* key0, char* key1);
|
bool cptr_compare(char* key0, char* key1);
|
||||||
|
|
||||||
//multiplies char n times
|
// multiplies char n times
|
||||||
char* char_multiply(char c, uint32 n);
|
char* char_multiply(char c, uint32 n);
|
||||||
|
|
||||||
//my fixed length string struct
|
// my fixed length string struct
|
||||||
//doesn't store '\0' at the end
|
// doesn't store '\0' at the end
|
||||||
typedef struct string{
|
typedef struct string{
|
||||||
char* ptr; //char pointer
|
char* ptr; // char pointer
|
||||||
uint32 length; //amount of chars in ptr value
|
uint32 length; // amount of chars in ptr value
|
||||||
} string;
|
} string;
|
||||||
|
|
||||||
static const string stringNull={NULL,0};
|
static const string stringNull={NULL,0};
|
||||||
|
|
||||||
//copies str content to new char pointer value (adding '\0' at the end)
|
// copies str content to new char pointer value (adding '\0' at the end)
|
||||||
char* string_cpToCptr(string str);
|
char* string_cpToCptr(string str);
|
||||||
|
|
||||||
//copies cptr content (excluding '\0' at the end) to new string
|
// copies cptr content (excluding '\0' at the end) to new string
|
||||||
string string_cpFromCharPtr(char* cptr);
|
string string_cpFromCharPtr(char* cptr);
|
||||||
|
|
||||||
//compares two strings, NullPtr-friendly
|
// compares two strings, NullPtr-friendly
|
||||||
bool string_compare(string str0, string str1);
|
bool string_compare(string str0, string str1);
|
||||||
|
|
||||||
//creates new string which is reversed variant of <s>
|
// creates new string which is reversed variant of <s>
|
||||||
string string_reverse(string s);
|
string string_reverse(string s);
|
||||||
|
|
||||||
#if __cplusplus
|
#if __cplusplus
|
||||||
|
|||||||
@ -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:
|
||||||
|
|||||||
@ -47,7 +47,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);
|
||||||
void sprintuni(char* s, Unitype v);
|
void sprintuni(char* s, Unitype v);
|
||||||
|
|||||||
@ -13,7 +13,7 @@ void test_all(){
|
|||||||
int main(){
|
int main(){
|
||||||
setlocale(LC_ALL, "en-US.Unicode");
|
setlocale(LC_ALL, "en-US.Unicode");
|
||||||
printf("\e[92mdtsod parser in c language!\e[97m\n");
|
printf("\e[92mdtsod parser in c language!\e[97m\n");
|
||||||
optime("test_all",1,{test_all();});
|
optime("test_all",1,test_all());
|
||||||
printf("\e[0m\n");
|
printf("\e[0m\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user