diff --git a/JString.cpp b/JString.cpp new file mode 100644 index 0000000..f90c22b --- /dev/null +++ b/JString.cpp @@ -0,0 +1,120 @@ +#include "JString.h" +#include "Exception.h" + +namespace coreutils { + + JString::JString() : MString("{}") {} + + JString::JString(const char *data) : MString(data) {} + + void JString::set(ZString path, const char *value) { + reset(); + if(locate(path)) { + push(); + if(ifNext("\"")) { + ZString old = getTokenExclude("\""); + pop(); + remove(offset(), old.getLength() + 2); + MString key; + key << "\"" << value << "\""; + insert(key, offset()); + } else { + pop(); + throw coreutils::Exception("Fix me to accept integer type."); + } + } else { + std::cout << "[" << unparsed() << "]" << std::endl; + MString key; + if(notfirst) + key << ","; + key << "\"" << path << "\":" << "\"" << value << "\""; + insert(key, offset()); + } + } + + ZString JString::find(ZString path) { + if(locate(path)) { + if(ifNext("\"")) + return getTokenExclude("\""); + if(startsWith("[")) { + ZString temp = getContainer(); + std::cout << temp << std::endl; + } + } + return ZString(); + } + + void JString::operator=(ZString &value) { + } + + JString& JString::operator=(const char *value) { + std::cout << "operator=" << value << "\n"; + set(path, value); + return *this; + } + + // ZString JString::operator[](const char *path) { + // std::cout << "operator[" << path << "]\n"; + // this->path = path; + // reset(); + // return find(path); + // } + + JString& JString::operator[](const char *path) { + std::cout << "operator[" << path << "]\n"; + reset(); + this->path = path; + find(path); + return *this; + } + + bool JString::locate(ZString path) { + path.split(".", 1); + notfirst = false; + if(ifNext("{")) { + while(startsWith("\"")) { + notfirst = true; + ZString label; + ZString value; + if(ifNext("\"")) { + label = getTokenExclude("\""); + ifNext("\""); + } else + throw coreutils::Exception("Labels must be contained in double quotes."); + if(ifNext(":")) { + if(label == path[0]) { + if(path.getList().size() == 1) + return true; + return locate(path[1]); + } if(startsWith("\"")) { + getContainer();; + } else if(startsWith("[")) { + getContainer(); + } else { + throw coreutils::Exception("Unrecognized data type."); + } + ifNext(","); + } else + throw coreutils::Exception("Expected colon after label."); + } + } else if(ifNext("[")) { + ZString label = path[0].getTokenExclude("["); + path[0].ifNext("["); + ZString key = path[0].getTokenExclude("="); + path[0].ifNext("="); + ZString value = path[0].getTokenExclude("]"); + while(!ifNext("]")) { + push(); + ZString check = find(key); + pop(); + if(check.equals(value)) { + return locate(label); + } else { + getContainer(); + ifNext(","); + } + } + } + return false; + } +} diff --git a/testing/jstring_test b/testing/jstring_test new file mode 100755 index 0000000..359d168 Binary files /dev/null and b/testing/jstring_test differ diff --git a/testing/jstring_test.cpp b/testing/jstring_test.cpp new file mode 100644 index 0000000..3794bbd --- /dev/null +++ b/testing/jstring_test.cpp @@ -0,0 +1,33 @@ +#include +#include "../JString.h" + +int main(int argc, char **argv) { + + // coreutils::JString test1; + // std::cout << "empty jstring: [" << test1 << "]." << std::endl; + // test1.set("key1", "data1"); + // std::cout << "add key1 jstring: [" << test1 << "]." << std::endl; + // test1.set("key2", "data2"); + // std::cout << "add key2 jstring: [" << test1 << "]." << std::endl; + // test1.set("key3", "data3"); + // std::cout << test1["key1"] << test1["key2"] << test1["key3"] << std::endl; + // std::cout << test1 << std::endl; + // test1.set("key2", "data5"); + // test1.set("key4", "data4"); + // std::cout << test1 << std::endl; + + coreutils::JString test2("{\"key1\":[{\"id\":\"1\",\"name\":\"Brad\"},{\"id\":\"2\",\"name\":\"Jenn\"},{\"id\":\"3\",\"name\":\"Skye\"}],\"key2\":\"data5\",\"key3\":\"data3\",\"key4\":\"data4\"}"); + + // std::cout << test2["key2"] << std::endl; + // std::cout << test2["key1"] << std::endl; + // std::cout << "value: " << test2["key1.name[id=2]"] << std::endl; + // std::cout << "value: " << test2["key1.name[id=3]"] << std::endl; + + test2["key2"] = "newvalue1"; + std::cout << test2["key2"] << std::endl; + // test2 = "newvalue"; + std::cout << test2 << std::endl; + + return 0; +} + diff --git a/testing/testshadow b/testing/testshadow new file mode 100755 index 0000000..f9b6d85 Binary files /dev/null and b/testing/testshadow differ diff --git a/testing/testshadow.cpp b/testing/testshadow.cpp new file mode 100644 index 0000000..a03d7d1 --- /dev/null +++ b/testing/testshadow.cpp @@ -0,0 +1,70 @@ +#include +#include + +using namespace std; + +struct proxy_obj; + +struct my_bit_array +{ + + uint8_t bit_array_; + + proxy_obj operator[](int n); +} +; + +struct proxy_obj +{ + + my_bit_array& my_bit_array_; + int n_; + + proxy_obj(my_bit_array& bit_array, int n) + : my_bit_array_(bit_array), n_(n) + { + } + + + proxy_obj& operator=(bool b) + { + + uint8_t set_mask = 0x01 << n_; + + if(b) my_bit_array_.bit_array_ |= set_mask; + else my_bit_array_.bit_array_ &= (~set_mask); + + return *this; + } + + + operator bool() + { + return my_bit_array_.bit_array_ & (0x01 << n_); + } + +} +; + +proxy_obj my_bit_array::operator[](int n) +{ + + return proxy_obj(*this, n); +} + + +int main() +{ + + my_bit_array bit_set; + + bit_set[0] = false; + bool b = bit_set[0]; + cout << b << endl; + + bit_set[3] = true; + b = bit_set[3]; + cout << b << endl; + + return 0; +}