PropertyMap.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #define LOG_TAG "PropertyMap"
  17. #include <utils/PropertyMap.h>
  18. // Enables debug output for the parser.
  19. #define DEBUG_PARSER 0
  20. // Enables debug output for parser performance.
  21. #define DEBUG_PARSER_PERFORMANCE 0
  22. namespace android {
  23. static const char* WHITESPACE = " \t\r";
  24. static const char* WHITESPACE_OR_PROPERTY_DELIMITER = " \t\r=";
  25. // --- PropertyMap ---
  26. PropertyMap::PropertyMap() {
  27. }
  28. PropertyMap::~PropertyMap() {
  29. }
  30. void PropertyMap::clear() {
  31. mProperties.clear();
  32. }
  33. void PropertyMap::addProperty(const String8& key, const String8& value) {
  34. mProperties.add(key, value);
  35. }
  36. bool PropertyMap::hasProperty(const String8& key) const {
  37. return mProperties.indexOfKey(key) >= 0;
  38. }
  39. bool PropertyMap::tryGetProperty(const String8& key, String8& outValue) const {
  40. ssize_t index = mProperties.indexOfKey(key);
  41. if (index < 0) {
  42. return false;
  43. }
  44. outValue = mProperties.valueAt(index);
  45. return true;
  46. }
  47. bool PropertyMap::tryGetProperty(const String8& key, bool& outValue) const {
  48. int32_t intValue;
  49. if (!tryGetProperty(key, intValue)) {
  50. return false;
  51. }
  52. outValue = intValue;
  53. return true;
  54. }
  55. bool PropertyMap::tryGetProperty(const String8& key, int32_t& outValue) const {
  56. String8 stringValue;
  57. if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
  58. return false;
  59. }
  60. char* end;
  61. int value = strtol(stringValue.string(), & end, 10);
  62. if (*end != '\0') {
  63. ALOGW("Property key '%s' has invalid value '%s'. Expected an integer.",
  64. key.string(), stringValue.string());
  65. return false;
  66. }
  67. outValue = value;
  68. return true;
  69. }
  70. bool PropertyMap::tryGetProperty(const String8& key, float& outValue) const {
  71. String8 stringValue;
  72. if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
  73. return false;
  74. }
  75. char* end;
  76. float value = strtof(stringValue.string(), & end);
  77. if (*end != '\0') {
  78. ALOGW("Property key '%s' has invalid value '%s'. Expected a float.",
  79. key.string(), stringValue.string());
  80. return false;
  81. }
  82. outValue = value;
  83. return true;
  84. }
  85. void PropertyMap::addAll(const PropertyMap* map) {
  86. for (size_t i = 0; i < map->mProperties.size(); i++) {
  87. mProperties.add(map->mProperties.keyAt(i), map->mProperties.valueAt(i));
  88. }
  89. }
  90. status_t PropertyMap::load(const String8& filename, PropertyMap** outMap) {
  91. *outMap = nullptr;
  92. Tokenizer* tokenizer;
  93. status_t status = Tokenizer::open(filename, &tokenizer);
  94. if (status) {
  95. ALOGE("Error %d opening property file %s.", status, filename.string());
  96. } else {
  97. PropertyMap* map = new PropertyMap();
  98. if (!map) {
  99. ALOGE("Error allocating property map.");
  100. status = NO_MEMORY;
  101. } else {
  102. #if DEBUG_PARSER_PERFORMANCE
  103. nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
  104. #endif
  105. Parser parser(map, tokenizer);
  106. status = parser.parse();
  107. #if DEBUG_PARSER_PERFORMANCE
  108. nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
  109. ALOGD("Parsed property file '%s' %d lines in %0.3fms.",
  110. tokenizer->getFilename().string(), tokenizer->getLineNumber(),
  111. elapsedTime / 1000000.0);
  112. #endif
  113. if (status) {
  114. delete map;
  115. } else {
  116. *outMap = map;
  117. }
  118. }
  119. delete tokenizer;
  120. }
  121. return status;
  122. }
  123. // --- PropertyMap::Parser ---
  124. PropertyMap::Parser::Parser(PropertyMap* map, Tokenizer* tokenizer) :
  125. mMap(map), mTokenizer(tokenizer) {
  126. }
  127. PropertyMap::Parser::~Parser() {
  128. }
  129. status_t PropertyMap::Parser::parse() {
  130. while (!mTokenizer->isEof()) {
  131. #if DEBUG_PARSER
  132. ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
  133. mTokenizer->peekRemainderOfLine().string());
  134. #endif
  135. mTokenizer->skipDelimiters(WHITESPACE);
  136. if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
  137. String8 keyToken = mTokenizer->nextToken(WHITESPACE_OR_PROPERTY_DELIMITER);
  138. if (keyToken.isEmpty()) {
  139. ALOGE("%s: Expected non-empty property key.", mTokenizer->getLocation().string());
  140. return BAD_VALUE;
  141. }
  142. mTokenizer->skipDelimiters(WHITESPACE);
  143. if (mTokenizer->nextChar() != '=') {
  144. ALOGE("%s: Expected '=' between property key and value.",
  145. mTokenizer->getLocation().string());
  146. return BAD_VALUE;
  147. }
  148. mTokenizer->skipDelimiters(WHITESPACE);
  149. String8 valueToken = mTokenizer->nextToken(WHITESPACE);
  150. if (valueToken.find("\\", 0) >= 0 || valueToken.find("\"", 0) >= 0) {
  151. ALOGE("%s: Found reserved character '\\' or '\"' in property value.",
  152. mTokenizer->getLocation().string());
  153. return BAD_VALUE;
  154. }
  155. mTokenizer->skipDelimiters(WHITESPACE);
  156. if (!mTokenizer->isEol()) {
  157. ALOGE("%s: Expected end of line, got '%s'.",
  158. mTokenizer->getLocation().string(),
  159. mTokenizer->peekRemainderOfLine().string());
  160. return BAD_VALUE;
  161. }
  162. if (mMap->hasProperty(keyToken)) {
  163. ALOGE("%s: Duplicate property value for key '%s'.",
  164. mTokenizer->getLocation().string(), keyToken.string());
  165. return BAD_VALUE;
  166. }
  167. mMap->addProperty(keyToken, valueToken);
  168. }
  169. mTokenizer->nextLine();
  170. }
  171. return OK;
  172. }
  173. } // namespace android