diff options
author | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-01 17:40:13 +0000 |
---|---|---|
committer | mark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-01 17:40:13 +0000 |
commit | 836f134c56bfa714217ca12a73482205c1480774 (patch) | |
tree | 5c5b9d938a712e09831bdd32022e1b3716c5fbdf /base | |
parent | d46d6f3b80099ce0c630185a2e86b649bba49254 (diff) | |
download | chromium_src-836f134c56bfa714217ca12a73482205c1480774.zip chromium_src-836f134c56bfa714217ca12a73482205c1480774.tar.gz chromium_src-836f134c56bfa714217ca12a73482205c1480774.tar.bz2 |
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
Diffstat (limited to 'base')
-rw-r--r-- | base/event_recorder.cc | 15 | ||||
-rw-r--r-- | base/file_util.cc | 20 | ||||
-rw-r--r-- | base/file_util.h | 9 | ||||
-rw-r--r-- | base/file_util_posix.cc | 9 | ||||
-rw-r--r-- | base/file_util_win.cc | 17 | ||||
-rw-r--r-- | base/gfx/vector_canvas_unittest.cc | 6 | ||||
-rw-r--r-- | base/perftimer.cc | 14 | ||||
-rw-r--r-- | base/trace_event.cc | 32 | ||||
-rw-r--r-- | base/trace_event.h | 8 |
9 files changed, 69 insertions, 61 deletions
diff --git a/base/event_recorder.cc b/base/event_recorder.cc index a7650a3..ab7fcf9 100644 --- a/base/event_recorder.cc +++ b/base/event_recorder.cc @@ -6,6 +6,7 @@ #include <mmsystem.h> +#include "base/file_util.h" #include "base/logging.h" #include "base/time.h" @@ -48,7 +49,8 @@ bool EventRecorder::StartRecording(const std::wstring& filename) { // Open the recording file. DCHECK(file_ == NULL); - if (_wfopen_s(&file_, filename.c_str(), L"wb+") != 0) { + file_ = file_util::OpenFile(filename, "wb+"); + if (!file_) { DLOG(ERROR) << "EventRecorder could not open log file"; return false; } @@ -61,7 +63,7 @@ bool EventRecorder::StartRecording(const std::wstring& filename) { GetModuleHandle(NULL), 0); if (!journal_hook_) { DLOG(ERROR) << "EventRecorder Record Hook failed"; - fclose(file_); + file_util::CloseFile(file_); return false; } @@ -82,7 +84,7 @@ void EventRecorder::StopRecording() { ::timeEndPeriod(1); DCHECK(file_ != NULL); - fclose(file_); + file_util::CloseFile(file_); file_ = NULL; journal_hook_ = NULL; @@ -98,14 +100,15 @@ bool EventRecorder::StartPlayback(const std::wstring& filename) { // Open the recording file. DCHECK(file_ == NULL); - if (_wfopen_s(&file_, filename.c_str(), L"rb") != 0) { + file_ = file_util::OpenFile(filename, "rb"); + if (!file_) { DLOG(ERROR) << "EventRecorder Playback could not open log file"; return false; } // Read the first event from the record. if (fread(&playback_msg_, sizeof(EVENTMSG), 1, file_) != 1) { DLOG(ERROR) << "EventRecorder Playback has no records!"; - fclose(file_); + file_util::CloseFile(file_); return false; } @@ -147,7 +150,7 @@ void EventRecorder::StopPlayback() { } DCHECK(file_ != NULL); - fclose(file_); + file_util::CloseFile(file_); file_ = NULL; ::timeEndPeriod(1); 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 ContentsEqual(const std::wstring& filename1, } 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) - return false; -#elif defined(OS_POSIX) - FILE* file = fopen(WideToUTF8(path).c_str(), "r"); - if (!file) + FILE* file = OpenFile(path, "rb"); + 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 @@ bool GetFileSize(const std::wstring& file_path, int64* file_size) { return true; } +bool CloseFile(FILE* file) { + return fclose(file) == 0; +} + } // namespace diff --git a/base/file_util.h b/base/file_util.h index 9d52f36..b060579 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -16,6 +16,8 @@ #include <fts.h> #endif +#include <stdio.h> + #include <stack> #include <string> #include <vector> @@ -246,6 +248,13 @@ struct FileInfo { // Returns information about the given file path. bool GetFileInfo(const std::wstring& file_path, FileInfo* info); +// Wrapper for fopen-like calls. Returns non-NULL FILE* on success. +FILE* OpenFile(const std::string& filename, const char* mode); +FILE* OpenFile(const std::wstring& filename, const char* mode); + +// Closes file opened by OpenFile. Returns true on success. +bool CloseFile(FILE* file); + // Reads the given number of bytes from the file into the buffer. Returns // the number of read bytes, or -1 on error. int ReadFile(const std::wstring& filename, char* data, int size); diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc index 65dbfcb..edb20c7 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -9,6 +9,7 @@ #include <fnmatch.h> #include <fts.h> #include <libgen.h> +#include <stdio.h> #include <string.h> #include <sys/errno.h> #include <sys/stat.h> @@ -309,6 +310,14 @@ bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { return true; } +FILE* OpenFile(const std::string& filename, const char* mode) { + return fopen(filename.c_str(), mode); +} + +FILE* OpenFile(const std::wstring& filename, const char* mode) { + return fopen(WideToUTF8(filename).c_str(), mode); +} + int ReadFile(const std::wstring& filename, char* data, int size) { int fd = open(WideToUTF8(filename).c_str(), O_RDONLY); if (fd < 0) diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 6a89b3f..fc53dc9 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -447,6 +447,23 @@ bool GetFileInfo(const std::wstring& file_path, FileInfo* results) { return true; } +FILE* OpenFile(const std::string& filename, const char* mode) { + FILE* file; + if (fopen_s(&file, filename.c_str(), mode) != 0) { + return NULL; + } + return file; +} + +FILE* OpenFile(const std::wstring& filename, const char* mode) { + std::wstring w_mode = ASCIIToWide(std::string(mode)); + FILE* file; + if (_wfopen_s(&file, filename.c_str(), w_mode.c_str()) != 0) { + return NULL; + } + return file; +} + int ReadFile(const std::wstring& filename, char* data, int size) { ScopedHandle file(CreateFile(filename.c_str(), GENERIC_READ, diff --git a/base/gfx/vector_canvas_unittest.cc b/base/gfx/vector_canvas_unittest.cc index 9d99a57..65d3d5f 100644 --- a/base/gfx/vector_canvas_unittest.cc +++ b/base/gfx/vector_canvas_unittest.cc @@ -184,11 +184,11 @@ class Image { true, &compressed)); ASSERT_TRUE(compressed.size()); - FILE* f; - ASSERT_EQ(_wfopen_s(&f, filename.c_str(), L"wbS"), 0); + FILE* f = file_util::OpenFile(filename, "wb"); + ASSERT_TRUE(f); ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), compressed.size()); - fclose(f); + file_util::CloseFile(f); } // Returns the percentage of the image that is different from the other, diff --git a/base/perftimer.cc b/base/perftimer.cc index 1231923..9fc0328 100644 --- a/base/perftimer.cc +++ b/base/perftimer.cc @@ -2,11 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <stdio.h> - #include "base/perftimer.h" +#include <stdio.h> +#include <string> + #include "base/basictypes.h" +#include "base/file_util.h" #include "base/logging.h" static FILE* perf_log_file = NULL; @@ -18,12 +20,8 @@ bool InitPerfLog(const char* log_file) { return false; } -#if defined(OS_WIN) - return fopen_s(&perf_log_file, log_file, "w") == 0; -#elif defined(OS_POSIX) - perf_log_file = fopen(log_file, "w"); + perf_log_file = file_util::OpenFile(std::string(log_file), "w"); return perf_log_file != NULL; -#endif } void FinalizePerfLog() { @@ -32,7 +30,7 @@ void FinalizePerfLog() { NOTREACHED(); return; } - fclose(perf_log_file); + file_util::CloseFile(perf_log_file); } void LogPerfResult(const char* test_name, double value, const char* units) { diff --git a/base/trace_event.cc b/base/trace_event.cc index 92939d5..972f17c 100644 --- a/base/trace_event.cc +++ b/base/trace_event.cc @@ -78,11 +78,7 @@ void TraceLog::Heartbeat() { void TraceLog::CloseLogFile() { if (log_file_) { -#if defined(OS_WIN) - ::CloseHandle(log_file_); -#elif defined(OS_POSIX) - fclose(log_file_); -#endif + file_util::CloseFile(log_file_); } } @@ -92,26 +88,14 @@ bool TraceLog::OpenLogFile() { std::wstring log_file_name; PathService::Get(base::DIR_EXE, &log_file_name); file_util::AppendToPath(&log_file_name, pid_filename); -#if defined(OS_WIN) - log_file_ = ::CreateFile(log_file_name.c_str(), GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (log_file_ == INVALID_HANDLE_VALUE || log_file_ == NULL) { + log_file_ = file_util::OpenFile(log_file_name, "a"); + if (!log_file_) { // try the current directory - log_file_ = ::CreateFile(pid_filename.c_str(), GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (log_file_ == INVALID_HANDLE_VALUE || log_file_ == NULL) { - log_file_ = NULL; + log_file_ = file_util::OpenFile(pid_filename, "a"); + if (!log_file_) { return false; } } - ::SetFilePointer(log_file_, 0, 0, FILE_END); -#elif defined(OS_POSIX) - log_file_ = fopen(WideToUTF8(log_file_name).c_str(), "a"); - if (log_file_ == NULL) - return false; -#endif return true; } @@ -162,13 +146,7 @@ void TraceLog::Trace(const std::string& name, void TraceLog::Log(const std::string& msg) { AutoLock lock(file_lock_); -#if defined (OS_WIN) - SetFilePointer(log_file_, 0, 0, SEEK_END); - DWORD num; - WriteFile(log_file_, (void*)msg.c_str(), (DWORD)msg.length(), &num, NULL); -#elif defined (OS_POSIX) fprintf(log_file_, "%s", msg.c_str()); -#endif } } // namespace base diff --git a/base/trace_event.h b/base/trace_event.h index 932c784..fc531ec 100644 --- a/base/trace_event.h +++ b/base/trace_event.h @@ -73,12 +73,6 @@ #define TRACE_EVENT_INSTANT(name, id, extra) #endif -#if defined(OS_WIN) -typedef HANDLE FileHandle; -#else -typedef FILE* FileHandle; -#endif - namespace process_util { class ProcessMetrics; } @@ -129,7 +123,7 @@ class TraceLog { void Log(const std::string& msg); bool enabled_; - FileHandle log_file_; + FILE* log_file_; Lock file_lock_; TimeTicks trace_start_time_; scoped_ptr<process_util::ProcessMetrics> process_metrics_; |