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