diff options
author | Evan Martin <martine@danga.com> | 2010-10-30 20:13:06 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2010-10-30 20:13:06 (GMT) |
commit | 0e56cfbde90fd2471f70d826f3d5c70930730119 (patch) | |
tree | 88e3cd2ef640f81c2c405a0f38ab56eaf7301b9a | |
parent | f9c6232544fa1a604fa4109b15e88df39798565d (diff) | |
download | Ninja-0e56cfbde90fd2471f70d826f3d5c70930730119.zip Ninja-0e56cfbde90fd2471f70d826f3d5c70930730119.tar.gz Ninja-0e56cfbde90fd2471f70d826f3d5c70930730119.tar.bz2 |
refactor file reading
-rw-r--r-- | ninja.cc | 9 | ||||
-rw-r--r-- | ninja.h | 2 | ||||
-rw-r--r-- | ninja_jumble.cc | 26 | ||||
-rw-r--r-- | parsers.cc | 9 |
4 files changed, 29 insertions, 17 deletions
@@ -19,6 +19,12 @@ void usage() { ); } +struct RealFileReader : public ManifestParser::FileReader { + bool ReadFile(const string& path, string* content, string* err) { + return ::ReadFile(path, content, err) == 0; + } +}; + int main(int argc, char** argv) { const char* input_file = "build.ninja"; @@ -41,7 +47,8 @@ int main(int argc, char** argv) { } State state; - ManifestParser parser(&state); + RealFileReader file_reader; + ManifestParser parser(&state, &file_reader); string err; if (!parser.Load(input_file, &err)) { fprintf(stderr, "error loading '%s': %s\n", input_file, err.c_str()); @@ -11,6 +11,8 @@ using namespace std; #include "eval_env.h" +int ReadFile(const string& path, string* contents, string* err); + struct DiskInterface { // stat() a file, returning the mtime, or 0 if missing and -1 on other errors. virtual int Stat(const string& path) = 0; diff --git a/ninja_jumble.cc b/ninja_jumble.cc index e9da25e..fa0023f 100644 --- a/ninja_jumble.cc +++ b/ninja_jumble.cc @@ -8,26 +8,26 @@ #include <stdio.h> #include <string.h> -string ReadFile(const string& path, string* err) { +int ReadFile(const string& path, string* contents, string* err) { FILE* f = fopen(path.c_str(), "r"); if (!f) { - if (errno != ENOENT) - err->assign(strerror(errno)); - return ""; + err->assign(strerror(errno)); + return -errno; } - string text; char buf[64 << 10]; size_t len; while ((len = fread(buf, 1, sizeof(buf), f)) > 0) { - text.append(buf, len); + contents->append(buf, len); } if (ferror(f)) { - err->assign(strerror(errno)); - text = ""; + err->assign(strerror(errno)); // XXX errno? + contents->clear(); + fclose(f); + return -errno; } fclose(f); - return text; + return 0; } int RealDiskInterface::Stat(const string& path) { @@ -70,7 +70,13 @@ bool DiskInterface::MakeDirs(const string& path) { } string RealDiskInterface::ReadFile(const string& path, string* err) { - return ::ReadFile(path, err); + string contents; + int ret = ::ReadFile(path, &contents, err); + if (ret == -ENOENT) { + // Swallow ENOENT. + err->clear(); + } + return contents; } bool RealDiskInterface::MakeDir(const string& path) { @@ -208,14 +208,11 @@ bool MakefileParser::Parse(const string& input, string* err) { return true; } -// XXX refactor. -extern string ReadFile(const string& path, string* err); - bool ManifestParser::Load(const string& filename, string* err) { - string input = ReadFile(filename, err); - if (!err->empty()) + string contents; + if (!file_reader_->ReadFile(filename, &contents, err)) return false; - return Parse(input, err); + return Parse(contents, err); } bool ManifestParser::Parse(const string& input, string* err) { |