diff --git a/JString.h b/JString.h index df5561b..3ead6f2 100644 --- a/JString.h +++ b/JString.h @@ -11,19 +11,44 @@ namespace coreutils { /// Use the JString object when you need a JSON interface to C++. JString uses /// an MString to store a JSON string representing the object(s) contained. /// The [] operator is overriden from the ZString and provides a method to - /// access the objects elements using a named path as the index to the items. + /// access the objects elements using a named path as the index to the items. /// class JString : public MString { public: - MString & operator[](char *key); + JString(); + JString(const char *data); - MString & operator[](ZString &key); + void set(ZString path, const char *value); + + /// + /// Use the find method to look through an object for member specified by + /// the key in parameter 1. The cursor must be pointing to the { character + /// that begins the object's list. + /// + + ZString find(ZString path); + + void operator=(ZString &value); + JString& operator=(const char *value); + + // ZString operator[](const char *path); + JString& operator[](const char *path); + // operator const ZString () const { + // return *this; + // } + + // MString & operator[](ZString &key); + + // MString & operator[](std::string key); + + private: + bool locate(ZString path); + bool notfirst; + MString path; - MString & operator[](std::string key); - }; } diff --git a/MString.cpp b/MString.cpp index 55cbd46..26530a6 100644 --- a/MString.cpp +++ b/MString.cpp @@ -166,6 +166,7 @@ namespace coreutils { } void MString::replace(ZString &value, int offset) { + } void MString::replace(std::string value, int offset) { @@ -202,4 +203,9 @@ namespace coreutils { length = size; } + int MString::offset() { + return cursor - data; + } + + } diff --git a/MString.h b/MString.h index 24b1a88..caae1fd 100644 --- a/MString.h +++ b/MString.h @@ -121,7 +121,7 @@ namespace coreutils { /// void remove(int offset, int length); - + /// /// /// @@ -134,6 +134,8 @@ namespace coreutils { MString& write(ZString &value); + int offset(); + private: int bufferSize = 0; void setSize(int size); diff --git a/ZString.cpp b/ZString.cpp index e583997..dda7a23 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -74,15 +74,15 @@ namespace coreutils { return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0); } - bool ZString::operator==(std::string value) { - if(value.length() != length) - return false; - return (strncmp(data, value.c_str(), length) == 0); - } + // bool ZString::operator==(std::string value) { + // if(value.length() != length) + // return false; + // return (strncmp(data, value.c_str(), length) == 0); + // } - bool ZString::operator==(const char *valuex) const { - return (strncmp(data, valuex, length) == 0); - } +// bool ZString::operator==(const char *valuex) const { +// return (strncmp(data, valuex, length) == 0); +// } bool ZString::operator!=(const ZString &valuex) const { // if(length != valuex.getLength()) @@ -205,11 +205,35 @@ namespace coreutils { ++cursor; return ZString(start, cursor - start); } - + ZString ZString::getTokenExclude(std::string exclude) { return getTokenExclude(exclude.c_str()); } + ZString ZString::getContainer() { + char term = *cursor == '"' ? '"': *cursor == '(' ? ')': *cursor == '[' ? ']': *cursor == '{' ? '}': 0; + if(!term) + return ZString(); + char *start = cursor; + char nest = *cursor; + char *end = cursor + length; + int level = 0; + ++cursor; + while(cursor != end) { + if(*cursor == term) { + if(!level) { + ++cursor; + return ZString(start, cursor - start); + } + --level; + } else if(*cursor == nest) { + ++level; + } + ++cursor; + } + return ZString(); + } + ZString &ZString::operator[](int index) { return list[index]; } @@ -227,7 +251,7 @@ namespace coreutils { return false; return strncmp(data, value, length) == 0; } - + bool ZString::equals(char *value) { if (strlen(value) != length) return false; diff --git a/ZString.h b/ZString.h index af0b45f..dca4685 100644 --- a/ZString.h +++ b/ZString.h @@ -105,8 +105,8 @@ namespace coreutils { bool operator>(const ZString &valuex) const; bool operator==(const ZString &valuex); - bool operator==(std::string value); - bool operator==(const char *valuex) const; +// bool operator==(std::string value); +// bool operator==(const char *valuex) const; bool operator!=(const ZString &valuex) const; bool operator!=(const char *valuex) const; @@ -207,6 +207,14 @@ namespace coreutils { ZString getTokenExclude(std::string exclude); + /// + /// If the cursor is over a container character then a call to this method + /// will return the contents of the container. The cursor will remain at + /// the position following the container. + /// + + ZString getContainer(); + /// /// Use the [] operator to retrieve values delimited by the split method. /// @@ -358,9 +366,9 @@ namespace coreutils { char *cursor; size_t length; char *cdata = NULL; + std::vector list; private: - std::vector list; std::stack stack; bool isCharacter(char ch, const char *string); diff --git a/testing/compile b/testing/compile index a17f5cf..1869368 100755 --- a/testing/compile +++ b/testing/compile @@ -1,3 +1,4 @@ #!/bin/bash g++ -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils g++ -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils +g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils diff --git a/testing/mstring_test b/testing/mstring_test index 2590637..15550ae 100755 Binary files a/testing/mstring_test and b/testing/mstring_test differ diff --git a/testing/zstring_test b/testing/zstring_test index f886d29..1ed0b90 100755 Binary files a/testing/zstring_test and b/testing/zstring_test differ diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index 45d8275..2e9ca55 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -32,4 +32,11 @@ int main(int argc, char **argv) { coreutils::ZString test2("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); std::cout << "substring: [" << test2.substring(10) << "]" << std::endl; + // Test getContainer. + + coreutils::ZString test4("test(this is a container (with a nesting))012345"); + test4.getTokenExclude("("); + std::cout << "container: " << test4.getContainer() << std::endl; + std::cout << "rest: " << test4.unparsed() << std::endl; + }