Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "file.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "ast.h" |
| 25 | #include "log.h" |
| 26 | #include "parser.h" |
| 27 | |
| 28 | Makefile::Makefile(const string& filename) |
Shinichiro Hamaji | 6e6de8d | 2015-06-18 11:12:58 +0900 | [diff] [blame] | 29 | : buf_(NULL), len_(0), mtime_(0), filename_(filename) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 30 | int fd = open(filename.c_str(), O_RDONLY); |
| 31 | if (fd < 0) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | struct stat st; |
| 36 | if (fstat(fd, &st) < 0) { |
| 37 | PERROR("fstat failed for %s", filename.c_str()); |
| 38 | } |
| 39 | |
| 40 | len_ = st.st_size; |
| 41 | mtime_ = st.st_mtime; |
| 42 | buf_ = new char[len_]; |
| 43 | ssize_t r = read(fd, buf_, len_); |
| 44 | if (r != static_cast<ssize_t>(len_)) { |
| 45 | if (r < 0) |
| 46 | PERROR("read failed for %s", filename.c_str()); |
| 47 | ERROR("Unexpected read length=%zd expected=%zu", r, len_); |
| 48 | } |
| 49 | |
| 50 | if (close(fd) < 0) { |
| 51 | PERROR("close failed for %s", filename.c_str()); |
| 52 | } |
| 53 | |
| 54 | Parse(this); |
| 55 | } |
| 56 | |
| 57 | Makefile::~Makefile() { |
| 58 | delete[] buf_; |
| 59 | for (AST* ast : asts_) |
| 60 | delete ast; |
| 61 | } |