blob: 2d940a325f5606c0b40af3932b2d795b26e7e495 [file] [log] [blame]
Andreas Gampe73dae112015-11-19 14:12:14 -08001/*
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 Gampe1842af32016-03-16 14:28:50 -070024#include <file_parsing.h>
25
Andreas Gampe73dae112015-11-19 14:12:14 -080026namespace android {
27namespace installd {
28
29// Helper class to read system properties into and manage as a string->string map.
30class SystemProperties {
31 public:
32 bool Load(const std::string& strFile) {
Andreas Gampe1842af32016-03-16 14:28:50 -070033 return ParseFile(strFile, [&](const std::string& line) {
Andreas Gampe73dae112015-11-19 14:12:14 -080034 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 Gampe1842af32016-03-16 14:28:50 -070037 return true;
Andreas Gampe73dae112015-11-19 14:12:14 -080038 }
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 Gampe73dae112015-11-19 14:12:14 -080045
Andreas Gampe1842af32016-03-16 14:28:50 -070046 return true;
47 });
Andreas Gampe73dae112015-11-19 14:12:14 -080048 }
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_