diff --git a/File.cpp b/File.cpp index a66b4eb..cd6a4db 100644 --- a/File.cpp +++ b/File.cpp @@ -29,7 +29,7 @@ namespace coreutils { File::File(coreutils::ZString &fileName, int mode, int authority) { File(fileName.str(), mode, authority); - } + } File::~File() { close(fd); diff --git a/MString.cpp b/MString.cpp index 05a83ca..3016cb8 100644 --- a/MString.cpp +++ b/MString.cpp @@ -24,7 +24,7 @@ namespace coreutils { } MString::MString(const char *data, size_t length) { - setSize(length); + setSize(length); memcpy(this->data, data, length); cursor = this->data; } @@ -36,6 +36,12 @@ namespace coreutils { // Log(LOG_DEBUG_2) << "MString Copy Constructor: "; } + MString::MString(ZString &zstring) { + setSize(zstring.getLength()); + memcpy(data, zstring.getData(), zstring.getLength()); + cursor = data; + } + MString::MString(std::string data) { setSize(data.length()); memcpy(this->data, (char *)data.c_str(), data.length()); @@ -53,41 +59,50 @@ namespace coreutils { int len = length; setSize(length + value.getLength()); memcpy(data + len, value.getData(), value.getLength()); - return *this; + return *this; + } + + MString& MString::operator=(coreutils::MString& 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=(const char *value) { int len = strlen(value); setSize(len); memcpy(data, value, len); - return *this; + return *this; } MString& MString::operator=(char value) { int len = 1; setSize(1); *data = value; - return *this; + return *this; } MString& MString::operator<<(const char *value) { int len = strlen(value); setSize(len); memcpy(data, value, len); - return *this; + return *this; } - - int MString::write(char ch) { + + MString& MString::write(char ch) { setSize(length + 1); *(data + length - 1) = ch; - return 1; + return *this; } - - int MString::write(ZString &value) { + + MString& MString::write(ZString &value) { int len = length; setSize(length + value.getLength()); memcpy(data + len, value.getData(), value.getLength()); - return value.getLength(); + return *this; } void MString::setSize(int size) { @@ -95,10 +110,10 @@ namespace coreutils { int newBufferSize = ((size / 256) + 1) * 256; if(bufferSize != newBufferSize) { bufferSize = newBufferSize; - data = (char *)realloc(data, bufferSize); + data = (char *)realloc(data, bufferSize); cursor = data + cursorOffset; } length = size; } - + } diff --git a/MString.h b/MString.h index 9252399..5c48dab 100644 --- a/MString.h +++ b/MString.h @@ -8,12 +8,12 @@ namespace coreutils { /// - /// Use the MString object when you need a permanent backing store on the heap + /// Use the MString object when you need a permanent backing store on the heap /// for a ZString style functionality. Because MString has a backing store we /// added functionalities to build strings as well as the parsing power of the /// ZString. /// - + class MString : public ZString { public: @@ -21,9 +21,9 @@ namespace coreutils { /// /// /// - + MString(); - + /// /// /// @@ -42,13 +42,19 @@ namespace coreutils { /// Consructor providing a copy of a ZString. /// - MString(const MString &zstring); + MString(ZString &zstring); + + /// + /// Consructor providing a copy of a ZString. + /// + + MString(const MString &mstring); /// /// Consructor from a string. /// - MString(std::string string); + MString(std::string data); ~MString(); @@ -62,6 +68,12 @@ namespace coreutils { /// Assignment operator will copy data to backing store. /// + MString& operator=(coreutils::MString& data); + + /// + /// Assignment operator will copy data to backing store. + /// + MString& operator=(const char *data); /// @@ -74,24 +86,24 @@ namespace coreutils { /// /// - MString& operator<<(const char *value); - - /// - /// - /// - - int write(char ch); + MString& operator<<(const char *value); /// /// /// - - int write(ZString &value); - + + MString& write(char ch); + + /// + /// + /// + + MString& write(ZString &value); + private: int bufferSize = 0; void setSize(int size); - + }; } diff --git a/ZString.cpp b/ZString.cpp index 8d64c0d..f63d34d 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -6,8 +6,8 @@ namespace coreutils { std::ostream &operator<<(std::ostream &os, const ZString &zstring) { - for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix) - os << *ix; + for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix) + os << *ix; return os; } @@ -21,6 +21,11 @@ namespace coreutils { return os; } + std::ostream &operator+(std::ostream &os, const char *value) { + os << value; + return os; + } + ZString::ZString() { data = NULL; length = 0; @@ -49,16 +54,16 @@ namespace coreutils { ZString::~ZString() { if(cdata) free(cdata); - } - + } + 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(data, valuex.data, valuex.length <= length ? valuex.length : length) > 0); } - + bool ZString::operator==(const ZString &valuex) const { return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0); } diff --git a/ZString.h b/ZString.h index f544d26..3987b4c 100644 --- a/ZString.h +++ b/ZString.h @@ -72,7 +72,7 @@ namespace coreutils { /// ~ZString(); - + /// /// A friend method used to write the value of the ZString from the cursor /// point to an ostream using the << syntax. @@ -81,6 +81,7 @@ namespace coreutils { friend std::ostream& operator<<(std::ostream& os, const ZString& zstring); friend std::ostream& operator<<(std::ostream& os, const std::string& string); friend std::ostream& operator+(std::ostream& os, const ZString& zstring); + friend std::ostream& operator+(std::ostream& os, const char *value); bool operator<(const ZString &valuex) const; bool operator>(const ZString &valuex) const; @@ -121,7 +122,7 @@ namespace coreutils { /// void pop(); - + /// /// Return the value of the ZString from the cursor to the end. /// @@ -129,7 +130,7 @@ namespace coreutils { std::string str(); /// - /// Return the current value of the ZString as a string of + /// Return the current value of the ZString as a string of /// specified length. /// @@ -140,7 +141,7 @@ namespace coreutils { /// char* c_str(); - + /// /// /// diff --git a/testing/mstring_test b/testing/mstring_test index ecf0e8f..4248355 100755 Binary files a/testing/mstring_test and b/testing/mstring_test differ diff --git a/testing/mstring_test.cpp b/testing/mstring_test.cpp index e0aae0d..8a51bc7 100644 --- a/testing/mstring_test.cpp +++ b/testing/mstring_test.cpp @@ -20,10 +20,12 @@ int main(int argc, char **argv) { coreutils::MString test4; test4 << "this is a test."; std::cout << "char* operator<<: [" << test4 << "]." << std::endl; - -// coreutils::ZString temp("this is literal zstring data."); -// coreutils::MString test = temp; -// std::cout << "operator=: [" << test << "]." << std::endl; + + // String building tests. + coreutils::MString test5("/this/is/a/path"); + coreutils::MString test6("/filename"); + test5.write(test6); + std::cout << "test write: [" << test5 << "]." << std::endl; return 0; }