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 
16 #ifndef KV_DATASERVICE_CONSTANT_H
17 #define KV_DATASERVICE_CONSTANT_H
18 
19 #include <algorithm>
20 #include <cctype>
21 #include <locale>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <type_traits>
26 #include <vector>
27 #include "visibility.h"
28 
29 namespace OHOS {
30 namespace DistributedData {
31 class Constant {
32 public:
33     // concatenate strings and return a composition string.
34     API_EXPORT static std::string Concatenate(std::initializer_list<std::string> stringList);
35 
36     API_EXPORT static std::string Join(
37         const std::string &prefix, const std::string &separator, std::initializer_list<std::string> params);
38 
39     API_EXPORT static bool IsBackground(pid_t pid);
40 
41     API_EXPORT static bool Equal(bool first, bool second);
42 
43     API_EXPORT static bool NotEqual(bool first, bool second);
44 
45     API_EXPORT static std::vector<std::string> Split(const std::string &str, const std::string &delim);
46 
47     template<typename T>
48     inline static constexpr bool is_pod = (std::is_standard_layout_v<T> && std::is_trivial_v<T>);
49 
50     template<typename T, typename S>
Copy(T * tag,const S * src)51     API_EXPORT inline static std::enable_if_t<is_pod<T> && is_pod<S>, bool> Copy(T *tag, const S *src)
52     {
53         return DCopy(reinterpret_cast<uint8_t *>(tag), sizeof(T), reinterpret_cast<const uint8_t *>(src), sizeof(S));
54     };
55 
56     // delete left bland in s by reference.
57     template<typename T>
58     static void LeftTrim(T &s);
59 
60     // delete right bland in s by reference.
61     template<typename T>
62     static void RightTrim(T &s);
63 
64     // delete both left and right bland in s by reference.
65     template<typename T>
66     static void Trim(T &s);
67 
68     // delete both left and right bland in s by reference, not change raw string.
69     template<typename T>
70     static T TrimCopy(T s);
71 
72     API_EXPORT static constexpr const char *KEY_SEPARATOR = "###";
73 
74 private:
75     API_EXPORT static bool DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen);
76 };
77 
78 // trim from start (in place)
79 template<typename T>
LeftTrim(T & s)80 void Constant::LeftTrim(T &s)
81 {
82     s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
83 }
84 
85 // trim from end (in place)
86 template<typename T>
RightTrim(T & s)87 void Constant::RightTrim(T &s)
88 {
89     s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
90 }
91 
92 // trim from both ends (in place)
93 template<typename T>
Trim(T & s)94 void Constant::Trim(T &s)
95 {
96     LeftTrim(s);
97     RightTrim(s);
98 }
99 
100 // trim from both ends (copying)
101 template<typename T>
TrimCopy(T s)102 T Constant::TrimCopy(T s)
103 {
104     Trim(s);
105     return s;
106 }
107 }  // namespace DistributedKv
108 }  // namespace OHOS
109 #endif // KV_DATASERVICE_CONSTANT_H
110