From 4de8f55113007fdc8e34107950e605fc0209d465 Mon Sep 17 00:00:00 2001 From: "jieluo@google.com" Date: Fri, 18 Jul 2014 00:47:59 +0000 Subject: down integrate to svn --- src/google/protobuf/testing/file.cc | 18 ++++++++++++++++++ src/google/protobuf/testing/file.h | 14 ++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'src/google/protobuf/testing') diff --git a/src/google/protobuf/testing/file.cc b/src/google/protobuf/testing/file.cc index e224781d..9a5b35e8 100644 --- a/src/google/protobuf/testing/file.cc +++ b/src/google/protobuf/testing/file.cc @@ -82,6 +82,24 @@ void File::ReadFileToStringOrDie(const string& name, string* output) { GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name; } +bool File::WriteStringToFile(const string& contents, const string& name) { + FILE* file = fopen(name.c_str(), "wb"); + if (file == NULL) { + GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno); + return false; + } + + if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { + GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno); + return false; + } + + if (fclose(file) != 0) { + return false; + } + return true; +} + void File::WriteStringToFileOrDie(const string& contents, const string& name) { FILE* file = fopen(name.c_str(), "wb"); GOOGLE_CHECK(file != NULL) diff --git a/src/google/protobuf/testing/file.h b/src/google/protobuf/testing/file.h index a6b1c764..91ecce92 100644 --- a/src/google/protobuf/testing/file.h +++ b/src/google/protobuf/testing/file.h @@ -56,6 +56,10 @@ class File { static void ReadFileToStringOrDie(const string& name, string* output); // Create a file and write a string to it. + static bool WriteStringToFile(const string& contents, + const string& name); + + // Same as above, but crash on failure. static void WriteStringToFileOrDie(const string& contents, const string& name); @@ -73,6 +77,16 @@ class File { static void DeleteRecursively(const string& name, void* dummy1, void* dummy2); + static bool GetContents( + const string& name, string* output, bool /*is_default*/) { + return ReadFileToString(name, output); + } + + static bool SetContents( + const string& name, const string& contents, bool /*is_default*/) { + return WriteStringToFile(contents, name); + } + private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(File); }; -- cgit v1.2.3