MString updates.
This commit is contained in:
parent
d2f13c192c
commit
036978130d
2
File.cpp
2
File.cpp
@ -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);
|
||||||
|
41
MString.cpp
41
MString.cpp
@ -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());
|
||||||
@ -53,41 +59,50 @@ 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) {
|
||||||
int len = strlen(value);
|
int len = strlen(value);
|
||||||
setSize(len);
|
setSize(len);
|
||||||
memcpy(data, value, len);
|
memcpy(data, value, len);
|
||||||
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) {
|
||||||
@ -95,10 +110,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
46
MString.h
46
MString.h
@ -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);
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -74,24 +86,24 @@ namespace coreutils {
|
|||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
|
||||||
MString& operator<<(const char *value);
|
MString& operator<<(const char *value);
|
||||||
|
|
||||||
///
|
|
||||||
///
|
|
||||||
///
|
|
||||||
|
|
||||||
int write(char ch);
|
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
|
||||||
int write(ZString &value);
|
MString& write(char ch);
|
||||||
|
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
|
||||||
|
MString& write(ZString &value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int bufferSize = 0;
|
int bufferSize = 0;
|
||||||
void setSize(int size);
|
void setSize(int size);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
17
ZString.cpp
17
ZString.cpp
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
Binary file not shown.
@ -20,10 +20,12 @@ int main(int argc, char **argv) {
|
|||||||
coreutils::MString test4;
|
coreutils::MString test4;
|
||||||
test4 << "this is a test.";
|
test4 << "this is a test.";
|
||||||
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
|
std::cout << "char* operator<<: [" << test4 << "]." << std::endl;
|
||||||
|
|
||||||
// coreutils::ZString temp("this is literal zstring data.");
|
// String building tests.
|
||||||
// coreutils::MString test = temp;
|
coreutils::MString test5("/this/is/a/path");
|
||||||
// std::cout << "operator=: [" << test << "]." << std::endl;
|
coreutils::MString test6("/filename");
|
||||||
|
test5.write(test6);
|
||||||
|
std::cout << "test write: [" << test5 << "]." << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user