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 "common/log_wrapper.h"
17 #include "tooling/client/utils/utils.h"
18 #include <ctime>
19 #include <climits>
20 #include <cerrno>
21 #include <string>
22 #include "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
SplitString(const std::string & str,const std::string & delimiter)71 std::vector<std::string> Utils::SplitString(const std::string &str, const std::string &delimiter)
72 {
73 std::size_t strIndex = 0;
74 std::vector<std::string> value;
75 std::size_t pos = str.find_first_of(delimiter, strIndex);
76 while ((pos < str.size()) && (pos > strIndex)) {
77 std::string subStr = str.substr(strIndex, pos - strIndex);
78 value.push_back(std::move(subStr));
79 strIndex = pos;
80 strIndex = str.find_first_not_of(delimiter, strIndex);
81 pos = str.find_first_of(delimiter, strIndex);
82 }
83 if (pos > strIndex) {
84 std::string subStr = str.substr(strIndex, pos - strIndex);
85 if (!subStr.empty()) {
86 value.push_back(std::move(subStr));
87 }
88 }
89 return value;
90 }
91
RealPath(const std::string & path,std::string & realPath,bool readOnly)92 bool Utils::RealPath(const std::string &path, std::string &realPath, [[maybe_unused]] bool readOnly)
93 {
94 realPath = "";
95 if (path.empty() || path.size() > PATH_MAX) {
96 LOGE("arkdb: File path is illeage");
97 return false;
98 }
99 char buffer[PATH_MAX] = { '\0' };
100 #if defined(PANDA_TARGET_WINDOWS)
101 if (!_fullpath(buffer, path.c_str(), sizeof(buffer) - 1)) {
102 LOGE("arkdb: full path failure");
103 return false;
104 }
105 #endif
106 #if defined(PANDA_TARGET_UNIX)
107 if (!realpath(path.c_str(), buffer)) {
108 // Maybe file does not exist.
109 if (!readOnly && errno == ENOENT) {
110 realPath = path;
111 return true;
112 }
113 LOGE("arkdb: realpath failure");
114 return false;
115 }
116 #endif
117 realPath = std::string(buffer);
118 return true;
119 }
120
IsNumber(const std::string & str)121 bool Utils::IsNumber(const std::string &str)
122 {
123 std::string Str = str;
124 Str.erase(0, Str.find_first_not_of("0"));
125 if (Str.size() > 9) { //9: size of int
126 return false;
127 }
128
129 for (char c : Str) {
130 if (!std::isdigit(c)) {
131 return false;
132 }
133 }
134 return true;
135 }
136
137 } // OHOS::ArkCompiler::Toolchain
138