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 #include <cstdio>
17 #include <string>
18 #include "os/filesystem.h"
19 #include "path.h"
20
21 namespace panda::es2panda::util {
22
23 Path::Path() = default;
24
Path(const util::StringView & absolutePath,ArenaAllocator * allocator)25 Path::Path(const util::StringView &absolutePath, ArenaAllocator *allocator)
26 {
27 Initializer(absolutePath.Mutf8(), allocator);
28 }
29
Initializer(const std::string & path,ArenaAllocator * allocator)30 void Path::Initializer(const std::string &path, ArenaAllocator *allocator)
31 {
32 isRelative_ = false;
33 allocator_ = allocator;
34 path_ = util::UString(path, allocator).View();
35 if (*(path_.Bytes()) == '.') {
36 isRelative_ = true;
37 }
38
39 if (isRelative_) {
40 absolutePath_ = util::UString(os::GetAbsolutePath(path_.Utf8()), allocator_).View();
41 } else {
42 absolutePath_ = path_;
43 }
44
45 InitializeFileExtension();
46 InitializeFileName();
47 InitializeParentFolder();
48 InitializeAbsoluteParentFolder();
49 }
50
InitializeFileName()51 void Path::InitializeFileName()
52 {
53 if (path_.Empty()) {
54 return;
55 }
56
57 int position = path_.Mutf8().find_last_of(PATH_DELIMITER);
58
59 util::StringView fileName = path_.Substr(position + 1, path_.Length());
60 if (GetExtension().Empty()) {
61 fileName_ = fileName;
62 } else {
63 int extensionPosition = fileName.Mutf8().find_last_of('.');
64 fileName_ = fileName.Substr(0, extensionPosition);
65 }
66 }
67
InitializeFileExtension()68 void Path::InitializeFileExtension()
69 {
70 if (path_.Empty()) {
71 return;
72 }
73
74 size_t position = path_.Mutf8().find_last_of('.');
75 if (position != std::string::npos && position + 1 <= path_.Length()) {
76 fileExtension_ = path_.Substr(position + 1, path_.Length());
77 }
78 }
79
InitializeAbsoluteParentFolder()80 void Path::InitializeAbsoluteParentFolder()
81 {
82 if (path_.Empty()) {
83 return;
84 }
85
86 int position = absolutePath_.Mutf8().find_last_of(PATH_DELIMITER);
87
88 if (!absolutePath_.Empty() && isRelative_) {
89 absoluteParentFolder_ = absolutePath_.Substr(0, position);
90 }
91 }
92
InitializeParentFolder()93 void Path::InitializeParentFolder()
94 {
95 if (path_.Empty()) {
96 return;
97 }
98
99 int position = path_.Mutf8().find_last_of(PATH_DELIMITER);
100
101 parentFolder_ = path_.Substr(0, position);
102 }
103
InitializeBasePath(std::string basePath)104 void Path::InitializeBasePath(std::string basePath)
105 {
106 if (!basePath.empty() && basePath.back() == PATH_DELIMITER) {
107 basePath_ = util::UString(basePath.substr(0, basePath.length() - 1), allocator_).View();
108 } else {
109 basePath_ = util::UString(basePath, allocator_).View();
110 }
111
112 isRelative_ = true;
113 }
114
Path(const util::StringView & relativePath,const util::StringView & basePath,ArenaAllocator * allocator)115 Path::Path(const util::StringView &relativePath, const util::StringView &basePath, ArenaAllocator *allocator)
116 {
117 Initializer(relativePath.Mutf8(), allocator);
118 InitializeBasePath(basePath.Mutf8());
119 }
120
Path(const std::string & absolutePath,ArenaAllocator * allocator)121 Path::Path(const std::string &absolutePath, ArenaAllocator *allocator)
122 {
123 Initializer(absolutePath, allocator);
124 }
125
Path(const std::string & relativePath,const std::string & basePath,ArenaAllocator * allocator)126 Path::Path(const std::string &relativePath, const std::string &basePath, ArenaAllocator *allocator)
127 {
128 Initializer(relativePath, allocator);
129 InitializeBasePath(basePath);
130 }
131
IsRelative()132 bool Path::IsRelative()
133 {
134 return isRelative_;
135 }
136
IsAbsolute()137 bool Path::IsAbsolute()
138 {
139 return !isRelative_;
140 }
141
GetPath() const142 const util::StringView &Path::GetPath() const
143 {
144 return path_;
145 }
146
GetAbsolutePath() const147 const util::StringView &Path::GetAbsolutePath() const
148 {
149 return absolutePath_;
150 }
151
GetExtension() const152 const util::StringView &Path::GetExtension() const
153 {
154 return fileExtension_;
155 }
156
GetFileName() const157 const util::StringView &Path::GetFileName() const
158 {
159 return fileName_;
160 }
161
GetParentFolder() const162 const util::StringView &Path::GetParentFolder() const
163 {
164 return parentFolder_;
165 }
166
GetAbsoluteParentFolder() const167 const util::StringView &Path::GetAbsoluteParentFolder() const
168 {
169 return absoluteParentFolder_;
170 }
171
172 } // namespace panda::es2panda::util
173