• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <cerrno>
19 #include <dirent.h>
20 #include <fstream>
21 #include <unistd.h>
22 #include "log_print.h"
23 
24 namespace OHOS {
25 namespace DistributedData {
26 constexpr const char *Constant::KEY_SEPARATOR;
Concatenate(std::initializer_list<std::string> stringList)27 std::string Constant::Concatenate(std::initializer_list<std::string> stringList)
28 {
29     std::string result;
30     size_t result_size = 0;
31     for (const std::string &str : stringList) {
32         result_size += str.size();
33     }
34     result.reserve(result_size);
35     for (const std::string &str : stringList) {
36         result.append(str.data(), str.size());
37     }
38     return result;
39 }
40 
Join(const std::string & prefix,const std::string & separator,std::initializer_list<std::string> params)41 std::string Constant::Join(const std::string &prefix, const std::string &separator,
42     std::initializer_list<std::string> params)
43 {
44     std::string::size_type size = prefix.size();
45     for (const std::string &param : params) {
46         size += separator.size() + param.size();
47     }
48     std::string result;
49     result.reserve(size);
50     result.append(prefix);
51     for (const std::string &str : params) {
52         result.append(separator).append(str);
53     }
54     return result;
55 }
56 
Equal(bool first,bool second)57 bool Constant::Equal(bool first, bool second)
58 {
59     return (first && second) || (!first && !second);
60 }
61 
NotEqual(bool first,bool second)62 bool Constant::NotEqual(bool first, bool second)
63 {
64     return (first || second) && (!first || !second);
65 }
66 
IsBackground(pid_t pid)67 bool Constant::IsBackground(pid_t pid)
68 {
69     std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup", std::ios::in);
70     ZLOGD("pid %d open %d", pid, ifs.good());
71     if (!ifs.good()) {
72         return false;
73     }
74 
75     while (!ifs.eof()) {
76         const int MAX_LEN = 256; // enough
77         char buffer[MAX_LEN] = { 0 };
78         ifs.getline(buffer, sizeof(buffer));
79         std::string line = buffer;
80 
81         size_t pos = line.find("background");
82         if (pos != std::string::npos) {
83             return true;
84         }
85     }
86     return false;
87 }
88 } // namespace DistributedData
89 } // namespace OHOS
90