From eb48a5180d4f813d39cce32923e9f778c7141cdb Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Fri, 5 Aug 2022 06:18:22 -0700 Subject: [PATCH] Fix File object. --- File.cpp | 19 ++++++++++++++----- File.h | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/File.cpp b/File.cpp index cd6a4db..aed1731 100644 --- a/File.cpp +++ b/File.cpp @@ -1,6 +1,7 @@ #include "File.h" #include "Exception.h" #include +#include #include #include #include @@ -9,6 +10,14 @@ namespace coreutils { File::File(std::string fileName, int mode, int authority) { + open(fileName, mode, authority); + } + + File::File(coreutils::ZString &fileName, int mode, int authority) { + open(fileName.str(), mode, authority); + } + + void File::open(std::string fileName, int mode, int authority) { this->fileName = fileName; @@ -19,7 +28,7 @@ namespace coreutils { setBufferSize(status.st_size); - fd = open(fileName.c_str(), mode, authority); + fd = ::open(fileName.c_str(), mode, authority); if(fd < 0) { std::stringstream temp; temp << "Error opening file " << fileName << "."; @@ -27,10 +36,6 @@ namespace coreutils { } } - File::File(coreutils::ZString &fileName, int mode, int authority) { - File(fileName.str(), mode, authority); - } - File::~File() { close(fd); setBufferSize(0); @@ -50,6 +55,10 @@ namespace coreutils { ::write(fd, data.c_str(), data.length()); } + void File::write(coreutils::ZString &data) { + int len = ::write(fd, data.getData(), data.getLength()); + } + std::string File::asString() { return std::string(buffer, size); } diff --git a/File.h b/File.h index 1535ad3..7f2c13f 100644 --- a/File.h +++ b/File.h @@ -22,6 +22,7 @@ namespace coreutils { void setBufferSize(size_t size); void read(); void write(std::string data); + void write(coreutils::ZString &data); std::string asString(); char *buffer; @@ -30,6 +31,7 @@ namespace coreutils { std::string fileName; private: + void open(std::string fileName, int mode, int authority); int fd; };