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