blob: 37daf9a10092dda9b21ad09426c23e72a94a4661 [file] [log] [blame]
Elliott Hughes5a7788c2016-09-21 16:53:15 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "android-base/properties.h"
18
19#include <sys/system_properties.h>
20
21#include <string>
22
23#include <android-base/parseint.h>
24
25namespace android {
26namespace base {
27
28std::string GetProperty(const std::string& key, const std::string& default_value) {
29 const prop_info* pi = __system_property_find(key.c_str());
30 if (pi == nullptr) return default_value;
31
32 char buf[PROP_VALUE_MAX];
33 if (__system_property_read(pi, nullptr, buf) > 0) return buf;
34
35 // If the property exists but is empty, also return the default value.
36 // Since we can't remove system properties, "empty" is traditionally
37 // the same as "missing" (this was true for cutils' property_get).
38 return default_value;
39}
40
41bool GetBoolProperty(const std::string& key, bool default_value) {
42 std::string value = GetProperty(key, "");
43 if (value == "1" || value == "y" || value == "yes" || value == "on" || value == "true") {
44 return true;
45 } else if (value == "0" || value == "n" || value == "no" || value == "off" || value == "false") {
46 return false;
47 }
48 return default_value;
49}
50
51template <typename T>
52T GetIntProperty(const std::string& key, T default_value, T min, T max) {
53 T result;
54 std::string value = GetProperty(key, "");
Elliott Hughes31d04db2016-10-11 17:09:00 -070055 if (!value.empty() && android::base::ParseInt(value, &result, min, max)) return result;
Elliott Hughes5a7788c2016-09-21 16:53:15 -070056 return default_value;
57}
58
59template <typename T>
60T GetUintProperty(const std::string& key, T default_value, T max) {
61 T result;
62 std::string value = GetProperty(key, "");
Elliott Hughes31d04db2016-10-11 17:09:00 -070063 if (!value.empty() && android::base::ParseUint(value, &result, max)) return result;
Elliott Hughes5a7788c2016-09-21 16:53:15 -070064 return default_value;
65}
66
67template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t);
68template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t);
69template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t);
70template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t);
71
72template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t);
73template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t);
74template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t);
75template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t);
76
77bool SetProperty(const std::string& key, const std::string& value) {
78 return (__system_property_set(key.c_str(), value.c_str()) == 0);
79}
80
81} // namespace base
82} // namespace android