1 /*
2 * Copyright (c) 2022-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
16 #include "parameters.h"
17
18 #include <map>
19 #include <mutex>
20
21 namespace OHOS {
22 namespace system {
23 std::map<std::string, std::string> paramMap;
24 std::mutex mutex;
25
GetDeviceType()26 std::string GetDeviceType()
27 {
28 return "default";
29 }
30
GetBoolParameter(const std::string & key,bool def)31 bool GetBoolParameter(const std::string& key, bool def)
32 {
33 #ifndef GET_BOOL_PARAMETER_TRUE
34 auto item = paramMap.find(key);
35 if (item == paramMap.end()) {
36 return def;
37 }
38 return "true" == item->second;
39 #else
40 auto item = paramMap.find(key);
41 if (item == paramMap.end()) {
42 return true;
43 }
44 return "true" == item->second;
45 #endif
46 }
47
48 template<typename T>
GetIntParameter(const std::string & key,T def)49 T GetIntParameter(const std::string& key, T def)
50 {
51 std::lock_guard<std::mutex> lock(mutex);
52 try {
53 auto item = paramMap.find(key);
54 if (item == paramMap.end()) {
55 return def;
56 }
57 return atoi(item->second.c_str());
58 } catch (...) {
59 return def;
60 }
61 }
62
GetParameter(const std::string & key,const std::string & def)63 std::string GetParameter(const std::string& key, const std::string& def)
64 {
65 std::lock_guard<std::mutex> lock(mutex);
66 auto item = paramMap.find(key);
67 if (item == paramMap.end()) {
68 return def;
69 }
70 return item->second;
71 }
72
SetParameter(const std::string & key,const std::string & val)73 bool SetParameter(const std::string& key, const std::string& val)
74 {
75 std::lock_guard<std::mutex> lock(mutex);
76 paramMap[key] = val;
77 return true;
78 }
79
80 template int32_t GetIntParameter(const std::string& key, int32_t def);
81 } // system
82 } // OHOS