MString updates.

This commit is contained in:
Brad Arant 2022-07-28 08:06:33 -07:00
parent d2f13c192c
commit 036978130d
7 changed files with 80 additions and 45 deletions

View File

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

View File

@ -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;
}
}

View File

@ -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);
};
}

View File

@ -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);
}

View File

@ -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();
///
///
///

Binary file not shown.

View File

@ -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;
}