file.cc revision 776ca3085c44e6570813270df75278849c37d400
1#include "file.h" 2 3#include <fcntl.h> 4#include <sys/stat.h> 5#include <sys/types.h> 6#include <unistd.h> 7 8#include "ast.h" 9#include "log.h" 10#include "parser.h" 11 12Makefile::Makefile(const string& filename) 13 : len_(0), mtime_(0), filename_(filename) { 14 int fd = open(filename.c_str(), O_RDONLY); 15 if (fd < 0) { 16 return; 17 } 18 19 struct stat st; 20 if (fstat(fd, &st) < 0) { 21 PERROR("fstat failed for %s", filename.c_str()); 22 } 23 24 len_ = st.st_size; 25 mtime_ = st.st_mtime; 26 buf_ = new char[len_]; 27 ssize_t r = read(fd, buf_, len_); 28 if (r != static_cast<ssize_t>(len_)) { 29 if (r < 0) 30 PERROR("read failed for %s", filename.c_str()); 31 ERROR("Unexpected read length=%zd expected=%zu", r, len_); 32 } 33 34 if (close(fd) < 0) { 35 PERROR("close failed for %s", filename.c_str()); 36 } 37 38 Parse(this); 39} 40 41Makefile::~Makefile() { 42 delete[] buf_; 43 for (AST* ast : asts_) 44 delete ast; 45} 46