xref: /aosp_15_r20/frameworks/native/cmds/installd/system_properties.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #ifndef OTAPREOPT_SYSTEM_PROPERTIES_H_
18*38e8c45fSAndroid Build Coastguard Worker #define OTAPREOPT_SYSTEM_PROPERTIES_H_
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <fstream>
21*38e8c45fSAndroid Build Coastguard Worker #include <string>
22*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <file_parsing.h>
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker namespace android {
27*38e8c45fSAndroid Build Coastguard Worker namespace installd {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker // Helper class to read system properties into and manage as a string->string map.
30*38e8c45fSAndroid Build Coastguard Worker class SystemProperties {
31*38e8c45fSAndroid Build Coastguard Worker  public:
Load(const std::string & strFile)32*38e8c45fSAndroid Build Coastguard Worker     bool Load(const std::string& strFile) {
33*38e8c45fSAndroid Build Coastguard Worker         return ParseFile(strFile, [&](const std::string& line) {
34*38e8c45fSAndroid Build Coastguard Worker             size_t equals_pos = line.find('=');
35*38e8c45fSAndroid Build Coastguard Worker             if (equals_pos == std::string::npos || equals_pos == 0) {
36*38e8c45fSAndroid Build Coastguard Worker                 // Did not find equals sign, or it's the first character - isn't a valid line.
37*38e8c45fSAndroid Build Coastguard Worker                 return true;
38*38e8c45fSAndroid Build Coastguard Worker             }
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker             std::string key = line.substr(0, equals_pos);
41*38e8c45fSAndroid Build Coastguard Worker             std::string value = line.substr(equals_pos + 1,
42*38e8c45fSAndroid Build Coastguard Worker                                             line.length() - equals_pos + 1);
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker             properties_.insert(std::make_pair(key, value));
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker             return true;
47*38e8c45fSAndroid Build Coastguard Worker         });
48*38e8c45fSAndroid Build Coastguard Worker     }
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker     // Look up the key in the map. Returns null if the key isn't mapped.
GetProperty(const std::string & key)51*38e8c45fSAndroid Build Coastguard Worker     const std::string* GetProperty(const std::string& key) const {
52*38e8c45fSAndroid Build Coastguard Worker         auto it = properties_.find(key);
53*38e8c45fSAndroid Build Coastguard Worker         if (it != properties_.end()) {
54*38e8c45fSAndroid Build Coastguard Worker             return &it->second;
55*38e8c45fSAndroid Build Coastguard Worker         }
56*38e8c45fSAndroid Build Coastguard Worker         return nullptr;
57*38e8c45fSAndroid Build Coastguard Worker     }
58*38e8c45fSAndroid Build Coastguard Worker 
SetProperty(const std::string & key,const std::string & value)59*38e8c45fSAndroid Build Coastguard Worker     void SetProperty(const std::string& key, const std::string& value) {
60*38e8c45fSAndroid Build Coastguard Worker         properties_.insert(std::make_pair(key, value));
61*38e8c45fSAndroid Build Coastguard Worker     }
62*38e8c45fSAndroid Build Coastguard Worker 
63*38e8c45fSAndroid Build Coastguard Worker  private:
64*38e8c45fSAndroid Build Coastguard Worker     // The actual map.
65*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<std::string, std::string> properties_;
66*38e8c45fSAndroid Build Coastguard Worker };
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker }  // namespace installd
69*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
70*38e8c45fSAndroid Build Coastguard Worker 
71*38e8c45fSAndroid Build Coastguard Worker #endif  // OTAPREOPT_SYSTEM_PROPERTIES_H_
72