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