blob: db0c98fc91ab7793f5447c583c7569a99e7ffac4 [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
Harry Cuttsf13161a2023-03-08 14:15:49 +000063std::optional<std::string> PropertyMap::getString(const std::string& key) const {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070064 auto it = mProperties.find(key);
Harry Cuttsf13161a2023-03-08 14:15:49 +000065 return it != mProperties.end() ? std::make_optional(it->second) : std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070066}
67
Harry Cuttsf13161a2023-03-08 14:15:49 +000068std::optional<bool> PropertyMap::getBool(const std::string& key) const {
69 std::optional<int32_t> intValue = getInt(key);
70 return intValue.has_value() ? std::make_optional(*intValue != 0) : std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070071}
72
Harry Cuttsf13161a2023-03-08 14:15:49 +000073std::optional<int32_t> PropertyMap::getInt(const std::string& key) const {
74 std::optional<std::string> stringValue = getString(key);
75 if (!stringValue.has_value() || stringValue->length() == 0) {
76 return std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070077 }
78
79 char* end;
Harry Cuttsf13161a2023-03-08 14:15:49 +000080 int32_t value = static_cast<int32_t>(strtol(stringValue->c_str(), &end, 10));
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070081 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070082 ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(),
Harry Cuttsf13161a2023-03-08 14:15:49 +000083 stringValue->c_str());
84 return std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070085 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000086 return value;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070087}
88
Harry Cuttsf13161a2023-03-08 14:15:49 +000089std::optional<float> PropertyMap::getFloat(const std::string& key) const {
90 std::optional<std::string> stringValue = getString(key);
91 if (!stringValue.has_value() || stringValue->length() == 0) {
92 return std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070093 }
94
95 char* end;
Harry Cuttsf13161a2023-03-08 14:15:49 +000096 float value = strtof(stringValue->c_str(), &end);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070097 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070098 ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(),
Harry Cuttsf13161a2023-03-08 14:15:49 +000099 stringValue->c_str());
100 return std::nullopt;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700101 }
Harry Cuttsf13161a2023-03-08 14:15:49 +0000102 return value;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700103}
104
Harry Cuttsf13161a2023-03-08 14:15:49 +0000105std::optional<double> PropertyMap::getDouble(const std::string& key) const {
106 std::optional<std::string> stringValue = getString(key);
107 if (!stringValue.has_value() || stringValue->length() == 0) {
108 return std::nullopt;
Harry Cutts2b67ff12023-03-13 11:32:06 +0000109 }
110
111 char* end;
Harry Cuttsf13161a2023-03-08 14:15:49 +0000112 double value = strtod(stringValue->c_str(), &end);
Harry Cutts2b67ff12023-03-13 11:32:06 +0000113 if (*end != '\0') {
114 ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(),
Harry Cuttsf13161a2023-03-08 14:15:49 +0000115 stringValue->c_str());
116 return std::nullopt;
Harry Cutts2b67ff12023-03-13 11:32:06 +0000117 }
Harry Cuttsf13161a2023-03-08 14:15:49 +0000118 return value;
Harry Cutts2b67ff12023-03-13 11:32:06 +0000119}
120
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700121void PropertyMap::addAll(const PropertyMap* map) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700122 for (const auto& [key, value] : map->mProperties) {
123 mProperties.emplace(key, value);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700124 }
125}
126
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500127android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char* filename) {
128 std::unique_ptr<PropertyMap> outMap = std::make_unique<PropertyMap>();
129 if (outMap == nullptr) {
130 return android::base::Error(NO_MEMORY) << "Error allocating property map.";
131 }
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700132
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500133 Tokenizer* rawTokenizer;
134 status_t status = Tokenizer::open(String8(filename), &rawTokenizer);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700135 if (status) {
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800136 return android::base::Error(-status) << "Could not open file: " << filename;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700137 }
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800138#if DEBUG_PARSER_PERFORMANCE
139 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
140#endif
141 std::unique_ptr<Tokenizer> tokenizer(rawTokenizer);
142 Parser parser(outMap.get(), tokenizer.get());
143 status = parser.parse();
144#if DEBUG_PARSER_PERFORMANCE
145 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
146 ALOGD("Parsed property file '%s' %d lines in %0.3fms.", tokenizer->getFilename().string(),
147 tokenizer->getLineNumber(), elapsedTime / 1000000.0);
148#endif
149 if (status) {
150 return android::base::Error(BAD_VALUE) << "Could not parse " << filename;
151 }
152
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500153 return std::move(outMap);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700154}
155
156// --- PropertyMap::Parser ---
157
158PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
159 : mMap(map), mTokenizer(tokenizer) {}
160
161PropertyMap::Parser::~Parser() {}
162
163status_t PropertyMap::Parser::parse() {
164 while (!mTokenizer->isEof()) {
165#if DEBUG_PARSER
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000166 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().c_str(),
167 mTokenizer->peekRemainderOfLine().c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700168#endif
169
170 mTokenizer->skipDelimiters(WHITESPACE);
171
172 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
173 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
Tomasz Wasilczyke50b2de2023-08-14 18:03:09 +0000174 if (keyToken.empty()) {
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000175 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700176 return BAD_VALUE;
177 }
178
179 mTokenizer->skipDelimiters(WHITESPACE);
180
181 if (mTokenizer->nextChar() != '=') {
182 ALOGE("%s: Expected '=' between property key and value.",
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000183 mTokenizer->getLocation().c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700184 return BAD_VALUE;
185 }
186
187 mTokenizer->skipDelimiters(WHITESPACE);
188
189 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
190 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
191 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000192 mTokenizer->getLocation().c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700193 return BAD_VALUE;
194 }
195
196 mTokenizer->skipDelimiters(WHITESPACE);
197 if (!mTokenizer->isEol()) {
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000198 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().c_str(),
199 mTokenizer->peekRemainderOfLine().c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700200 return BAD_VALUE;
201 }
202
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700203 if (mMap->hasProperty(keyToken.string())) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700204 ALOGE("%s: Duplicate property value for key '%s'.",
Tomasz Wasilczyk00eac292023-08-16 15:04:36 +0000205 mTokenizer->getLocation().c_str(), keyToken.c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700206 return BAD_VALUE;
207 }
208
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700209 mMap->addProperty(keyToken.string(), valueToken.string());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700210 }
211
212 mTokenizer->nextLine();
213 }
214 return OK;
215}
216
217} // namespace android