blob: 4bcdd0f7f0d3f89cc6e083768e54e1ccab538c40 [file] [log] [blame]
Jeff Brown04cbbc12010-11-29 17:37:49 -08001/*
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
Jeff Brown04cbbc12010-11-29 17:37:49 -080019#include <utils/PropertyMap.h>
Jeff Brown04cbbc12010-11-29 17:37:49 -080020
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
27
28namespace android {
29
30static const char* WHITESPACE = " \t\r";
31static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
32
33
34// --- PropertyMap ---
35
36PropertyMap::PropertyMap() {
37}
38
39PropertyMap::~PropertyMap() {
40}
41
42void PropertyMap::clear() {
43 mProperties.clear();
44}
45
46void PropertyMap::addProperty(const String8& key, const String8& value) {
47 mProperties.add(key, value);
48}
49
50bool PropertyMap::hasProperty(const String8& key) const {
51 return mProperties.indexOfKey(key) >= 0;
52}
53
54bool PropertyMap::tryGetProperty(const String8& key, String8& outValue) const {
55 ssize_t index = mProperties.indexOfKey(key);
56 if (index < 0) {
57 return false;
58 }
59
60 outValue = mProperties.valueAt(index);
61 return true;
62}
63
64bool PropertyMap::tryGetProperty(const String8& key, bool& outValue) const {
65 int32_t intValue;
66 if (!tryGetProperty(key, intValue)) {
67 return false;
68 }
69
70 outValue = intValue;
71 return true;
72}
73
74bool PropertyMap::tryGetProperty(const String8& key, int32_t& outValue) const {
75 String8 stringValue;
76 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
77 return false;
78 }
79
80 char* end;
81 int value = strtol(stringValue.string(), & end, 10);
82 if (*end != '\0') {
Steve Block61d341b2012-01-05 23:22:43 +000083 ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.",
Jeff Brown04cbbc12010-11-29 17:37:49 -080084 key.string(), stringValue.string());
85 return false;
86 }
87 outValue = value;
88 return true;
89}
90
91bool PropertyMap::tryGetProperty(const String8& key, float& outValue) const {
92 String8 stringValue;
93 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
94 return false;
95 }
96
97 char* end;
98 float value = strtof(stringValue.string(), & end);
99 if (*end != '\0') {
Steve Block61d341b2012-01-05 23:22:43 +0000100 ALOGW("Property key '%s' has invalid value '%s'. Expected a float.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800101 key.string(), stringValue.string());
102 return false;
103 }
104 outValue = value;
105 return true;
106}
107
Jeff Brown8659f0b2010-12-23 17:50:18 -0800108void PropertyMap::addAll(const PropertyMap* map) {
109 for (size_t i = 0; i < map->mProperties.size(); i++) {
110 mProperties.add(map->mProperties.keyAt(i), map->mProperties.valueAt(i));
111 }
112}
113
Jeff Brown04cbbc12010-11-29 17:37:49 -0800114status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
115 *outMap = NULL;
116
117 Tokenizer* tokenizer;
118 status_t status = Tokenizer::open(filename, &tokenizer);
119 if (status) {
Steve Block1b781ab2012-01-06 19:20:56 +0000120 ALOGE("Error %d opening property file %s.", status, filename.string());
Jeff Brown04cbbc12010-11-29 17:37:49 -0800121 } else {
122 PropertyMap* map = new PropertyMap();
123 if (!map) {
Steve Block1b781ab2012-01-06 19:20:56 +0000124 ALOGE("Error allocating property map.");
Jeff Brown04cbbc12010-11-29 17:37:49 -0800125 status = NO_MEMORY;
126 } else {
127#if DEBUG_PARSER_PERFORMANCE
128 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
129#endif
130 Parser parser(map, tokenizer);
131 status = parser.parse();
132#if DEBUG_PARSER_PERFORMANCE
133 nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
Steve Blockeb095332011-12-20 16:23:08 +0000134 ALOGD("Parsed property file '%s' %d lines in %0.3fms.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800135 tokenizer->getFilename().string(), tokenizer->getLineNumber(),
136 elapsedTime / 1000000.0);
137#endif
138 if (status) {
139 delete map;
140 } else {
141 *outMap = map;
142 }
143 }
144 delete tokenizer;
145 }
146 return status;
147}
148
149
150// --- PropertyMap::Parser ---
151
152PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) :
153 mMap(map), mTokenizer(tokenizer) {
154}
155
156PropertyMap::Parser::~Parser() {
157}
158
159status_t PropertyMap::Parser::parse() {
160 while (!mTokenizer->isEof()) {
161#if DEBUG_PARSER
Steve Blockeb095332011-12-20 16:23:08 +0000162 ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
Jeff Brown04cbbc12010-11-29 17:37:49 -0800163 mTokenizer->peekRemainderOfLine().string());
164#endif
165
166 mTokenizer->skipDelimiters(WHITESPACE);
167
168 if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
169 String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
170 if (keyToken.isEmpty()) {
Steve Block1b781ab2012-01-06 19:20:56 +0000171 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
Jeff Brown04cbbc12010-11-29 17:37:49 -0800172 return BAD_VALUE;
173 }
174
175 mTokenizer->skipDelimiters(WHITESPACE);
176
177 if (mTokenizer->nextChar() != '=') {
Steve Block1b781ab2012-01-06 19:20:56 +0000178 ALOGE("%s: Expected '=' between property key and value.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800179 mTokenizer->getLocation().string());
180 return BAD_VALUE;
181 }
182
183 mTokenizer->skipDelimiters(WHITESPACE);
184
185 String8 valueToken = mTokenizer->nextToken(WHITESPACE);
186 if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
Steve Block1b781ab2012-01-06 19:20:56 +0000187 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800188 mTokenizer->getLocation().string());
189 return BAD_VALUE;
190 }
191
192 mTokenizer->skipDelimiters(WHITESPACE);
193 if (!mTokenizer->isEol()) {
Steve Block1b781ab2012-01-06 19:20:56 +0000194 ALOGE("%s: Expected end of line, got '%s'.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800195 mTokenizer->getLocation().string(),
196 mTokenizer->peekRemainderOfLine().string());
197 return BAD_VALUE;
198 }
199
200 if (mMap->hasProperty(keyToken)) {
Steve Block1b781ab2012-01-06 19:20:56 +0000201 ALOGE("%s: Duplicate property value for key '%s'.",
Jeff Brown04cbbc12010-11-29 17:37:49 -0800202 mTokenizer->getLocation().string(), keyToken.string());
203 return BAD_VALUE;
204 }
205
206 mMap->addProperty(keyToken, valueToken);
207 }
208
209 mTokenizer->nextLine();
210 }
211 return NO_ERROR;
212}
213
214} // namespace android