diff options
-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 | ||||
-rw-r--r-- | chrome/browser/download/download_file.cc | 12 | ||||
-rw-r--r-- | chrome/browser/download/download_file.h | 2 | ||||
-rw-r--r-- | chrome/browser/history/thumbnail_database.cc | 10 | ||||
-rw-r--r-- | chrome/browser/printing/printing_layout_uitest.cc | 9 | ||||
-rw-r--r-- | chrome/browser/spellchecker.cc | 5 | ||||
-rw-r--r-- | chrome/third_party/hunspell/google/hunspell_tests.cc | 7 | ||||
-rw-r--r-- | chrome/tools/convert_dict/aff_reader.cc | 5 | ||||
-rw-r--r-- | chrome/tools/convert_dict/convert_dict.cc | 6 | ||||
-rw-r--r-- | chrome/tools/convert_dict/dic_reader.cc | 5 | ||||
-rw-r--r-- | chrome/tools/test/image_diff/image_diff.cc | 7 |
19 files changed, 103 insertions, 95 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_; diff --git a/chrome/browser/download/download_file.cc b/chrome/browser/download/download_file.cc index 99f5caa..4fa3637 100644 --- a/chrome/browser/download/download_file.cc +++ b/chrome/browser/download/download_file.cc @@ -67,7 +67,7 @@ DownloadFile::~DownloadFile() { bool DownloadFile::Initialize() { if (file_util::CreateTemporaryFileName(&full_path_)) - return Open(L"wb"); + return Open("wb"); return false; } @@ -107,22 +107,22 @@ bool DownloadFile::Rename(const std::wstring& new_path) { if (!in_progress_) return true; - if (!Open(L"a+b")) + if (!Open("a+b")) return false; return true; } void DownloadFile::Close() { if (file_) { - fclose(file_); + file_util::CloseFile(file_); file_ = NULL; } } -bool DownloadFile::Open(const wchar_t* open_mode) { +bool DownloadFile::Open(const char* open_mode) { DCHECK(!full_path_.empty()); - if (_wfopen_s(&file_, full_path_.c_str(), open_mode)) { - file_ = NULL; + file_ = file_util::OpenFile(full_path_, open_mode); + if (!file_) { return false; } // Sets the Zone to tell Windows that this file comes from the internet. diff --git a/chrome/browser/download/download_file.h b/chrome/browser/download/download_file.h index 5a6158d..5dc5d63 100644 --- a/chrome/browser/download/download_file.h +++ b/chrome/browser/download/download_file.h @@ -111,7 +111,7 @@ class DownloadFile { // based on creation information passed to it, and automatically closed in // the destructor. void Close(); - bool Open(const wchar_t* open_mode); + bool Open(const char* open_mode); // OS file handle for writing FILE* file_; diff --git a/chrome/browser/history/thumbnail_database.cc b/chrome/browser/history/thumbnail_database.cc index b384963..05126b8 100644 --- a/chrome/browser/history/thumbnail_database.cc +++ b/chrome/browser/history/thumbnail_database.cc @@ -4,6 +4,7 @@ #include "chrome/browser/history/thumbnail_database.h" +#include "base/file_util.h" #include "base/time.h" #include "base/string_util.h" #include "chrome/browser/history/url_database.h" @@ -104,11 +105,10 @@ InitStatus ThumbnailDatabase::Init(const std::wstring& db_name) { char filename[256]; sprintf(filename, "<<< YOUR PATH HERE >>>\\%d.jpeg", idx); - FILE* f; - if (fopen_s(&f, filename, "wb") == 0) { - if (!data.empty()) - fwrite(&data[0], 1, data.size(), f); - fclose(f); + if (!data.empty()) { + file_util::WriteFile(ASCIIToWide(std::string(filename)), + reinterpret_cast<char*>(data.data()), + data.size()); } } #endif diff --git a/chrome/browser/printing/printing_layout_uitest.cc b/chrome/browser/printing/printing_layout_uitest.cc index 943aeb6..359237b 100644 --- a/chrome/browser/printing/printing_layout_uitest.cc +++ b/chrome/browser/printing/printing_layout_uitest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/command_line.h" +#include "base/file_util.h" #include "base/gfx/bitmap_header.h" #include "base/gfx/platform_device_win.h" #include "base/gfx/png_decoder.h" @@ -58,11 +59,9 @@ class Image { true, &compressed)); ASSERT_TRUE(compressed.size()); - FILE* f; - ASSERT_EQ(_wfopen_s(&f, filename.c_str(), L"wbS"), 0); - ASSERT_EQ(fwrite(&*compressed.begin(), 1, compressed.size(), f), - compressed.size()); - fclose(f); + ASSERT_EQ(compressed.size(), file_util::WriteFile( + filename, + reinterpret_cast<char*>(&*compressed.begin()), compressed.size())); } double PercentageDifferent(const Image& rhs) const { diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc index 43df962..186791a 100644 --- a/chrome/browser/spellchecker.cc +++ b/chrome/browser/spellchecker.cc @@ -471,10 +471,9 @@ class AddWordToCustomDictionaryTask : public Task { // faster, compared to verifying everytime whether to append a new line // or not. word_ += "\n"; - const char* file_name_char = file_name_.c_str(); - FILE* f = fopen(file_name_char, "a+"); + FILE* f = file_util::OpenFile(file_name_, "a+"); fputs(word_.c_str(), f); - fclose(f); + file_util::CloseFile(f); } std::string file_name_; diff --git a/chrome/third_party/hunspell/google/hunspell_tests.cc b/chrome/third_party/hunspell/google/hunspell_tests.cc index b9775e0..8397c76 100644 --- a/chrome/third_party/hunspell/google/hunspell_tests.cc +++ b/chrome/third_party/hunspell/google/hunspell_tests.cc @@ -104,9 +104,8 @@ void HunspellTest::RunTest(const char* test_base_name) const { serialized.size()); #else // Use "regular" Hunspell. - FILE *aff_file, *dic_file; - fopen_s(&aff_file, aff_name.c_str(), "r"); - fopen_s(&dic_file, dic_name.c_str(), "r"); + FILE* aff_file = file_util::OpenFile(aff_name, "r"); + FILE* dic_file = file_util::OpenFile(dic_name, "r"); EXPECT_TRUE(aff_file && dic_file); Hunspell hunspell(aff_file, dic_file); @@ -211,4 +210,4 @@ TEST_F(HunspellTest, All) { RunTest("utf8_nonbmp"); RunTest("utfcompound"); RunTest("zeroaffix"); -}
\ No newline at end of file +} diff --git a/chrome/tools/convert_dict/aff_reader.cc b/chrome/tools/convert_dict/aff_reader.cc index bc04cde..06ec131 100644 --- a/chrome/tools/convert_dict/aff_reader.cc +++ b/chrome/tools/convert_dict/aff_reader.cc @@ -6,6 +6,7 @@ #include <algorithm> +#include "base/file_util.h" #include "base/string_util.h" #include "chrome/tools/convert_dict/hunspell_reader.h" @@ -44,7 +45,7 @@ void CollapseDuplicateSpaces(std::string* str) { } // namespace AffReader::AffReader(const std::string& filename) { - fopen_s(&file_, filename.c_str(), "r"); + file_ = file_util::OpenFile(filename, "r"); // Default to Latin1 in case the file doesn't specify it. encoding_ = "ISO8859-1"; @@ -52,7 +53,7 @@ AffReader::AffReader(const std::string& filename) { AffReader::~AffReader() { if (file_) - fclose(file_); + file_util::CloseFile(file_); } bool AffReader::Read() { diff --git a/chrome/tools/convert_dict/convert_dict.cc b/chrome/tools/convert_dict/convert_dict.cc index bf0a820..ab486ac 100644 --- a/chrome/tools/convert_dict/convert_dict.cc +++ b/chrome/tools/convert_dict/convert_dict.cc @@ -13,6 +13,7 @@ #include <stdio.h> +#include "base/file_util.h" #include "base/icu_util.h" #include "base/process_util.h" #include "base/string_util.h" @@ -113,14 +114,13 @@ int main(int argc, char* argv[]) { std::string out_name = file_base + ".bdic"; printf("Writing %s ...\n", out_name.c_str()); - FILE* out_file; - fopen_s(&out_file, out_name.c_str(), "wb"); + FILE* out_file = file_util::OpenFile(out_name, "wb"); if (!out_file) { printf("ERROR writing file\n"); return 1; } fwrite(&serialized[0], 1, serialized.size(), out_file); - fclose(out_file); + file_util::CloseFile(out_file); return 0; } diff --git a/chrome/tools/convert_dict/dic_reader.cc b/chrome/tools/convert_dict/dic_reader.cc index 6e549e3..8bdb8ec 100644 --- a/chrome/tools/convert_dict/dic_reader.cc +++ b/chrome/tools/convert_dict/dic_reader.cc @@ -7,6 +7,7 @@ #include <algorithm> #include <set> +#include "base/file_util.h" #include "base/string_util.h" #include "chrome/tools/convert_dict/aff_reader.h" #include "chrome/tools/convert_dict/hunspell_reader.h" @@ -45,12 +46,12 @@ void SplitDicLine(const std::string& line, std::vector<std::string>* output) { } // namespace DicReader::DicReader(const std::string& filename) { - fopen_s(&file_, filename.c_str(), "r"); + file_ = file_util::OpenFile(filename, "r"); } DicReader::~DicReader() { if (file_) - fclose(file_); + file_util::CloseFile(file_); } bool DicReader::Read(AffReader* aff_reader) { diff --git a/chrome/tools/test/image_diff/image_diff.cc b/chrome/tools/test/image_diff/image_diff.cc index 7b06ed3..65ec67a 100644 --- a/chrome/tools/test/image_diff/image_diff.cc +++ b/chrome/tools/test/image_diff/image_diff.cc @@ -13,6 +13,7 @@ #include "base/basictypes.h" #include "base/command_line.h" +#include "base/file_util.h" #include "base/gfx/png_decoder.h" #include "base/logging.h" #include "base/process_util.h" @@ -65,8 +66,8 @@ class Image { // Creates the image from the given filename on disk, and returns true on // success. bool CreateFromFilename(const char* filename) { - FILE* f; - if (fopen_s(&f, filename, "rb") != 0) + FILE* f = file_util::OpenFile(std::string(filename), "rb"); + if (!f) return false; std::vector<unsigned char> compressed; @@ -77,7 +78,7 @@ class Image { std::copy(buf, &buf[num_read], std::back_inserter(compressed)); } - fclose(f); + file_util::CloseFile(f); if (!PNGDecoder::Decode(&compressed[0], compressed.size(), PNGDecoder::FORMAT_RGBA, &data_, &w_, &h_)) { |