| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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 |  | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 19 | #include <utils/PropertyMap.h> | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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 |  | 
|  | 36 | PropertyMap::PropertyMap() { | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | PropertyMap::~PropertyMap() { | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | void PropertyMap::clear() { | 
|  | 43 | mProperties.clear(); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | void PropertyMap::addProperty(const String8& key, const String8& value) { | 
|  | 47 | mProperties.add(key, value); | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | bool PropertyMap::hasProperty(const String8& key) const { | 
|  | 51 | return mProperties.indexOfKey(key) >= 0; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | 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 |  | 
|  | 64 | 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 |  | 
|  | 74 | 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') { | 
| Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 83 | ALOGW("Property key '%s' has invalid value '%s'.  Expected an integer.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 84 | key.string(), stringValue.string()); | 
|  | 85 | return false; | 
|  | 86 | } | 
|  | 87 | outValue = value; | 
|  | 88 | return true; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | 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') { | 
| Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 100 | ALOGW("Property key '%s' has invalid value '%s'.  Expected a float.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 101 | key.string(), stringValue.string()); | 
|  | 102 | return false; | 
|  | 103 | } | 
|  | 104 | outValue = value; | 
|  | 105 | return true; | 
|  | 106 | } | 
|  | 107 |  | 
| Jeff Brown | 8659f0b | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 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 |  | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 114 | status_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 Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("Error %d opening property file %s.", status, filename.string()); | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 121 | } else { | 
|  | 122 | PropertyMap* map = new PropertyMap(); | 
|  | 123 | if (!map) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 124 | ALOGE("Error allocating property map."); | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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; | 
| Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 134 | ALOGD("Parsed property file '%s' %d lines in %0.3fms.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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 |  | 
|  | 152 | PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) : | 
|  | 153 | mMap(map), mTokenizer(tokenizer) { | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | PropertyMap::Parser::~Parser() { | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | status_t PropertyMap::Parser::parse() { | 
|  | 160 | while (!mTokenizer->isEof()) { | 
|  | 161 | #if DEBUG_PARSER | 
| Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 162 | ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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()) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 171 | ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string()); | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 172 | return BAD_VALUE; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | mTokenizer->skipDelimiters(WHITESPACE); | 
|  | 176 |  | 
|  | 177 | if (mTokenizer->nextChar() != '=') { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 178 | ALOGE("%s: Expected '=' between property key and value.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 187 | ALOGE("%s: Found reserved character '\\' or '\"' in property value.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 188 | mTokenizer->getLocation().string()); | 
|  | 189 | return BAD_VALUE; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | mTokenizer->skipDelimiters(WHITESPACE); | 
|  | 193 | if (!mTokenizer->isEol()) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 194 | ALOGE("%s: Expected end of line, got '%s'.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 195 | mTokenizer->getLocation().string(), | 
|  | 196 | mTokenizer->peekRemainderOfLine().string()); | 
|  | 197 | return BAD_VALUE; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | if (mMap->hasProperty(keyToken)) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 201 | ALOGE("%s: Duplicate property value for key '%s'.", | 
| Jeff Brown | 04cbbc1 | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 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 NO_ERROR; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | } // namespace android |