1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2010 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/result.h> 20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Tokenizer.h> 21*38e8c45fSAndroid Build Coastguard Worker 22*38e8c45fSAndroid Build Coastguard Worker #include <optional> 23*38e8c45fSAndroid Build Coastguard Worker #include <string> 24*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map> 25*38e8c45fSAndroid Build Coastguard Worker #include <unordered_set> 26*38e8c45fSAndroid Build Coastguard Worker 27*38e8c45fSAndroid Build Coastguard Worker namespace android { 28*38e8c45fSAndroid Build Coastguard Worker 29*38e8c45fSAndroid Build Coastguard Worker /* 30*38e8c45fSAndroid Build Coastguard Worker * Provides a mechanism for passing around string-based property key / value pairs 31*38e8c45fSAndroid Build Coastguard Worker * and loading them from property files. 32*38e8c45fSAndroid Build Coastguard Worker * 33*38e8c45fSAndroid Build Coastguard Worker * The property files have the following simple structure: 34*38e8c45fSAndroid Build Coastguard Worker * 35*38e8c45fSAndroid Build Coastguard Worker * # Comment 36*38e8c45fSAndroid Build Coastguard Worker * key = value 37*38e8c45fSAndroid Build Coastguard Worker * 38*38e8c45fSAndroid Build Coastguard Worker * Keys and values are any sequence of printable ASCII characters. 39*38e8c45fSAndroid Build Coastguard Worker * The '=' separates the key from the value. 40*38e8c45fSAndroid Build Coastguard Worker * The key and value may not contain whitespace. 41*38e8c45fSAndroid Build Coastguard Worker * 42*38e8c45fSAndroid Build Coastguard Worker * The '\' character is reserved for escape sequences and is not currently supported. 43*38e8c45fSAndroid Build Coastguard Worker * The '"" character is reserved for quoting and is not currently supported. 44*38e8c45fSAndroid Build Coastguard Worker * Files that contain the '\' or '"' character will fail to parse. 45*38e8c45fSAndroid Build Coastguard Worker * 46*38e8c45fSAndroid Build Coastguard Worker * The file must not contain duplicate keys. 47*38e8c45fSAndroid Build Coastguard Worker * 48*38e8c45fSAndroid Build Coastguard Worker * TODO Support escape sequences and quoted values when needed. 49*38e8c45fSAndroid Build Coastguard Worker */ 50*38e8c45fSAndroid Build Coastguard Worker class PropertyMap { 51*38e8c45fSAndroid Build Coastguard Worker public: 52*38e8c45fSAndroid Build Coastguard Worker /* Creates an empty property map. */ 53*38e8c45fSAndroid Build Coastguard Worker PropertyMap(); 54*38e8c45fSAndroid Build Coastguard Worker ~PropertyMap(); 55*38e8c45fSAndroid Build Coastguard Worker 56*38e8c45fSAndroid Build Coastguard Worker /* Clears the property map. */ 57*38e8c45fSAndroid Build Coastguard Worker void clear(); 58*38e8c45fSAndroid Build Coastguard Worker 59*38e8c45fSAndroid Build Coastguard Worker /* Adds a property. 60*38e8c45fSAndroid Build Coastguard Worker * Replaces the property with the same key if it is already present. 61*38e8c45fSAndroid Build Coastguard Worker */ 62*38e8c45fSAndroid Build Coastguard Worker void addProperty(const std::string& key, const std::string& value); 63*38e8c45fSAndroid Build Coastguard Worker 64*38e8c45fSAndroid Build Coastguard Worker /* Returns a set of all property keys starting with the given prefix. */ 65*38e8c45fSAndroid Build Coastguard Worker std::unordered_set<std::string> getKeysWithPrefix(const std::string& prefix) const; 66*38e8c45fSAndroid Build Coastguard Worker 67*38e8c45fSAndroid Build Coastguard Worker /* Gets the value of a property and parses it. Returns nullopt if the key wasn't found or 68*38e8c45fSAndroid Build Coastguard Worker * couldn't be parsed as the requested type. (Warnings are also logged in the case of parsing 69*38e8c45fSAndroid Build Coastguard Worker * failures.) 70*38e8c45fSAndroid Build Coastguard Worker */ 71*38e8c45fSAndroid Build Coastguard Worker std::optional<std::string> getString(const std::string& key) const; 72*38e8c45fSAndroid Build Coastguard Worker std::optional<bool> getBool(const std::string& key) const; 73*38e8c45fSAndroid Build Coastguard Worker std::optional<int32_t> getInt(const std::string& key) const; 74*38e8c45fSAndroid Build Coastguard Worker std::optional<float> getFloat(const std::string& key) const; 75*38e8c45fSAndroid Build Coastguard Worker std::optional<double> getDouble(const std::string& key) const; 76*38e8c45fSAndroid Build Coastguard Worker 77*38e8c45fSAndroid Build Coastguard Worker /* Adds all values from the specified property map. */ 78*38e8c45fSAndroid Build Coastguard Worker void addAll(const PropertyMap* map); 79*38e8c45fSAndroid Build Coastguard Worker 80*38e8c45fSAndroid Build Coastguard Worker /* Loads a property map from a file. */ 81*38e8c45fSAndroid Build Coastguard Worker static android::base::Result<std::unique_ptr<PropertyMap>> load(const char* filename); 82*38e8c45fSAndroid Build Coastguard Worker 83*38e8c45fSAndroid Build Coastguard Worker private: 84*38e8c45fSAndroid Build Coastguard Worker /* Returns true if the property map contains the specified key. */ 85*38e8c45fSAndroid Build Coastguard Worker bool hasProperty(const std::string& key) const; 86*38e8c45fSAndroid Build Coastguard Worker 87*38e8c45fSAndroid Build Coastguard Worker class Parser { 88*38e8c45fSAndroid Build Coastguard Worker PropertyMap* mMap; 89*38e8c45fSAndroid Build Coastguard Worker Tokenizer* mTokenizer; 90*38e8c45fSAndroid Build Coastguard Worker 91*38e8c45fSAndroid Build Coastguard Worker public: 92*38e8c45fSAndroid Build Coastguard Worker Parser(PropertyMap* map, Tokenizer* tokenizer); 93*38e8c45fSAndroid Build Coastguard Worker ~Parser(); 94*38e8c45fSAndroid Build Coastguard Worker status_t parse(); 95*38e8c45fSAndroid Build Coastguard Worker 96*38e8c45fSAndroid Build Coastguard Worker private: 97*38e8c45fSAndroid Build Coastguard Worker status_t parseType(); 98*38e8c45fSAndroid Build Coastguard Worker status_t parseKey(); 99*38e8c45fSAndroid Build Coastguard Worker status_t parseKeyProperty(); 100*38e8c45fSAndroid Build Coastguard Worker status_t parseModifier(const std::string& token, int32_t* outMetaState); 101*38e8c45fSAndroid Build Coastguard Worker status_t parseCharacterLiteral(char16_t* outCharacter); 102*38e8c45fSAndroid Build Coastguard Worker }; 103*38e8c45fSAndroid Build Coastguard Worker 104*38e8c45fSAndroid Build Coastguard Worker std::unordered_map<std::string, std::string> mProperties; 105*38e8c45fSAndroid Build Coastguard Worker }; 106*38e8c45fSAndroid Build Coastguard Worker 107*38e8c45fSAndroid Build Coastguard Worker } // namespace android 108