PropertyMap.h revision 04cbbc1c47c68e805d2674d9fc702fc44385805f
1/* 2 * Copyright (C) 2010 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#ifndef _UTILS_PROPERTY_MAP_H 18#define _UTILS_PROPERTY_MAP_H 19 20#include <utils/KeyedVector.h> 21#include <utils/String8.h> 22#include <utils/Errors.h> 23#include <utils/Tokenizer.h> 24 25namespace android { 26 27/* 28 * Provides a mechanism for passing around string-based property key / value pairs 29 * and loading them from property files. 30 * 31 * The property files have the following simple structure: 32 * 33 * # Comment 34 * key = value 35 * 36 * Keys and values are any sequence of printable ASCII characters. 37 * The '=' separates the key from the value. 38 * The key and value may not contain whitespace. 39 * 40 * The '\' character is reserved for escape sequences and is not currently supported. 41 * The '"" character is reserved for quoting and is not currently supported. 42 * Files that contain the '\' or '"' character will fail to parse. 43 * 44 * The file must not contain duplicate keys. 45 * 46 * TODO Support escape sequences and quoted values when needed. 47 */ 48class PropertyMap { 49public: 50 /* Creates an empty property map. */ 51 PropertyMap(); 52 ~PropertyMap(); 53 54 /* Clears the property map. */ 55 void clear(); 56 57 /* Adds a property. 58 * Replaces the property with the same key if it is already present. 59 */ 60 void addProperty(const String8& key, const String8& value); 61 62 /* Returns true if the property map contains the specified key. */ 63 bool hasProperty(const String8& key) const; 64 65 /* Gets the value of a property and parses it. 66 * Returns true and sets outValue if the key was found and its value was parsed successfully. 67 * Otherwise returns false and does not modify outValue. (Also logs a warning.) 68 */ 69 bool tryGetProperty(const String8& key, String8& outValue) const; 70 bool tryGetProperty(const String8& key, bool& outValue) const; 71 bool tryGetProperty(const String8& key, int32_t& outValue) const; 72 bool tryGetProperty(const String8& key, float& outValue) const; 73 74 /* Loads a property map from a file. */ 75 static status_t load(const String8& filename, PropertyMap** outMap); 76 77private: 78 class Parser { 79 PropertyMap* mMap; 80 Tokenizer* mTokenizer; 81 82 public: 83 Parser(PropertyMap* map, Tokenizer* tokenizer); 84 ~Parser(); 85 status_t parse(); 86 87 private: 88 status_t parseType(); 89 status_t parseKey(); 90 status_t parseKeyProperty(); 91 status_t parseModifier(const String8& token, int32_t* outMetaState); 92 status_t parseCharacterLiteral(char16_t* outCharacter); 93 }; 94 95 KeyedVector<String8, String8> mProperties; 96}; 97 98} // namespace android 99 100#endif // _UTILS_PROPERTY_MAP_H 101