Skip to content

Commit

Permalink
Json::write() now writes to file in chunks, using less memory
Browse files Browse the repository at this point in the history
  • Loading branch information
aslze committed Dec 3, 2023
1 parent 609da43 commit 7421c4f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 80 deletions.
12 changes: 6 additions & 6 deletions include/asl/Var.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class ASL_API Var
else {
_type=STRING;
NEW_STRINGC(s, v.length()+1);
memcpy((*s).ptr(), *v, v.length() + 1);
memcpy(s->data(), *v, v.length() + 1);
}
}
template<class T>
Expand Down Expand Up @@ -415,9 +415,9 @@ class ASL_API Var
bool operator==(const Var& other) const
{
if(_type == STRING && other._type == SSTRING)
return !strcmp((*s).ptr(), other.ss);
return !strcmp(s->data(), other.ss);
else if(_type == SSTRING && other._type == STRING)
return !strcmp(ss, (*other.s).ptr());
return !strcmp(ss, other.s->data());
else if (_type == NUMBER || _type == FLOAT || _type == INT)
{
double x = *this;
Expand All @@ -429,7 +429,7 @@ class ASL_API Var
case FLOAT: return d == other.d;
case INT: return i==other.i;
case BOOL: return b==other.b;
case STRING: return !strcmp((*s).ptr(), (*other.s).ptr());
case STRING: return !strcmp(s->data(), other.s->data());
case SSTRING: return !strcmp(ss, other.ss);
case ARRAY: return *a==*other.a;
case DIC: return *o == *other.o;
Expand Down Expand Up @@ -480,7 +480,7 @@ class ASL_API Var
bool operator==(const char* other) const
{
switch(_type){
case STRING: return !strcmp((*s).ptr(), other);
case STRING: return !strcmp(s->data(), other);
case SSTRING: return !strcmp(ss, other);
default: return false;
}
Expand All @@ -489,7 +489,7 @@ class ASL_API Var
bool operator==(const String& other) const
{
switch(_type){
case STRING: return !strcmp((*s).ptr(), &other[0]);
case STRING: return !strcmp(s->data(), &other[0]);
case SSTRING: return !strcmp(ss, other);
default: return false;
}
Expand Down
29 changes: 9 additions & 20 deletions include/asl/Xdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,7 @@ namespace asl {
* @{
*/

class ASL_API XdlCodec
{
public:
XdlCodec() {}
virtual ~XdlCodec() {}
virtual void new_number(int x) {}
virtual void new_number(double x) {}
virtual void new_string(const char* s) {}
virtual void new_string(const String& x) {}
virtual void new_bool(bool b) {}
virtual void begin_array() {}
virtual void end_array() {}
virtual void begin_object(const char* c) {}
virtual void end_object() {}
virtual void new_property(const String& name) {}
};

class ASL_API XdlParser: public XdlCodec
class ASL_API XdlParser
{
typedef char State;
typedef char Context;
Expand Down Expand Up @@ -70,7 +53,9 @@ class ASL_API XdlParser: public XdlCodec
virtual void new_property(const String& name);
};

class ASL_API XdlEncoder: public XdlCodec
struct XdlSink;

class ASL_API XdlEncoder
{
protected:
String _out;
Expand All @@ -83,10 +68,14 @@ class ASL_API XdlEncoder: public XdlCodec
String _sep1; // between items in same line
String _sep2; // between items, end of line
int _level;
XdlSink* _sink;
void _encode(const Var& v);
public:
XdlEncoder();
~XdlEncoder() {}
~XdlEncoder();

void use(XdlSink* sink);

String data() const {return _out;}

String encode(const Var& v, Json::Mode mode);
Expand Down
57 changes: 19 additions & 38 deletions src/Var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Var::copy(const Var& v)
switch(_type) {
case STRING:
NEW_STRINGC(s, v.s->length());
memcpy(s->ptr(), v.s->ptr(), v.s->length());
memcpy(s->data(), v.s->data(), v.s->length());
break;
case ARRAY:
NEW_ARRAYC(a, *v.a);
Expand Down Expand Up @@ -97,7 +97,7 @@ Var::Var(const char* y)
else {
_type=STRING;
NEW_STRINGC(s, len + 1);
memcpy(s->ptr(), y, len + 1);
memcpy(s->data(), y, len + 1);
}
}

Expand All @@ -123,7 +123,7 @@ Var::operator double() const
case INT:
return i;
case STRING:
return atof(s->ptr());
return atof(s->data());
case SSTRING:
return atof(ss);
case NUL:
Expand All @@ -143,7 +143,7 @@ Var::operator float() const
case INT:
return (float)i;
case STRING:
return (float)atof(s->ptr());
return (float)atof(s->data());
case SSTRING:
return (float)atof(ss);
case NUL:
Expand All @@ -162,7 +162,7 @@ Var::operator int() const
case FLOAT:
return (int)d;
case STRING:
return atoi(s->ptr());
return atoi(s->data());
case SSTRING:
return atoi(ss);
default: break;
Expand All @@ -179,7 +179,7 @@ Var::operator unsigned() const
case FLOAT:
return (unsigned)d;
case STRING:
return (unsigned)atoi(s->ptr());
return (unsigned)atoi(s->data());
case SSTRING:
return (unsigned)atoi(ss);
default: break;
Expand All @@ -196,7 +196,7 @@ Var::operator Long() const
case FLOAT:
return (Long)d;
case STRING:
return (Long)atoi(s->ptr());
return (Long)atoi(s->data());
case SSTRING:
return (Long)atoi(ss);
default: break;
Expand All @@ -207,7 +207,7 @@ Var::operator Long() const
Var::operator String() const
{
if(_type==STRING)
return (*s).ptr();
return s->data();
if(_type==SSTRING)
return ss;
return toString();
Expand Down Expand Up @@ -247,7 +247,7 @@ const char* Var::operator*() const
switch(_type)
{
case STRING:
return (s->ptr()); break;
return (s->data()); break;
case SSTRING:
return ss; break;
case ARRAY:
Expand Down Expand Up @@ -284,7 +284,7 @@ String Var::toString() const
r=b?"true":"false";
break;
case STRING:
r=(*s).ptr();
r=s->data();
break;
case SSTRING:
r=ss;
Expand Down Expand Up @@ -319,9 +319,8 @@ void Var::free()
void Var::operator=(const Var& v)
{
if(_type == STRING && v._type == STRING) {
//(*s) = (*v.s);
s->resize(v.s->length());
memcpy(s->ptr(), v.s->ptr(), v.s->length());
memcpy(s->data(), v.s->data(), v.s->length());
return;
}
if(_type == ARRAY && v._type == ARRAY) {
Expand All @@ -332,15 +331,15 @@ void Var::operator=(const Var& v)
(*o) = (*v.o);
return;
}
//if(_type!=NONE)

if(!isPod())
free();
memcpy(this, &v, sizeof(v));
switch(_type)
{
case STRING:
NEW_STRINGC(s, v.s->length());
memcpy(s->ptr(), v.s->ptr(), v.s->length());
memcpy(s->data(), v.s->data(), v.s->length());
break;
case ARRAY:
NEW_ARRAYC(a, *v.a);
Expand Down Expand Up @@ -416,8 +415,8 @@ void Var::operator=(const char* x)
{
int n = (int)strlen(x);
if (_type == STRING) {
(*s).resize(n + 1);
memcpy((*s).ptr(), x, n + 1);
s->resize(n + 1);
memcpy(s->data(), x, n + 1);
}
else if(_type==SSTRING && n < VAR_SSPACE)
memcpy(ss, x, n + 1);
Expand All @@ -433,7 +432,7 @@ void Var::operator=(const char* x)
{
_type=STRING;
NEW_STRINGC(s, n + 1);
memcpy((*s).ptr(), x, n + 1);
memcpy(s->data(), x, n + 1);
}
}
}
Expand All @@ -444,8 +443,8 @@ void Var::operator=(const String& x)
Type t = _type;
if(t==NONE) {}
else if(t==STRING) {
(*s).resize(len + 1);
memcpy((*s).ptr(), *x, len + 1);
s->resize(len + 1);
memcpy(s->data(), *x, len + 1);
return;
}
else if(t==SSTRING && len < VAR_SSPACE) {
Expand All @@ -466,7 +465,7 @@ void Var::operator=(const String& x)
{
_type=STRING;
NEW_STRINGC(s, len + 1);
memcpy((*s).ptr(), *x, len + 1);
memcpy(s->data(), *x, len + 1);
}
}
}
Expand All @@ -477,14 +476,6 @@ const Var& Var::operator[](int i) const
return (*a)[i];

return none;
/*else if (_type == NONE)
{
((Var*)this)->_type=ARRAY;
NEW_ARRAY( ((Var*)this)->a );
((Var*)this)->a->resize(i+1);
return (*a)[i];
}
return *(Var*)this;*/
}

Var& Var::operator[](int i)
Expand Down Expand Up @@ -533,16 +524,6 @@ const Var& Var::operator[](const String& k) const
return none;
}

/*
const Var& Var::operator()(const String& k) const
{
if(_type==DIC)
return (*o)[k];
asl_error("Var(String)");
return *(Var*)this;
}
*/

int Var::length() const
{
switch(_type) {
Expand Down
Loading

0 comments on commit 7421c4f

Please sign in to comment.