summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2011-05-24 16:47:24 (GMT)
committerEvan Martin <martine@danga.com>2011-05-24 16:47:24 (GMT)
commit9227b6d0ddd8ae0171aa466ceda797a948441740 (patch)
treef8c6049ed9ab350fb83dda6249aca84e07e5e381 /src
parentb1c1bae3e0da4cd8fdd2b34a8619b233624d8513 (diff)
downloadNinja-9227b6d0ddd8ae0171aa466ceda797a948441740.zip
Ninja-9227b6d0ddd8ae0171aa466ceda797a948441740.tar.gz
Ninja-9227b6d0ddd8ae0171aa466ceda797a948441740.tar.bz2
move ReadFile into util
Diffstat (limited to 'src')
-rw-r--r--src/ninja.h2
-rw-r--r--src/ninja_jumble.cc22
-rw-r--r--src/util.cc23
-rw-r--r--src/util.h4
4 files changed, 27 insertions, 24 deletions
diff --git a/src/ninja.h b/src/ninja.h
index b73d7be..5487cbe 100644
--- a/src/ninja.h
+++ b/src/ninja.h
@@ -33,8 +33,6 @@ struct FileStat;
struct Node;
struct Rule;
-int ReadFile(const string& path, string* contents, string* err);
-
/// Interface for accessing the disk.
///
/// Abstract so it can be mocked out for tests. The real implementation
diff --git a/src/ninja_jumble.cc b/src/ninja_jumble.cc
index bef562b..b6bf097 100644
--- a/src/ninja_jumble.cc
+++ b/src/ninja_jumble.cc
@@ -26,28 +26,6 @@
#include "graph.h"
#include "util.h"
-int ReadFile(const string& path, string* contents, string* err) {
- FILE* f = fopen(path.c_str(), "r");
- if (!f) {
- err->assign(strerror(errno));
- return -errno;
- }
-
- char buf[64 << 10];
- size_t len;
- while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
- contents->append(buf, len);
- }
- if (ferror(f)) {
- err->assign(strerror(errno)); // XXX errno?
- contents->clear();
- fclose(f);
- return -errno;
- }
- fclose(f);
- return 0;
-}
-
int RealDiskInterface::Stat(const string& path) {
struct stat st;
if (stat(path.c_str(), &st) < 0) {
diff --git a/src/util.cc b/src/util.cc
index 63d3800..ff6d18e 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -14,6 +14,7 @@
#include "util.h"
+#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -150,3 +151,25 @@ int MakeDir(const string& path) {
return mkdir(path.c_str(), 0777);
#endif
}
+
+int ReadFile(const string& path, string* contents, string* err) {
+ FILE* f = fopen(path.c_str(), "r");
+ if (!f) {
+ err->assign(strerror(errno));
+ return -errno;
+ }
+
+ char buf[64 << 10];
+ size_t len;
+ while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
+ contents->append(buf, len);
+ }
+ if (ferror(f)) {
+ err->assign(strerror(errno)); // XXX errno?
+ contents->clear();
+ fclose(f);
+ return -errno;
+ }
+ fclose(f);
+ return 0;
+}
diff --git a/src/util.h b/src/util.h
index 49a9aec..8d42ed4 100644
--- a/src/util.h
+++ b/src/util.h
@@ -35,4 +35,8 @@ bool CanonicalizePath(string* path, string* err);
/// Portability abstraction.
int MakeDir(const string& path);
+/// Read a file to a string.
+/// Returns -errno and fills in \a err on error.
+int ReadFile(const string& path, string* contents, string* err);
+
#endif // NINJA_UTIL_H_