Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #define LOG_TAG "PropertyMap" |
| 18 | |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 21 | #include <input/PropertyMap.h> |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 22 | #include <log/log.h> |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 23 | |
| 24 | // Enables debug output for the parser. |
| 25 | #define DEBUG_PARSER 0 |
| 26 | |
| 27 | // Enables debug output for parser performance. |
| 28 | #define DEBUG_PARSER_PERFORMANCE 0 |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | static const char* WHITESPACE = " \t\r"; |
| 33 | static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r="; |
| 34 | |
| 35 | // --- PropertyMap --- |
| 36 | |
| 37 | PropertyMap::PropertyMap() {} |
| 38 | |
| 39 | PropertyMap::~PropertyMap() {} |
| 40 | |
| 41 | void PropertyMap::clear() { |
| 42 | mProperties.clear(); |
| 43 | } |
| 44 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 45 | void PropertyMap::addProperty(const std::string& key, const std::string& value) { |
| 46 | mProperties.emplace(key, value); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 49 | std::unordered_set<std::string> PropertyMap::getKeysWithPrefix(const std::string& prefix) const { |
| 50 | std::unordered_set<std::string> keys; |
| 51 | for (const auto& [key, _] : mProperties) { |
| 52 | if (key.starts_with(prefix)) { |
| 53 | keys.insert(key); |
| 54 | } |
| 55 | } |
| 56 | return keys; |
| 57 | } |
| 58 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 59 | bool PropertyMap::hasProperty(const std::string& key) const { |
| 60 | return mProperties.find(key) != mProperties.end(); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 63 | bool PropertyMap::tryGetProperty(const std::string& key, std::string& outValue) const { |
| 64 | auto it = mProperties.find(key); |
| 65 | if (it == mProperties.end()) { |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 69 | outValue = it->second; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 73 | bool PropertyMap::tryGetProperty(const std::string& key, bool& outValue) const { |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 74 | int32_t intValue; |
| 75 | if (!tryGetProperty(key, intValue)) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | outValue = intValue; |
| 80 | return true; |
| 81 | } |
| 82 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 83 | bool PropertyMap::tryGetProperty(const std::string& key, int32_t& outValue) const { |
| 84 | std::string stringValue; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 85 | if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | char* end; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 90 | int32_t value = static_cast<int32_t>(strtol(stringValue.c_str(), &end, 10)); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 91 | if (*end != '\0') { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 92 | ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(), |
| 93 | stringValue.c_str()); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | outValue = value; |
| 97 | return true; |
| 98 | } |
| 99 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 100 | bool PropertyMap::tryGetProperty(const std::string& key, float& outValue) const { |
| 101 | std::string stringValue; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 102 | if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | char* end; |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 107 | float value = strtof(stringValue.c_str(), &end); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 108 | if (*end != '\0') { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 109 | ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(), |
| 110 | stringValue.c_str()); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | outValue = value; |
| 114 | return true; |
| 115 | } |
| 116 | |
Harry Cutts | 2b67ff1 | 2023-03-13 11:32:06 +0000 | [diff] [blame] | 117 | bool PropertyMap::tryGetProperty(const std::string& key, double& outValue) const { |
| 118 | std::string stringValue; |
| 119 | if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | char* end; |
| 124 | double value = strtod(stringValue.c_str(), &end); |
| 125 | if (*end != '\0') { |
| 126 | ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(), |
| 127 | stringValue.c_str()); |
| 128 | return false; |
| 129 | } |
| 130 | outValue = value; |
| 131 | return true; |
| 132 | } |
| 133 | |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 134 | void PropertyMap::addAll(const PropertyMap* map) { |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 135 | for (const auto& [key, value] : map->mProperties) { |
| 136 | mProperties.emplace(key, value); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 140 | android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char* filename) { |
| 141 | std::unique_ptr<PropertyMap> outMap = std::make_unique<PropertyMap>(); |
| 142 | if (outMap == nullptr) { |
| 143 | return android::base::Error(NO_MEMORY) << "Error allocating property map."; |
| 144 | } |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 145 | |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 146 | Tokenizer* rawTokenizer; |
| 147 | status_t status = Tokenizer::open(String8(filename), &rawTokenizer); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 148 | if (status) { |
Siarhei Vishniakou | 1be799e | 2022-11-23 12:47:14 -0800 | [diff] [blame] | 149 | return android::base::Error(-status) << "Could not open file: " << filename; |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 150 | } |
Siarhei Vishniakou | 1be799e | 2022-11-23 12:47:14 -0800 | [diff] [blame] | 151 | #if DEBUG_PARSER_PERFORMANCE |
| 152 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 153 | #endif |
| 154 | std::unique_ptr<Tokenizer> tokenizer(rawTokenizer); |
| 155 | Parser parser(outMap.get(), tokenizer.get()); |
| 156 | status = parser.parse(); |
| 157 | #if DEBUG_PARSER_PERFORMANCE |
| 158 | nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
| 159 | ALOGD("Parsed property file '%s' %d lines in %0.3fms.", tokenizer->getFilename().string(), |
| 160 | tokenizer->getLineNumber(), elapsedTime / 1000000.0); |
| 161 | #endif |
| 162 | if (status) { |
| 163 | return android::base::Error(BAD_VALUE) << "Could not parse " << filename; |
| 164 | } |
| 165 | |
Siarhei Vishniakou | 4d9f977 | 2020-09-02 22:28:29 -0500 | [diff] [blame] | 166 | return std::move(outMap); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // --- PropertyMap::Parser --- |
| 170 | |
| 171 | PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) |
| 172 | : mMap(map), mTokenizer(tokenizer) {} |
| 173 | |
| 174 | PropertyMap::Parser::~Parser() {} |
| 175 | |
| 176 | status_t PropertyMap::Parser::parse() { |
| 177 | while (!mTokenizer->isEof()) { |
| 178 | #if DEBUG_PARSER |
| 179 | ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(), |
| 180 | mTokenizer->peekRemainderOfLine().string()); |
| 181 | #endif |
| 182 | |
| 183 | mTokenizer->skipDelimiters(WHITESPACE); |
| 184 | |
| 185 | if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') { |
| 186 | String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER); |
| 187 | if (keyToken.isEmpty()) { |
| 188 | ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string()); |
| 189 | return BAD_VALUE; |
| 190 | } |
| 191 | |
| 192 | mTokenizer->skipDelimiters(WHITESPACE); |
| 193 | |
| 194 | if (mTokenizer->nextChar() != '=') { |
| 195 | ALOGE("%s: Expected '=' between property key and value.", |
| 196 | mTokenizer->getLocation().string()); |
| 197 | return BAD_VALUE; |
| 198 | } |
| 199 | |
| 200 | mTokenizer->skipDelimiters(WHITESPACE); |
| 201 | |
| 202 | String8 valueToken = mTokenizer->nextToken(WHITESPACE); |
| 203 | if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) { |
| 204 | ALOGE("%s: Found reserved character '\\' or '\"' in property value.", |
| 205 | mTokenizer->getLocation().string()); |
| 206 | return BAD_VALUE; |
| 207 | } |
| 208 | |
| 209 | mTokenizer->skipDelimiters(WHITESPACE); |
| 210 | if (!mTokenizer->isEol()) { |
| 211 | ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(), |
| 212 | mTokenizer->peekRemainderOfLine().string()); |
| 213 | return BAD_VALUE; |
| 214 | } |
| 215 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 216 | if (mMap->hasProperty(keyToken.string())) { |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 217 | ALOGE("%s: Duplicate property value for key '%s'.", |
| 218 | mTokenizer->getLocation().string(), keyToken.string()); |
| 219 | return BAD_VALUE; |
| 220 | } |
| 221 | |
Siarhei Vishniakou | 4f94c1a | 2022-07-13 07:29:51 -0700 | [diff] [blame] | 222 | mMap->addProperty(keyToken.string(), valueToken.string()); |
Siarhei Vishniakou | 32f36ae | 2020-09-02 20:17:10 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | mTokenizer->nextLine(); |
| 226 | } |
| 227 | return OK; |
| 228 | } |
| 229 | |
| 230 | } // namespace android |