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
Equal(bool first,bool second)54 bool Constant::Equal(bool first, bool second)
55 {
56 return (first && second) || (!first && !second);
57 }
58
NotEqual(bool first,bool second)59 bool Constant::NotEqual(bool first, bool second)
60 {
61 return (first || second) && (!first || !second);
62 }
63
IsBackground(pid_t pid)64 bool Constant::IsBackground(pid_t pid)
65 {
66 return false;
67 }
68
DCopy(uint8_t * tag,size_t tagLen,const uint8_t * src,size_t srcLen)69 bool Constant::DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen)
70 {
71 if (tagLen != srcLen || tag == nullptr || src == nullptr) {
72 return false;
73 }
74 auto ret = memcpy_s(tag, tagLen, src, srcLen);
75 return ret == EOK;
76 }
77 } // namespace DistributedData
78 } // namespace OHOS
79