1 /*
2  * Copyright (C) 2024 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 #ifndef OHOS_DHCP_RESULT_STORE_MANAGER_H
16 #define OHOS_DHCP_RESULT_STORE_MANAGER_H
17 
18 #include <fstream>
19 #include <sstream>
20 #include <string>
21 #include <unistd.h>
22 #include "dhcp_client_def.h"
23 
24 namespace OHOS {
25 namespace DHCP {
26 class DhcpResultStoreManager {
27 public:
28     DhcpResultStoreManager();
29     ~DhcpResultStoreManager();
30     static DhcpResultStoreManager &GetInstance();
31     int32_t SaveIpInfoInLocalFile(const IpInfoCached ipInfo);
32     int32_t GetCachedIp(const std::string targetBssid, IpInfoCached &outIpResult);
33 
34 private:
35     void SetConfigFilePath(const std::string &fileName);
36     int32_t LoadAllIpCached(const std::string &fileName);
37     int32_t ReadNetworkSection(IpInfoCached &item, std::ifstream &fs, std::string &line);
38     int32_t ReadNetwork(IpInfoCached &item, std::ifstream &fs, std::string &line);
39     int32_t SaveConfig();
40     int32_t SetClassKeyValue(IpInfoCached &item, const std::string &key, const std::string &value);
41     std::string GetClassName();
42     std::string OutClassString(IpInfoCached &item);
43     void ClearClass(IpInfoCached &item);
44 
45 private:
46     std::mutex m_ipResultMutex;
47     std::vector<IpInfoCached> m_allIpCached;
48     std::string m_fileName;
49 };
50 
TrimString(std::string & str)51 static inline void TrimString(std::string &str)
52 {
53     int32_t i = 0;
54     int32_t j = static_cast<int32_t>(str.length()) - 1;
55     while (i < static_cast<int32_t>(str.length()) && str[i] == ' ') {
56         ++i;
57     }
58     while (j >= 0 && str[j] == ' ') {
59         --j;
60     }
61     str = ((i > j) ? "" : str.substr(i, j - i + 1));
62 }
63 }  // namespace Wifi
64 }  // namespace OHOS
65 #endif
66