[go: nahoru, domu]

Cross-platform wrappers for fopen, _wfopen_s, etc.
Patch by Paweł Hajdan jr <phajdan.jr@gmail.com>.

http://codereview.chromium.org/6005


git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2760 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/file_util.cc b/base/file_util.cc
index bec910b..09f8249 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -4,6 +4,8 @@
 
 #include "base/file_util.h"
 
+#include <stdio.h>
+
 #include <fstream>
 
 #include "base/logging.h"
@@ -269,23 +271,17 @@
 }
 
 bool ReadFileToString(const std::wstring& path, std::string* contents) {
-#if defined(OS_WIN)
-  FILE* file;
-  errno_t err = _wfopen_s(&file, path.c_str(), L"rbS");
-  if (err != 0)
+  FILE* file = OpenFile(path, "rb");
+  if (!file) {
     return false;
-#elif defined(OS_POSIX)
-  FILE* file = fopen(WideToUTF8(path).c_str(), "r");
-  if (!file)
-    return false;
-#endif
+  }
 
   char buf[1 << 16];
   size_t len;
   while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
     contents->append(buf, len);
   }
-  fclose(file);
+  CloseFile(file);
 
   return true;
 }
@@ -298,5 +294,9 @@
   return true;
 }
 
+bool CloseFile(FILE* file) {
+  return fclose(file) == 0;
+}
+
 }  // namespace