blob: 662e568ac0bd4a8bc70057c294a02d5866a33a1b [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
19#include <input/PropertyMap.h>
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070020#include <log/log.h>
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070021
22// Enables debug output for the parser.
23#define DEBUG_PARSER 0
24
25// Enables debug output for parser performance.
26#define DEBUG_PARSER_PERFORMANCE 0
27
28namespace android {
29
30static const char* WHITESPACE = " \t\r";
31static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
32
33// --- PropertyMap ---
34
35PropertyMap::PropertyMap() {}
36
37PropertyMap::~PropertyMap() {}
38
39void PropertyMap::clear() {
40 mProperties.clear();
41}
42
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070043void PropertyMap::addProperty(const std::string& key, const std::string& value) {
44 mProperties.emplace(key, value);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070045}
46
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070047bool PropertyMap::hasProperty(const std::string& key) const {
48 return mProperties.find(key) != mProperties.end();
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070049}
50
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070051bool PropertyMap::tryGetProperty(const std::string& key, std::string& outValue) const {
52 auto it = mProperties.find(key);
53 if (it == mProperties.end()) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070054 return false;
55 }
56
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070057 outValue = it->second;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070058 return true;
59}
60
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070061bool PropertyMap::tryGetProperty(const std::string& key, bool& outValue) const {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070062 int32_t intValue;
63 if (!tryGetProperty(key, intValue)) {
64 return false;
65 }
66
67 outValue = intValue;
68 return true;
69}
70
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070071bool PropertyMap::tryGetProperty(const std::string& key, int32_t& outValue) const {
72 std::string stringValue;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070073 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
74 return false;
75 }
76
77 char* end;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070078 int value = strtol(stringValue.c_str(), &end, 10);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070079 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070080 ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(),
81 stringValue.c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070082 return false;
83 }
84 outValue = value;
85 return true;
86}
87
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070088bool PropertyMap::tryGetProperty(const std::string& key, float& outValue) const {
89 std::string stringValue;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070090 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
91 return false;
92 }
93
94 char* end;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070095 float value = strtof(stringValue.c_str(), &end);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070096 if (*end != '\0') {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070097 ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(),
98 stringValue.c_str());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070099 return false;
100 }
101 outValue = value;
102 return true;
103}
104
105void PropertyMap::addAll(const PropertyMap* map) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700106 for (const auto& [key, value] : map->mProperties) {
107 mProperties.emplace(key, value);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700108 }
109}
110
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500111android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char* filename) {
112 std::unique_ptr<PropertyMap> outMap = std::make_unique<PropertyMap>();
113 if (outMap == nullptr) {
114 return android::base::Error(NO_MEMORY) << "Error allocating property map.";
115 }
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700116
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500117 Tokenizer* rawTokenizer;
118 status_t status = Tokenizer::open(String8(filename), &rawTokenizer);
119 std::unique_ptr<Tokenizer> tokenizer(rawTokenizer);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700120 if (status) {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500121 ALOGE("Error %d opening property file %s.", status, filename);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700122 } else {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700123#if DEBUG_PARSER_PERFORMANCE
124 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
125#endif
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500126 Parser parser(outMap.get(), tokenizer.get());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700127 status = parser.parse();
128#if DEBUG_PARSER_PERFORMANCE
129 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
130 ALOGD("Parsed property file '%s' %d lines in %0.3fms.",
131 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
132 elapsedTime / 1000000.0);
133#endif
134 if (status) {
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500135 return android::base::Error(BAD_VALUE) << "Could not parse " << filename;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700136 }
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700137 }
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500138 return std::move(outMap);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700139}
140
141// --- PropertyMap::Parser ---
142
143PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
144 : mMap(map), mTokenizer(tokenizer) {}
145
146PropertyMap::Parser::~Parser() {}
147
148status_t PropertyMap::Parser::parse() {
149 while (!mTokenizer->isEof()) {
150#if DEBUG_PARSER
151 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
152 mTokenizer->peekRemainderOfLine().string());
153#endif
154
155 mTokenizer->skipDelimiters(WHITESPACE);
156
157 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
158 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
159 if (keyToken.isEmpty()) {
160 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
161 return BAD_VALUE;
162 }
163
164 mTokenizer->skipDelimiters(WHITESPACE);
165
166 if (mTokenizer->nextChar() != '=') {
167 ALOGE("%s: Expected '=' between property key and value.",
168 mTokenizer->getLocation().string());
169 return BAD_VALUE;
170 }
171
172 mTokenizer->skipDelimiters(WHITESPACE);
173
174 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
175 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
176 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
177 mTokenizer->getLocation().string());
178 return BAD_VALUE;
179 }
180
181 mTokenizer->skipDelimiters(WHITESPACE);
182 if (!mTokenizer->isEol()) {
183 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(),
184 mTokenizer->peekRemainderOfLine().string());
185 return BAD_VALUE;
186 }
187
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700188 if (mMap->hasProperty(keyToken.string())) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700189 ALOGE("%s: Duplicate property value for key '%s'.",
190 mTokenizer->getLocation().string(), keyToken.string());
191 return BAD_VALUE;
192 }
193
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700194 mMap->addProperty(keyToken.string(), valueToken.string());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700195 }
196
197 mTokenizer->nextLine();
198 }
199 return OK;
200}
201
202} // namespace android