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 #include "platform_linux.h" 17 18 #include <core/io/intf_file_manager.h> 19 #include <core/namespace.h> 20 #include <core/os/platform_create_info.h> 21 22 #include "io/path_tools.h" 23 #include "os/platform.h" 24 #include "util/string_util.h" 25 26 CORE_BEGIN_NAMESPACE() 27 using BASE_NS::string; 28 using BASE_NS::string_view; 29 PlatformLinux(PlatformCreateInfo const & createInfo)30PlatformLinux::PlatformLinux(PlatformCreateInfo const& createInfo) 31 { 32 // Convert the input paths to absolute. 33 auto cwd = GetCurrentDirectory(); 34 string_view curPath = cwd; 35 36 auto fixPath = [&](string_view pathRaw) -> string { 37 if (pathRaw.empty()) { 38 return {}; 39 } 40 // fix slashes. (just change \\ to /) 41 string_view pathIn = pathRaw; 42 string tmp; 43 if (pathIn.find("\\") != string_view::npos) { 44 tmp = pathIn; 45 StringUtil::FindAndReplaceAll(tmp, "\\", "/"); 46 pathIn = tmp; 47 } 48 49 string res = "/"; 50 string_view path = pathIn; 51 52 string normalizedPath; 53 if (path.empty()) { 54 normalizedPath = curPath; 55 } else { 56 if (path[0] != '/') { 57 // relative path. 58 normalizedPath = NormalizePath(curPath + path); 59 } else { 60 normalizedPath = NormalizePath(path); 61 } 62 } 63 if (normalizedPath.empty()) { 64 // Invalid path? how to handle this? 65 // just fallback on current path for now. (hoping that it's somewhat safe) 66 normalizedPath = curPath; 67 } 68 res += normalizedPath; 69 return string(res.substr(1)); 70 }; 71 72 plat_.coreRootPath = fixPath(createInfo.coreRootPath); 73 plat_.appRootPath = fixPath(createInfo.appRootPath); 74 plat_.appPluginPath = fixPath(createInfo.appPluginPath); 75 } 76 GetPlatformData() const77const PlatformData& PlatformLinux::GetPlatformData() const 78 { 79 return plat_; 80 } 81 RegisterPluginLocations(IPluginRegister & registry)82void PlatformLinux::RegisterPluginLocations(IPluginRegister& registry) 83 { 84 constexpr string_view fileproto("file://"); 85 registry.RegisterPluginPath(fileproto + GetPlatformData().coreRootPath + "plugins/"); 86 if (!GetPlatformData().appPluginPath.empty()) { 87 registry.RegisterPluginPath(fileproto + GetPlatformData().appPluginPath); 88 } 89 } 90 Create(PlatformCreateInfo const & createInfo)91CORE_NS::IPlatform::Ptr Platform::Create(PlatformCreateInfo const& createInfo) 92 { 93 return CORE_NS::IPlatform::Ptr(new PlatformLinux(createInfo)); 94 } 95 96 CORE_END_NAMESPACE() 97