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 COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H
17 #define COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H
18
19 #include <iosfwd>
20 #include <sstream>
21 #include <vector>
22
23 namespace OHOS::NetManagerStandard::CommonUtils {
Split(const std::string & str,const std::string & sep)24 inline std::vector<std::string> Split(const std::string &str, const std::string &sep)
25 {
26 std::string s = str;
27 std::vector<std::string> res;
28 while (!s.empty()) {
29 size_t pos = s.find(sep);
30 if (pos == std::string::npos) {
31 res.emplace_back(s);
32 break;
33 }
34 res.emplace_back(s.substr(0, pos));
35 s = s.substr(pos + sep.size());
36 }
37 return res;
38 }
39 std::string Strip(const std::string &str, char ch = ' ');
40 std::string ToLower(const std::string &s);
41 bool IsValidIPV4(const std::string &ip);
42 bool IsValidIPV6(const std::string &ip);
43 int8_t GetAddrFamily(const std::string &ip);
44 int GetMaskLength(const std::string &mask);
45 std::string GetMaskByLength(uint32_t length);
46 std::string ConvertIpv4Address(uint32_t addressIpv4);
47 uint32_t ConvertIpv4Address(const std::string &address);
48 int32_t Ipv4PrefixLen(const std::string &ip);
49 bool ParseInt(const std::string &str, int32_t *value);
50 int64_t ConvertToInt64(const std::string &str);
51 std::string ToAnonymousIp(const std::string &input);
52 int32_t StrToInt(const std::string &str);
53 uint32_t StrToUint(const std::string &str);
54 bool StrToBool(const std::string &str);
55 int64_t StrToLong(const std::string &str);
56 bool CheckIfaceName(const std::string &name);
57 int32_t ForkExec(const std::string &command, std::string *out = nullptr);
58 } // namespace OHOS::NetManagerStandard::CommonUtils
59
60 #endif /* COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H */
61