diff options
author | Lei Zhang <thestig@chromium.org> | 2018-10-03 17:54:39 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-10-03 17:54:39 +0000 |
commit | caca3e0bb0d5ae31f60e2904986f231a6f9ad659 (patch) | |
tree | fdb6cade6dee547ad1912ffd34d5b8962ebad018 | |
parent | f9e0498bb1ce2a52628065bc13389b4fc2768f42 (diff) | |
download | pdfium-caca3e0bb0d5ae31f60e2904986f231a6f9ad659.tar.xz |
Add FxFolderHandleCloser for use with std::unique_ptr.
Use it in a couple of places. Make the similar code in the two places
even more so.
Change-Id: I3fc6a567088217e24506cdf7ab927b94cb5a4d52
Reviewed-on: https://pdfium-review.googlesource.com/43330
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
-rw-r--r-- | core/fxcrt/fx_stream.h | 5 | ||||
-rw-r--r-- | core/fxge/android/cfpf_skiafontmgr.cpp | 7 | ||||
-rw-r--r-- | core/fxge/cfx_folderfontinfo.cpp | 10 |
3 files changed, 14 insertions, 8 deletions
diff --git a/core/fxcrt/fx_stream.h b/core/fxcrt/fx_stream.h index b41d413723..5d473782a4 100644 --- a/core/fxcrt/fx_stream.h +++ b/core/fxcrt/fx_stream.h @@ -28,6 +28,11 @@ FX_FileHandle* FX_OpenFolder(const char* path); bool FX_GetNextFile(FX_FileHandle* handle, ByteString* filename, bool* bFolder); void FX_CloseFolder(FX_FileHandle* handle); +// Used with std::unique_ptr to automatically call FX_CloseFolder(). +struct FxFolderHandleCloser { + inline void operator()(FX_FileHandle* h) const { FX_CloseFolder(h); } +}; + #define FX_FILEMODE_ReadOnly 1 #define FX_FILEMODE_Truncate 2 diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp index 2288a86e92..2aec56a628 100644 --- a/core/fxge/android/cfpf_skiafontmgr.cpp +++ b/core/fxge/android/cfpf_skiafontmgr.cpp @@ -348,12 +348,14 @@ FXFT_Face CFPF_SkiaFontMgr::GetFontFace(const ByteStringView& bsFile, } void CFPF_SkiaFontMgr::ScanPath(const ByteString& path) { - DIR* handle = FX_OpenFolder(path.c_str()); + std::unique_ptr<FX_FileHandle, FxFolderHandleCloser> handle( + FX_OpenFolder(path.c_str())); if (!handle) return; + ByteString filename; bool bFolder = false; - while (FX_GetNextFile(handle, &filename, &bFolder)) { + while (FX_GetNextFile(handle.get(), &filename, &bFolder)) { if (bFolder) { if (filename == "." || filename == "..") continue; @@ -371,7 +373,6 @@ void CFPF_SkiaFontMgr::ScanPath(const ByteString& path) { else ScanFile(fullpath); } - FX_CloseFolder(handle); } void CFPF_SkiaFontMgr::ScanFile(const ByteString& file) { diff --git a/core/fxge/cfx_folderfontinfo.cpp b/core/fxge/cfx_folderfontinfo.cpp index b39c57637f..bbd1f246c9 100644 --- a/core/fxge/cfx_folderfontinfo.cpp +++ b/core/fxge/cfx_folderfontinfo.cpp @@ -131,20 +131,21 @@ bool CFX_FolderFontInfo::EnumFontList(CFX_FontMapper* pMapper) { } void CFX_FolderFontInfo::ScanPath(const ByteString& path) { - FX_FileHandle* handle = FX_OpenFolder(path.c_str()); + std::unique_ptr<FX_FileHandle, FxFolderHandleCloser> handle( + FX_OpenFolder(path.c_str())); if (!handle) return; ByteString filename; bool bFolder; - while (FX_GetNextFile(handle, &filename, &bFolder)) { + while (FX_GetNextFile(handle.get(), &filename, &bFolder)) { if (bFolder) { if (filename == "." || filename == "..") continue; } else { ByteString ext = filename.Right(4); - ext.MakeUpper(); - if (ext != ".TTF" && ext != ".OTF" && ext != ".TTC") + ext.MakeLower(); + if (ext != ".ttf" && ext != ".ttc" && ext != ".otf") continue; } @@ -158,7 +159,6 @@ void CFX_FolderFontInfo::ScanPath(const ByteString& path) { fullpath += filename; bFolder ? ScanPath(fullpath) : ScanFile(fullpath); } - FX_CloseFolder(handle); } void CFX_FolderFontInfo::ScanFile(const ByteString& path) { |