Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | c08b0db | 2022-09-10 00:57:15 +0000 | [diff] [blame] | 17 | #pragma once |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 18 | |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 19 | #include <android-base/result.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 20 | #include <utils/Tokenizer.h> |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 21 | |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 22 | #include <optional> |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 23 | #include <string> |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 24 | #include <unordered_map> |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 25 | #include <unordered_set> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | /* |
| 30 | * Provides a mechanism for passing around string-based property key / value pairs |
| 31 | * and loading them from property files. |
| 32 | * |
| 33 | * The property files have the following simple structure: |
| 34 | * |
| 35 | * # Comment |
| 36 | * key = value |
| 37 | * |
| 38 | * Keys and values are any sequence of printable ASCII characters. |
| 39 | * The '=' separates the key from the value. |
| 40 | * The key and value may not contain whitespace. |
| 41 | * |
| 42 | * The '\' character is reserved for escape sequences and is not currently supported. |
| 43 | * The '"" character is reserved for quoting and is not currently supported. |
| 44 | * Files that contain the '\' or '"' character will fail to parse. |
| 45 | * |
| 46 | * The file must not contain duplicate keys. |
| 47 | * |
| 48 | * TODO Support escape sequences and quoted values when needed. |
| 49 | */ |
| 50 | class PropertyMap { |
| 51 | public: |
| 52 | /* Creates an empty property map. */ |
| 53 | PropertyMap(); |
| 54 | ~PropertyMap(); |
| 55 | |
| 56 | /* Clears the property map. */ |
| 57 | void clear(); |
| 58 | |
| 59 | /* Adds a property. |
| 60 | * Replaces the property with the same key if it is already present. |
| 61 | */ |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 62 | void addProperty(const std::string& key, const std::string& value); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 63 | |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 64 | /* Returns a set of all property keys starting with the given prefix. */ |
| 65 | std::unordered_set<std::string> getKeysWithPrefix(const std::string& prefix) const; |
| 66 | |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 67 | /* Gets the value of a property and parses it. Returns nullopt if the key wasn't found or |
| 68 | * couldn't be parsed as the requested type. (Warnings are also logged in the case of parsing |
| 69 | * failures.) |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 70 | */ |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 71 | std::optional<std::string> getString(const std::string& key) const; |
| 72 | std::optional<bool> getBool(const std::string& key) const; |
| 73 | std::optional<int32_t> getInt(const std::string& key) const; |
| 74 | std::optional<float> getFloat(const std::string& key) const; |
| 75 | std::optional<double> getDouble(const std::string& key) const; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 76 | |
| 77 | /* Adds all values from the specified property map. */ |
| 78 | void addAll(const PropertyMap* map); |
| 79 | |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 80 | /* Loads a property map from a file. */ |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 81 | static android::base::Result<std::unique_ptr<PropertyMap>> load(const char* filename); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 84 | /* Returns true if the property map contains the specified key. */ |
| 85 | bool hasProperty(const std::string& key) const; |
| 86 | |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 87 | class Parser { |
| 88 | PropertyMap* mMap; |
| 89 | Tokenizer* mTokenizer; |
| 90 | |
| 91 | public: |
| 92 | Parser(PropertyMap* map, Tokenizer* tokenizer); |
| 93 | ~Parser(); |
| 94 | status_t parse(); |
| 95 | |
| 96 | private: |
| 97 | status_t parseType(); |
| 98 | status_t parseKey(); |
| 99 | status_t parseKeyProperty(); |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 100 | status_t parseModifier(const std::string& token, int32_t* outMetaState); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 101 | status_t parseCharacterLiteral(char16_t* outCharacter); |
| 102 | }; |
| 103 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 104 | std::unordered_map<std::string, std::string> mProperties; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace android |