1 /*
2 * Copyright (C) 2021 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 #include "adapter_config.h"
17
18 #include <fstream>
19
20 #include "xml_parse.h"
21
22 namespace OHOS {
23 namespace bluetooth {
24 struct AdapterConfig::impl {
25 utility::XmlParse parse_ = {};
26 std::string fileName_ = {"bt_config.xml"};
27 std::string filePath_ = {BT_CONFIG_PATH + fileName_};
28 std::string fileBasePath_ = {BT_CONFIG_PATH_BASE + fileName_};
29 };
30
GetInstance()31 IAdapterConfig *AdapterConfig::GetInstance()
32 {
33 static AdapterConfig instance;
34 return reinterpret_cast<IAdapterConfig *>(&instance);
35 }
36
AdapterConfig()37 AdapterConfig::AdapterConfig() : pimpl(std::make_unique<impl>())
38 {}
39
~AdapterConfig()40 AdapterConfig::~AdapterConfig()
41 {}
42
Load()43 bool AdapterConfig::Load()
44 {
45 if (pimpl->parse_.Load(pimpl->filePath_)) {
46 return true;
47 } else {
48 if (!Reload()) {
49 return false;
50 }
51 return pimpl->parse_.Load(pimpl->filePath_);
52 }
53 }
54
Reload()55 bool AdapterConfig::Reload()
56 {
57 std::ifstream fin(pimpl->fileBasePath_, std::ios::in | std::ios::binary);
58 if (!fin) {
59 return false;
60 }
61 std::ofstream fout(pimpl->filePath_, std::ios::out | std::ios::trunc);
62 if (!fout) {
63 return false;
64 }
65 fout << fin.rdbuf();
66 return true;
67 }
68
GetValue(const std::string & section,const std::string & property,int & value)69 bool AdapterConfig::GetValue(const std::string §ion, const std::string &property, int &value)
70 {
71 return pimpl->parse_.GetValue(section, property, value);
72 }
GetValue(const std::string & section,const std::string & property,std::string & value)73 bool AdapterConfig::GetValue(const std::string §ion, const std::string &property, std::string &value)
74 {
75 return pimpl->parse_.GetValue(section, property, value);
76 }
GetValue(const std::string & section,const std::string & property,bool & value)77 bool AdapterConfig::GetValue(const std::string §ion, const std::string &property, bool &value)
78 {
79 return pimpl->parse_.GetValue(section, property, value);
80 }
HasSection(const std::string & section)81 bool AdapterConfig::HasSection(const std::string §ion)
82 {
83 return pimpl->parse_.HasSection(section);
84 }
85 } // namespace bluetooth
86 } // namespace OHOS