diff --git a/JString.h b/JString.h new file mode 100644 index 0000000..df5561b --- /dev/null +++ b/JString.h @@ -0,0 +1,31 @@ +#ifndef __JString_h__ +#define __JString_h__ + +#include "MString.h" +#include +#include + +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. + /// + + class JString : public MString { + + public: + + MString & operator[](char *key); + + MString & operator[](ZString &key); + + MString & operator[](std::string key); + + }; + +} + +#endif diff --git a/MString.cpp b/MString.cpp index e7043f0..55cbd46 100644 --- a/MString.cpp +++ b/MString.cpp @@ -2,6 +2,7 @@ #include "Log.h" #include #include +#include namespace coreutils { @@ -139,6 +140,44 @@ namespace coreutils { return *this; } + void MString::insert(char ch, int offset) { + setSize(length + 1); + for(int ix = length; ix >= offset; ix--) + getData()[ix + 1] = getData()[ix]; + data[offset] = ch; + } + + void MString::insert(ZString &value, int offset) { + setSize(length + value.getLength()); + for(int ix = length; ix >= offset; ix--) + getData()[ix + value.getLength()] = getData()[ix]; + std::memcpy(data + offset, value.getData(), value.getLength()); + } + + void MString::insert(std::string value, int offset) { + setSize(length + value.size()); + for(int ix = length; ix >= offset; ix--) + getData()[ix + value.size()] = getData()[ix]; + std::memcpy(data + offset, value.c_str(), value.size()); + } + + void MString::replace(char ch, int offset) { + + } + + void MString::replace(ZString &value, int offset) { + } + + void MString::replace(std::string value, int offset) { + } + + void MString::remove(int offset, int length) { +// (data + this->length) > (data + offset + length) ? length: length; // TODO: Need to calculate correct length. + for(int ix = offset; ix < this->length; ix++) + getData()[ix] = getData()[ix + length]; + setSize(this->length - length); + } + MString& MString::write(char ch) { setSize(length + 1); *(data + length - 1) = ch; diff --git a/MString.h b/MString.h index ea334fd..24b1a88 100644 --- a/MString.h +++ b/MString.h @@ -31,7 +31,7 @@ namespace coreutils { MString(const char *data); /// - /// Consructor providing the initial setup of the ZString object. + /// Consructor providing the initial setup of the MString object. /// MString(char *data, size_t length); @@ -104,6 +104,28 @@ namespace coreutils { /// /// + void insert(char ch, int offset); + void insert(ZString &value, int offset); + void insert(std::string value, int offset); + + /// + /// + /// + + void replace(char ch, int offset); + void replace(ZString &value, int offset); + void replace(std::string value, int offset); + + /// + /// + /// + + void remove(int offset, int length); + + /// + /// + /// + MString& write(char ch); /// diff --git a/testing/mstring_test b/testing/mstring_test index 422aee4..2590637 100755 Binary files a/testing/mstring_test and b/testing/mstring_test differ diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp index e499d0a..3306e55 100644 --- a/testing/mstring_test.cpp +++ b/testing/mstring_test.cpp @@ -30,6 +30,16 @@ int main(int argc, char **argv) { test6 << "Will this work? " << number << test4 << " See how this works."; std::cout << "streaming: [" << test6 << "]." << std::endl; + coreutils::MString test9("XXXXXXXXXYYYYYYYYYY"); + std::cout << "test insert: " << test9 << std::endl; + coreutils::MString test8("zzz"); + test9.insert(test8, 9); + std::cout << "inserting: " << test9 << std::endl; + test9.insert("aaaaa", 9); + std::cout << "inserting: " << test9 << std::endl; + test9.remove(10, 7); + std::cout << "removing: " << test9 << std::endl; + return 0; } diff --git a/testing/zstring_test b/testing/zstring_test index c2d7ff6..f886d29 100755 Binary files a/testing/zstring_test and b/testing/zstring_test differ