blob: 9a4f10b21d28ea3aa2374b34012bc52907e13789 [file] [log] [blame]
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -07001/*
2 * Copyright (C) 2008 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#define LOG_TAG "PropertyMap"
18
Harry Cutts2b67ff12023-03-13 11:32:06 +000019#include <cstdlib>
20
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070021#include <input/PropertyMap.h>
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070022#include <log/log.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070023
24// Enables debug output for the parser.
25#define DEBUG_PARSER 0
26
27// Enables debug output for parser performance.
28#define DEBUG_PARSER_PERFORMANCE 0
29
30namespace android {
31
32static const char* WHITESPACE = " \t\r";
33static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
34
35// --- PropertyMap ---
36
37PropertyMap::PropertyMap() {}
38
39PropertyMap::~PropertyMap() {}
40
41void PropertyMap::clear() {
42 mProperties.clear();
43}
44
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070045void PropertyMap::addProperty(const std::string& key, const std::string& value) {
46 mProperties.emplace(key, value);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070047}
48
Harry Cutts2b67ff12023-03-13 11:32:06 +000049std::unordered_set<std::string> PropertyMap::getKeysWithPrefix(const std::string& prefix) const {
50 std::unordered_set<std::string> keys;
51 for (const auto& [key, _] : mProperties) {
52 if (key.starts_with(prefix)) {
53 keys.insert(key);
54 }
55 }
56 return keys;
57}
58
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070059bool PropertyMap::hasProperty(const std::string& key) const {
60 return mProperties.find(key) != mProperties.end();
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070061}
62
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070063bool PropertyMap::tryGetProperty(const std::string& key, std::string& outValue) const {
64 auto it = mProperties.find(key);
65 if (it == mProperties.end()) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070066 return false;
67 }
68
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070069 outValue = it->second;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070070 return true;
71}
72
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070073bool PropertyMap::tryGetProperty(const std::string& key, bool& outValue) const {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070074 int32_t intValue;
75 if (!tryGetProperty(key, intValue)) {
76 return false;
77 }
78
79 outValue = intValue;
80 return true;
81}
82
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070083bool PropertyMap::tryGetProperty(const std::string& key, int32_t& outValue) const {
84 std::string stringValue;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070085 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
86 return false;
87 }
88
89 char* end;
Michael Ensingb8d93262020-05-12 00:41:30 -070090 int32_t value = static_cast<int32_t>(strtol(stringValue.c_str(), &end, 10));
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070091 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070092 ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(),
93 stringValue.c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070094 return false;
95 }
96 outValue = value;
97 return true;
98}
99
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700100bool PropertyMap::tryGetProperty(const std::string& key, float& outValue) const {
101 std::string stringValue;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700102 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
103 return false;
104 }
105
106 char* end;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700107 float value = strtof(stringValue.c_str(), &end);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700108 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700109 ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(),
110 stringValue.c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700111 return false;
112 }
113 outValue = value;
114 return true;
115}
116
Harry Cutts2b67ff12023-03-13 11:32:06 +0000117bool PropertyMap::tryGetProperty(const std::string& key, double& outValue) const {
118 std::string stringValue;
119 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
120 return false;
121 }
122
123 char* end;
124 double value = strtod(stringValue.c_str(), &end);
125 if (*end != '\0') {
126 ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(),
127 stringValue.c_str());
128 return false;
129 }
130 outValue = value;
131 return true;
132}
133
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700134void PropertyMap::addAll(const PropertyMap* map) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700135 for (const auto& [key, value] : map->mProperties) {
136 mProperties.emplace(key, value);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700137 }
138}
139
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500140android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char* filename) {
141 std::unique_ptr<PropertyMap> outMap = std::make_unique<PropertyMap>();
142 if (outMap == nullptr) {
143 return android::base::Error(NO_MEMORY) << "Error allocating property map.";
144 }
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700145
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500146 Tokenizer* rawTokenizer;
147 status_t status = Tokenizer::open(String8(filename), &rawTokenizer);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700148 if (status) {
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800149 return android::base::Error(-status) << "Could not open file: " << filename;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700150 }
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800151#if DEBUG_PARSER_PERFORMANCE
152 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
153#endif
154 std::unique_ptr<Tokenizer> tokenizer(rawTokenizer);
155 Parser parser(outMap.get(), tokenizer.get());
156 status = parser.parse();
157#if DEBUG_PARSER_PERFORMANCE
158 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
159 ALOGD("Parsed property file '%s' %d lines in %0.3fms.", tokenizer->getFilename().string(),
160 tokenizer->getLineNumber(), elapsedTime / 1000000.0);
161#endif
162 if (status) {
163 return android::base::Error(BAD_VALUE) << "Could not parse " << filename;
164 }
165
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500166 return std::move(outMap);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700167}
168
169// --- PropertyMap::Parser ---
170
171PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
172 : mMap(map), mTokenizer(tokenizer) {}
173
174PropertyMap::Parser::~Parser() {}
175
176status_t PropertyMap::Parser::parse() {
177 while (!mTokenizer->isEof()) {
178#if DEBUG_PARSER
179 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
180 mTokenizer->peekRemainderOfLine().string());
181#endif
182
183 mTokenizer->skipDelimiters(WHITESPACE);
184
185 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
186 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
187 if (keyToken.isEmpty()) {
188 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
189 return BAD_VALUE;
190 }
191
192 mTokenizer->skipDelimiters(WHITESPACE);
193
194 if (mTokenizer->nextChar() != '=') {
195 ALOGE("%s: Expected '=' between property key and value.",
196 mTokenizer->getLocation().string());
197 return BAD_VALUE;
198 }
199
200 mTokenizer->skipDelimiters(WHITESPACE);
201
202 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
203 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
204 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
205 mTokenizer->getLocation().string());
206 return BAD_VALUE;
207 }
208
209 mTokenizer->skipDelimiters(WHITESPACE);
210 if (!mTokenizer->isEol()) {
211 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(),
212 mTokenizer->peekRemainderOfLine().string());
213 return BAD_VALUE;
214 }
215
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700216 if (mMap->hasProperty(keyToken.string())) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700217 ALOGE("%s: Duplicate property value for key '%s'.",
218 mTokenizer->getLocation().string(), keyToken.string());
219 return BAD_VALUE;
220 }
221
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700222 mMap->addProperty(keyToken.string(), valueToken.string());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700223 }
224
225 mTokenizer->nextLine();
226 }
227 return OK;
228}
229
230} // namespace android