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 <ctime>
17 #include <climits>
18 #include <cerrno>
19 #include <string>
20
21 #include "common/log_wrapper.h"
22 #include "tooling/utils/utils.h"
23
24 namespace OHOS::ArkCompiler::Toolchain {
GetCurrentTime(char * date,char * tim,size_t size)25 bool Utils::GetCurrentTime(char *date, char *tim, size_t size)
26 {
27 time_t timep = time(nullptr);
28 if (timep == -1) {
29 LOGE("get timep failed");
30 return false;
31 }
32
33 tm* currentDate = localtime(&timep);
34 if (currentDate == nullptr) {
35 LOGE("get currentDate failed");
36 return false;
37 }
38
39 size_t timeResult = 0;
40 timeResult = strftime(date, size, "%Y%m%d", currentDate);
41 if (timeResult == 0) {
42 LOGE("format date failed");
43 return false;
44 }
45
46 tm* currentTime = localtime(&timep);
47 if (currentTime == nullptr) {
48 LOGE("get currentTime failed");
49 return false;
50 }
51
52 timeResult = strftime(tim, size, "%H%M%S", currentTime);
53 if (timeResult == 0) {
54 LOGE("format time failed");
55 return false;
56 }
57 return true;
58 }
59
StrToUInt(const char * content,uint32_t * result)60 bool Utils::StrToUInt(const char *content, uint32_t *result)
61 {
62 const int dec = 10;
63 char *endPtr = nullptr;
64 *result = std::strtoul(content, &endPtr, dec);
65 if (endPtr == content || *endPtr != '\0') {
66 return false;
67 }
68 return true;
69 }
70
StrToInt32(const std::string & str,int32_t & result)71 bool Utils::StrToInt32(const std::string &str, int32_t &result)
72 {
73 const int dec = 10;
74 char *endPtr = nullptr;
75 long long num = std::strtoll(str.c_str(), &endPtr, dec);
76 if (endPtr == str.c_str() || *endPtr != '\0' || num > INT_MAX || num < INT_MIN) {
77 return false;
78 }
79 result = static_cast<int32_t>(num);
80 return true;
81 }
82
StrToInt32(const std::string & str,std::optional<int32_t> & result)83 bool Utils::StrToInt32(const std::string &str, std::optional<int32_t> &result)
84 {
85 const int dec = 10;
86 char *endPtr = nullptr;
87 long long num = std::strtoll(str.c_str(), &endPtr, dec);
88 if (endPtr == str.c_str() || *endPtr != '\0' || num > INT_MAX || num < INT_MIN) {
89 return false;
90 }
91 result = static_cast<int32_t>(num);
92 return true;
93 }
94
SplitString(const std::string & str,const std::string & delimiter)95 std::vector<std::string> Utils::SplitString(const std::string &str, const std::string &delimiter)
96 {
97 std::size_t strIndex = 0;
98 std::vector<std::string> value;
99 std::size_t pos = str.find_first_of(delimiter, strIndex);
100 while ((pos < str.size()) && (pos > strIndex)) {
101 std::string subStr = str.substr(strIndex, pos - strIndex);
102 value.push_back(std::move(subStr));
103 strIndex = pos;
104 strIndex = str.find_first_not_of(delimiter, strIndex);
105 pos = str.find_first_of(delimiter, strIndex);
106 }
107 if (pos > strIndex) {
108 std::string subStr = str.substr(strIndex, pos - strIndex);
109 if (!subStr.empty()) {
110 value.push_back(std::move(subStr));
111 }
112 }
113 return value;
114 }
115
RealPath(const std::string & path,std::string & realPath,bool readOnly)116 bool Utils::RealPath(const std::string &path, std::string &realPath, [[maybe_unused]] bool readOnly)
117 {
118 realPath = "";
119 if (path.empty() || path.size() > PATH_MAX) {
120 LOGE("arkdb: File path is illeage");
121 return false;
122 }
123 char buffer[PATH_MAX] = { '\0' };
124 #if defined(PANDA_TARGET_WINDOWS)
125 if (!_fullpath(buffer, path.c_str(), sizeof(buffer) - 1)) {
126 LOGE("arkdb: full path failure");
127 return false;
128 }
129 #endif
130 #if defined(PANDA_TARGET_UNIX)
131 if (!realpath(path.c_str(), buffer)) {
132 // Maybe file does not exist.
133 if (!readOnly && errno == ENOENT) {
134 realPath = path;
135 return true;
136 }
137 LOGE("arkdb: realpath failure");
138 return false;
139 }
140 #endif
141 realPath = std::string(buffer);
142 return true;
143 }
144
IsNumber(const std::string & str)145 bool Utils::IsNumber(const std::string &str)
146 {
147 std::string tmpStr = str;
148 tmpStr.erase(0, tmpStr.find_first_not_of("0"));
149 if (tmpStr.size() > 9) { //9: size of int
150 return false;
151 }
152
153 for (char c : tmpStr) {
154 if (!std::isdigit(c)) {
155 return false;
156 }
157 }
158 return true;
159 }
160
161 } // OHOS::ArkCompiler::Toolchain
162