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