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

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

View File

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

View File

@ -16,12 +16,12 @@ const char* errname(err_t err){
void _throwint(int err, const char* srcfile, int line, const char* funcname){
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);
}
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){
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);
}

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 <stdbool.h>
#include <locale.h>
#include <uchar.h>
#include <wchar.h>
#include <time.h>
#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){
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 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;
case UInt64: printf("{%s : %lu}",typename(v.type),v.UInt64);break;
case Int64: printf("{%s : %ld}",typename(v.type),v.Int64);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"
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");
}

View File

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