updated throw_wrongchar(), removoved null parsing, commits cleared
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
#include "DtsodV24.h"
|
||||
|
||||
//returns UniNull if key not found
|
||||
// returns UniNull if key not found
|
||||
Unitype DtsodV24_get(Hashtable* dtsod, char* key){
|
||||
return Hashtable_get(dtsod, key);
|
||||
}
|
||||
|
||||
//adds or sets value
|
||||
// adds or sets value
|
||||
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
|
||||
Unitype* val=Hashtable_getptr(dtsod, key);
|
||||
if(val) *val=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){
|
||||
Unitype* val=Hashtable_getptr(dtsod, key);
|
||||
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){
|
||||
Unitype* val=Hashtable_getptr(dtsod, key);
|
||||
if (!val) return false;
|
||||
@@ -26,7 +26,7 @@ bool DtsodV24_remove(Hashtable* dtsod, char* key){
|
||||
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){
|
||||
Hashtable_free(dtsod);
|
||||
}
|
||||
@@ -6,25 +6,25 @@ extern "C" {
|
||||
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
|
||||
//parses text to binary values
|
||||
// parses text to binary values
|
||||
Maybe DtsodV24_deserialize(char* text);
|
||||
|
||||
//creates text representation of dtsod
|
||||
// creates text representation of 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);
|
||||
|
||||
//adds or sets value
|
||||
// adds or sets 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);
|
||||
|
||||
//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);
|
||||
|
||||
//frees memory including memory of elements (hashtables, autoarrs, etc.)
|
||||
// frees memory including memory of elements (hashtables, autoarrs, etc.)
|
||||
void DtsodV24_free(Hashtable* dtsod);
|
||||
|
||||
#if __cplusplus
|
||||
|
||||
@@ -6,19 +6,36 @@
|
||||
#define STRB_BC 64
|
||||
#define STRB_BL 1024
|
||||
|
||||
Maybe ERROR_WRONGCHAR(char c, char* text){
|
||||
char errBuf[]="unexpected <c> at:\n \""
|
||||
"00000000000000000000000000000000\"";
|
||||
errBuf[12]=c;
|
||||
for(uint8 i=0; i<32; i++)
|
||||
// special func for throwing error messages about wrong characters in deserializing text
|
||||
Maybe ERROR_WRONGCHAR(const char c, char* text, char* text_first, const char* srcfile, int line, const char* funcname){
|
||||
char errBuf[33];
|
||||
errBuf[32]='\0';
|
||||
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
|
||||
errBuf[i+22]=*(text - 16 + i);
|
||||
safethrow(cptr_copy(errBuf));
|
||||
char _c=errText[i];
|
||||
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{
|
||||
const char* sh_text_first;
|
||||
char* sh_text;
|
||||
bool sh_partOfDollarList;
|
||||
bool sh_readingList;
|
||||
@@ -58,7 +75,7 @@ Maybe __ReadName(DeserializeSharedData* shared){
|
||||
try(SkipComment(),_);
|
||||
if(nameStr.length!=0)
|
||||
safethrow_wrongchar(c);
|
||||
nameStr.ptr=text+1; //skips '\n'
|
||||
nameStr.ptr=text+1; // skips '\n'
|
||||
break;
|
||||
case '}':
|
||||
if(!calledRecursively) safethrow_wrongchar(c);
|
||||
@@ -86,7 +103,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively);
|
||||
Maybe __ReadValue(DeserializeSharedData* shared);
|
||||
#define ReadValue() __ReadValue(shared)
|
||||
|
||||
//returns part of <text> without quotes
|
||||
// returns part of <text> without quotes
|
||||
Maybe __ReadString(DeserializeSharedData* shared){
|
||||
char c;
|
||||
bool prevIsBackslash=false;
|
||||
@@ -95,7 +112,7 @@ Maybe __ReadString(DeserializeSharedData* shared){
|
||||
while ((c=*++text)){
|
||||
if(c=='"') {
|
||||
if(prevIsBackslash) {
|
||||
//replacing <\"> with <">
|
||||
// replacing <\"> with <">
|
||||
Autoarr_remove(b);
|
||||
StringBuilder_append_char(b,c);
|
||||
}
|
||||
@@ -128,59 +145,51 @@ Maybe __ReadList(DeserializeSharedData* shared){
|
||||
#define ReadList() __ReadList(shared)
|
||||
|
||||
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 falseStr={"false",5};
|
||||
switch(*str.ptr){
|
||||
case 'n':
|
||||
if(string_compare(str,nullStr))
|
||||
return SUCCESS(UniNull);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
case 't':
|
||||
switch(str.ptr[str.length-1]){
|
||||
// Bool
|
||||
case 'e':
|
||||
if(string_compare(str,trueStr))
|
||||
return SUCCESS(UniTrue);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
case 'f':
|
||||
if(string_compare(str,falseStr))
|
||||
else if(string_compare(str,falseStr))
|
||||
return SUCCESS(UniFalse);
|
||||
else safethrow_wrongchar(*str.ptr);
|
||||
break;
|
||||
default:
|
||||
switch(str.ptr[str.length-1]){
|
||||
case 'f': {
|
||||
char* _c=string_cpToCptr(str);
|
||||
Unitype rez=Uni(Float64,strtod(_c,NULL));
|
||||
free(_c);
|
||||
return SUCCESS(rez);
|
||||
}
|
||||
case 'u': {
|
||||
uint64 lu=0;
|
||||
char* _c=string_cpToCptr(str);
|
||||
sscanf(_c,"%lu",&lu);
|
||||
free(_c);
|
||||
return SUCCESS(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;
|
||||
char* _c=string_cpToCptr(str);
|
||||
if(sscanf(_c,"%li",&li)!=1){
|
||||
char err[64];
|
||||
IFWIN(
|
||||
sprintf_s(err,64,"can't parse to int: <%s>",_c),
|
||||
sprintf(err,"can't parse to int: <%s>",_c)
|
||||
);
|
||||
safethrow(err);
|
||||
}
|
||||
free(_c);
|
||||
return SUCCESS(Uni(Int64,li));
|
||||
}
|
||||
default:
|
||||
safethrow_wrongchar(str.ptr[str.length-1]);
|
||||
// Float64
|
||||
case 'f': {
|
||||
char* _c=string_cpToCptr(str);
|
||||
Unitype rez=Uni(Float64,strtod(_c,NULL));
|
||||
free(_c);
|
||||
return SUCCESS(rez);
|
||||
}
|
||||
// UInt64
|
||||
case 'u': {
|
||||
uint64 lu=0;
|
||||
char* _c=string_cpToCptr(str);
|
||||
sscanf(_c,"%lu",&lu);
|
||||
free(_c);
|
||||
return SUCCESS(Uni(UInt64,lu));
|
||||
}
|
||||
// Int64
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9': {
|
||||
int64 li=0;
|
||||
char* _c=string_cpToCptr(str);
|
||||
if(sscanf(_c,"%li",&li)!=1){
|
||||
char err[64];
|
||||
IFWIN(
|
||||
sprintf_s(err,64,"can't parse to int: <%s>",_c),
|
||||
sprintf(err,"can't parse to int: <%s>",_c)
|
||||
);
|
||||
safethrow(err);
|
||||
}
|
||||
free(_c);
|
||||
return SUCCESS(Uni(Int64,li));
|
||||
}
|
||||
// wrong type
|
||||
default:
|
||||
safethrow_wrongchar(str.ptr[str.length-1]);
|
||||
}
|
||||
safethrow(ERR_ENDOFSTR);
|
||||
};
|
||||
@@ -207,7 +216,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
||||
try(SkipComment(),_);
|
||||
if(valueStr.length!=0)
|
||||
safethrow_wrongchar(_c);
|
||||
valueStr.ptr=text+1; //skips '\n'
|
||||
valueStr.ptr=text+1; // skips '\n'
|
||||
break;
|
||||
case '"':
|
||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||
@@ -223,7 +232,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
||||
break;
|
||||
case '{':
|
||||
if(valueStr.length!=0) safethrow_wrongchar(c);
|
||||
++text; //skips '{'
|
||||
++text; // skips '{'
|
||||
try(__deserialize(&text,true), val)
|
||||
return SUCCESS(val.value);
|
||||
case ';':
|
||||
@@ -244,6 +253,7 @@ Maybe __ReadValue(DeserializeSharedData* shared){
|
||||
|
||||
Maybe __deserialize(char** _text, bool _calledRecursively) {
|
||||
DeserializeSharedData _shared={
|
||||
.sh_text_first=*_text,
|
||||
.sh_text=*_text,
|
||||
.sh_partOfDollarList=false,
|
||||
.sh_readingList=false,
|
||||
@@ -256,7 +266,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) {
|
||||
text--;
|
||||
while((c=*++text)){
|
||||
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;
|
||||
char* nameCPtr=maybeName.value.VoidPtr;
|
||||
try(ReadValue(), val)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//
|
||||
//
|
||||
// I planned to export functions from DtsodV24.h,
|
||||
// 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
|
||||
//
|
||||
//
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
@@ -10,39 +10,39 @@ extern "C" {
|
||||
|
||||
#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){
|
||||
Maybe r=DtsodV24_deserialize(text);
|
||||
*errmsg= r.errmsg ? r.errmsg : NULL;
|
||||
*output=r.value.VoidPtr;
|
||||
}
|
||||
|
||||
//creates text representation of dtsod
|
||||
// creates text representation of dtsod
|
||||
EXPORT void CALL kerep_DtsodV24_serialize(Hashtable* dtsod, char** output){
|
||||
*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){
|
||||
*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){
|
||||
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){
|
||||
*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){
|
||||
*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){
|
||||
DtsodV24_free(dtsod);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "DtsodV24.h"
|
||||
#include "../Autoarr/StringBuilder.h"
|
||||
|
||||
//65536 max length!
|
||||
// 65536 max length!
|
||||
#define STRB_BC 64
|
||||
#define STRB_BL 1024
|
||||
|
||||
@@ -51,8 +51,7 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){
|
||||
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
|
||||
break;
|
||||
case Null:
|
||||
if(!u.VoidPtr) StringBuilder_append_cptr(b, "null");
|
||||
else throw("Null-type pointer is not 0");
|
||||
throw("Null isn't supported in DtsodV24");
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
addc('[');
|
||||
|
||||
Reference in New Issue
Block a user