1 // Copyright 2021 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "core/fxcrt/fx_folder.h" 8 9 #include <memory> 10 11 #include "build/build_config.h" 12 #include "third_party/base/memory/ptr_util.h" 13 14 #if !BUILDFLAG(IS_WIN) 15 #error "built on wrong platform" 16 #endif 17 18 #include <direct.h> 19 20 class FX_WindowsFolder : public FX_Folder { 21 public: 22 ~FX_WindowsFolder() override; 23 bool GetNextFile(ByteString* filename, bool* bFolder) override; 24 25 private: 26 friend class FX_Folder; 27 FX_WindowsFolder(); 28 29 HANDLE m_Handle = INVALID_HANDLE_VALUE; 30 bool m_bReachedEnd = false; 31 WIN32_FIND_DATAA m_FindData; 32 }; 33 OpenFolder(const ByteString & path)34std::unique_ptr<FX_Folder> FX_Folder::OpenFolder(const ByteString& path) { 35 // Private ctor. 36 auto handle = pdfium::WrapUnique(new FX_WindowsFolder()); 37 ByteString search_path = path + "/*.*"; 38 handle->m_Handle = 39 FindFirstFileExA(search_path.c_str(), FindExInfoStandard, 40 &handle->m_FindData, FindExSearchNameMatch, nullptr, 0); 41 if (handle->m_Handle == INVALID_HANDLE_VALUE) 42 return nullptr; 43 44 return handle; 45 } 46 47 FX_WindowsFolder::FX_WindowsFolder() = default; 48 ~FX_WindowsFolder()49FX_WindowsFolder::~FX_WindowsFolder() { 50 if (m_Handle != INVALID_HANDLE_VALUE) 51 FindClose(m_Handle); 52 } 53 GetNextFile(ByteString * filename,bool * bFolder)54bool FX_WindowsFolder::GetNextFile(ByteString* filename, bool* bFolder) { 55 if (m_bReachedEnd) 56 return false; 57 58 *filename = m_FindData.cFileName; 59 *bFolder = !!(m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); 60 if (!FindNextFileA(m_Handle, &m_FindData)) 61 m_bReachedEnd = true; 62 return true; 63 } 64