diff --git a/File.cpp b/File.cpp index e56e512..a66b4eb 100644 --- a/File.cpp +++ b/File.cpp @@ -27,6 +27,10 @@ namespace coreutils { } } + File::File(coreutils::ZString &fileName, int mode, int authority) { + File(fileName.str(), mode, authority); + } + File::~File() { close(fd); setBufferSize(0); diff --git a/File.h b/File.h index 49fc6bc..1535ad3 100644 --- a/File.h +++ b/File.h @@ -3,6 +3,7 @@ #include #include +#include "ZString.h" /// /// File @@ -16,6 +17,7 @@ namespace coreutils { public: File(std::string fileName, int mode = O_RDONLY, int authority = 0664); + File(ZString &fileName, int mode = O_RDONLY, int authority = 0664); ~File(); void setBufferSize(size_t size); void read(); diff --git a/MString.cpp b/MString.cpp index 88104bd..32a18fe 100644 --- a/MString.cpp +++ b/MString.cpp @@ -47,6 +47,15 @@ namespace coreutils { 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; + } int MString::write(char ch) { setSize(length + 1); diff --git a/MString.h b/MString.h index 26a59e3..1773714 100644 --- a/MString.h +++ b/MString.h @@ -7,7 +7,9 @@ namespace coreutils { /// /// Use the MString object when you need a permanent backing store on the heap - /// for a ZString style functionality. + /// 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 { @@ -48,8 +50,22 @@ namespace coreutils { ~MString(); + /// + /// Assignment operator will copy data to backing store. + /// + + MString& operator=(coreutils::ZString& data); + + /// + /// + /// + int write(char ch); + /// + /// + /// + int write(ZString &value); private: diff --git a/ZString.cpp b/ZString.cpp index 6a4d0b9..d5ee756 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -46,6 +46,11 @@ namespace coreutils { cursor = (char *)data.c_str(); } + 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); } @@ -101,24 +106,30 @@ namespace coreutils { return std::string(data, len); } + char* ZString::c_str() { + cdata = (char *)malloc(length + 1); + strncpy(cdata, data, length); + cdata[length] = '\0'; + return cdata; + } + std::vector &ZString::split(std::string delimiter, size_t maxSize) { coreutils::ZString zDelimiter((char *)delimiter.c_str(), delimiter.size()); return split(zDelimiter, maxSize); } - std::vector &ZString::split(ZString &delimiter, size_t maxSize) - { + std::vector &ZString::split(ZString &delimiter, size_t maxSize) { list.clear(); if (length == 0) { list.push_back(ZString("")); return list; } - + maxSize = maxSize == 0 ? 255: maxSize; char *end = data + length; char *pos = cursor; while ((pos + delimiter.getLength()) <= end) { - if ((strncmp(pos, delimiter.getData(), delimiter.getLength()) == 0) && (maxSize > 0)) + if ((strncmp(pos, delimiter.getData(), delimiter.getLength()) == 0) && (maxSize != 0)) { list.push_back(ZString(cursor, pos - cursor)); cursor = pos + delimiter.getLength(); diff --git a/ZString.h b/ZString.h index 07f769d..629dd9f 100644 --- a/ZString.h +++ b/ZString.h @@ -67,6 +67,12 @@ namespace coreutils { ZString(std::string string); + /// + /// Destructor for the ZString. + /// + + ~ZString(); + /// /// A friend method used to write the value of the ZString from the cursor /// point to an ostream using the << syntax. @@ -131,6 +137,12 @@ namespace coreutils { /// /// + char* c_str(); + + /// + /// + /// + std::vector &split(ZString &delimiter, size_t maxSize = 0); /// @@ -293,6 +305,7 @@ namespace coreutils { char *data; char *cursor; size_t length; + char *cdata = NULL; private: std::vector list; diff --git a/testing/zstring_test b/testing/zstring_test index cce96fb..67b85d5 100755 Binary files a/testing/zstring_test and b/testing/zstring_test differ