Merge up with master branch.

This commit is contained in:
Brad Arant 2022-07-28 18:52:30 -07:00
commit f8bc2df133
5 changed files with 68 additions and 35 deletions

View File

@ -29,7 +29,7 @@ namespace coreutils {
File::File(coreutils::ZString &fileName, int mode, int authority) { File::File(coreutils::ZString &fileName, int mode, int authority) {
File(fileName.str(), mode, authority); File(fileName.str(), mode, authority);
} }
File::~File() { File::~File() {
close(fd); close(fd);

View File

@ -24,7 +24,7 @@ namespace coreutils {
} }
MString::MString(const char *data, size_t length) { MString::MString(const char *data, size_t length) {
setSize(length); setSize(length);
memcpy(this->data, data, length); memcpy(this->data, data, length);
cursor = this->data; cursor = this->data;
} }
@ -36,6 +36,12 @@ namespace coreutils {
// Log(LOG_DEBUG_2) << "MString Copy Constructor: "; // 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) { MString::MString(std::string data) {
setSize(data.length()); setSize(data.length());
memcpy(this->data, (char *)data.c_str(), data.length()); memcpy(this->data, (char *)data.c_str(), data.length());
@ -58,21 +64,30 @@ namespace coreutils {
int len = length; int len = length;
setSize(length + value.getLength()); setSize(length + value.getLength());
memcpy(data + len, value.getData(), 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) { MString& MString::operator=(const char *value) {
int len = strlen(value); int len = strlen(value);
setSize(len); setSize(len);
memcpy(data, value, len); memcpy(data, value, len);
return *this; return *this;
} }
MString& MString::operator=(char value) { MString& MString::operator=(char value) {
int len = 1; int len = 1;
setSize(1); setSize(1);
*data = value; *data = value;
return *this; return *this;
} }
MString& MString::operator<<(const char *value) { MString& MString::operator<<(const char *value) {
@ -105,18 +120,18 @@ namespace coreutils {
memcpy(data + temp, value.c_str(), value.length()); memcpy(data + temp, value.c_str(), value.length());
return *this; return *this;
} }
int MString::write(char ch) { MString& MString::write(char ch) {
setSize(length + 1); setSize(length + 1);
*(data + length - 1) = ch; *(data + length - 1) = ch;
return 1; return *this;
} }
int MString::write(ZString &value) { MString& MString::write(ZString &value) {
int len = length; int len = length;
setSize(length + value.getLength()); setSize(length + value.getLength());
memcpy(data + len, value.getData(), value.getLength()); memcpy(data + len, value.getData(), value.getLength());
return value.getLength(); return *this;
} }
void MString::setSize(int size) { void MString::setSize(int size) {
@ -124,10 +139,10 @@ namespace coreutils {
int newBufferSize = ((size / 256) + 1) * 256; int newBufferSize = ((size / 256) + 1) * 256;
if(bufferSize != newBufferSize) { if(bufferSize != newBufferSize) {
bufferSize = newBufferSize; bufferSize = newBufferSize;
data = (char *)realloc(data, bufferSize); data = (char *)realloc(data, bufferSize);
cursor = data + cursorOffset; cursor = data + cursorOffset;
} }
length = size; length = size;
} }
} }

View File

@ -8,12 +8,12 @@
namespace coreutils { 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 /// 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 /// added functionalities to build strings as well as the parsing power of the
/// ZString. /// ZString.
/// ///
class MString : public ZString { class MString : public ZString {
public: public:
@ -21,9 +21,9 @@ namespace coreutils {
/// ///
/// ///
/// ///
MString(); MString();
/// ///
/// ///
/// ///
@ -42,13 +42,19 @@ namespace coreutils {
/// Consructor providing a copy of a ZString. /// 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. /// Consructor from a string.
/// ///
MString(std::string string); MString(std::string data);
~MString(); ~MString();
@ -62,6 +68,12 @@ namespace coreutils {
/// Assignment operator will copy data to backing store. /// 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); MString& operator=(const char *data);
/// ///
@ -85,19 +97,19 @@ namespace coreutils {
/// ///
/// ///
/// ///
int write(char ch); MString& write(char ch);
/// ///
/// ///
/// ///
int write(ZString &value); MString& write(ZString &value);
private: private:
int bufferSize = 0; int bufferSize = 0;
void setSize(int size); void setSize(int size);
}; };
} }

View File

@ -6,8 +6,8 @@
namespace coreutils { namespace coreutils {
std::ostream &operator<<(std::ostream &os, const ZString &zstring) { std::ostream &operator<<(std::ostream &os, const ZString &zstring) {
for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix) for (char *ix = zstring.data; ix < (zstring.data + zstring.length); ++ix)
os << *ix; os << *ix;
return os; return os;
} }
@ -21,6 +21,11 @@ namespace coreutils {
return os; return os;
} }
std::ostream &operator+(std::ostream &os, const char *value) {
os << value;
return os;
}
ZString::ZString() { ZString::ZString() {
data = NULL; data = NULL;
length = 0; length = 0;
@ -49,16 +54,16 @@ namespace coreutils {
ZString::~ZString() { ZString::~ZString() {
if(cdata) if(cdata)
free(cdata); free(cdata);
} }
bool ZString::operator<(const ZString &valuex) const { bool ZString::operator<(const ZString &valuex) const {
return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length)) < 0); return (strncmp(cursor, valuex.cursor, (valuex.length <= length ? valuex.length : length)) < 0);
} }
bool ZString::operator>(const ZString &valuex) const { bool ZString::operator>(const ZString &valuex) const {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) > 0); return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) > 0);
} }
bool ZString::operator==(const ZString &valuex) const { bool ZString::operator==(const ZString &valuex) const {
return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0); return (strncmp(data, valuex.data, valuex.length <= length ? valuex.length : length) == 0);
} }

View File

@ -72,7 +72,7 @@ namespace coreutils {
/// ///
~ZString(); ~ZString();
/// ///
/// A friend method used to write the value of the ZString from the cursor /// A friend method used to write the value of the ZString from the cursor
/// point to an ostream using the << syntax. /// 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 ZString& zstring);
friend std::ostream& operator<<(std::ostream& os, const std::string& string); 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 ZString& zstring);
friend std::ostream& operator+(std::ostream& os, const char *value);
bool operator<(const ZString &valuex) const; bool operator<(const ZString &valuex) const;
bool operator>(const ZString &valuex) const; bool operator>(const ZString &valuex) const;
@ -121,7 +122,7 @@ namespace coreutils {
/// ///
void pop(); void pop();
/// ///
/// Return the value of the ZString from the cursor to the end. /// Return the value of the ZString from the cursor to the end.
/// ///
@ -129,7 +130,7 @@ namespace coreutils {
std::string str(); 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. /// specified length.
/// ///
@ -140,7 +141,7 @@ namespace coreutils {
/// ///
char* c_str(); char* c_str();
/// ///
/// ///
/// ///