Elliott Hughes | 1e88c8c | 2016-09-21 16:53:15 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 25 | namespace android { |
| 26 | namespace base { |
| 27 | |
| 28 | std::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 | |
| 41 | bool 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 | |
| 51 | template <typename T> |
| 52 | T GetIntProperty(const std::string& key, T default_value, T min, T max) { |
| 53 | T result; |
| 54 | std::string value = GetProperty(key, ""); |
| 55 | if (!value.empty() && android::base::ParseInt(value.c_str(), &result, min, max)) return result; |
| 56 | return default_value; |
| 57 | } |
| 58 | |
| 59 | template <typename T> |
| 60 | T GetUintProperty(const std::string& key, T default_value, T max) { |
| 61 | T result; |
| 62 | std::string value = GetProperty(key, ""); |
| 63 | if (!value.empty() && android::base::ParseUint(value.c_str(), &result, max)) return result; |
| 64 | return default_value; |
| 65 | } |
| 66 | |
| 67 | template int8_t GetIntProperty(const std::string&, int8_t, int8_t, int8_t); |
| 68 | template int16_t GetIntProperty(const std::string&, int16_t, int16_t, int16_t); |
| 69 | template int32_t GetIntProperty(const std::string&, int32_t, int32_t, int32_t); |
| 70 | template int64_t GetIntProperty(const std::string&, int64_t, int64_t, int64_t); |
| 71 | |
| 72 | template uint8_t GetUintProperty(const std::string&, uint8_t, uint8_t); |
| 73 | template uint16_t GetUintProperty(const std::string&, uint16_t, uint16_t); |
| 74 | template uint32_t GetUintProperty(const std::string&, uint32_t, uint32_t); |
| 75 | template uint64_t GetUintProperty(const std::string&, uint64_t, uint64_t); |
| 76 | |
| 77 | bool 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 |