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     void Close() override;
38 
39     BASE_NS::vector<Entry> GetEntries() const override;
40 
41     // NOTE: Helper function for resolving paths does not really belong here but it's here
42     // so that msdirent.h is not needed in multiple compilation units.
43     static BASE_NS::string ResolveAbsolutePath(BASE_NS::string_view path, bool isDirectory);
44     static void FormatPath(BASE_NS::string& path, bool isDirectory);
45     static BASE_NS::string GetDirName(BASE_NS::string_view path);
46     static BASE_NS::string GetBaseName(BASE_NS::string_view path);
47 
48 protected:
Destroy()49     void Destroy() override
50     {
51         delete this;
52     }
53 
54 private:
55     BASE_NS::unique_ptr<DirImpl> dir_;
56 };
57 CORE_END_NAMESPACE()
58 
59 #endif // CORE__IO__STD_DIRECTORY_H
60