This commit is contained in:
Matt Arant 2023-10-07 02:31:33 +00:00
commit badfc4c709
12 changed files with 166 additions and 45 deletions

View File

@ -12,7 +12,7 @@ namespace coreutils {
else
this->errorNumber = errorNumber;
Log(LOG_EXCEPT) << text;
Log(LOG_EXCEPT) << text << "(" << file << ":" << line << ")";
}
}

View File

@ -3,13 +3,19 @@
#include <string.h>
#include <string>
#include <errno.h>
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:
Exception(std::string text, std::string file = __FILE__, int line = __LINE__, int errorNumber = -1);
Exception(std::string text, std::string file = __FILE__, int line = __LINE__, int errorNumber = errno);
std::string className;
std::string file;

View File

@ -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.");

View File

@ -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;

View File

@ -81,7 +81,7 @@ namespace coreutils {
if (logFile)
logFile->write(out.str());
std::cout << out.str();
// std::cout << out.str();
++seq;
mtx.unlock();

View File

@ -29,6 +29,13 @@ 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);
@ -65,34 +72,21 @@ 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();

View File

@ -35,10 +35,16 @@ namespace coreutils
/// Consructor providing the initial setup of the MString object.
///
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.
///
///
/// Consructor providing a copy of a ZString.
///
@ -56,8 +62,20 @@ namespace coreutils
///
/// Consructor from a string.
///
MString(std::string data);
///
///
///
MString(char *data, size_t length);
MString(std::FILE *file);
///
///
///
virtual ~MString();
~MString();

View File

@ -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;

View File

@ -60,14 +60,18 @@ namespace coreutils
ZString(const char *data, size_t length);
ZString(unsigned char *data, size_t length);
///
///
/// Consructor providing a copy of a ZString.
///
ZString(const ZString &zstring);
ZString(const unsigned char *data, size_t length);
///
/// Consructor from a string.
/// Consructor providing a copy of a ZString.
///
ZString(std::string &string);
@ -95,7 +99,8 @@ namespace coreutils
friend std::ostream &operator+(std::ostream &os, const char *value);
///
///
/// A friend method used to write the value of the ZString
/// to an ostream using the << syntax.
///
bool operator<(const ZString &valuex) const;
@ -298,7 +303,14 @@ namespace coreutils
ZString readBlock(size_t size);
///
/// Return a pointer to the beginning of the ZString data.
///
///
int find(ZString comparator);
///
/// Return a block of data as a ZString from the cursor location for
/// the number of bytes specified by size.
///
char *getData();
@ -362,6 +374,25 @@ namespace coreutils
void nextChar();
protected:
char *data;
char *cursor;
size_t length;
char *cdata = NULL;
std::vector<ZString> list;
///
///
///
bool boolValue();
///
///
///
void nextChar();
protected:
char *data;
char *cursor;

View File

@ -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;
}

View File

@ -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;
@ -28,22 +31,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;
}

View File

@ -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;
}