Added support for MString in File

This commit is contained in:
Brad Arant 2022-11-19 10:18:09 -08:00
parent 2a349c6ceb
commit 88d51cb598
4 changed files with 36 additions and 10 deletions

View File

@ -46,9 +46,28 @@ namespace coreutils {
this->size = size;
}
void File::read() {
int File::read() {
size = ::read(fd, buffer, size);
setBufferSize(size);
return size;
}
int File::readLine() {
setBufferSize(4096);
size = 0;
int len = ::read(fd, buffer + size, 1);
if(len == 0)
mEof = true;
while(!mEof && (*(buffer + size) != '\n')) {
if(*(buffer + size) != '\r') {
// setBufferSize(size);
++size;
}
len = ::read(fd, buffer + size, 1);
if(len == 0)
mEof = true;
}
return size;
}
void File::write(std::string data) {
@ -68,4 +87,8 @@ namespace coreutils {
return zstring;
}
bool File::eof() {
return mEof;
}
}

5
File.h
View File

@ -20,13 +20,15 @@ namespace coreutils {
File(ZString &fileName, int mode = O_RDONLY, int authority = 0664);
~File();
void setBufferSize(size_t size);
void read();
int read();
int readLine();
void write(std::string data);
void write(coreutils::ZString &data);
std::string asString();
coreutils::ZString& asZString();
char *buffer;
size_t size;
bool eof();
std::string fileName;
@ -34,6 +36,7 @@ namespace coreutils {
void open(std::string fileName, int mode, int authority);
int fd;
coreutils::ZString zstring;
bool mEof = false;
};

View File

@ -56,7 +56,7 @@ namespace coreutils {
MString::~MString() {
if(data)
free(data);
free(data);
}
// MString& MString::operator=(coreutils::ZString& value) {
@ -77,12 +77,12 @@ namespace coreutils {
// return *this;
// }
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());
MString& MString::operator=(coreutils::ZString &value) {
// if(*this == value)
// return *this;
setSize(value.getLength());
memcpy(data, value.getData(), value.getLength());
length = value.getLength();
return *this;
}

View File

@ -72,7 +72,7 @@ namespace coreutils {
// MString& operator=(coreutils::ZString& data);
MString& operator=(coreutils::ZString data);
MString& operator=(coreutils::ZString &data);
///
/// Assignment operator will copy data to backing store.