deserialize() fixed and tested on windows

This commit is contained in:
Timerix22 2022-03-08 01:18:53 +03:00
parent 46e7991867
commit 3cb878c75f
8 changed files with 40 additions and 56 deletions

View File

@ -11,14 +11,13 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
bool readingList=false; bool readingList=false;
void __throw_wrongchar(char* file, int line, char* fname,char _c){ void __throw_wrongchar(char* file, int line, char* fname,char _c){
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);
errBuf[i+22]=*(text-16+i); printf("\n\e[91m[%s:%d %s] throwed error: %s\n",file,line,fname,errBuf);
printf("\n\e[31m[%s:%d/%s]\n",file,line,fname); exit(128);
throw(errBuf);
}; };
#define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C) #define throw_wrongchar(C) __throw_wrongchar(__FILE__,__LINE__,__func__,C)
@ -54,14 +53,14 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
if(!calledRecursively) throw_wrongchar(c); if(!calledRecursively) throw_wrongchar(c);
if((*++text)!=';') if((*++text)!=';')
throw_wrongchar(c); throw_wrongchar(c);
case ':':
return nameStr;
case '$': case '$':
if(nameStr.length!=0) if(nameStr.length!=0)
throw_wrongchar(c); throw_wrongchar(c);
nameStr.ptr++; nameStr.ptr++;
partOfDollarList=true; partOfDollarList=true;
break; break;
case ':':
return nameStr;
default: default:
nameStr.length++; nameStr.length++;
break; break;
@ -72,7 +71,6 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
}; };
Unitype ReadValue(){ Unitype ReadValue(){
//returns part of <text> without quotes //returns part of <text> without quotes
string ReadString(){ string ReadString(){
bool prevIsBackslash=false; bool prevIsBackslash=false;
@ -97,16 +95,12 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
}; };
Hashtable* ReadDtsod(){ Hashtable* ReadDtsod(){
if(*++text) //skips { ++text; //skips '{'
return __deserialize(&text,true); return __deserialize(&text,true);
else { }
throw(ERR_ENDOFSTR);
return NULL;
}
};
Unitype ParseValue(string str){ Unitype ParseValue(string str){
printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCharPtr(str)); //printf("\e[94m<\e[96m%s\e[94m>\n",string_cpToCharPtr(str));
const string nullStr={"null",4}; 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};
@ -175,10 +169,11 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
case '}': case '$': case '}': case '$':
throw_wrongchar(c); throw_wrongchar(c);
break; break;
case '#': case '#':;
char _c=c;
SkipComment(); SkipComment();
if(valueStr.length!=0) if(valueStr.length!=0)
throw_wrongchar(c); throw_wrongchar(_c);
valueStr.ptr=text; valueStr.ptr=text;
break; break;
case '"': case '"':
@ -187,7 +182,6 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
break; break;
case '\'': case '\'':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) throw_wrongchar(c);
text++;
char valueChar=*++text; char valueChar=*++text;
if (*++text != '\'') throw("after <'> should be char"); if (*++text != '\'') throw("after <'> should be char");
value=Uni(Char,valueChar); value=Uni(Char,valueChar);
@ -201,6 +195,7 @@ Hashtable* __deserialize(char** _text, bool calledRecursively){
case '{': case '{':
if(valueStr.length!=0) throw_wrongchar(c); if(valueStr.length!=0) throw_wrongchar(c);
value=UniPtr(HashtablePtr,ReadDtsod()); value=UniPtr(HashtablePtr,ReadDtsod());
return value;
case ';': case ';':
case ',': case ',':
if(valueStr.length!=0) if(valueStr.length!=0)

View File

@ -5,15 +5,13 @@
#include "errors.h" #include "errors.h"
#include "mystr.h" #include "mystr.h"
// sleep function based on std nanosleep()
void fsleep(float sec);
// executes codeblock and prints execution time // executes codeblock and prints execution time
#define optime(opname,repeats,codeblock) ({\ #define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\ struct timespec start, stop;\
clock_gettime(CLOCK_REALTIME, &start);\
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\ for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
(codeblock);\ (codeblock);\
clock_t stop=clock();\ clock_gettime(CLOCK_REALTIME, &stop);\
double t=(double)(stop-start)/CLOCKS_PER_SEC/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);\
}) })

View File

@ -16,12 +16,12 @@ const char* errname(err_t err){
void _throwint(int err, const char* srcfile, int line, const char* funcname){ void _throwint(int err, const char* srcfile, int line, const char* funcname){
if(err){ // SUCCESS=0 is not an error if(err){ // SUCCESS=0 is not an error
printf("\e[91m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err)); printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
exit(err); exit(err);
} }
else printf("\e[93m[%s:%d/%s] throwed SUCCESS as an error",srcfile,line,funcname); else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
} }
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){ void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
printf("\e[91m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg); printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
exit(255); exit(255);
} }

View File

@ -1,8 +0,0 @@
#include "base.h"
void fsleep(float sec){
struct timespec t;
t.tv_sec=sec+0.0001f;
t.tv_nsec=(sec-t.tv_sec)*1000000000;
nanosleep(&t, NULL);
}

View File

@ -4,8 +4,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <locale.h> #include <locale.h>
#include <uchar.h>
#include <wchar.h>
#include <time.h> #include <time.h>
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N) #define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)

View File

@ -119,11 +119,12 @@ void Unitype_free(Unitype u){
void printuni(Unitype v){ void printuni(Unitype v){
switch (v.type) { switch (v.type) {
case Null: printf("{Null}");break; case Null: printf("{Null}");break;
case Double: printf("{%s:%lf}",typename(v.type),v.Double);break; case Double: printf("{%s : %lf}",typename(v.type),v.Double);break;
case Char: printf("{%s:%c}",typename(v.type),v.Char);break; case Char: printf("{%s : '%c'}",typename(v.type),v.Char);break;
case Bool: case Bool:
case UInt64: printf("{%s:%lu}",typename(v.type),v.UInt64);break; case UInt64: printf("{%s : %lu}",typename(v.type),v.UInt64);break;
case Int64: printf("{%s:%ld}",typename(v.type),v.Int64);break; case Int64: printf("{%s : %ld}",typename(v.type),v.Int64);break;
default: printf("{%s:%p}",typename(v.type),v.VoidPtr);break; case CharPtr: printf("{%s : \"%s\"}",typename(v.type),(char*)v.VoidPtr);break;
default: printf("{%s : %p}",typename(v.type),v.VoidPtr);break;
} }
} }

View File

@ -2,10 +2,10 @@
#include "tests.h" #include "tests.h"
void test_all(){ void test_all(){
//test_searchtree(); test_searchtree();
//test_autoarr(); test_autoarr();
//test_hashtable(); test_hashtable();
//test_string(); test_string();
test_dtsod(); test_dtsod();
printf("\e[96m---------------------------------------\e[0m\n"); printf("\e[96m---------------------------------------\e[0m\n");
} }

View File

@ -4,16 +4,16 @@
const char text[]= const char text[]=
"message: {\n" "message: {\n"
" bool: false;" " bool: false;"
//" char: 'v';" " char: 'v';"
" int: -2515;" " int: -2515;"
" uint: 0u;" " uint:#comment!\n 0u;"
" double: 965.557f;" " double: 965.557f;#another comment!\n"
" text: \"_$\\\"\\\\'''\n\ta ыыы000;2;=:%d;```\";\n" " text: \"_$\\\"\\\\'''\n\ta ыыы000;2;=:%d;```\";\n"
"}; \n"; "}; ";
void test_dtsod(){ void test_dtsod(){
printf("\e[96m-------------[test_dtsod]-------------\n"); printf("\e[96m-------------[test_dtsod]-------------\n");
//optime(__func__,1,({ optime(__func__,1,({
Hashtable* dtsod; Hashtable* dtsod;
optime("deserialize",1,(dtsod=DtsodV24_deserialize(text))); optime("deserialize",1,(dtsod=DtsodV24_deserialize(text)));
Hashtable_foreach(dtsod, p,({ Hashtable_foreach(dtsod, p,({
@ -31,5 +31,5 @@ void test_dtsod(){
printf("\n"); printf("\n");
})); }));
Hashtable_free(dtsod); Hashtable_free(dtsod);
//})); }));
} }