• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <utils/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 
27 
28 namespace android {
29 
30 static const char* WHITESPACE = " \t\r";
31 static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
32 
33 
34 // --- PropertyMap ---
35 
PropertyMap()36 PropertyMap::PropertyMap() {
37 }
38 
~PropertyMap()39 PropertyMap::~PropertyMap() {
40 }
41 
clear()42 void PropertyMap::clear() {
43     mProperties.clear();
44 }
45 
addProperty(const String8 & key,const String8 & value)46 void PropertyMap::addProperty(const String8& key, const String8& value) {
47     mProperties.add(key, value);
48 }
49 
hasProperty(const String8 & key) const50 bool PropertyMap::hasProperty(const String8& key) const {
51     return mProperties.indexOfKey(key) >= 0;
52 }
53 
tryGetProperty(const String8 & key,String8 & outValue) const54 bool 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 
tryGetProperty(const String8 & key,bool & outValue) const64 bool 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 
tryGetProperty(const String8 & key,int32_t & outValue) const74 bool 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') {
83         ALOGW("Property key '%s' has invalid value '%s'.  Expected an integer.",
84                 key.string(), stringValue.string());
85         return false;
86     }
87     outValue = value;
88     return true;
89 }
90 
tryGetProperty(const String8 & key,float & outValue) const91 bool 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') {
100         ALOGW("Property key '%s' has invalid value '%s'.  Expected a float.",
101                 key.string(), stringValue.string());
102         return false;
103     }
104     outValue = value;
105     return true;
106 }
107 
addAll(const PropertyMap * map)108 void 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 
load(const String8 & filename,PropertyMap ** outMap)114 status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
115     *outMap = nullptr;
116 
117     Tokenizer* tokenizer;
118     status_t status = Tokenizer::open(filename, &tokenizer);
119     if (status) {
120         ALOGE("Error %d opening property file %s.", status, filename.string());
121     } else {
122         PropertyMap* map = new PropertyMap();
123         if (!map) {
124             ALOGE("Error allocating property map.");
125             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;
134             ALOGD("Parsed property file '%s' %d lines in %0.3fms.",
135                     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 
Parser(PropertyMap * map,Tokenizer * tokenizer)152 PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) :
153         mMap(map), mTokenizer(tokenizer) {
154 }
155 
~Parser()156 PropertyMap::Parser::~Parser() {
157 }
158 
parse()159 status_t PropertyMap::Parser::parse() {
160     while (!mTokenizer->isEof()) {
161 #if DEBUG_PARSER
162         ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
163                 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()) {
171                 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
172                 return BAD_VALUE;
173             }
174 
175             mTokenizer->skipDelimiters(WHITESPACE);
176 
177             if (mTokenizer->nextChar() != '=') {
178                 ALOGE("%s: Expected '=' between property key and value.",
179                         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) {
187                 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
188                         mTokenizer->getLocation().string());
189                 return BAD_VALUE;
190             }
191 
192             mTokenizer->skipDelimiters(WHITESPACE);
193             if (!mTokenizer->isEol()) {
194                 ALOGE("%s: Expected end of line, got '%s'.",
195                         mTokenizer->getLocation().string(),
196                         mTokenizer->peekRemainderOfLine().string());
197                 return BAD_VALUE;
198             }
199 
200             if (mMap->hasProperty(keyToken)) {
201                 ALOGE("%s: Duplicate property value for key '%s'.",
202                         mTokenizer->getLocation().string(), keyToken.string());
203                 return BAD_VALUE;
204             }
205 
206             mMap->addProperty(keyToken, valueToken);
207         }
208 
209         mTokenizer->nextLine();
210     }
211     return OK;
212 }
213 
214 } // namespace android
215