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