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