Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef OTAPREOPT_SYSTEM_PROPERTIES_H_ |
| 18 | #define OTAPREOPT_SYSTEM_PROPERTIES_H_ |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <string> |
| 22 | #include <unordered_map> |
| 23 | |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 24 | #include <file_parsing.h> |
| 25 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace installd { |
| 28 | |
| 29 | // Helper class to read system properties into and manage as a string->string map. |
| 30 | class SystemProperties { |
| 31 | public: |
| 32 | bool Load(const std::string& strFile) { |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 33 | return ParseFile(strFile, [&](const std::string& line) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 34 | size_t equals_pos = line.find('='); |
| 35 | if (equals_pos == std::string::npos || equals_pos == 0) { |
| 36 | // Did not find equals sign, or it's the first character - isn't a valid line. |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 37 | return true; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | std::string key = line.substr(0, equals_pos); |
| 41 | std::string value = line.substr(equals_pos + 1, |
| 42 | line.length() - equals_pos + 1); |
| 43 | |
| 44 | properties_.insert(std::make_pair(key, value)); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 45 | |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 46 | return true; |
| 47 | }); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Look up the key in the map. Returns null if the key isn't mapped. |
| 51 | const std::string* GetProperty(const std::string& key) const { |
| 52 | auto it = properties_.find(key); |
| 53 | if (it != properties_.end()) { |
| 54 | return &it->second; |
| 55 | } |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | void SetProperty(const std::string& key, const std::string& value) { |
| 60 | properties_.insert(std::make_pair(key, value)); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | // The actual map. |
| 65 | std::unordered_map<std::string, std::string> properties_; |
| 66 | }; |
| 67 | |
| 68 | } // namespace installd |
| 69 | } // namespace android |
| 70 | |
| 71 | #endif // OTAPREOPT_SYSTEM_PROPERTIES_H_ |