Added ZString.c_str() function; Added MStrin operator=.

This commit is contained in:
Brad Arant 2022-07-16 21:33:55 -07:00
parent 2a4fc0ad6f
commit 287933f9ce
7 changed files with 60 additions and 5 deletions

View File

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

2
File.h
View File

@ -3,6 +3,7 @@
#include <string>
#include <fcntl.h>
#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();

View File

@ -48,6 +48,15 @@ namespace coreutils {
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);
*(data + length - 1) = ch;

View File

@ -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:

View File

@ -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> &ZString::split(std::string delimiter, size_t maxSize)
{
coreutils::ZString zDelimiter((char *)delimiter.c_str(), delimiter.size());
return split(zDelimiter, maxSize);
}
std::vector<ZString> &ZString::split(ZString &delimiter, size_t maxSize)
{
std::vector<ZString> &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();

View File

@ -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<ZString> &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<ZString> list;

Binary file not shown.