1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "Constant"
16 #include "utils/constant.h"
17 #include <fstream>
18 #include "log_print.h"
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace DistributedData {
23 constexpr const char *Constant::KEY_SEPARATOR;
Concatenate(std::initializer_list<std::string> stringList)24 std::string Constant::Concatenate(std::initializer_list<std::string> stringList)
25 {
26 std::string result;
27 size_t result_size = 0;
28 for (const std::string &str : stringList) {
29 result_size += str.size();
30 }
31 result.reserve(result_size);
32 for (const std::string &str : stringList) {
33 result.append(str.data(), str.size());
34 }
35 return result;
36 }
37
Join(const std::string & prefix,const std::string & separator,std::initializer_list<std::string> params)38 std::string Constant::Join(const std::string &prefix, const std::string &separator,
39 std::initializer_list<std::string> params)
40 {
41 std::string::size_type size = prefix.size();
42 for (const std::string ¶m : params) {
43 size += separator.size() + param.size();
44 }
45 std::string result;
46 result.reserve(size);
47 result.append(prefix);
48 for (const std::string &str : params) {
49 result.append(separator).append(str);
50 }
51 return result;
52 }
53
Split(const std::string & str,const std::string & delim)54 std::vector<std::string> Constant::Split(const std::string &str, const std::string &delim)
55 {
56 if (str.empty()) {
57 return { str };
58 }
59 std::vector<std::string> res;
60 size_t pos = 0;
61 while (pos < str.size()) {
62 size_t found = str.find(delim, pos);
63 if (found == std::string::npos) {
64 res.push_back(str.substr(pos));
65 break;
66 }
67 res.push_back(str.substr(pos, found - pos));
68 pos = found + delim.size();
69 }
70 return res;
71 }
72
Equal(bool first,bool second)73 bool Constant::Equal(bool first, bool second)
74 {
75 return (first && second) || (!first && !second);
76 }
77
NotEqual(bool first,bool second)78 bool Constant::NotEqual(bool first, bool second)
79 {
80 return (first || second) && (!first || !second);
81 }
82
IsBackground(pid_t pid)83 bool Constant::IsBackground(pid_t pid)
84 {
85 return false;
86 }
87
DCopy(uint8_t * tag,size_t tagLen,const uint8_t * src,size_t srcLen)88 bool Constant::DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen)
89 {
90 if (tagLen != srcLen || tag == nullptr || src == nullptr) {
91 return false;
92 }
93 auto ret = memcpy_s(tag, tagLen, src, srcLen);
94 return ret == EOK;
95 }
96 } // namespace DistributedData
97 } // namespace OHOS
98