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 template<typename T>
46 inline static constexpr bool is_pod = (std::is_standard_layout_v<T> && std::is_trivial_v<T>);
47
48 template<typename T, typename S>
Copy(T * tag,const S * src)49 API_EXPORT inline static std::enable_if_t<is_pod<T> && is_pod<S>, bool> Copy(T *tag, const S *src)
50 {
51 return DCopy(reinterpret_cast<uint8_t *>(tag), sizeof(T), reinterpret_cast<const uint8_t *>(src), sizeof(S));
52 };
53
54 // delete left bland in s by reference.
55 template<typename T>
56 static void LeftTrim(T &s);
57
58 // delete right bland in s by reference.
59 template<typename T>
60 static void RightTrim(T &s);
61
62 // delete both left and right bland in s by reference.
63 template<typename T>
64 static void Trim(T &s);
65
66 // delete both left and right bland in s by reference, not change raw string.
67 template<typename T>
68 static T TrimCopy(T s);
69
70 API_EXPORT static constexpr const char *KEY_SEPARATOR = "###";
71
72 private:
73 API_EXPORT static bool DCopy(uint8_t *tag, size_t tagLen, const uint8_t *src, size_t srcLen);
74 };
75
76 // trim from start (in place)
77 template<typename T>
LeftTrim(T & s)78 void Constant::LeftTrim(T &s)
79 {
80 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
81 }
82
83 // trim from end (in place)
84 template<typename T>
RightTrim(T & s)85 void Constant::RightTrim(T &s)
86 {
87 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
88 }
89
90 // trim from both ends (in place)
91 template<typename T>
Trim(T & s)92 void Constant::Trim(T &s)
93 {
94 LeftTrim(s);
95 RightTrim(s);
96 }
97
98 // trim from both ends (copying)
99 template<typename T>
TrimCopy(T s)100 T Constant::TrimCopy(T s)
101 {
102 Trim(s);
103 return s;
104 }
105 } // namespace DistributedKv
106 } // namespace OHOS
107 #endif // KV_DATASERVICE_CONSTANT_H
108