#include "File.h"
#include "Exception.h"

namespace core {
   
   File::File(std::string fileName, int mode, int authority) {
      
      this->fileName = fileName;
      
      struct stat status;
      stat(fileName.c_str(), &status);
      
      buffer = NULL;
      
      setBufferSize(status.st_size);
      
      fd = open(fileName.c_str(), mode, authority);   
      if(fd < 0) {
	 std::stringstream temp;
	 temp << "Error opening file " << fileName << ".";
	 throw Exception(temp.str(), __FILE__, __LINE__);
      }
   }
   
   File::~File() {
      
   }
   
   void File::setBufferSize(size_t size) {
      buffer = (char *)realloc(buffer, size);
      this->size = size;
   }
   
   void File::read() {
      size = ::read(fd, buffer, size);
      setBufferSize(size);   
   }
   
   void File::write(std::string data) {
      ::write(fd, data.c_str(), data.length());
   }
   
}
