blob: 4833eb9c053a9a68f4bc99701f1131d113f4ab83 [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>
20
21// Enables debug output for the parser.
22#define DEBUG_PARSER 0
23
24// Enables debug output for parser performance.
25#define DEBUG_PARSER_PERFORMANCE 0
26
27namespace android {
28
29static const char* WHITESPACE = " \t\r";
30static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
31
32// --- PropertyMap ---
33
34PropertyMap::PropertyMap() {}
35
36PropertyMap::~PropertyMap() {}
37
38void PropertyMap::clear() {
39 mProperties.clear();
40}
41
42void PropertyMap::addProperty(const String8& key, const String8& value) {
43 mProperties.add(key, value);
44}
45
46bool PropertyMap::hasProperty(const String8& key) const {
47 return mProperties.indexOfKey(key) >= 0;
48}
49
50bool PropertyMap::tryGetProperty(const String8& key, String8& outValue) const {
51 ssize_t index = mProperties.indexOfKey(key);
52 if (index < 0) {
53 return false;
54 }
55
56 outValue = mProperties.valueAt(index);
57 return true;
58}
59
60bool PropertyMap::tryGetProperty(const String8& key, bool& outValue) const {
61 int32_t intValue;
62 if (!tryGetProperty(key, intValue)) {
63 return false;
64 }
65
66 outValue = intValue;
67 return true;
68}
69
70bool PropertyMap::tryGetProperty(const String8& key, int32_t& outValue) const {
71 String8 stringValue;
72 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
73 return false;
74 }
75
76 char* end;
77 int value = strtol(stringValue.string(), &end, 10);
78 if (*end != '\0') {
79 ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.string(),
80 stringValue.string());
81 return false;
82 }
83 outValue = value;
84 return true;
85}
86
87bool PropertyMap::tryGetProperty(const String8& key, float& outValue) const {
88 String8 stringValue;
89 if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) {
90 return false;
91 }
92
93 char* end;
94 float value = strtof(stringValue.string(), &end);
95 if (*end != '\0') {
96 ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.string(),
97 stringValue.string());
98 return false;
99 }
100 outValue = value;
101 return true;
102}
103
104void PropertyMap::addAll(const PropertyMap* map) {
105 for (size_t i = 0; i < map->mProperties.size(); i++) {
106 mProperties.add(map->mProperties.keyAt(i), map->mProperties.valueAt(i));
107 }
108}
109
110status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
111 *outMap = nullptr;
112
113 Tokenizer* tokenizer;
114 status_t status = Tokenizer::open(filename, &tokenizer);
115 if (status) {
116 ALOGE("Error %d opening property file %s.", status, filename.string());
117 } else {
118 PropertyMap* map = new PropertyMap();
119 if (!map) {
120 ALOGE("Error allocating property map.");
121 status = NO_MEMORY;
122 } else {
123#if DEBUG_PARSER_PERFORMANCE
124 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
125#endif
126 Parser parser(map, tokenizer);
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.",
131 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
132 elapsedTime / 1000000.0);
133#endif
134 if (status) {
135 delete map;
136 } else {
137 *outMap = map;
138 }
139 }
140 delete tokenizer;
141 }
142 return status;
143}
144
145// --- PropertyMap::Parser ---
146
147PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
148 : mMap(map), mTokenizer(tokenizer) {}
149
150PropertyMap::Parser::~Parser() {}
151
152status_t PropertyMap::Parser::parse() {
153 while (!mTokenizer->isEof()) {
154#if DEBUG_PARSER
155 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
156 mTokenizer->peekRemainderOfLine().string());
157#endif
158
159 mTokenizer->skipDelimiters(WHITESPACE);
160
161 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
162 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
163 if (keyToken.isEmpty()) {
164 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
165 return BAD_VALUE;
166 }
167
168 mTokenizer->skipDelimiters(WHITESPACE);
169
170 if (mTokenizer->nextChar() != '=') {
171 ALOGE("%s: Expected '=' between property key and value.",
172 mTokenizer->getLocation().string());
173 return BAD_VALUE;
174 }
175
176 mTokenizer->skipDelimiters(WHITESPACE);
177
178 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
179 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
180 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
181 mTokenizer->getLocation().string());
182 return BAD_VALUE;
183 }
184
185 mTokenizer->skipDelimiters(WHITESPACE);
186 if (!mTokenizer->isEol()) {
187 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(),
188 mTokenizer->peekRemainderOfLine().string());
189 return BAD_VALUE;
190 }
191
192 if (mMap->hasProperty(keyToken)) {
193 ALOGE("%s: Duplicate property value for key '%s'.",
194 mTokenizer->getLocation().string(), keyToken.string());
195 return BAD_VALUE;
196 }
197
198 mMap->addProperty(keyToken, valueToken);
199 }
200
201 mTokenizer->nextLine();
202 }
203 return OK;
204}
205
206} // namespace android