diff --git a/JString.cpp b/JString.cpp index ba0fbd0..a6b33c3 100644 --- a/JString.cpp +++ b/JString.cpp @@ -21,7 +21,12 @@ namespace coreutils { pop(); remove(offset(), old.getLength() + 2); MString key; - key << "\"" << value << "\""; + if(value.startsWith("[")) + key << value; + else if(value.startsWith("{")) + key << value; + else + key << "\"" << value << "\""; insert(key, offset()); } else { pop(); @@ -32,17 +37,13 @@ namespace coreutils { MString key; if(notfirst) key << ","; - // TODO: The value added must be parsed down to remove all whitespace. - value.push(); - value.skipWhitespace(); if(value.startsWith("[")) key << "\"" << path << "\":" << value; else if(value.startsWith("{")) key << "\"" << path << "\":" << value; else key << "\"" << path << "\":" << "\"" << value << "\""; - value.pop(); insert(key, offset()); } } @@ -64,9 +65,24 @@ namespace coreutils { } bool JString::locate(ZString &path) { + std::cout << "path locate: " << path << std::endl; path.split(".", 1); + ZString pathx = path[0].getTokenExclude("["); + ZString index; + if(path[0].ifNext("[")) { + index = path[0].getTokenExclude("]"); + if(path[0].ifNext("]")) { + // TODO: index value here + std::cout << ":::" << pathx << ":" << index << std::endl; + } + } + else + pathx = path[0]; + pathx.reset(); + index.reset(); + path[0].reset(); notfirst = false; - if(ifNext("{")) { + if(ifNext("{")) { while(startsWith("\"")) { notfirst = true; ZString label; @@ -77,9 +93,10 @@ namespace coreutils { } else throw coreutils::Exception("Labels must be contained in double quotes."); if(ifNext(":")) { - if(label == path[0]) { +// std::cout << "path: (" << pathx << ") " << label << std::endl; + if(label == pathx) { if(path.getList().size() == 1) { -// std::cout << "found cursor: " << unparsed() << std::endl; + std::cout << "found cursor: " << unparsed() << std::endl; return true; } return locate(path[1]); diff --git a/JString.h b/JString.h index 62258e0..360fae7 100644 --- a/JString.h +++ b/JString.h @@ -83,7 +83,9 @@ namespace coreutils { JString & operator=(coreutils:: ZString value) { std::cout << "operator=" << value << std::endl; - + setSize(value.getLength()); + memcpy(data, value.getData(), value.getLength()); + length = value.getLength(); return *this; } diff --git a/MString.cpp b/MString.cpp index 28f9ce1..2b8f3f6 100644 --- a/MString.cpp +++ b/MString.cpp @@ -258,6 +258,7 @@ namespace coreutils { int newBufferSize = ((size / 256) + 1) * 256; if (bufferSize != newBufferSize) { bufferSize = newBufferSize; + std::cout << "Adjusting storage size to " << bufferSize << " to accomodate " << size << "bytes." << std::endl; data = (char *)realloc(data, bufferSize); cursor = data + cursorOffset; } diff --git a/MString.h b/MString.h index e24d22e..9125cd2 100644 --- a/MString.h +++ b/MString.h @@ -227,10 +227,12 @@ namespace coreutils { MString &read(int fd); int offset(); + + protected: + void setSize(int size); private: int bufferSize = 0; - void setSize(int size); }; } diff --git a/testing/compile b/testing/compile index ea451bc..03aa841 100755 --- a/testing/compile +++ b/testing/compile @@ -1,4 +1,4 @@ #!/bin/bash #g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils -#g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils +g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils diff --git a/testing/jstring_test b/testing/jstring_test index 58ac36a..d6cb86e 100755 Binary files a/testing/jstring_test and b/testing/jstring_test differ diff --git a/testing/jstring_test.cpp b/testing/jstring_test.cpp index bc0d136..838fee6 100644 --- a/testing/jstring_test.cpp +++ b/testing/jstring_test.cpp @@ -3,22 +3,30 @@ int main(int argc, char **argv) { - coreutils::MString test0("{\"Number:\"0\",\"id\":\"XXXXX\"}"); + coreutils::MString test0("{\"Number\":\"0\",\"id\":\"XXXXX\"}"); coreutils::JString test1; test1 = test0; + std::cout << test1 << std::endl; + std::cout << test1["id"] << std::endl; + test1["name"] = "Cohen"; + + std::cout << test1 << std::endl; + test1["health"] = "100"; test1["comment"] = "this is a comment"; test1["race"] = "human"; test1["array"] = "[\"test1\",\"test2\",\"test3\"]"; test1["tester"] = "test field"; test1["object1"] = "{\"attr1\":\"value1\",\"attr2\":\"value2\",\"attr3\":\"value3\"}"; + test1["object1.attr2"] = "{\"xattr1\":\"xvalue1\",\"xattr2\":\"xvalue2\",\"xattr3\":\"xvalue3\"}"; + test1["object1.attr3"] = "Im not an object"; std::cout << test1 << std::endl; - coreutils::MString test2 = test1["name"]; + coreutils::ZString test2 = test1["name"]; std::cout << test2 << std::endl; std::cout << test1["name"] << std::endl; @@ -29,6 +37,11 @@ int main(int argc, char **argv) { std::cout << test1["tester"] << std::endl; std::cout << test1["object1"] << std::endl; std::cout << test1["object1.attr2"] << std::endl; + std::cout << test1["object1.attr2.xattr3"] << std::endl; + std::cout << test1["object1.attr3"] << std::endl; + std::cout << test1["array[0]"] << std::endl; + std::cout << test1["array[1]"] << std::endl; + std::cout << test1["array[2]"] << std::endl; return 0; } diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp index 47e0596..cfb4d66 100644 --- a/testing/mstring_test.cpp +++ b/testing/mstring_test.cpp @@ -8,8 +8,11 @@ int main(int argc, char **argv) { std::cout << "char * operator=: [" << test1 << "]." << std::endl; test1 = "new value."; std::cout << "char * operator=: [" << test1 << "]." << std::endl; + std::cout << "001" << std::endl; std::string test11 = "this is a test"; + std::cout << "002" << std::endl; coreutils::MString test12 = test11; + std::cout << "003" << std::endl; std::cout << "assign from std::string: [" << test12 << "]." << std::endl; coreutils::MString test2("this is another test.");