• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
20 
21 #include <input/PropertyMap.h>
22 #include <log/log.h>
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 
PropertyMap()37 PropertyMap::PropertyMap() {}
38 
~PropertyMap()39 PropertyMap::~PropertyMap() {}
40 
clear()41 void PropertyMap::clear() {
42     mProperties.clear();
43 }
44 
addProperty(const std::string & key,const std::string & value)45 void PropertyMap::addProperty(const std::string& key, const std::string& value) {
46     mProperties.emplace(key, value);
47 }
48 
getKeysWithPrefix(const std::string & prefix) const49 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 
hasProperty(const std::string & key) const59 bool PropertyMap::hasProperty(const std::string& key) const {
60     return mProperties.find(key) != mProperties.end();
61 }
62 
getString(const std::string & key) const63 std::optional<std::string> PropertyMap::getString(const std::string& key) const {
64     auto it = mProperties.find(key);
65     return it != mProperties.end() ? std::make_optional(it->second) : std::nullopt;
66 }
67 
getBool(const std::string & key) const68 std::optional<bool> PropertyMap::getBool(const std::string& key) const {
69     std::optional<int32_t> intValue = getInt(key);
70     return intValue.has_value() ? std::make_optional(*intValue != 0) : std::nullopt;
71 }
72 
getInt(const std::string & key) const73 std::optional<int32_t> PropertyMap::getInt(const std::string& key) const {
74     std::optional<std::string> stringValue = getString(key);
75     if (!stringValue.has_value() || stringValue->length() == 0) {
76         return std::nullopt;
77     }
78 
79     char* end;
80     int32_t value = static_cast<int32_t>(strtol(stringValue->c_str(), &end, 10));
81     if (*end != '\0') {
82         ALOGW("Property key '%s' has invalid value '%s'.  Expected an integer.", key.c_str(),
83               stringValue->c_str());
84         return std::nullopt;
85     }
86     return value;
87 }
88 
getFloat(const std::string & key) const89 std::optional<float> PropertyMap::getFloat(const std::string& key) const {
90     std::optional<std::string> stringValue = getString(key);
91     if (!stringValue.has_value() || stringValue->length() == 0) {
92         return std::nullopt;
93     }
94 
95     char* end;
96     float value = strtof(stringValue->c_str(), &end);
97     if (*end != '\0') {
98         ALOGW("Property key '%s' has invalid value '%s'.  Expected a float.", key.c_str(),
99               stringValue->c_str());
100         return std::nullopt;
101     }
102     return value;
103 }
104 
getDouble(const std::string & key) const105 std::optional<double> PropertyMap::getDouble(const std::string& key) const {
106     std::optional<std::string> stringValue = getString(key);
107     if (!stringValue.has_value() || stringValue->length() == 0) {
108         return std::nullopt;
109     }
110 
111     char* end;
112     double value = strtod(stringValue->c_str(), &end);
113     if (*end != '\0') {
114         ALOGW("Property key '%s' has invalid value '%s'.  Expected a double.", key.c_str(),
115               stringValue->c_str());
116         return std::nullopt;
117     }
118     return value;
119 }
120 
addAll(const PropertyMap * map)121 void PropertyMap::addAll(const PropertyMap* map) {
122     for (const auto& [key, value] : map->mProperties) {
123         mProperties.emplace(key, value);
124     }
125 }
126 
load(const char * filename)127 android::base::Result<std::unique_ptr<PropertyMap>> PropertyMap::load(const char* filename) {
128     std::unique_ptr<PropertyMap> outMap = std::make_unique<PropertyMap>();
129     if (outMap == nullptr) {
130         return android::base::Error(NO_MEMORY) << "Error allocating property map.";
131     }
132 
133     Tokenizer* rawTokenizer;
134     status_t status = Tokenizer::open(String8(filename), &rawTokenizer);
135     if (status) {
136         return android::base::Error(-status) << "Could not open file: " << filename;
137     }
138 #if DEBUG_PARSER_PERFORMANCE
139     nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
140 #endif
141     std::unique_ptr<Tokenizer> tokenizer(rawTokenizer);
142     Parser parser(outMap.get(), tokenizer.get());
143     status = parser.parse();
144 #if DEBUG_PARSER_PERFORMANCE
145     nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
146     ALOGD("Parsed property file '%s' %d lines in %0.3fms.", tokenizer->getFilename().string(),
147           tokenizer->getLineNumber(), elapsedTime / 1000000.0);
148 #endif
149     if (status) {
150         return android::base::Error(BAD_VALUE) << "Could not parse " << filename;
151     }
152 
153     return std::move(outMap);
154 }
155 
156 // --- PropertyMap::Parser ---
157 
Parser(PropertyMap * map,Tokenizer * tokenizer)158 PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer)
159       : mMap(map), mTokenizer(tokenizer) {}
160 
~Parser()161 PropertyMap::Parser::~Parser() {}
162 
parse()163 status_t PropertyMap::Parser::parse() {
164     while (!mTokenizer->isEof()) {
165 #if DEBUG_PARSER
166         ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
167               mTokenizer->peekRemainderOfLine().string());
168 #endif
169 
170         mTokenizer->skipDelimiters(WHITESPACE);
171 
172         if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
173             String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
174             if (keyToken.isEmpty()) {
175                 ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
176                 return BAD_VALUE;
177             }
178 
179             mTokenizer->skipDelimiters(WHITESPACE);
180 
181             if (mTokenizer->nextChar() != '=') {
182                 ALOGE("%s: Expected '=' between property key and value.",
183                       mTokenizer->getLocation().string());
184                 return BAD_VALUE;
185             }
186 
187             mTokenizer->skipDelimiters(WHITESPACE);
188 
189             String8 valueToken = mTokenizer->nextToken(WHITESPACE);
190             if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
191                 ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
192                       mTokenizer->getLocation().string());
193                 return BAD_VALUE;
194             }
195 
196             mTokenizer->skipDelimiters(WHITESPACE);
197             if (!mTokenizer->isEol()) {
198                 ALOGE("%s: Expected end of line, got '%s'.", mTokenizer->getLocation().string(),
199                       mTokenizer->peekRemainderOfLine().string());
200                 return BAD_VALUE;
201             }
202 
203             if (mMap->hasProperty(keyToken.string())) {
204                 ALOGE("%s: Duplicate property value for key '%s'.",
205                       mTokenizer->getLocation().string(), keyToken.string());
206                 return BAD_VALUE;
207             }
208 
209             mMap->addProperty(keyToken.string(), valueToken.string());
210         }
211 
212         mTokenizer->nextLine();
213     }
214     return OK;
215 }
216 
217 } // namespace android
218