• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 GetIpv6Prefix(const std::string &ipv6Addr, uint8_t prefixLen);
47 std::string ConvertIpv4Address(uint32_t addressIpv4);
48 uint32_t ConvertIpv4Address(const std::string &address);
49 int32_t Ipv4PrefixLen(const std::string &ip);
50 int32_t Ipv6PrefixLen(const std::string &ip);
51 bool ParseInt(const std::string &str, int32_t *value);
52 int64_t ConvertToInt64(const std::string &str);
53 std::string ToAnonymousIp(const std::string &input);
54 int32_t StrToInt(const std::string &value, int32_t defaultErr = -1);
55 uint32_t StrToUint(const std::string &value, uint32_t defaultErr = 0);
56 bool StrToBool(const std::string &value, bool defaultErr = false);
57 int64_t StrToLong(const std::string &value, int64_t defaultErr = -1);
58 uint64_t StrToUint64(const std::string &value, uint64_t defaultErr = 0);
59 bool CheckIfaceName(const std::string &name);
60 int32_t ForkExec(const std::string &command, std::string *out = nullptr);
61 bool IsValidDomain(const std::string &domain);
62 
GetCurrentSecond()63 inline uint64_t GetCurrentSecond()
64 {
65     return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
66         .count();
67 }
68 } // namespace OHOS::NetManagerStandard::CommonUtils
69 
70 #endif /* COMMUNICATIONNETMANAGER_BASE_COMMON_UTILS_H */
71