From b25650c5a7b302bb8d41e69edab99dfcabb6a112 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Mon, 4 Apr 2022 13:35:15 -0700 Subject: [PATCH] << operator now outputs fulll ZString. --- MString.cpp | 40 +++++++++++++++++++++------------------- MString.h | 5 ++++- ZString.cpp | 52 ++++++++++++++++++++++++++++++++-------------------- ZString.h | 29 ++++++++++++++++++++++++++--- 4 files changed, 83 insertions(+), 43 deletions(-) diff --git a/MString.cpp b/MString.cpp index ce07a9d..05b700c 100644 --- a/MString.cpp +++ b/MString.cpp @@ -1,6 +1,7 @@ #include "MString.h" #include "Log.h" #include +#include namespace coreutils { @@ -12,7 +13,7 @@ namespace coreutils { MString::MString(const char *data) { this->length = strlen(data); - bufferSize = (this->length % 256) + 256; + setSize(this->length); this->data = (char *)malloc(bufferSize); memcpy(this->data, data, length); cursor = this->data; @@ -20,16 +21,14 @@ namespace coreutils { MString::MString(char *data, size_t length) { this->length = length; - bufferSize = (this->length % 256) + 256; - this->data = (char *)malloc(bufferSize); + setSize(length); memcpy(this->data, data, length); cursor = this->data; } MString::MString(const char *data, size_t length) { this->length = length; - bufferSize = (this->length % 256) + 256; - this->data = (char *)malloc(bufferSize); + setSize(length); memcpy(this->data, data, length); cursor = this->data; } @@ -44,8 +43,7 @@ namespace coreutils { } MString::MString(std::string data) { - bufferSize = (data.length() % 256) + 256; - this->data = (char *)malloc(bufferSize); + setSize(data.length()); memcpy(this->data, (char *)data.c_str(), data.length()); length = data.length(); cursor = this->data; @@ -53,22 +51,26 @@ namespace coreutils { MString::~MString() { if(data) - free(data); + free(data); } - + int MString::write(char ch) { - if(bufferSize == 0) { - bufferSize = 256; - data = (char *)malloc(bufferSize); - cursor = data; - length = 0; - } else if(bufferSize < (length + 1)) { - bufferSize += 256; - data = (char *)realloc(data, bufferSize); - cursor = data; - } + setSize(bufferSize + 1); data[length++] = ch; return 1; } + + int MString::write(ZString &value) { + setSize(bufferSize + value.getLength()); + memcpy(data, value.getData(), value.getLength()); + length += value.getLength(); + return value.getLength(); + } + void MString::setSize(int size) { + bufferSize = (size % 256) + 256; + data = (char *)realloc(data, bufferSize); + length = size; + } + } diff --git a/MString.h b/MString.h index 9fec81b..26a59e3 100644 --- a/MString.h +++ b/MString.h @@ -49,10 +49,13 @@ namespace coreutils { ~MString(); int write(char ch); + + int write(ZString &value); private: int bufferSize = 0; - + void setSize(int size); + }; } diff --git a/ZString.cpp b/ZString.cpp index 50acdb5..f990551 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -1,32 +1,27 @@ #include "ZString.h" #include "Log.h" +#include "Exception.h" +#include -namespace coreutils -{ +namespace coreutils { - std::ostream &operator<<(std::ostream &os, const ZString &zstring) - { - for (char *ix = zstring.cursor; ix < (zstring.data + zstring.length); ++ix) - { - os << *ix; - } + std::ostream &operator<<(std::ostream &os, const ZString &zstring) { + for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix) + os << *ix; return os; } - std::ostream &operator<<(std::ostream &os, const std::string &string) - { + std::ostream &operator<<(std::ostream &os, const std::string &string) { os << string; return os; } - std::ostream &operator+(std::ostream &os, const ZString &zstring) - { + std::ostream &operator+(std::ostream &os, const ZString &zstring) { os << zstring; return os; } - ZString::ZString() - { + ZString::ZString() { data = NULL; length = 0; cursor = data; @@ -38,16 +33,14 @@ namespace coreutils ZString::ZString(const char *data, size_t length) : data((char *)data), length(length), cursor((char *)data) {} - ZString::ZString(const ZString &zstring) - { + ZString::ZString(const ZString &zstring) { data = zstring.data; length = zstring.length; cursor = zstring.cursor; // Log(LOG_DEBUG_2) << "ZString Copy Constructor: "; } - ZString::ZString(std::string data) - { + ZString::ZString(std::string data) { this->data = (char *)data.c_str(); length = data.length(); cursor = (char *)data.c_str(); @@ -80,6 +73,15 @@ namespace coreutils return tempInt; } + int ZString::incrementCursor(int length) { + char *temp = cursor; + if((cursor + length) > (data + this->length)) + cursor = data + this->length; + else + cursor += length; + return cursor - temp; + } + std::string ZString::str() { return std::string(data, length); @@ -279,6 +281,12 @@ namespace coreutils char *ZString::getCursor() { return cursor; } + + void ZString::setCursor(char *cursor) { + if((cursor < data) || (cursor > (data + length))) + throw Exception("Cursor out of range on setCursor."); + this->cursor = cursor; + } size_t ZString::getLength() { @@ -292,10 +300,14 @@ namespace coreutils cursor = data; } - ZString ZString::getParsed() { + ZString ZString::parsed() { return ZString(data, cursor - data); } - + + ZString ZString::unparsed() { + return ZString(cursor, data + length - cursor); + } + void ZString::reset() { cursor = data; diff --git a/ZString.h b/ZString.h index 6ee019c..25c217c 100644 --- a/ZString.h +++ b/ZString.h @@ -1,3 +1,4 @@ + #ifndef __ZString_h__ #define __ZString_h__ @@ -23,6 +24,12 @@ namespace coreutils { /// ZString and is used to point to the current parsing point. Several methods are /// provided to advance through the parsing based upon content and rule checks. /// + /// During the parsing process you can retrieve the parsed and unparsed sections of + /// the ZString as ZStrings. + /// + /// You can stream the ZString to an output stream usinf the << operator and the entire + /// contents of the ZString will be output regardless of parsing activity. + /// class ZString { @@ -87,6 +94,12 @@ namespace coreutils { int asInteger(); + /// + /// + /// + + int incrementCursor(int length); + /// /// Return the value of the ZString from the cursor to the end. /// @@ -213,6 +226,8 @@ namespace coreutils { char* getCursor(); + void setCursor(char *cursor); + /// /// Return the length of the ZString. /// @@ -227,10 +242,18 @@ namespace coreutils { void setZString(ZString zstring); /// - /// + /// Use the parsed method to return a ZString that represents the portion + /// ZString that has been parsed. The returned ZString represents the + /// portion of the ZString from the data pointer to the cursor. /// - ZString getParsed(); + ZString parsed(); + + /// + /// + /// + + ZString unparsed(); /// /// @@ -252,7 +275,7 @@ namespace coreutils { void nextChar(); -// protected: + protected: char *data; char *cursor; size_t length;