serialize()
This commit is contained in:
parent
8c9431460e
commit
6cc3acea1d
@ -17,3 +17,16 @@ declare_Autoarr(Unitype)
|
||||
|
||||
//right func to clear array of unitype values
|
||||
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar);
|
||||
|
||||
#define Autoarr_foreach(ar,elem,codeblock)({\
|
||||
typeof(**ar->values) elem;\
|
||||
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)\
|
||||
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++){\
|
||||
elem=ar->values[blockI][elemI];\
|
||||
(codeblock);\
|
||||
}\
|
||||
for(uint32 elemI=0;elemI<ar->block_length;elemI++){\
|
||||
elem=ar->values[ar->blocks_count-1][elemI];\
|
||||
(codeblock);\
|
||||
}\
|
||||
})
|
||||
|
||||
@ -50,3 +50,12 @@ Autoarr_##type __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block
|
||||
autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length)
|
||||
#define Autoarr_max_length(autoarr)\
|
||||
(uint32)(autoarr->max_block_length*autoarr->max_blocks_count)
|
||||
|
||||
#define Autoarr_remove(AR){\
|
||||
if(AR->block_length==1){\
|
||||
AR->blocks_count--;\
|
||||
AR->block_length=AR->max_block_length;\
|
||||
free(AR->values[AR->blocks_count]);\
|
||||
}\
|
||||
else AR->block_length--;\
|
||||
}
|
||||
|
||||
@ -4,16 +4,61 @@ StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_len
|
||||
return Autoarr_create(int8,max_blocks_count,max_block_length);
|
||||
}
|
||||
|
||||
void StringBuilder_append(StringBuilder* b, char c){
|
||||
void StringBuilder_append_char(StringBuilder* b, char c){
|
||||
Autoarr_add(b,c);
|
||||
}
|
||||
|
||||
void StringBuilder_append_str(StringBuilder* b, char* s){
|
||||
void StringBuilder_append_cptr(StringBuilder* b, char* s){
|
||||
char c;
|
||||
while((c=*s++))
|
||||
Autoarr_add(b,c);
|
||||
}
|
||||
|
||||
void StringBuilder_append_string(StringBuilder* b, string s){
|
||||
while(s.length>0){
|
||||
Autoarr_add(b,*s.ptr++);
|
||||
s.length--;
|
||||
}
|
||||
}
|
||||
|
||||
void StringBuilder_append_int64(StringBuilder* b, int64 a){
|
||||
uint8 i=0;
|
||||
if(a==0){
|
||||
Autoarr_add(b,'0');
|
||||
}
|
||||
else if(a<0){
|
||||
Autoarr_add(b,'-');
|
||||
a=-a;
|
||||
}
|
||||
char buf[24];
|
||||
while(a!=0){
|
||||
buf[i++]='0'+a%10;
|
||||
a/=10;
|
||||
}
|
||||
string rev=string_reverse((string){buf,i});
|
||||
StringBuilder_append_string(b,rev);
|
||||
}
|
||||
|
||||
void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
|
||||
uint8 i=0;
|
||||
if(a==0){
|
||||
Autoarr_add(b,'0');
|
||||
}
|
||||
char buf[24];
|
||||
while(a!=0){
|
||||
buf[i++]='0'+a%10;
|
||||
a/=10;
|
||||
}
|
||||
string rev=string_reverse((string){buf,i});
|
||||
StringBuilder_append_string(b,rev);
|
||||
}
|
||||
|
||||
void StringBuilder_append_double(StringBuilder* b, double a){
|
||||
char buf[32];
|
||||
sprintf(buf,32,"%f",a);
|
||||
StringBuilder_append_cptr(b,buf);
|
||||
}
|
||||
|
||||
char* StringBuilder_build(StringBuilder* b){
|
||||
uint32 len=Autoarr_length(b);
|
||||
char* str=malloc(len+1);
|
||||
|
||||
@ -5,6 +5,10 @@
|
||||
typedef Autoarr(int8) StringBuilder;
|
||||
|
||||
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
|
||||
void StringBuilder_append(StringBuilder* b, char c);
|
||||
void StringBuilder_append_str(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cptr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_string(StringBuilder* b, string s);
|
||||
void StringBuilder_append_int64(StringBuilder* b, int64 a);
|
||||
void StringBuilder_append_uint64(StringBuilder* b, uint64 a);
|
||||
void StringBuilder_append_double(StringBuilder* b, double a);
|
||||
char* StringBuilder_build(StringBuilder* b);
|
||||
|
||||
@ -3,4 +3,4 @@
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
|
||||
Hashtable* DtsodV24_deserialize(char* text);
|
||||
string DtsodV24_serialize(Hashtable* dtsod);
|
||||
char* DtsodV24_serialize(Hashtable* dtsod);
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
#include "DtsodV24.h"
|
||||
#include "../Autoarr/StringBuilder.h"
|
||||
|
||||
#define ARR_BC 2
|
||||
#define ARR_BL 8
|
||||
#define ARR_BC 8
|
||||
#define ARR_BL 16
|
||||
|
||||
Hashtable* __deserialize(char** _text, bool calledRecursively){
|
||||
Hashtable* dict=Hashtable_create();
|
||||
@ -11,17 +10,17 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
|
||||
bool partOfDollarList=false;
|
||||
bool readingList=false;
|
||||
|
||||
void __throw_wrongchar(char* fname,char _c){
|
||||
void __throw_wrongchar(char* file, int line, char* fname,char _c){
|
||||
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[31mfunc: %s\n",fname);
|
||||
printf("\n\e[31m>%s:%d/%s\n",file,line,fname);
|
||||
throw(errBuf);
|
||||
};
|
||||
#define throw_wrongchar(C) __throw_wrongchar(__func__,C)
|
||||
#define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C)
|
||||
|
||||
|
||||
void SkipComment(){
|
||||
@ -31,7 +30,8 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
|
||||
|
||||
string ReadName(){
|
||||
string nameStr={text,0};
|
||||
while ((c=*text++)) switch (c){
|
||||
text--;
|
||||
while ((c=*++text)) switch (c){
|
||||
case ' ': case '\t':
|
||||
case '\r': case '\n':
|
||||
if(nameStr.length!=0)
|
||||
@ -46,6 +46,8 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
|
||||
break;
|
||||
case '}':
|
||||
if(!calledRecursively) throw_wrongchar(c);
|
||||
if((*++text)!=';')
|
||||
throw_wrongchar(c);
|
||||
case ':':
|
||||
return nameStr;
|
||||
case '$':
|
||||
77
DtsodC/DtsodParser/DtsodV24_serialize.c
Normal file
77
DtsodC/DtsodParser/DtsodV24_serialize.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include "DtsodV24.h"
|
||||
#include "../Autoarr/StringBuilder.h"
|
||||
|
||||
//65536 max length!
|
||||
#define STRB_BC 64
|
||||
#define STRB_BL 1024
|
||||
|
||||
#define addc(B,C) StringBuilder_append_char(B,C)
|
||||
|
||||
void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
|
||||
|
||||
void AppendTabs(){
|
||||
for(uint8 t; t<tabs; t++)
|
||||
addc(b,'\t');
|
||||
};
|
||||
|
||||
void AppendValue(Unitype u){
|
||||
switch(u.type){
|
||||
case Int64:
|
||||
StringBuilder_append_int64(b,u.Int64);
|
||||
break;
|
||||
case UInt64:
|
||||
StringBuilder_append_uint64(b,u.UInt64);
|
||||
addc(b,'u');
|
||||
break;
|
||||
case Double:
|
||||
StringBuilder_append_double(b,u.Double);
|
||||
addc(b,'d');
|
||||
break;
|
||||
case CharPtr:
|
||||
addc(b,'"');
|
||||
StringBuilder_append_cptr(b,u.VoidPtr);
|
||||
addc(b,'"');
|
||||
break;
|
||||
case Char:
|
||||
addc(b,'\'');
|
||||
addc(b,u.Char);
|
||||
addc(b,'\'');
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
addc(b,'[');
|
||||
Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({
|
||||
addc(b,' ');
|
||||
AppendValue(e);
|
||||
addc(b,',');
|
||||
}));
|
||||
Autoarr_remove(b);
|
||||
addc(b,' ');
|
||||
addc(b,']');
|
||||
break;
|
||||
case HashtablePtr:
|
||||
addc(b,'{');
|
||||
addc(b,'\n');
|
||||
__serialize(b,tabs+1,u.VoidPtr);
|
||||
AppendTabs();
|
||||
addc(b,'}');
|
||||
break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
};
|
||||
|
||||
Hashtable_foreach(dtsod, p, ({
|
||||
AppendTabs();
|
||||
StringBuilder_append_cptr(b,p.key);
|
||||
addc(b,':');
|
||||
addc(b,' ');
|
||||
AppendValue(p.value);
|
||||
addc(b,';');
|
||||
addc(b,'\n');
|
||||
}));
|
||||
}
|
||||
|
||||
char* DtsodV24_serialize(Hashtable* dtsod){
|
||||
StringBuilder b=StringBuilder_create(STRB_BC,STRB_BL);
|
||||
__serialize(&b,0,dtsod);
|
||||
return StringBuilder_build(&b);
|
||||
}
|
||||
76
DtsodC/DtsodV24_serialize.c
Normal file
76
DtsodC/DtsodV24_serialize.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include "DtsodV24.h"
|
||||
#include "../Autoarr/StringBuilder.h"
|
||||
|
||||
#define STRB_BC 64
|
||||
#define STRB_BL 1024
|
||||
|
||||
#define addc(B,C) StringBuilder_append_char(B,C)
|
||||
|
||||
void __serialize(StringBuilder* b, uint8 tabs, Hashtable* dtsod){
|
||||
|
||||
void AppendTabs(){
|
||||
for(uint8 t; t<tabs; t++)
|
||||
addc('\t');
|
||||
};
|
||||
|
||||
void AppendValue(Unitype u){
|
||||
switch(u.type){
|
||||
case Int64:
|
||||
StringBuilder_append_int64(b,u.Int64);
|
||||
break;
|
||||
case UInt64:
|
||||
StringBuilder_append_uint64(b,u.UInt64);
|
||||
addc(b,'u');
|
||||
break;
|
||||
case Double:
|
||||
StringBuilder_append_double(b,u.Double);
|
||||
addc(b,'d');
|
||||
break;
|
||||
case String:
|
||||
addc(b,'"');
|
||||
StringBuilder_append_cptr(b,u.VoidPtr);
|
||||
addc(b,'"');
|
||||
break;
|
||||
case Char:
|
||||
addc(b,'\'');
|
||||
addc(b,u.Char);
|
||||
addc(b,'\'');
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
addc(b,'[');
|
||||
Autoarr_foreach(((Autoarr_Unitype)(u.VoidPtr)), e, ({
|
||||
addc(b,' ');
|
||||
AppendValue(e);
|
||||
addc(b,',');
|
||||
}));
|
||||
Autoarr_remove(b);
|
||||
addc(b,' ');
|
||||
addc(b,']');
|
||||
break;
|
||||
case HashtablePtr:
|
||||
addc(b,'{');
|
||||
addc(b,'\n');
|
||||
__serialize(b,tabs+1,u.VoidPtr);
|
||||
AppendTabs();
|
||||
addc(b,'}');
|
||||
break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
};
|
||||
|
||||
Hashtable_foreach(dtsod, p, ({
|
||||
AppendTabs();
|
||||
StringBuilder_append_cptr(p.key);
|
||||
addc(b,':');
|
||||
addc(b,' ');
|
||||
AppendValue(p.value);
|
||||
addc(b,';');
|
||||
addc(b,'\n');
|
||||
}));
|
||||
}
|
||||
|
||||
char* DtsodV24_serialize(Hashtable* dtsod){
|
||||
StringBuilder b=StringBuilder_create(STRB_BC,STR_BL); //65536 max!
|
||||
__serialize(&b,0,dtsod);
|
||||
return StringBuilder_build(&b);
|
||||
}
|
||||
@ -33,3 +33,11 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
|
||||
//not implemented yet
|
||||
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p);
|
||||
void Hashtable_set(Hashtable* ht, char* key, Unitype u);
|
||||
|
||||
#define Hashtable_foreach(HT, EL, codeblock)({\
|
||||
uint16 hmax=Hashtable_height(HT);\
|
||||
for(uint16 h=0; h<hmax; h++){\
|
||||
Autoarr(KeyValuePair)* AR=HT->rows+h;\
|
||||
Autoarr_foreach(AR, EL, codeblock);\
|
||||
}\
|
||||
})
|
||||
|
||||
@ -18,3 +18,9 @@ void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
|
||||
KeyValuePair_free(ar->values[ar->blocks_count-1][elemI]);
|
||||
Autoarr_clear(ar);
|
||||
}
|
||||
|
||||
void printkvp(KeyValuePair p){
|
||||
printf("{\"%s\", ",p.key);
|
||||
printuni(p.value);
|
||||
printf("}");
|
||||
}
|
||||
|
||||
@ -13,3 +13,5 @@ void KeyValuePair_free(KeyValuePair p);
|
||||
|
||||
//func to clear KVP array
|
||||
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
|
||||
|
||||
void printkvp(KeyValuePair p);
|
||||
|
||||
@ -17,13 +17,12 @@ clang: all
|
||||
|
||||
CMPARGS= -Wall -Wno-discarded-qualifiers $(SRC) $(TESTS) -o $(OUTFILE)
|
||||
build:
|
||||
@echo $(SRC)
|
||||
@echo -e '\n\e[96m----------------[build]----------------\e[0m'
|
||||
@mkdir -p $(OUTDIR)
|
||||
$(CMP) -O1 -flto $(CMPARGS)
|
||||
build_dbg:
|
||||
@mkdir -p $(OUTDIR)
|
||||
@echo -e '\n\e[96m--------------[build_dbg]--------------\e[0m'
|
||||
@mkdir -p $(OUTDIR)
|
||||
$(CMP) -O0 -g $(CMPARGS).dbg
|
||||
test:
|
||||
@echo -e '\n\e[96m----------------[test]-----------------\e[0m'
|
||||
|
||||
@ -66,3 +66,11 @@ bool string_compare(string str0, string str1){
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
//creates new string which is reversed variant of <s>
|
||||
string string_reverse(string s){
|
||||
string r={malloc(s.length), s.length};
|
||||
for(uint32 i; i<s.length; i++)
|
||||
r.ptr[i]=s.ptr[s.length-i-1];
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -31,3 +31,6 @@ string string_cpFromCharPtr(char* cptr);
|
||||
|
||||
//compares two strings, NullPtr-friendly
|
||||
bool string_compare(string str0, string str1);
|
||||
|
||||
//creates new string which is reversed variant of <s>
|
||||
string string_reverse(string s);
|
||||
|
||||
@ -114,3 +114,16 @@ void Unitype_free(Unitype u){
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printuni(Unitype v){
|
||||
switch (v.type) {
|
||||
case Null: printf("{Null}");break;
|
||||
case Double: printf("{%s:%lf}",typename(v.type),v.Double);break;
|
||||
case Char: printf("{%s:%c}",typename(v.type),v.Char);break;
|
||||
case Bool:
|
||||
case UInt64: printf("{%s:%lu}",typename(v.type),v.UInt64);break;
|
||||
case Int64: printf("{%s:%ld}",typename(v.type),v.Int64);break;
|
||||
default: printf("{%s:%p}",typename(v.type),v.VoidPtr);break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,3 +49,4 @@ static const Unitype UniFalse={Bool,.Bool=false};
|
||||
|
||||
//frees VoidPtr value or does nothing if type isn't pointer
|
||||
void Unitype_free(Unitype u);
|
||||
void printuni(Unitype v);
|
||||
|
||||
@ -1,24 +1,11 @@
|
||||
#include "../base/base.h"
|
||||
#include "tests.h"
|
||||
|
||||
|
||||
void printuni(Unitype v){
|
||||
switch (v.type) {
|
||||
case Null: printf("{Null}");break;
|
||||
case Double: printf("{%s:%lf}",typename(v.type),v.Double);break;
|
||||
case Char: printf("{%s:%c}",typename(v.type),v.Char);break;
|
||||
case Bool:
|
||||
case UInt64: printf("{%s:%lu}",typename(v.type),v.UInt64);break;
|
||||
case Int64: printf("{%s:%ld}",typename(v.type),v.Int64);break;
|
||||
default: printf("{%s:%p}",typename(v.type),v.VoidPtr);break;
|
||||
}
|
||||
}
|
||||
|
||||
void test_all(){
|
||||
test_searchtree();
|
||||
test_autoarr();
|
||||
test_hashtable();
|
||||
test_string();
|
||||
//test_searchtree();
|
||||
//test_autoarr();
|
||||
//test_hashtable();
|
||||
//test_string();
|
||||
test_dtsod();
|
||||
printf("\e[96m---------------------------------------\e[0m\n");
|
||||
}
|
||||
|
||||
@ -14,15 +14,18 @@ void test_dtsod(){
|
||||
" message_id: 920734809096077353ul;\n"
|
||||
" text: \"_$\\\"\\\\'''\n\ta ыыы000;2;=:%d;```\";\n"
|
||||
"};\n";
|
||||
Unitype id;
|
||||
KeyValuePair id;
|
||||
Hashtable* dtsod;
|
||||
optime("deserialize",1,(dtsod=DtsodV24_deserialize(text)));
|
||||
printf("\e[92mhashtable_get(message)->\n\e[94m");
|
||||
Unitype msg=Hashtable_get(dtsod,"message");
|
||||
printuni(msg);
|
||||
Autoarr(Unitype)* ar=msg.VoidPtr;
|
||||
id=Hashtable_get(Autoarr_get(ar,0).VoidPtr,"message_id");
|
||||
printf("\e[92m\nmessage_id: %lu\n",id.UInt64);
|
||||
printf("\e[92mhashtable_get(\"message\")->\n\e[94m");
|
||||
KeyValuePair msg=Hashtable_get_pair(dtsod,"message");
|
||||
printkvp(msg);
|
||||
printf("\n");
|
||||
Autoarr(Unitype)* ar=msg.value.VoidPtr;
|
||||
printf("\e[92mhashtable_get(\"message_id\")->\n\e[94m");
|
||||
id=Hashtable_get_pair(Autoarr_get(ar,0).VoidPtr,"message_id");
|
||||
printkvp(id);
|
||||
printf("\n");
|
||||
Hashtable_free(dtsod);
|
||||
}));
|
||||
}
|
||||
@ -1,12 +1,6 @@
|
||||
#include "tests.h"
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
|
||||
void printkvp(KeyValuePair p){
|
||||
printf("{\"%s\", ",p.key);
|
||||
printuni(p.value);
|
||||
printf("}");
|
||||
}
|
||||
|
||||
void print_hashtable(Hashtable* ht){
|
||||
printf("\e[94mHashtable:%lu\n"
|
||||
" hein: %u\n"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user