diff options
Diffstat (limited to 'libs/input/PropertyMap.cpp')
-rw-r--r-- | libs/input/PropertyMap.cpp | 124 |
1 files changed, 70 insertions, 54 deletions
diff --git a/libs/input/PropertyMap.cpp b/libs/input/PropertyMap.cpp index 9f72c86a11..5f6f9e26b6 100644 --- a/libs/input/PropertyMap.cpp +++ b/libs/input/PropertyMap.cpp @@ -16,7 +16,10 @@ #define LOG_TAG "PropertyMap" +#include <cstdlib> + #include <input/PropertyMap.h> +#include <log/log.h> // Enables debug output for the parser. #define DEBUG_PARSER 0 @@ -39,71 +42,85 @@ void PropertyMap::clear() { mProperties.clear(); } -void PropertyMap::addProperty(const String8& key, const String8& value) { - mProperties.add(key, value); -} - -bool PropertyMap::hasProperty(const String8& key) const { - return mProperties.indexOfKey(key) >= 0; +void PropertyMap::addProperty(const std::string& key, const std::string& value) { + mProperties.emplace(key, value); } -bool PropertyMap::tryGetProperty(const String8& key, String8& outValue) const { - ssize_t index = mProperties.indexOfKey(key); - if (index < 0) { - return false; +std::unordered_set<std::string> PropertyMap::getKeysWithPrefix(const std::string& prefix) const { + std::unordered_set<std::string> keys; + for (const auto& [key, _] : mProperties) { + if (key.starts_with(prefix)) { + keys.insert(key); + } } + return keys; +} - outValue = mProperties.valueAt(index); - return true; +bool PropertyMap::hasProperty(const std::string& key) const { + return mProperties.find(key) != mProperties.end(); } -bool PropertyMap::tryGetProperty(const String8& key, bool& outValue) const { - int32_t intValue; - if (!tryGetProperty(key, intValue)) { - return false; - } +std::optional<std::string> PropertyMap::getString(const std::string& key) const { + auto it = mProperties.find(key); + return it != mProperties.end() ? std::make_optional(it->second) : std::nullopt; +} - outValue = intValue; - return true; +std::optional<bool> PropertyMap::getBool(const std::string& key) const { + std::optional<int32_t> intValue = getInt(key); + return intValue.has_value() ? std::make_optional(*intValue != 0) : std::nullopt; } -bool PropertyMap::tryGetProperty(const String8& key, int32_t& outValue) const { - String8 stringValue; - if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { - return false; +std::optional<int32_t> PropertyMap::getInt(const std::string& key) const { + std::optional<std::string> stringValue = getString(key); + if (!stringValue.has_value() || stringValue->length() == 0) { + return std::nullopt; } char* end; - int value = strtol(stringValue.c_str(), &end, 10); + int32_t value = static_cast<int32_t>(strtol(stringValue->c_str(), &end, 10)); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.", key.c_str(), - stringValue.c_str()); - return false; + stringValue->c_str()); + return std::nullopt; } - outValue = value; - return true; + return value; } -bool PropertyMap::tryGetProperty(const String8& key, float& outValue) const { - String8 stringValue; - if (!tryGetProperty(key, stringValue) || stringValue.length() == 0) { - return false; +std::optional<float> PropertyMap::getFloat(const std::string& key) const { + std::optional<std::string> stringValue = getString(key); + if (!stringValue.has_value() || stringValue->length() == 0) { + return std::nullopt; } char* end; - float value = strtof(stringValue.c_str(), &end); + float value = strtof(stringValue->c_str(), &end); if (*end != '\0') { ALOGW("Property key '%s' has invalid value '%s'. Expected a float.", key.c_str(), - stringValue.c_str()); - return false; + stringValue->c_str()); + return std::nullopt; + } + return value; +} + +std::optional<double> PropertyMap::getDouble(const std::string& key) const { + std::optional<std::string> stringValue = getString(key); + if (!stringValue.has_value() || stringValue->length() == 0) { + return std::nullopt; } - outValue = value; - return true; + + char* end; + double value = strtod(stringValue->c_str(), &end); + if (*end != '\0') { + ALOGW("Property key '%s' has invalid value '%s'. Expected a double.", key.c_str(), + stringValue->c_str()); + return std::nullopt; + } + return value; } void PropertyMap::addAll(const PropertyMap* map) { - for (size_t i = 0; i < map->mProperties.size(); i++) { - mProperties.add(map->mProperties.keyAt(i), map->mProperties.valueAt(i)); + for (const auto& [key, value] : map->mProperties) { + mProperties.emplace(key, value); } } @@ -115,25 +132,24 @@ android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char Tokenizer* rawTokenizer; status_t status = Tokenizer::open(String8(filename), &rawTokenizer); - std::unique_ptr<Tokenizer> tokenizer(rawTokenizer); if (status) { - ALOGE("Error %d opening property file %s.", status, filename); - } else { + return android::base::Error(-status) << "Could not open file: " << filename; + } #if DEBUG_PARSER_PERFORMANCE - nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); + nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); #endif - Parser parser(outMap.get(), tokenizer.get()); - status = parser.parse(); + std::unique_ptr<Tokenizer> tokenizer(rawTokenizer); + Parser parser(outMap.get(), tokenizer.get()); + status = parser.parse(); #if DEBUG_PARSER_PERFORMANCE - nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; - ALOGD("Parsed property file '%s' %d lines in %0.3fms.", - tokenizer->getFilename().c_str(), tokenizer->getLineNumber(), - elapsedTime / 1000000.0); + nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; + ALOGD("Parsed property file '%s' %d lines in %0.3fms.", tokenizer->getFilename().string(), + tokenizer->getLineNumber(), elapsedTime / 1000000.0); #endif - if (status) { - return android::base::Error(BAD_VALUE) << "Could not parse " << filename; - } + if (status) { + return android::base::Error(BAD_VALUE) << "Could not parse " << filename; } + return std::move(outMap); } @@ -184,13 +200,13 @@ status_t PropertyMap::Parser::parse() { return BAD_VALUE; } - if (mMap->hasProperty(keyToken)) { + if (mMap->hasProperty(keyToken.c_str())) { ALOGE("%s: Duplicate property value for key '%s'.", mTokenizer->getLocation().c_str(), keyToken.c_str()); return BAD_VALUE; } - mMap->addProperty(keyToken, valueToken); + mMap->addProperty(keyToken.c_str(), valueToken.c_str()); } mTokenizer->nextLine(); |