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
parseBytesString(std::string in,std::vector<uint8_t> & out)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 (const 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
ConfigValue()44 ConfigValue::ConfigValue() {
45 type_ = UNSIGNED;
46 value_unsigned_ = 0;
47 }
48
ConfigValue(std::string value)49 ConfigValue::ConfigValue(std::string value) {
50 // Don't allow empty strings
51 CHECK(!(value.empty()));
52 type_ = STRING;
53 value_string_ = value;
54 value_unsigned_ = 0;
55 }
56
ConfigValue(unsigned value)57 ConfigValue::ConfigValue(unsigned value) {
58 type_ = UNSIGNED;
59 value_unsigned_ = value;
60 }
61
ConfigValue(std::vector<int8_t> value)62 ConfigValue::ConfigValue(std::vector<int8_t> value) {
63 CHECK(!(value.empty()));
64 type_ = BYTES;
65 value_bytes_ = std::vector<uint8_t>(value.begin(), value.end());
66 value_unsigned_ = 0;
67 }
68
ConfigValue(std::vector<uint8_t> value)69 ConfigValue::ConfigValue(std::vector<uint8_t> value) {
70 CHECK(!(value.empty()));
71 type_ = BYTES;
72 value_bytes_ = value;
73 value_unsigned_ = 0;
74 }
75
getType() const76 ConfigValue::Type ConfigValue::getType() const { return type_; }
77
getString() const78 std::string ConfigValue::getString() const {
79 CHECK(type_ == STRING);
80 return value_string_;
81 };
82
getUnsigned() const83 unsigned ConfigValue::getUnsigned() const {
84 CHECK(type_ == UNSIGNED);
85 return value_unsigned_;
86 };
87
getBytes() const88 std::vector<uint8_t> ConfigValue::getBytes() const {
89 CHECK(type_ == BYTES);
90 return value_bytes_;
91 };
92
parseFromString(std::string in)93 bool ConfigValue::parseFromString(std::string in) {
94 if (in.length() > 1 && in[0] == '"' && in[in.length() - 1] == '"') {
95 CHECK(in.length() > 2); // Don't allow empty strings
96 type_ = STRING;
97 value_string_ = in.substr(1, in.length() - 2);
98 return true;
99 }
100
101 if (in.length() > 1 && in[0] == '{' && in[in.length() - 1] == '}') {
102 CHECK(in.length() >= 4); // Needs at least one byte
103 type_ = BYTES;
104 return parseBytesString(in.substr(1, in.length() - 2), value_bytes_);
105 }
106
107 unsigned tmp = 0;
108 if (ParseUint(in.c_str(), &tmp)) {
109 type_ = UNSIGNED;
110 value_unsigned_ = tmp;
111 return true;
112 }
113
114 return false;
115 }
116
addConfig(const std::string & key,ConfigValue & value)117 void ConfigFile::addConfig(const std::string& key, ConfigValue& value) {
118 CHECK(!hasKey(key));
119 values_.emplace(key, value);
120 }
121
parseFromFile(const std::string & file_name)122 void ConfigFile::parseFromFile(const std::string& file_name) {
123 string config;
124 bool config_read = ReadFileToString(file_name, &config);
125 CHECK(config_read);
126 LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
127 parseFromString(config);
128 }
129
parseFromString(const std::string & config)130 void ConfigFile::parseFromString(const std::string& config) {
131 stringstream ss(config);
132 string line;
133 while (getline(ss, line)) {
134 line = Trim(line);
135 if (line.empty()) continue;
136 if (line.at(0) == '#') continue;
137 if (line.at(0) == 0) continue;
138
139 auto search = line.find('=');
140 CHECK(search != string::npos);
141
142 string key(Trim(line.substr(0, search)));
143 string value_string(Trim(line.substr(search + 1, string::npos)));
144
145 ConfigValue value;
146 bool value_parsed = value.parseFromString(value_string);
147 CHECK(value_parsed);
148 addConfig(key, value);
149
150 LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string;
151 }
152 }
153
hasKey(const std::string & key)154 bool ConfigFile::hasKey(const std::string& key) {
155 return values_.count(key) != 0;
156 }
157
getValue(const std::string & key)158 ConfigValue& ConfigFile::getValue(const std::string& key) {
159 auto search = values_.find(key);
160 CHECK(search != values_.end());
161 return search->second;
162 }
163
getString(const std::string & key)164 std::string ConfigFile::getString(const std::string& key) {
165 return getValue(key).getString();
166 }
167
getUnsigned(const std::string & key)168 unsigned ConfigFile::getUnsigned(const std::string& key) {
169 return getValue(key).getUnsigned();
170 }
171
getBytes(const std::string & key)172 std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) {
173 return getValue(key).getBytes();
174 }
175
isEmpty()176 bool ConfigFile::isEmpty() { return values_.empty(); }
clear()177 void ConfigFile::clear() { values_.clear(); }
178