diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-12 20:12:38 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-12 20:12:38 +0000 |
commit | 45cbfb0444d2ae27d9dbc9880c547c0d42978782 (patch) | |
tree | 3f5a3477831c68b73f87365c8222b3033758358a /base/file_util_win.cc | |
parent | f33651e32ab77d1413e01f5fed1e90f5b36f7534 (diff) | |
download | chromium_src-45cbfb0444d2ae27d9dbc9880c547c0d42978782.zip chromium_src-45cbfb0444d2ae27d9dbc9880c547c0d42978782.tar.gz chromium_src-45cbfb0444d2ae27d9dbc9880c547c0d42978782.tar.bz2 |
Add a MemoryMappedFile class to file_util.
Factor out windowisms from the spell checker.
Review URL: http://codereview.chromium.org/14041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6919 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_win.cc')
-rw-r--r-- | base/file_util_win.cc | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/base/file_util_win.cc b/base/file_util_win.cc index db22fa3..c287129 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -595,7 +595,7 @@ bool SetCurrentDirectory(const FilePath& directory) { } /////////////////////////////////////////////// - +// FileEnumerator FileEnumerator::FileEnumerator(const FilePath& root_path, bool recursive, @@ -694,6 +694,70 @@ FilePath FileEnumerator::Next() { return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); } +/////////////////////////////////////////////// +// MemoryMappedFile + +MemoryMappedFile::MemoryMappedFile() + : file_(INVALID_HANDLE_VALUE), + file_mapping_(INVALID_HANDLE_VALUE), + data_(NULL), + length_(INVALID_FILE_SIZE) { +} + +MemoryMappedFile::~MemoryMappedFile() { + CloseHandles(); +} + +bool MemoryMappedFile::Initialize(const FilePath& file_name) { + if (IsValid()) + return false; + + if (!MapFileToMemory(file_name)) { + CloseHandles(); + return false; + } + + return true; +} + +bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { + file_ = ::CreateFile(file_name.value().c_str(), GENERIC_READ, + FILE_SHARE_READ, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + if (file_ == INVALID_HANDLE_VALUE) + return false; + + length_ = ::GetFileSize(file_, NULL); + if (length_ == INVALID_FILE_SIZE) + return false; + + file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY, + 0, length_, NULL); + if (file_mapping_ == INVALID_HANDLE_VALUE) + return false; + + data_ = reinterpret_cast<const uint8*>( + ::MapViewOfFile(file_mapping_, FILE_MAP_READ, 0, 0, length_)); + return data_ != NULL; +} + +bool MemoryMappedFile::IsValid() { + return data_ != NULL; +} + +void MemoryMappedFile::CloseHandles() { + if (data_) + ::UnmapViewOfFile(data_); + if (file_mapping_ != INVALID_HANDLE_VALUE) + ::CloseHandle(file_mapping_); + if (file_ != INVALID_HANDLE_VALUE) + ::CloseHandle(file_); + + data_ = NULL; + file_mapping_ = file_ = INVALID_HANDLE_VALUE; + length_ = INVALID_FILE_SIZE; +} + // Deprecated functions ---------------------------------------------------- void InsertBeforeExtension(std::wstring* path_str, |