Worked on ZString, MString and JString
This commit is contained in:
parent
b873bbf164
commit
8a9c1c3bd7
@ -7,6 +7,11 @@
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
///
|
||||
/// Use an Exception oject whenever you want to throw an error for an exception condition
|
||||
/// to your calling stack to inform the program of the condition.
|
||||
///
|
||||
|
||||
class Exception {
|
||||
|
||||
public:
|
||||
|
10
JString.cpp
10
JString.cpp
@ -36,6 +36,8 @@ namespace coreutils {
|
||||
if(locate(path)) {
|
||||
if(ifNext("\""))
|
||||
return getTokenExclude("\"");
|
||||
// TODO: Handle numbers that are not in double quotes.
|
||||
|
||||
if(startsWith("[")) {
|
||||
ZString temp = getContainer();
|
||||
std::cout << temp << std::endl;
|
||||
@ -45,6 +47,7 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
void JString::operator=(ZString &value) {
|
||||
std::cout << "operator=(ZString &)" << value << "\n";
|
||||
}
|
||||
|
||||
JString& JString::operator=(const char *value) {
|
||||
@ -84,11 +87,14 @@ namespace coreutils {
|
||||
if(ifNext(":")) {
|
||||
if(label == path[0]) {
|
||||
if(path.getList().size() == 1)
|
||||
return true;
|
||||
{
|
||||
std::cout << "cursor: " << unparsed() << std::endl;
|
||||
return true;
|
||||
}
|
||||
return locate(path[1]);
|
||||
} if(startsWith("\"")) {
|
||||
getContainer();;
|
||||
} else if(startsWith("[")) {
|
||||
} else if(startsWith("[")) {
|
||||
getContainer();
|
||||
} else {
|
||||
throw coreutils::Exception("Unrecognized data type.");
|
||||
|
15
JString.h
15
JString.h
@ -13,6 +13,20 @@ namespace coreutils {
|
||||
/// 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.
|
||||
///
|
||||
/// JString default constructor will create an empty JString object pointing
|
||||
/// to an empty string.
|
||||
///
|
||||
/// Use a constructor or assignment operator to initialize a JString to an
|
||||
/// initial JSON string.
|
||||
///
|
||||
/// Several constructors are available including a handle to a std::FILE
|
||||
/// object to initialize the JSON string. On destruction the file is saved back.
|
||||
///
|
||||
/// The [] operator can be used to access elements within the JSON string.
|
||||
/// Use:
|
||||
/// jstring["key"] = "value";
|
||||
/// std::cout << jstring["key"];
|
||||
///
|
||||
|
||||
class JString : public MString {
|
||||
|
||||
@ -34,7 +48,6 @@ namespace coreutils {
|
||||
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;
|
||||
|
2
Log.cpp
2
Log.cpp
@ -81,7 +81,7 @@ namespace coreutils {
|
||||
if (logFile)
|
||||
logFile->write(out.str());
|
||||
|
||||
std::cout << out.str();
|
||||
// std::cout << out.str();
|
||||
++seq;
|
||||
|
||||
mtx.unlock();
|
||||
|
39
MString.cpp
39
MString.cpp
@ -24,6 +24,12 @@ namespace coreutils {
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(unsigned char *data, size_t length) {
|
||||
setSize(length);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(const char *data, size_t length) {
|
||||
setSize(length);
|
||||
memcpy(this->data, data, length);
|
||||
@ -55,32 +61,19 @@ namespace coreutils {
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(std::FILE *file) {
|
||||
setSize(1000000);
|
||||
this->length = fread(this->data, 1000000, 1000000, file);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
|
||||
MString::~MString() {
|
||||
if(data)
|
||||
free(data);
|
||||
}
|
||||
|
||||
// MString& MString::operator=(coreutils::ZString& value) {
|
||||
// if(*this == value)
|
||||
// return *this;
|
||||
// int len = length;
|
||||
// setSize(length + value.getLength());
|
||||
// memcpy(data + len, value.getData(), value.getLength());
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
// MString& MString::operator=(coreutils::ZString& value) {
|
||||
// if(*this == value)
|
||||
// return *this;
|
||||
// int len = length;
|
||||
// setSize(length + value.getLength());
|
||||
// memcpy(data + len, value.getData(), value.getLength());
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
MString& MString::operator=(coreutils::ZString &value) {
|
||||
// if(*this == value)
|
||||
// return *this;
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
@ -196,9 +189,9 @@ namespace coreutils {
|
||||
int cursorOffset = cursor - data;
|
||||
int newBufferSize = ((size / 256) + 1) * 256;
|
||||
if(bufferSize != newBufferSize) {
|
||||
bufferSize = newBufferSize;
|
||||
data = (char *)realloc(data, bufferSize);
|
||||
cursor = data + cursorOffset;
|
||||
bufferSize = newBufferSize;
|
||||
data = (char *)realloc(data, bufferSize);
|
||||
cursor = data + cursorOffset;
|
||||
}
|
||||
length = size;
|
||||
}
|
||||
|
16
MString.h
16
MString.h
@ -36,8 +36,12 @@ namespace coreutils {
|
||||
|
||||
MString(char *data, size_t length);
|
||||
|
||||
MString(unsigned char *data, size_t length);
|
||||
|
||||
MString(const char *data, size_t length);
|
||||
|
||||
MString(const unsigned char *data, size_t length);
|
||||
|
||||
///
|
||||
/// Consructor providing a copy of a ZString.
|
||||
///
|
||||
@ -58,7 +62,17 @@ namespace coreutils {
|
||||
|
||||
MString(std::string data);
|
||||
|
||||
~MString();
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
MString(std::FILE *file);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
virtual ~MString();
|
||||
|
||||
///
|
||||
/// Assignment operator will copy data to backing store.
|
||||
|
41
ZString.cpp
41
ZString.cpp
@ -36,8 +36,12 @@ namespace coreutils {
|
||||
|
||||
ZString::ZString(char *data, size_t length) : data(data), length(length), cursor(data) {}
|
||||
|
||||
ZString::ZString(unsigned char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {}
|
||||
|
||||
ZString::ZString(const char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {}
|
||||
|
||||
ZString::ZString(const unsigned char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {}
|
||||
|
||||
ZString::ZString(const ZString &zstring) {
|
||||
data = zstring.data;
|
||||
length = zstring.length;
|
||||
@ -326,7 +330,25 @@ namespace coreutils {
|
||||
}
|
||||
return ZString(temp, tempend - temp);
|
||||
}
|
||||
|
||||
|
||||
int ZString::find(ZString comparator) {
|
||||
int count = 0;
|
||||
char *temp = cursor;
|
||||
char *temp2 = cursor;
|
||||
while(!ifNext(comparator)) {
|
||||
++count;
|
||||
++cursor;
|
||||
++temp2;
|
||||
if(cursor > (data + length)) {
|
||||
cursor = temp;
|
||||
count = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cursor = temp2;
|
||||
return count;
|
||||
}
|
||||
|
||||
ZString ZString::readBlock(size_t size) {
|
||||
char *temp = cursor;
|
||||
cursor += size;
|
||||
@ -356,7 +378,7 @@ namespace coreutils {
|
||||
length = zstring.getLength();
|
||||
cursor = data;
|
||||
}
|
||||
|
||||
|
||||
ZString ZString::parsed() {
|
||||
return ZString(data, cursor - data);
|
||||
}
|
||||
@ -380,10 +402,21 @@ namespace coreutils {
|
||||
if (*(data + length - 1) == '\r')
|
||||
--length;
|
||||
if (cursor >= (data + length))
|
||||
cursor = data + length;
|
||||
cursor = data + length;
|
||||
return len != length;
|
||||
}
|
||||
|
||||
|
||||
bool ZString::boolValue() {
|
||||
if(this->equals("false"))
|
||||
return false;
|
||||
else if(this->equals("0"))
|
||||
return false;
|
||||
else if(this->equals(""))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ZString::nextChar() {
|
||||
if(!eod())
|
||||
++cursor;
|
||||
|
22
ZString.h
22
ZString.h
@ -53,12 +53,16 @@ namespace coreutils {
|
||||
|
||||
ZString(char *data, size_t length);
|
||||
|
||||
ZString(unsigned char *data, size_t length);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
ZString(const char *data, size_t length);
|
||||
|
||||
ZString(const unsigned char *data, size_t length);
|
||||
|
||||
///
|
||||
/// Consructor providing a copy of a ZString.
|
||||
///
|
||||
@ -84,7 +88,7 @@ namespace coreutils {
|
||||
~ZString();
|
||||
|
||||
///
|
||||
/// A friend method used to write the value of the ZString
|
||||
/// A friend method used to write the value of the ZString
|
||||
/// to an ostream using the << syntax.
|
||||
///
|
||||
|
||||
@ -289,6 +293,12 @@ namespace coreutils {
|
||||
|
||||
ZString goeol();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
int find(ZString comparator);
|
||||
|
||||
///
|
||||
/// Return a block of data as a ZString from the cursor location for
|
||||
/// the number of bytes specified by size.
|
||||
@ -359,6 +369,16 @@ namespace coreutils {
|
||||
|
||||
bool ifCRLF();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
bool boolValue();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
|
||||
void nextChar();
|
||||
|
||||
protected:
|
||||
|
Binary file not shown.
@ -17,16 +17,19 @@ int main(int argc, char **argv) {
|
||||
// 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 << std::endl;
|
||||
std::cout << "--------" << std::endl;
|
||||
// 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;
|
||||
std::cout << "--------" << std::endl;
|
||||
// test2 = "newvalue";
|
||||
std::cout << test2 << std::endl;
|
||||
std::cout << test2 << std::endl;
|
||||
std::cout << "--------" << std::endl;
|
||||
std::cout << "key2: " << test2["key2"] << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
@ -1,13 +1,16 @@
|
||||
#include <iostream>
|
||||
#include "../MString.h"
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
// Literal string assignment tests.
|
||||
coreutils::MString test1 = "this is an assignment test.";
|
||||
std::cout << "char * operator=: [" << test1 << "]." << std::endl;
|
||||
test1 = "new value.";
|
||||
std::cout << "char * operator=: [" << test1 << "]." << std::endl;
|
||||
std::cout << "char * operator=: [" << test1 << "]." << std::endl;
|
||||
std::string test11 = "this is a test";
|
||||
coreutils::MString test12 = test11;
|
||||
std::cout << "assign from std::string: [" << test12 << "]." << std::endl;
|
||||
|
||||
coreutils::MString test2("this is another test.");
|
||||
std::cout << "assign on constructor: [" << test2 << "]." << std::endl;
|
||||
@ -23,22 +26,31 @@ int main(int argc, char **argv) {
|
||||
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
|
||||
test4 << "another test " << 75;
|
||||
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
|
||||
|
||||
|
||||
// Stream assignment capabilities.
|
||||
coreutils::MString test6;
|
||||
int number = 75;
|
||||
test6 << "Will this work? " << number << test4 << " See how this works.";
|
||||
std::cout << "streaming: [" << test6 << "]." << std::endl;
|
||||
std::cout << "streaming: [" << test6 << "]." << std::endl;
|
||||
std::string test13 = "stream from string test";
|
||||
coreutils::MString test14;
|
||||
test14 << test13;
|
||||
std::cout << "streaming from std::string: [" << test14 << "]." << 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;
|
||||
|
||||
// Reassignments.
|
||||
|
||||
coreutils::MString test10;
|
||||
test10 = "";
|
||||
std::cout << test10 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
@ -38,5 +38,10 @@ int main(int argc, char **argv) {
|
||||
test4.getTokenExclude("(");
|
||||
std::cout << "container: " << test4.getContainer() << std::endl;
|
||||
std::cout << "rest: " << test4.unparsed() << std::endl;
|
||||
|
||||
|
||||
// Test find();
|
||||
coreutils::ZString testA("this is a test string for MX 250xyzabc");
|
||||
std::cout << "find MX: " << testA.find("MX") << std::endl;
|
||||
std::cout << "unparsed: " << testA.unparsed() << std::endl;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user