1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CORE_IO_STD_DIRECTORY_H 17 #define CORE_IO_STD_DIRECTORY_H 18 19 #include <base/containers/string.h> 20 #include <base/containers/string_view.h> 21 #include <base/containers/unique_ptr.h> 22 #include <base/containers/vector.h> 23 #include <base/namespace.h> 24 #include <core/io/intf_directory.h> 25 #include <core/namespace.h> 26 27 CORE_BEGIN_NAMESPACE() 28 struct DirImpl; 29 30 class StdDirectory final : public IDirectory { 31 public: 32 StdDirectory(BASE_NS::unique_ptr<DirImpl>); 33 ~StdDirectory() override; 34 35 static IDirectory::Ptr Create(BASE_NS::string_view path); 36 static IDirectory::Ptr Open(BASE_NS::string_view path); 37 static bool DirectoryExists(BASE_NS::string_view path); 38 void Close() override; 39 40 BASE_NS::vector<Entry> GetEntries() const override; 41 42 // NOTE: Helper function for resolving paths does not really belong here but it's here 43 // so that msdirent.h is not needed in multiple compilation units. 44 static BASE_NS::string ResolveAbsolutePath(BASE_NS::string_view path, bool isDirectory); 45 static void FormatPath(BASE_NS::string& path, bool isDirectory); 46 static BASE_NS::string GetDirName(BASE_NS::string_view path); 47 static BASE_NS::string GetBaseName(BASE_NS::string_view path); 48 49 protected: Destroy()50 void Destroy() override 51 { 52 delete this; 53 } 54 55 private: 56 BASE_NS::unique_ptr<DirImpl> dir_; 57 }; 58 CORE_END_NAMESPACE() 59 60 #endif // CORE_IO_STD_DIRECTORY_H 61