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 std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup", std::ios::in);
67 ZLOGD("pid %d open %d", pid, ifs.good());
68 if (!ifs.good()) {
69 return false;
70 }
71
72 while (!ifs.eof()) {
73 const int MAX_LEN = 256; // enough
74 char buffer[MAX_LEN] = { 0 };
75 ifs.getline(buffer, sizeof(buffer));
76 std::string line = buffer;
77
78 size_t pos = line.find("background");
79 if (pos != std::string::npos) {
80 return true;
81 }
82 }
83 return false;
84 }
85
DCopy(uint8_t * tag,size_t tagLen,const uint8_t * src,size_t srcLen)86 bool Constant::DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen)
87 {
88 if (tagLen != srcLen || tag == nullptr || src == nullptr) {
89 return false;
90 }
91 auto ret = memcpy_s(tag, tagLen, src, srcLen);
92 return ret == EOK;
93 }
94 } // namespace DistributedData
95 } // namespace OHOS
96