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 <stdexcept>
17 #include <string>
18 #include <vector>
19 #include "hilog/log.h"
20 #include "parameter.h"
21 #include "utils.h"
22
23 namespace OHOS {
24 namespace Global {
25 namespace I18n {
26 using namespace std;
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "Utils" };
28 using namespace OHOS::HiviewDFX;
Split(const string & src,const string & sep,vector<string> & dest)29 void Split(const string &src, const string &sep, vector<string> &dest)
30 {
31 if (src == "") {
32 return;
33 }
34 string::size_type begin = 0;
35 string::size_type end = src.find(sep);
36 while (end != string::npos) {
37 dest.push_back(src.substr(begin, end - begin));
38 begin = end + sep.size();
39 end = src.find(sep, begin);
40 }
41 if (begin != src.size()) {
42 dest.push_back(src.substr(begin));
43 }
44 }
45
ReadSystemParameter(const char * paramKey,const int paramLength)46 std::string ReadSystemParameter(const char *paramKey, const int paramLength)
47 {
48 char param[paramLength];
49 int status = GetParameter(paramKey, "", param, paramLength);
50 if (status > 0) {
51 return param;
52 }
53 return "";
54 }
55
ConvertString2Int(const string & numberStr,int32_t & status)56 int32_t ConvertString2Int(const string &numberStr, int32_t& status)
57 {
58 try {
59 return std::stoi(numberStr);
60 } catch(const std::invalid_argument& except) {
61 status = -1;
62 HiLog::Error(LABEL, "ConvertString2Int: invalid argument");
63 return -1;
64 } catch (const std::out_of_range& except) {
65 status = -1;
66 HiLog::Error(LABEL, "ConvertString2Int: invalid argument out of range");
67 return -1;
68 }
69 }
70 } // namespace I18n
71 } // namespace Global
72 } // namespace OHOS