13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include <cstdio> 173af6ab5fSopenharmony_ci#include <string> 183af6ab5fSopenharmony_ci#include "os/filesystem.h" 193af6ab5fSopenharmony_ci#include "path.h" 203af6ab5fSopenharmony_ci 213af6ab5fSopenharmony_cinamespace ark::es2panda::util { 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_ciPath::Path() = default; 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ciPath::Path(const util::StringView &absolutePath, ArenaAllocator *allocator) 263af6ab5fSopenharmony_ci{ 273af6ab5fSopenharmony_ci Initializer(absolutePath.Mutf8(), allocator); 283af6ab5fSopenharmony_ci} 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_civoid Path::Initializer(const std::string &path, ArenaAllocator *allocator) 313af6ab5fSopenharmony_ci{ 323af6ab5fSopenharmony_ci isRelative_ = false; 333af6ab5fSopenharmony_ci allocator_ = allocator; 343af6ab5fSopenharmony_ci path_ = util::UString(path, allocator).View(); 353af6ab5fSopenharmony_ci if (*(path_.Bytes()) == '.') { 363af6ab5fSopenharmony_ci isRelative_ = true; 373af6ab5fSopenharmony_ci } 383af6ab5fSopenharmony_ci 393af6ab5fSopenharmony_ci absolutePath_ = util::UString(os::GetAbsolutePath(path_.Utf8()), allocator_).View(); 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_ci InitializeFileExtension(); 423af6ab5fSopenharmony_ci InitializeFileName(); 433af6ab5fSopenharmony_ci InitializeParentFolder(); 443af6ab5fSopenharmony_ci InitializeAbsoluteParentFolder(); 453af6ab5fSopenharmony_ci} 463af6ab5fSopenharmony_ci 473af6ab5fSopenharmony_civoid Path::InitializeFileName() 483af6ab5fSopenharmony_ci{ 493af6ab5fSopenharmony_ci if (path_.Empty()) { 503af6ab5fSopenharmony_ci return; 513af6ab5fSopenharmony_ci } 523af6ab5fSopenharmony_ci 533af6ab5fSopenharmony_ci int position = path_.Mutf8().find_last_of(PATH_DELIMITER); 543af6ab5fSopenharmony_ci 553af6ab5fSopenharmony_ci util::StringView fileName = path_.Substr(position + 1, path_.Length()); 563af6ab5fSopenharmony_ci if (GetExtension().Empty()) { 573af6ab5fSopenharmony_ci fileName_ = fileName; 583af6ab5fSopenharmony_ci } else { 593af6ab5fSopenharmony_ci int extensionPosition = fileName.Mutf8().find_last_of('.'); 603af6ab5fSopenharmony_ci fileName_ = fileName.Substr(0, extensionPosition); 613af6ab5fSopenharmony_ci } 623af6ab5fSopenharmony_ci} 633af6ab5fSopenharmony_ci 643af6ab5fSopenharmony_civoid Path::InitializeFileExtension() 653af6ab5fSopenharmony_ci{ 663af6ab5fSopenharmony_ci if (path_.Empty()) { 673af6ab5fSopenharmony_ci return; 683af6ab5fSopenharmony_ci } 693af6ab5fSopenharmony_ci 703af6ab5fSopenharmony_ci size_t position = path_.Mutf8().find_last_of('.'); 713af6ab5fSopenharmony_ci if (position != std::string::npos && position + 1 <= path_.Length()) { 723af6ab5fSopenharmony_ci fileExtension_ = path_.Substr(position + 1, path_.Length()); 733af6ab5fSopenharmony_ci } 743af6ab5fSopenharmony_ci} 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_civoid Path::InitializeAbsoluteParentFolder() 773af6ab5fSopenharmony_ci{ 783af6ab5fSopenharmony_ci if (path_.Empty()) { 793af6ab5fSopenharmony_ci return; 803af6ab5fSopenharmony_ci } 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci int position = absolutePath_.Mutf8().find_last_of(PATH_DELIMITER); 833af6ab5fSopenharmony_ci 843af6ab5fSopenharmony_ci if (!absolutePath_.Empty() && isRelative_) { 853af6ab5fSopenharmony_ci absoluteParentFolder_ = absolutePath_.Substr(0, position); 863af6ab5fSopenharmony_ci } 873af6ab5fSopenharmony_ci} 883af6ab5fSopenharmony_ci 893af6ab5fSopenharmony_civoid Path::InitializeParentFolder() 903af6ab5fSopenharmony_ci{ 913af6ab5fSopenharmony_ci if (path_.Empty()) { 923af6ab5fSopenharmony_ci return; 933af6ab5fSopenharmony_ci } 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_ci int position = path_.Mutf8().find_last_of(PATH_DELIMITER); 963af6ab5fSopenharmony_ci 973af6ab5fSopenharmony_ci parentFolder_ = path_.Substr(0, position); 983af6ab5fSopenharmony_ci} 993af6ab5fSopenharmony_ci 1003af6ab5fSopenharmony_civoid Path::InitializeBasePath(std::string basePath) 1013af6ab5fSopenharmony_ci{ 1023af6ab5fSopenharmony_ci if (!basePath.empty() && basePath.back() == PATH_DELIMITER) { 1033af6ab5fSopenharmony_ci basePath_ = util::UString(basePath.substr(0, basePath.length() - 1), allocator_).View(); 1043af6ab5fSopenharmony_ci } else { 1053af6ab5fSopenharmony_ci basePath_ = util::UString(basePath, allocator_).View(); 1063af6ab5fSopenharmony_ci } 1073af6ab5fSopenharmony_ci 1083af6ab5fSopenharmony_ci isRelative_ = true; 1093af6ab5fSopenharmony_ci} 1103af6ab5fSopenharmony_ci 1113af6ab5fSopenharmony_ciPath::Path(const util::StringView &relativePath, const util::StringView &basePath, ArenaAllocator *allocator) 1123af6ab5fSopenharmony_ci{ 1133af6ab5fSopenharmony_ci Initializer(relativePath.Mutf8(), allocator); 1143af6ab5fSopenharmony_ci InitializeBasePath(basePath.Mutf8()); 1153af6ab5fSopenharmony_ci} 1163af6ab5fSopenharmony_ci 1173af6ab5fSopenharmony_ciPath::Path(const std::string &absolutePath, ArenaAllocator *allocator) 1183af6ab5fSopenharmony_ci{ 1193af6ab5fSopenharmony_ci Initializer(absolutePath, allocator); 1203af6ab5fSopenharmony_ci} 1213af6ab5fSopenharmony_ci 1223af6ab5fSopenharmony_ciPath::Path(const std::string &relativePath, const std::string &basePath, ArenaAllocator *allocator) 1233af6ab5fSopenharmony_ci{ 1243af6ab5fSopenharmony_ci Initializer(relativePath, allocator); 1253af6ab5fSopenharmony_ci InitializeBasePath(basePath); 1263af6ab5fSopenharmony_ci} 1273af6ab5fSopenharmony_ci 1283af6ab5fSopenharmony_cibool Path::IsRelative() 1293af6ab5fSopenharmony_ci{ 1303af6ab5fSopenharmony_ci return isRelative_; 1313af6ab5fSopenharmony_ci} 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_cibool Path::IsAbsolute() 1343af6ab5fSopenharmony_ci{ 1353af6ab5fSopenharmony_ci return !isRelative_; 1363af6ab5fSopenharmony_ci} 1373af6ab5fSopenharmony_ci 1383af6ab5fSopenharmony_ciconst util::StringView &Path::GetPath() const 1393af6ab5fSopenharmony_ci{ 1403af6ab5fSopenharmony_ci return path_; 1413af6ab5fSopenharmony_ci} 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_ciconst util::StringView &Path::GetAbsolutePath() const 1443af6ab5fSopenharmony_ci{ 1453af6ab5fSopenharmony_ci return absolutePath_; 1463af6ab5fSopenharmony_ci} 1473af6ab5fSopenharmony_ci 1483af6ab5fSopenharmony_ciconst util::StringView &Path::GetExtension() const 1493af6ab5fSopenharmony_ci{ 1503af6ab5fSopenharmony_ci return fileExtension_; 1513af6ab5fSopenharmony_ci} 1523af6ab5fSopenharmony_ci 1533af6ab5fSopenharmony_ciconst util::StringView &Path::GetFileName() const 1543af6ab5fSopenharmony_ci{ 1553af6ab5fSopenharmony_ci return fileName_; 1563af6ab5fSopenharmony_ci} 1573af6ab5fSopenharmony_ci 1583af6ab5fSopenharmony_ciconst util::StringView &Path::GetParentFolder() const 1593af6ab5fSopenharmony_ci{ 1603af6ab5fSopenharmony_ci return parentFolder_; 1613af6ab5fSopenharmony_ci} 1623af6ab5fSopenharmony_ci 1633af6ab5fSopenharmony_ciconst util::StringView &Path::GetAbsoluteParentFolder() const 1643af6ab5fSopenharmony_ci{ 1653af6ab5fSopenharmony_ci return absoluteParentFolder_; 1663af6ab5fSopenharmony_ci} 1673af6ab5fSopenharmony_ci 1683af6ab5fSopenharmony_ci} // namespace ark::es2panda::util 169