• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 ES2PANDA_UTIL_PATH_H
17 #define ES2PANDA_UTIL_PATH_H
18 
19 #include <cstdio>
20 #include <string>
21 #include "util/ustring.h"
22 
23 namespace ark::es2panda::util {
24 const char PATH_DELIMITER =
25 #ifdef _WIN32
26     '\\';
27 #else
28     '/';
29 #endif
30 
31 class Path {
32 public:
33     Path();
34     Path(const std::string &absolutePath, ArenaAllocator *allocator);
35     Path(const std::string &relativePath, const std::string &basePath, ArenaAllocator *allocator);
36     Path(const util::StringView &absolutePath, ArenaAllocator *allocator);
37     Path(const util::StringView &relativePath, const util::StringView &basePath, ArenaAllocator *allocator);
38     bool IsRelative();
39     bool IsAbsolute();
40     const util::StringView &GetPath() const;
41     const util::StringView &GetAbsolutePath() const;
42     const util::StringView &GetExtension() const;
43     const util::StringView &GetFileName() const;
44     const util::StringView &GetFileNameWithExtension() const;
45     const util::StringView &GetParentFolder() const;
46     const util::StringView &GetAbsoluteParentFolder() const;
GetPathDelimiter()47     constexpr static char GetPathDelimiter()
48     {
49         return PATH_DELIMITER;
50     }
51 
52 private:
53     ArenaAllocator *allocator_ {};
54     bool isRelative_ {};
55     util::StringView path_ {};
56     util::StringView basePath_ {};
57     util::StringView absolutePath_ {};
58     util::StringView fileName_ {};
59     util::StringView fileNameWithExtension_ {};
60     util::StringView fileExtension_ {};
61     util::StringView parentFolder_ {};
62     util::StringView absoluteParentFolder_ {};
63 
64     void Initializer(const std::string &absolutePath, ArenaAllocator *allocator);
65     void InitializeBasePath(std::string basePath);
66     void InitializeAbsolutePath();
67     void InitializeParentFolder();
68     void InitializeAbsoluteParentFolder();
69     void InitializeFileName();
70     void InitializeFileNameWithExtension();
71     void InitializeFileExtension();
72 };
73 }  // namespace ark::es2panda::util
74 
75 #endif
76