blob: ed9ac9fc7289a5dba6a33c51ddb17829b7b3a4a9 [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;
Michael Ensingb8d93262020-05-12 00:41:30 -070078 int32_t value = static_cast<int32_t>(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);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700119 if (status) {
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800120 return android::base::Error(-status) << "Could not open file: " << filename;
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700121 }
Siarhei Vishniakou1be799e2022-11-23 12:47:14 -0800122#if DEBUG_PARSER_PERFORMANCE
123 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
124#endif
125 std::unique_ptr<Tokenizer> tokenizer(rawTokenizer);
126 Parser parser(outMap.get(), tokenizer.get());
127 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.", tokenizer->getFilename().string(),
131 tokenizer->getLineNumber(), elapsedTime / 1000000.0);
132#endif
133 if (status) {
134 return android::base::Error(BAD_VALUE) << "Could not parse " << filename;
135 }
136
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -0500137 return std::move(outMap);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700138}
139
140// --- PropertyMap::Parser ---
141
142PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
143 : mMap(map), mTokenizer(tokenizer) {}
144
145PropertyMap::Parser::~Parser() {}
146
147status_t PropertyMap::Parser::parse() {
148 while (!mTokenizer->isEof()) {
149#if DEBUG_PARSER
150 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
151 mTokenizer->peekRemainderOfLine().string());
152#endif
153
154 mTokenizer->skipDelimiters(WHITESPACE);
155
156 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
157 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
158 if (keyToken.isEmpty()) {
159 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
160 return BAD_VALUE;
161 }
162
163 mTokenizer->skipDelimiters(WHITESPACE);
164
165 if (mTokenizer->nextChar() != '=') {
166 ALOGE("%s: Expected '=' between property key and value.",
167 mTokenizer->getLocation().string());
168 return BAD_VALUE;
169 }
170
171 mTokenizer->skipDelimiters(WHITESPACE);
172
173 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
174 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
175 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
176 mTokenizer->getLocation().string());
177 return BAD_VALUE;
178 }
179
180 mTokenizer->skipDelimiters(WHITESPACE);
181 if (!mTokenizer->isEol()) {
182 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(),
183 mTokenizer->peekRemainderOfLine().string());
184 return BAD_VALUE;
185 }
186
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700187 if (mMap->hasProperty(keyToken.string())) {
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700188 ALOGE("%s: Duplicate property value for key '%s'.",
189 mTokenizer->getLocation().string(), keyToken.string());
190 return BAD_VALUE;
191 }
192
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700193 mMap->addProperty(keyToken.string(), valueToken.string());
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -0700194 }
195
196 mTokenizer->nextLine();
197 }
198 return OK;
199}
200
201} // namespace android