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