• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstring>
18 #include <climits>
19 #include <fstream>
20 #include <unistd.h>
21 #include "file_utils.h"
22 #include "string_utils.h"
23 #include "mpl_logging.h"
24 
25 #ifdef _WIN32
26 #include <shlwapi.h>
27 #endif
28 
29 namespace {
30 const char kFileSeperatorLinuxStyleChar = '/';
31 const char kFileSeperatorWindowsStyleChar = '\\';
32 const std::string kFileSeperatorLinuxStyleStr = std::string(1, kFileSeperatorLinuxStyleChar);
33 const std::string kFileSeperatorWindowsStyleStr = std::string(1, kFileSeperatorWindowsStyleChar);
34 }  // namespace
35 
36 namespace maple {
37 #if __WIN32
38 const char kFileSeperatorChar = kFileSeperatorWindowsStyleChar;
39 #else
40 const char kFileSeperatorChar = kFileSeperatorLinuxStyleChar;
41 #endif
42 
43 #if __WIN32
44 const std::string kFileSeperatorStr = kFileSeperatorWindowsStyleStr;
45 #else
46 const std::string kFileSeperatorStr = kFileSeperatorLinuxStyleStr;
47 #endif
48 
SafeGetenv(const char * envVar)49 std::string FileUtils::SafeGetenv(const char *envVar)
50 {
51     const char *tmpEnvPtr = std::getenv(envVar);
52     CHECK_FATAL((tmpEnvPtr != nullptr), "Failed! Unable to find environment variable %s \n", envVar);
53 
54     std::string tmpStr(tmpEnvPtr);
55     return tmpStr;
56 }
57 
GetRealPath(const std::string & filePath)58 std::string FileUtils::GetRealPath(const std::string &filePath)
59 {
60 #ifdef _WIN32
61     char *path = nullptr;
62     if (filePath.size() > PATH_MAX || !PathCanonicalize(path, filePath.c_str())) {
63         CHECK_FATAL(false, "invalid file path");
64     }
65 #else
66     char path[PATH_MAX] = {0};
67     if (filePath.size() > PATH_MAX || realpath(filePath.c_str(), path) == nullptr) {
68         CHECK_FATAL(false, "invalid file path");
69     }
70 #endif
71     std::string result(path, path + strlen(path));
72     return result;
73 }
74 
GetFileName(const std::string & filePath,bool isWithExtension)75 std::string FileUtils::GetFileName(const std::string &filePath, bool isWithExtension)
76 {
77     std::string fullFileName = StringUtils::GetStrAfterLast(filePath, kFileSeperatorStr);
78 #ifdef _WIN32
79     fullFileName = StringUtils::GetStrAfterLast(fullFileName, kFileSeperatorLinuxStyleStr);
80 #endif
81     if (isWithExtension) {
82         return fullFileName;
83     }
84     return StringUtils::GetStrBeforeLast(fullFileName, ".");
85 }
86 
GetFileExtension(const std::string & filePath)87 std::string FileUtils::GetFileExtension(const std::string &filePath)
88 {
89     return StringUtils::GetStrAfterLast(filePath, ".", true);
90 }
91 
GetFileFolder(const std::string & filePath)92 std::string FileUtils::GetFileFolder(const std::string &filePath)
93 {
94     std::string folder = StringUtils::GetStrBeforeLast(filePath, kFileSeperatorStr, true);
95     std::string curSlashType = kFileSeperatorStr;
96 #ifdef _WIN32
97     if (folder.empty()) {
98         curSlashType = kFileSeperatorLinuxStyleStr;
99         folder = StringUtils::GetStrBeforeLast(filePath, curSlashType, true);
100     }
101 #endif
102     return folder.empty() ? ("." + curSlashType) : (folder + curSlashType);
103 }
104 
Remove(const std::string & filePath)105 int FileUtils::Remove(const std::string &filePath)
106 {
107     return remove(filePath.c_str());
108 }
109 
IsFileExists(const std::string & filePath)110 bool FileUtils::IsFileExists(const std::string &filePath)
111 {
112     std::ifstream f(filePath);
113     return f.good();
114 }
115 
AppendMapleRootIfNeeded(bool needRootPath,const std::string & path,const std::string & defaultRoot)116 std::string FileUtils::AppendMapleRootIfNeeded(bool needRootPath, const std::string &path,
117                                                const std::string &defaultRoot)
118 {
119     if (!needRootPath) {
120         return path;
121     }
122     std::ostringstream ostrStream;
123     if (getenv(kMapleRoot) == nullptr) {
124         ostrStream << defaultRoot << path;
125     } else {
126         ostrStream << getenv(kMapleRoot) << kFileSeperatorStr << path;
127     }
128     return ostrStream.str();
129 }
130 }  // namespace maple
131