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 
24 #include <file_parsing.h>
25 
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:
Load(const std::string & strFile)32     bool Load(const std::string& strFile) {
33         return ParseFile(strFile, [&](const std::string& line) {
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.
37                 return true;
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));
45 
46             return true;
47         });
48     }
49 
50     // Look up the key in the map. Returns null if the key isn't mapped.
GetProperty(const std::string & key)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 
SetProperty(const std::string & key,const std::string & value)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_
72