From 4778d0ca2aebc28630897d60ebab0a2686c87a3e Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 2 Mar 2022 14:42:30 +0300 Subject: [PATCH] ParseValue() --- DtsodC/Makefile | 4 +- DtsodC/src/DtsodParser/DtsodV24.c | 71 +++++++++++++------------------ DtsodC/src/base/twmp.h | 14 ++++++ DtsodC/src/base/types.c | 13 ++++++ DtsodC/src/base/types.h | 7 --- DtsodC/tests/main.c | 23 ++-------- DtsodC/tests/test_dtsod.c | 4 ++ DtsodC/tests/test_hashtable.c | 2 +- DtsodC/tests/test_searchtree.c | 6 +-- 9 files changed, 71 insertions(+), 73 deletions(-) create mode 100644 DtsodC/src/base/twmp.h diff --git a/DtsodC/Makefile b/DtsodC/Makefile index a0b800c..3dcc61c 100644 --- a/DtsodC/Makefile +++ b/DtsodC/Makefile @@ -15,11 +15,13 @@ clear_bin: clang: CMP=clang clang: all -CMPARGS= -Wall $(SRC) $(TESTS) -o $(OUTFILE) +CMPARGS= -Wall -Wno-discarded-qualifiers $(SRC) $(TESTS) -o $(OUTFILE) build: @echo -e '\n\e[96m----------------[build]----------------\e[0m' + @mkdir -p bin $(CMP) -O1 -flto $(CMPARGS) build_dbg: + @mkdir -p bin @echo -e '\n\e[96m--------------[build_dbg]--------------\e[0m' $(CMP) -O0 -g $(CMPARGS).dbg test: diff --git a/DtsodC/src/DtsodParser/DtsodV24.c b/DtsodC/src/DtsodParser/DtsodV24.c index 3e6fb73..389d42c 100644 --- a/DtsodC/src/DtsodParser/DtsodV24.c +++ b/DtsodC/src/DtsodParser/DtsodV24.c @@ -4,21 +4,25 @@ #define ARR_BC 2 #define ARR_BL 8 -Hashtable* __deserialize(char* text, bool calledRecursively){ +Hashtable* __deserialize(char** _text, bool calledRecursively){ Hashtable* dict=Hashtable_create(); + char* text=*_text; char c; bool partOfDollarList=false; bool readingList=false; - void throw_wrongchar(char _c){ + void __throw_wrongchar(char* fname,char _c){ char errBuf[]="unexpected 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); throw(errBuf); }; + #define throw_wrongchar(C) __throw_wrongchar(__func__,C) + void SkipComment(){ while((c=*++text)!='\n') @@ -86,7 +90,7 @@ Hashtable* __deserialize(char* text, bool calledRecursively){ Hashtable* ReadDtsod(){ if(*++text) //skips { - return __deserialize(text,true); + return __deserialize(&text,true); else { throw(ERR_ENDOFSTR); return NULL; @@ -123,45 +127,23 @@ Hashtable* __deserialize(char* text, bool calledRecursively){ break; default: switch(str.ptr[str.length-1]){ - case 's': - return value_str[value_str.Length - 2] == 'u' - ? value_str.Remove(value_str.Length - 2).ToUShort() - : value_str.Remove(value_str.Length - 1).ToShort(); - case 'u': - return value_str.Remove(value_str.Length - 1).ToUInt(); - case 'i': - return value_str[value_str.Length - 2] == 'u' - ? value_str.Remove(value_str.Length - 2).ToUInt() - : value_str.Remove(value_str.Length - 1).ToInt(); - case 'l': - return value_str[value_str.Length - 2] == 'u' - ? value_str.Remove(value_str.Length - 2).ToULong() - : value_str.Remove(value_str.Length - 1).ToLong(); - case 'b': - return value_str[value_str.Length - 2] == 's' - ? value_str.Remove(value_str.Length - 2).ToSByte() - : value_str.Remove(value_str.Length - 1).ToByte(); - case 'f': - return value_str.Remove(value_str.Length - 1).ToFloat(); - case 'e': - return value_str[value_str.Length - 2] == 'd' - ? value_str.Remove(value_str.Length - 2).ToDecimal() - : throw new Exception("can't parse value:" + value_str); - default: - /* if (value_str.Contains('.')) - return (object)(value_str.ToDouble()); - else - { - try { return (object)(value_str.ToInt()); } - catch (FormatException) - { - Log("r", $"can't parse value: {value_str}"); - return null; - } - } */ + case 'f': + return Uni(Double,strtod(string_cpToCharPtr(str),NULL)); + case 'u': ; //some weird "empty statement" + //not "statement" + uint64 lu=0; + sscanf(string_cpToCharPtr(str),"%lu",&lu); + return Uni(UInt64,lu); + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': ; + int64 li=0; + sscanf(string_cpToCharPtr(str),"%li",&li); + return Uni(Int64,li); + default: + throw_wrongchar(str.ptr[str.length-1]); } } - throw(ERR_NOTIMPLEMENTED); + throw(ERR_ENDOFSTR); return UniNull; }; @@ -208,7 +190,7 @@ Hashtable* __deserialize(char* text, bool calledRecursively){ while((c=*text++)){ string name=ReadName(); if(name.length==0) //end of file or '}' in recursive call - return dict; + goto END; char* nameCPtr=string_cpToCharPtr(name); Unitype value=ReadValue(); if(partOfDollarList){ @@ -225,7 +207,12 @@ Hashtable* __deserialize(char* text, bool calledRecursively){ } else Hashtable_add(dict,nameCPtr,value); } + END: + *_text=text; return dict; } -Hashtable* DtsodV24_deserialize(char* text) { return __deserialize(text,false); } +Hashtable* DtsodV24_deserialize(char* text) { + Hashtable* r=__deserialize(&text,false); + return r; +} diff --git a/DtsodC/src/base/twmp.h b/DtsodC/src/base/twmp.h new file mode 100644 index 0000000..f21f1e3 --- /dev/null +++ b/DtsodC/src/base/twmp.h @@ -0,0 +1,14 @@ +case CharPtr: return "CharPtr"; +case UniversalType: return "UniversalType"; +case STNodePtr: return "STNodePtr"; +case HashtablePtr: return "HashtablePtr"; +case AutoarrInt8Ptr: return "AutoarrInt8Ptr"; +case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr"; +case AutoarrInt16Ptr: return "AutoarrInt16Ptr"; +case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr"; +case AutoarrInt32Ptr: return "AutoarrInt32Ptr"; +case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr"; +case AutoarrInt64Ptr: return "AutoarrInt64Ptr"; +case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr"; +case AutoarrUnitypePtr: return "AutoarrUnitypePtr"; +case AutoarrKVPairPtr: return "AutoarrKVPairPtr"; \ No newline at end of file diff --git a/DtsodC/src/base/types.c b/DtsodC/src/base/types.c index 0e1dec8..a70232e 100644 --- a/DtsodC/src/base/types.c +++ b/DtsodC/src/base/types.c @@ -25,6 +25,19 @@ const char* typename(my_type t){ case Int64Ptr: return "Int64Ptr"; case UInt64Ptr: return "UInt64Ptr"; case UniversalType: return "Unitype"; + case CharPtr: return "CharPtr"; +case STNodePtr: return "STNodePtr"; +case HashtablePtr: return "HashtablePtr"; +case AutoarrInt8Ptr: return "AutoarrInt8Ptr"; +case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr"; +case AutoarrInt16Ptr: return "AutoarrInt16Ptr"; +case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr"; +case AutoarrInt32Ptr: return "AutoarrInt32Ptr"; +case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr"; +case AutoarrInt64Ptr: return "AutoarrInt64Ptr"; +case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr"; +case AutoarrUnitypePtr: return "AutoarrUnitypePtr"; +case AutoarrKVPairPtr: return "AutoarrKVPairPtr"; default: throw(ERR_WRONGTYPE); return "ERROR"; } } diff --git a/DtsodC/src/base/types.h b/DtsodC/src/base/types.h index ca3359e..9d52dbd 100644 --- a/DtsodC/src/base/types.h +++ b/DtsodC/src/base/types.h @@ -30,15 +30,8 @@ int8 typesize(my_type type); typedef struct Unitype{ my_type type; union { - int8 Int8; - uint8 UInt8; - int16 Int16; - uint16 UInt16; - int32 Int32; - uint32 UInt32; int64 Int64; uint64 UInt64; - float Float; double Double; char Char; bool Bool; diff --git a/DtsodC/tests/main.c b/DtsodC/tests/main.c index dc8df7d..1746a84 100644 --- a/DtsodC/tests/main.c +++ b/DtsodC/tests/main.c @@ -4,28 +4,13 @@ void printuni(Unitype v){ switch (v.type) { - case Null: printf("{%s}",typename(v.type));break; + case Null: printf("{Null}");break; case Double: printf("{%s:%lf}",typename(v.type),v.Double);break; - case Float: printf("{%s:%f}",typename(v.type),v.Float);break; - case Char: printf("{%s:%c}",typename(v.type),v.Int8);break; - case UInt8: - case UInt16: printf("{%s:%u}",typename(v.type),v.UInt16);break; - case UInt32: + 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 Bool: - case Int8: - case Int16: printf("{%s:%d}",typename(v.type),v.Int16);break; - case Int32: case Int64: printf("{%s:%ld}",typename(v.type),v.Int64);break; - case Int8Ptr: - case UInt8Ptr: - case Int16Ptr: - case UInt16Ptr: - case Int32Ptr: - case UInt32Ptr: - case Int64Ptr: - case UInt64Ptr: printf("{%s:%p}",typename(v.type),v.VoidPtr);break; - default: throw(ERR_WRONGTYPE);break; + default: printf("{%s:%p}",typename(v.type),v.VoidPtr);break; } } diff --git a/DtsodC/tests/test_dtsod.c b/DtsodC/tests/test_dtsod.c index 4c09d50..5d1fd01 100644 --- a/DtsodC/tests/test_dtsod.c +++ b/DtsodC/tests/test_dtsod.c @@ -15,5 +15,9 @@ void test_dtsod(){ " text: \"_$\\\"\\\\'''\n\ta ыыы000;2;=:%d;```\";\n" "};\n"; Hashtable* dtsod=DtsodV24_deserialize(text); + Unitype msg=Hashtable_get(dtsod,"message"); + printuni(msg); + Unitype id=Hashtable_get(msg.VoidPtr,"message_id"); + printf("\e[92mid: %lu",id.UInt64); })); } \ No newline at end of file diff --git a/DtsodC/tests/test_hashtable.c b/DtsodC/tests/test_hashtable.c index 573fe33..d40b56c 100644 --- a/DtsodC/tests/test_hashtable.c +++ b/DtsodC/tests/test_hashtable.c @@ -45,7 +45,7 @@ void fill(Hashtable* ht){ for(uint32 i=0;i<100000;i++){ char* key=malloc(12); sprintf(key,"key__%u",i); - Hashtable_add(ht,key,Uni(UInt32,i)); + Hashtable_add(ht,key,Uni(UInt64,i)); } } diff --git a/DtsodC/tests/test_searchtree.c b/DtsodC/tests/test_searchtree.c index cf4d476..c3c2b68 100644 --- a/DtsodC/tests/test_searchtree.c +++ b/DtsodC/tests/test_searchtree.c @@ -25,11 +25,11 @@ void test_searchtree(){ STNode* node=STNode_create(); printf("\e[92mnode created\n"); printf("push:\e[94m\n "); - Unitype u={.type=Int16,.Int16=-3}; + Unitype u={.type=Int64,.Int64=-3}; printuni(u); ST_push(node,"type", u); printf(" -> type\n "); - u=(Unitype){.type=Int16,.Int16=25}; + u=(Unitype){.type=Int64,.Int64=25}; printuni(u); ST_push(node,"time", u); printf(" -> time\n "); @@ -41,7 +41,7 @@ void test_searchtree(){ printuni(u); ST_push(node,"channel_id", u); printf(" -> channel_id\n "); - u=(Unitype){.type=Float,.Float=32.2004}; + u=(Unitype){.type=Double,.Double=32.2004}; printuni(u); ST_push(node,"message_id", u); printf(" -> message_id\n ");