blob: 1b5fb3aac28ac6a4ed6ed3a82031022ebbadee60 [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
24namespace android {
25namespace installd {
26
27// Helper class to read system properties into and manage as a string->string map.
28class SystemProperties {
29 public:
30 bool Load(const std::string& strFile) {
31 std::ifstream input_stream(strFile);
32
33 if (!input_stream.is_open()) {
34 return false;
35 }
36
37 while (!input_stream.eof()) {
38 // Read the next line.
39 std::string line;
40 getline(input_stream, line);
41
42 // Is the line empty? Simplifies the next check.
43 if (line.empty()) {
44 continue;
45 }
46
47 // Is this a comment (starts with pound)?
48 if (line[0] == '#') {
49 continue;
50 }
51
52 size_t equals_pos = line.find('=');
53 if (equals_pos == std::string::npos || equals_pos == 0) {
54 // Did not find equals sign, or it's the first character - isn't a valid line.
55 continue;
56 }
57
58 std::string key = line.substr(0, equals_pos);
59 std::string value = line.substr(equals_pos + 1,
60 line.length() - equals_pos + 1);
61
62 properties_.insert(std::make_pair(key, value));
63 }
64
65 return true;
66 }
67
68 // Look up the key in the map. Returns null if the key isn't mapped.
69 const std::string* GetProperty(const std::string& key) const {
70 auto it = properties_.find(key);
71 if (it != properties_.end()) {
72 return &it->second;
73 }
74 return nullptr;
75 }
76
77 void SetProperty(const std::string& key, const std::string& value) {
78 properties_.insert(std::make_pair(key, value));
79 }
80
81 private:
82 // The actual map.
83 std::unordered_map<std::string, std::string> properties_;
84};
85
86} // namespace installd
87} // namespace android
88
89#endif // OTAPREOPT_SYSTEM_PROPERTIES_H_