diff --git a/JFile.cpp b/JFile.cpp index 4cfac59..ed64f43 100644 --- a/JFile.cpp +++ b/JFile.cpp @@ -4,7 +4,20 @@ namespace coreutils { - JFile::JFile(ZString filename) : MFile(filename) {} + // MFile and JString share a single virtual MString base (the diamond: + // both "MFile : virtual public MString" and "JString : virtual public + // MString"), and a virtual base is initialized only by the *most + // derived* class's constructor - so JString's own "{}" default + // initializer never runs here. Left alone, a JFile backed by a new or + // empty file starts as a raw empty string instead of a valid empty JSON + // object, and every subsequent locate()/set() call silently misbehaves + // (no leading "{" for ifNext("{") to match, so writes end up inserted + // at offset 0 with no braces or separators at all). Detect that case + // here and seed valid JSON explicitly. + JFile::JFile(ZString filename) : MFile(filename) { + if(getLength() == 0) + *this << "{}"; + } } diff --git a/MString.cpp b/MString.cpp index 360f91f..303a590 100644 --- a/MString.cpp +++ b/MString.cpp @@ -48,13 +48,13 @@ namespace coreutils { MString::MString(const ZString &zstring) { setSize(zstring.getLength()); - memcpy(data, zstring.getData(), zstring.getLength()); - cursor = data; - } + memcpy(data, zstring.getCursor(), zstring.getLength()); + cursor = data; + } MString::MString(ZString &zstring) { setSize(zstring.getLength()); - memcpy(data, zstring.getData(), zstring.getLength()); + memcpy(data, zstring.getCursor(), zstring.getLength()); cursor = data; } @@ -66,7 +66,7 @@ namespace coreutils { MString::MString(MString &mstring) { setSize(mstring.getLength()); - memcpy(data, mstring.getData(), mstring.getLength()); + memcpy(data, mstring.getCursor(), mstring.getLength()); cursor = data; } @@ -106,7 +106,7 @@ namespace coreutils { MString &MString::operator=(coreutils::MString value) { setSize(value.getLength()); - memcpy(data, value.getData(), value.getLength()); + memcpy(data, value.getCursor(), value.getLength()); length = value.getLength(); onChange(); return *this; @@ -114,7 +114,7 @@ namespace coreutils { MString &MString::operator=(coreutils::ZString value) { setSize(value.getLength()); - memcpy(data, value.getData(), value.getLength()); + memcpy(data, value.getCursor(), value.getLength()); length = value.getLength(); onChange(); return *this; @@ -195,7 +195,7 @@ namespace coreutils { int temp = length; int len = length + zstring.getLength(); setSize(len); - memcpy(data + temp, zstring.getData(), zstring.getLength()); + memcpy(data + temp, zstring.getCursor(), zstring.getLength()); onChange(); return *this; } @@ -240,7 +240,7 @@ namespace coreutils { setSize(length + value.getLength()); for (int ix = length; ix >= offset; ix--) getData()[ix] = getData()[ix - value.getLength()]; - std::memcpy(data + offset, value.getData(), value.getLength()); + std::memcpy(data + offset, value.getCursor(), value.getLength()); onChange(); } @@ -285,7 +285,7 @@ namespace coreutils { MString &MString::write(ZString value) { int len = length; setSize(length + value.getLength()); - memcpy(data + len, value.getData(), value.getLength()); + memcpy(data + len, value.getCursor(), value.getLength()); onChange(); return *this; }