Fix File object.

This commit is contained in:
Brad Arant 2022-08-05 06:18:22 -07:00
parent d38b7f0c55
commit eb48a5180d
2 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#include "File.h" #include "File.h"
#include "Exception.h" #include "Exception.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <iostream>
#include <sstream> #include <sstream>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
@ -9,6 +10,14 @@
namespace coreutils { namespace coreutils {
File::File(std::string fileName, int mode, int authority) { 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; this->fileName = fileName;
@ -19,7 +28,7 @@ namespace coreutils {
setBufferSize(status.st_size); setBufferSize(status.st_size);
fd = open(fileName.c_str(), mode, authority); fd = ::open(fileName.c_str(), mode, authority);
if(fd < 0) { if(fd < 0) {
std::stringstream temp; std::stringstream temp;
temp << "Error opening file " << fileName << "."; 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() { File::~File() {
close(fd); close(fd);
setBufferSize(0); setBufferSize(0);
@ -50,6 +55,10 @@ namespace coreutils {
::write(fd, data.c_str(), data.length()); ::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() { std::string File::asString() {
return std::string(buffer, size); return std::string(buffer, size);
} }

2
File.h
View File

@ -22,6 +22,7 @@ namespace coreutils {
void setBufferSize(size_t size); void setBufferSize(size_t size);
void read(); void read();
void write(std::string data); void write(std::string data);
void write(coreutils::ZString &data);
std::string asString(); std::string asString();
char *buffer; char *buffer;
@ -30,6 +31,7 @@ namespace coreutils {
std::string fileName; std::string fileName;
private: private:
void open(std::string fileName, int mode, int authority);
int fd; int fd;
}; };