diff --git a/MString.cpp b/MString.cpp new file mode 100644 index 0000000..ce07a9d --- /dev/null +++ b/MString.cpp @@ -0,0 +1,74 @@ +#include "MString.h" +#include "Log.h" +#include + +namespace coreutils { + + MString::MString() { + data = NULL; + length = 0; + cursor = NULL; + } + + MString::MString(const char *data) { + this->length = strlen(data); + bufferSize = (this->length % 256) + 256; + this->data = (char *)malloc(bufferSize); + memcpy(this->data, data, length); + cursor = this->data; + } + + MString::MString(char *data, size_t length) { + this->length = length; + bufferSize = (this->length % 256) + 256; + this->data = (char *)malloc(bufferSize); + 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); + memcpy(this->data, data, length); + cursor = this->data; + } + + MString::MString(const MString &mstring) { + bufferSize = (mstring.length % 256) + 256; + data = (char *)malloc(bufferSize); + memcpy(data, mstring.data, mstring.length); + length = mstring.length; + cursor = data; +// Log(LOG_DEBUG_2) << "MString Copy Constructor: "; + } + + MString::MString(std::string data) { + bufferSize = (data.length() % 256) + 256; + this->data = (char *)malloc(bufferSize); + memcpy(this->data, (char *)data.c_str(), data.length()); + length = data.length(); + cursor = this->data; + } + + MString::~MString() { + if(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; + } + data[length++] = ch; + return 1; + } + +} diff --git a/MString.h b/MString.h new file mode 100644 index 0000000..9fec81b --- /dev/null +++ b/MString.h @@ -0,0 +1,60 @@ +#ifndef __MString_h__ +#define __MString_h__ + +#include "ZString.h" + +namespace coreutils { + + /// + /// Use the MString object when you need a permanent backing store on the heap + /// for a ZString style functionality. + /// + + class MString : public ZString { + + public: + + /// + /// + /// + + MString(); + + /// + /// + /// + + MString(const char *data); + + /// + /// Consructor providing the initial setup of the ZString object. + /// + + MString(char *data, size_t length); + + MString(const char *data, size_t length); + + /// + /// Consructor providing a copy of a ZString. + /// + + MString(const MString &zstring); + + /// + /// Consructor from a string. + /// + + MString(std::string string); + + ~MString(); + + int write(char ch); + + private: + int bufferSize = 0; + + }; + +} + +#endif diff --git a/ZString.cpp b/ZString.cpp index f4991bb..50acdb5 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -6,9 +6,9 @@ namespace coreutils std::ostream &operator<<(std::ostream &os, const ZString &zstring) { - for (int ix = 0; ix < (zstring.length - (zstring.cursor - zstring.data)); ++ix) + for (char *ix = zstring.cursor; ix < (zstring.data + zstring.length); ++ix) { - os << zstring.cursor[ix]; + os << *ix; } return os; } @@ -53,9 +53,8 @@ namespace coreutils cursor = (char *)data.c_str(); } - bool ZString::operator<(const ZString &valuex) const - { - return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length) < 0)); + bool ZString::operator<(const ZString &valuex) const { + return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length)) < 0); } bool ZString::operator>(const ZString &valuex) const @@ -166,6 +165,10 @@ namespace coreutils return cursor >= data + length; } + bool ZString::startsWith(const char *value) { + return strncmp(cursor, value, strlen(value)) == 0; + } + bool ZString::equals(const char *value) { if (strlen(value) != length) @@ -202,6 +205,15 @@ namespace coreutils return test; } + bool ZString::ifNext(ZString &value) { + if (((data + length) - cursor) < value.getLength()) + return false; + bool test = (strncmp(cursor, value.getCursor(), value.getLength()) == 0); + if (test) + cursor += value.getLength(); + return test; + } + int ZString::ifEqualsCount(ZString &comparator) { int count = 0; @@ -223,7 +235,7 @@ namespace coreutils int ZString::skipWhitespace() { - int len = strspn(cursor, " \t"); + int len = strspn(cursor, " \n\t"); cursor += len; return len; } @@ -264,6 +276,10 @@ namespace coreutils return data; } + char *ZString::getCursor() { + return cursor; + } + size_t ZString::getLength() { return length - (cursor - data); @@ -276,6 +292,10 @@ namespace coreutils cursor = data; } + ZString ZString::getParsed() { + return ZString(data, cursor - data); + } + void ZString::reset() { cursor = data; @@ -283,7 +303,7 @@ namespace coreutils char ZString::charAt(int index) { - return *(data + index); + return *(cursor + index); } bool ZString::ifCRLF() @@ -298,4 +318,9 @@ namespace coreutils return len != length; } + void ZString::nextChar() { + if(!eod()) + ++cursor; + } + } diff --git a/ZString.h b/ZString.h index 0a63085..6ee019c 100644 --- a/ZString.h +++ b/ZString.h @@ -29,7 +29,7 @@ namespace coreutils { public: /// - /// Consructor providing an empty ZString.. + /// Consructor providing an empty ZString. /// ZString(); @@ -144,6 +144,12 @@ namespace coreutils { /// /// + bool startsWith(const char *value); + + /// + /// + /// + bool equals(const char *value); /// @@ -171,6 +177,8 @@ namespace coreutils { bool ifNext(const char *value); + bool ifNext(ZString &value); + int ifEqualsCount(ZString &comparator); /// @@ -199,6 +207,12 @@ namespace coreutils { char* getData(); + /// + /// + /// + + char* getCursor(); + /// /// Return the length of the ZString. /// @@ -216,6 +230,12 @@ namespace coreutils { /// /// + ZString getParsed(); + + /// + /// + /// + void reset(); /// @@ -230,12 +250,15 @@ namespace coreutils { bool ifCRLF(); - private: - char *data; - size_t length; - char *cursor; - std::vector list; + void nextChar(); +// protected: + char *data; + char *cursor; + size_t length; + + private: + std::vector list; bool isCharacter(char ch, const char *string); };