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