1 /*
2 * Copyright (c) 2021 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 <climits>
17 #include <filesystem>
18 #include <fstream>
19 #include <stdexcept>
20 #include <string>
21 #include <vector>
22 #include "hilog/log.h"
23 #include "parameter.h"
24 #include "utils.h"
25
26 namespace OHOS {
27 namespace Global {
28 namespace I18n {
29 using namespace std;
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "Utils" };
31 using namespace OHOS::HiviewDFX;
Split(const string & src,const string & sep,vector<string> & dest)32 void Split(const string &src, const string &sep, vector<string> &dest)
33 {
34 if (src == "") {
35 return;
36 }
37 string::size_type begin = 0;
38 string::size_type end = src.find(sep);
39 while (end != string::npos) {
40 dest.push_back(src.substr(begin, end - begin));
41 begin = end + sep.size();
42 end = src.find(sep, begin);
43 }
44 if (begin != src.size()) {
45 dest.push_back(src.substr(begin));
46 }
47 }
48
ReadSystemParameter(const char * paramKey,const int paramLength)49 std::string ReadSystemParameter(const char *paramKey, const int paramLength)
50 {
51 char param[paramLength];
52 int status = GetParameter(paramKey, "", param, paramLength);
53 if (status > 0) {
54 return param;
55 }
56 return "";
57 }
58
ConvertString2Int(const string & numberStr,int32_t & status)59 int32_t ConvertString2Int(const string &numberStr, int32_t& status)
60 {
61 try {
62 return std::stoi(numberStr);
63 } catch(const std::invalid_argument& except) {
64 status = -1;
65 HiLog::Error(LABEL, "ConvertString2Int: invalid argument");
66 return -1;
67 } catch (const std::out_of_range& except) {
68 status = -1;
69 HiLog::Error(LABEL, "ConvertString2Int: invalid argument out of range");
70 return -1;
71 } catch (...) {
72 status = -1;
73 return -1;
74 }
75 }
76
IsValidLocaleTag(icu::Locale & locale)77 bool IsValidLocaleTag(icu::Locale &locale)
78 {
79 static std::unordered_set<std::string> allValidLocalesLanguageTag;
80 GetAllValidLocalesTag(allValidLocalesLanguageTag);
81 std::string languageTag = locale.getLanguage();
82 if (allValidLocalesLanguageTag.find(languageTag) == allValidLocalesLanguageTag.end()) {
83 HiLog::Error(LABEL, "GetTimePeriodName does not support this languageTag: %{public}s", languageTag.c_str());
84 return false;
85 }
86 return true;
87 }
88
GetAllValidLocalesTag(std::unordered_set<std::string> & allValidLocalesLanguageTag)89 void GetAllValidLocalesTag(std::unordered_set<std::string>& allValidLocalesLanguageTag)
90 {
91 static bool init = false;
92 if (init) {
93 return;
94 }
95 int32_t validCount = 1;
96 const icu::Locale *validLocales = icu::Locale::getAvailableLocales(validCount);
97 for (int i = 0; i < validCount; i++) {
98 allValidLocalesLanguageTag.insert(validLocales[i].getLanguage());
99 }
100 init = true;
101 }
102
CheckTzDataFilePath(const std::string & filePath)103 bool CheckTzDataFilePath(const std::string &filePath)
104 {
105 char *realpathRes = nullptr;
106 realpathRes = realpath(filePath.c_str(), nullptr);
107 if (realpathRes == nullptr) {
108 return false;
109 }
110 std::ifstream file(filePath.c_str());
111 if (!file.good()) {
112 file.close();
113 free(realpathRes);
114 return false;
115 }
116 file.close();
117 free(realpathRes);
118 realpathRes = nullptr;
119 return true;
120 }
121 } // namespace I18n
122 } // namespace Global
123 } // namespace OHOS