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