Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #include "config.h" |
| 17 | |
| 18 | #include <android-base/file.h> |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/parseint.h> |
| 21 | #include <android-base/strings.h> |
| 22 | |
| 23 | using namespace ::std; |
| 24 | using namespace ::android::base; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | bool parseBytesString(std::string in, std::vector<uint8_t>& out) { |
| 29 | vector<string> values = Split(in, ":"); |
| 30 | if (values.size() == 0) return false; |
| 31 | for (string value : values) { |
| 32 | if (value.length() != 2) return false; |
| 33 | uint8_t tmp = 0; |
| 34 | string hexified = "0x"; |
| 35 | hexified.append(value); |
| 36 | if (!ParseUint(hexified.c_str(), &tmp)) return false; |
| 37 | out.push_back(tmp); |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
Ruchi Kandoi | f406415 | 2018-02-28 16:27:41 -0800 | [diff] [blame] | 44 | ConfigValue::ConfigValue() {} |
| 45 | |
| 46 | ConfigValue::ConfigValue(std::string value) { |
| 47 | // Don't allow empty strings |
| 48 | CHECK(!(value.empty())); |
| 49 | type_ = STRING; |
| 50 | value_string_ = value; |
| 51 | } |
| 52 | |
| 53 | ConfigValue::ConfigValue(unsigned value) { |
| 54 | type_ = UNSIGNED; |
| 55 | value_unsigned_ = value; |
| 56 | } |
| 57 | |
| 58 | ConfigValue::ConfigValue(std::vector<uint8_t> value) { |
| 59 | CHECK(!(value.empty())); |
| 60 | type_ = BYTES; |
| 61 | value_bytes_ = value; |
| 62 | } |
| 63 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 64 | ConfigValue::Type ConfigValue::getType() const { return type_; } |
| 65 | |
| 66 | std::string ConfigValue::getString() const { |
| 67 | CHECK(type_ == STRING); |
| 68 | return value_string_; |
| 69 | }; |
| 70 | |
| 71 | unsigned ConfigValue::getUnsigned() const { |
| 72 | CHECK(type_ == UNSIGNED); |
| 73 | return value_unsigned_; |
| 74 | }; |
| 75 | |
| 76 | std::vector<uint8_t> ConfigValue::getBytes() const { |
| 77 | CHECK(type_ == BYTES); |
| 78 | return value_bytes_; |
| 79 | }; |
| 80 | |
| 81 | bool ConfigValue::parseFromString(std::string in) { |
| 82 | if (in.length() > 1 && in[0] == '"' && in[in.length() - 1] == '"') { |
| 83 | CHECK(in.length() > 2); // Don't allow empty strings |
| 84 | type_ = STRING; |
| 85 | value_string_ = in.substr(1, in.length() - 2); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | if (in.length() > 1 && in[0] == '{' && in[in.length() - 1] == '}') { |
Akinobu Nakashima | 832d555 | 2017-12-19 12:10:29 +0900 | [diff] [blame] | 90 | CHECK(in.length() >= 4); // Needs at least one byte |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 91 | type_ = BYTES; |
| 92 | return parseBytesString(in.substr(1, in.length() - 2), value_bytes_); |
| 93 | } |
| 94 | |
| 95 | unsigned tmp = 0; |
| 96 | if (ParseUint(in.c_str(), &tmp)) { |
| 97 | type_ = UNSIGNED; |
| 98 | value_unsigned_ = tmp; |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | return false; |
| 103 | } |
| 104 | |
Ruchi Kandoi | f406415 | 2018-02-28 16:27:41 -0800 | [diff] [blame] | 105 | void ConfigFile::addConfig(const std::string& key, ConfigValue& value) { |
| 106 | CHECK(!hasKey(key)); |
| 107 | values_.emplace(key, value); |
| 108 | } |
| 109 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 110 | void ConfigFile::parseFromFile(const std::string& file_name) { |
| 111 | string config; |
| 112 | bool config_read = ReadFileToString(file_name, &config); |
| 113 | CHECK(config_read); |
Andre Eisenbach | 9c2d431 | 2017-11-27 13:35:33 -0800 | [diff] [blame] | 114 | LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'"; |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 115 | parseFromString(config); |
| 116 | } |
| 117 | |
| 118 | void ConfigFile::parseFromString(const std::string& config) { |
| 119 | stringstream ss(config); |
| 120 | string line; |
| 121 | while (getline(ss, line)) { |
| 122 | line = Trim(line); |
| 123 | if (line.empty()) continue; |
| 124 | if (line.at(0) == '#') continue; |
| 125 | if (line.at(0) == 0) continue; |
| 126 | |
| 127 | auto search = line.find('='); |
| 128 | CHECK(search != string::npos); |
| 129 | |
| 130 | string key(Trim(line.substr(0, search))); |
| 131 | string value_string(Trim(line.substr(search + 1, string::npos))); |
| 132 | |
| 133 | ConfigValue value; |
| 134 | bool value_parsed = value.parseFromString(value_string); |
| 135 | CHECK(value_parsed); |
Ruchi Kandoi | f406415 | 2018-02-28 16:27:41 -0800 | [diff] [blame] | 136 | addConfig(key, value); |
Andre Eisenbach | 9c2d431 | 2017-11-27 13:35:33 -0800 | [diff] [blame] | 137 | |
| 138 | LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string; |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | bool ConfigFile::hasKey(const std::string& key) { |
| 143 | return values_.count(key) != 0; |
| 144 | } |
| 145 | |
| 146 | ConfigValue& ConfigFile::getValue(const std::string& key) { |
| 147 | auto search = values_.find(key); |
| 148 | CHECK(search != values_.end()); |
| 149 | return search->second; |
| 150 | } |
| 151 | |
| 152 | std::string ConfigFile::getString(const std::string& key) { |
| 153 | return getValue(key).getString(); |
| 154 | } |
| 155 | |
| 156 | unsigned ConfigFile::getUnsigned(const std::string& key) { |
| 157 | return getValue(key).getUnsigned(); |
| 158 | } |
| 159 | |
| 160 | std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) { |
| 161 | return getValue(key).getBytes(); |
| 162 | } |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 163 | |
Ruchi Kandoi | af71628 | 2018-03-13 14:31:11 -0700 | [diff] [blame^] | 164 | bool ConfigFile::isEmpty() { return values_.empty(); } |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 165 | void ConfigFile::clear() { values_.clear(); } |