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