2010-11-30 02:37:49 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "PropertyMap"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <utils/PropertyMap.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
// Enables debug output for the parser.
|
|
|
|
#define DEBUG_PARSER 0
|
|
|
|
|
|
|
|
// Enables debug output for parser performance.
|
|
|
|
#define DEBUG_PARSER_PERFORMANCE 0
|
|
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
static const char* WHITESPACE = " \t\r";
|
|
|
|
static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
|
|
|
|
|
|
|
|
|
|
|
|
// --- PropertyMap ---
|
|
|
|
|
|
|
|
PropertyMap::PropertyMap() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyMap::~PropertyMap() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyMap::clear() {
|
|
|
|
mProperties.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyMap::addProperty(const String8& key, const String8& value) {
|
|
|
|
mProperties.add(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropertyMap::hasProperty(const String8& key) const {
|
|
|
|
return mProperties.indexOfKey(key) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropertyMap::tryGetProperty(const String8& key, String8& outValue) const {
|
|
|
|
ssize_t index = mProperties.indexOfKey(key);
|
|
|
|
if (index < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
outValue = mProperties.valueAt(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropertyMap::tryGetProperty(const String8& key, bool& outValue) const {
|
|
|
|
int32_t intValue;
|
|
|
|
if (!tryGetProperty(key, intValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
outValue = intValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropertyMap::tryGetProperty(const String8& key, int32_t& outValue) const {
|
|
|
|
String8 stringValue;
|
|
|
|
if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* end;
|
|
|
|
int value = strtol(stringValue.string(), & end, 10);
|
|
|
|
if (*end != '\0') {
|
2012-01-06 00:22:43 +01:00
|
|
|
ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.",
|
2010-11-30 02:37:49 +01:00
|
|
|
key.string(), stringValue.string());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
outValue = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PropertyMap::tryGetProperty(const String8& key, float& outValue) const {
|
|
|
|
String8 stringValue;
|
|
|
|
if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* end;
|
|
|
|
float value = strtof(stringValue.string(), & end);
|
|
|
|
if (*end != '\0') {
|
2012-01-06 00:22:43 +01:00
|
|
|
ALOGW("Property key '%s' has invalid value '%s'. Expected a float.",
|
2010-11-30 02:37:49 +01:00
|
|
|
key.string(), stringValue.string());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
outValue = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-24 02:50:18 +01:00
|
|
|
void PropertyMap::addAll(const PropertyMap* map) {
|
|
|
|
for (size_t i = 0; i < map->mProperties.size(); i++) {
|
|
|
|
mProperties.add(map->mProperties.keyAt(i), map->mProperties.valueAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 02:37:49 +01:00
|
|
|
status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
|
|
|
|
*outMap = NULL;
|
|
|
|
|
|
|
|
Tokenizer* tokenizer;
|
|
|
|
status_t status = Tokenizer::open(filename, &tokenizer);
|
|
|
|
if (status) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("Error %d opening property file %s.", status, filename.string());
|
2010-11-30 02:37:49 +01:00
|
|
|
} else {
|
|
|
|
PropertyMap* map = new PropertyMap();
|
|
|
|
if (!map) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("Error allocating property map.");
|
2010-11-30 02:37:49 +01:00
|
|
|
status = NO_MEMORY;
|
|
|
|
} else {
|
|
|
|
#if DEBUG_PARSER_PERFORMANCE
|
|
|
|
nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
#endif
|
|
|
|
Parser parser(map, tokenizer);
|
|
|
|
status = parser.parse();
|
|
|
|
#if DEBUG_PARSER_PERFORMANCE
|
|
|
|
nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
|
2011-12-20 17:23:08 +01:00
|
|
|
ALOGD("Parsed property file '%s' %d lines in %0.3fms.",
|
2010-11-30 02:37:49 +01:00
|
|
|
tokenizer->getFilename().string(), tokenizer->getLineNumber(),
|
|
|
|
elapsedTime / 1000000.0);
|
|
|
|
#endif
|
|
|
|
if (status) {
|
|
|
|
delete map;
|
|
|
|
} else {
|
|
|
|
*outMap = map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete tokenizer;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --- PropertyMap::Parser ---
|
|
|
|
|
|
|
|
PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) :
|
|
|
|
mMap(map), mTokenizer(tokenizer) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyMap::Parser::~Parser() {
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t PropertyMap::Parser::parse() {
|
|
|
|
while (!mTokenizer->isEof()) {
|
|
|
|
#if DEBUG_PARSER
|
2011-12-20 17:23:08 +01:00
|
|
|
ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
|
2010-11-30 02:37:49 +01:00
|
|
|
mTokenizer->peekRemainderOfLine().string());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
|
|
|
|
if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
|
|
|
|
String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
|
|
|
|
if (keyToken.isEmpty()) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
|
2010-11-30 02:37:49 +01:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
|
|
|
|
if (mTokenizer->nextChar() != '=') {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("%s: Expected '=' between property key and value.",
|
2010-11-30 02:37:49 +01:00
|
|
|
mTokenizer->getLocation().string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
|
|
|
|
String8 valueToken = mTokenizer->nextToken(WHITESPACE);
|
|
|
|
if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
|
2010-11-30 02:37:49 +01:00
|
|
|
mTokenizer->getLocation().string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->skipDelimiters(WHITESPACE);
|
|
|
|
if (!mTokenizer->isEol()) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("%s: Expected end of line, got '%s'.",
|
2010-11-30 02:37:49 +01:00
|
|
|
mTokenizer->getLocation().string(),
|
|
|
|
mTokenizer->peekRemainderOfLine().string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMap->hasProperty(keyToken)) {
|
2012-01-06 20:20:56 +01:00
|
|
|
ALOGE("%s: Duplicate property value for key '%s'.",
|
2010-11-30 02:37:49 +01:00
|
|
|
mTokenizer->getLocation().string(), keyToken.string());
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMap->addProperty(keyToken, valueToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenizer->nextLine();
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|