blob: 451918bb46e1b0d5aa60afa239ee92618ff23e95 [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/Errors.h>
22#include <utils/KeyedVector.h>
23#include <utils/String8.h>
24#include <utils/Tokenizer.h>
25
26namespace 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 */
49class PropertyMap {
50public:
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 */
61 void addProperty(const String8& key, const String8& value);
62
63 /* Returns true if the property map contains the specified key. */
64 bool hasProperty(const String8& key) const;
65
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 */
70 bool tryGetProperty(const String8& key, String8& outValue) const;
71 bool tryGetProperty(const String8& key, bool& outValue) const;
72 bool tryGetProperty(const String8& key, int32_t& outValue) const;
73 bool tryGetProperty(const String8& key, float& outValue) const;
74
75 /* Adds all values from the specified property map. */
76 void addAll(const PropertyMap* map);
77
78 /* Gets the underlying property map. */
79 inline const KeyedVector<String8, String8>& getProperties() const { return mProperties; }
80
81 /* Loads a property map from a file. */
Siarhei Vishniakou4d9f9772020-09-02 22:28:29 -050082 static android::base::Result<std::unique_ptr<PropertyMap>> load(const char* filename);
Siarhei Vishniakou32f36ae2020-09-02 20:17:10 -070083
84private:
85 class Parser {
86 PropertyMap* mMap;
87 Tokenizer* mTokenizer;
88
89 public:
90 Parser(PropertyMap* map, Tokenizer* tokenizer);
91 ~Parser();
92 status_t parse();
93
94 private:
95 status_t parseType();
96 status_t parseKey();
97 status_t parseKeyProperty();
98 status_t parseModifier(const String8& token, int32_t* outMetaState);
99 status_t parseCharacterLiteral(char16_t* outCharacter);
100 };
101
102 KeyedVector<String8, String8> mProperties;
103};
104
105} // namespace android
106
107#endif // _UTILS_PROPERTY_MAP_H