diff --git a/File.cpp b/File.cpp index 33211ad..0e54e38 100644 --- a/File.cpp +++ b/File.cpp @@ -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; + } + } diff --git a/File.h b/File.h index d2bb3e9..280a6b9 100644 --- a/File.h +++ b/File.h @@ -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; }; diff --git a/MString.cpp b/MString.cpp index 8c3d43f..e7043f0 100644 --- a/MString.cpp +++ b/MString.cpp @@ -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; } diff --git a/MString.h b/MString.h index 2af8803..ea334fd 100644 --- a/MString.h +++ b/MString.h @@ -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.