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 value_unsigned_ = 0;
46 type_ = UNSIGNED;
47 }
48
getType() const49 ConfigValue::Type ConfigValue::getType() const { return type_; }
50
getString() const51 std::string ConfigValue::getString() const {
52 CHECK(type_ == STRING);
53 return value_string_;
54 };
55
getUnsigned() const56 unsigned ConfigValue::getUnsigned() const {
57 CHECK(type_ == UNSIGNED);
58 return value_unsigned_;
59 };
60
getBytes() const61 std::vector<uint8_t> ConfigValue::getBytes() const {
62 CHECK(type_ == BYTES);
63 return value_bytes_;
64 };
65
parseFromString(std::string in)66 bool ConfigValue::parseFromString(std::string in) {
67 if (in.length() > 1 && in[0] == '"' && in[in.length() - 1] == '"') {
68 CHECK(in.length() > 2); // Don't allow empty strings
69 type_ = STRING;
70 value_string_ = in.substr(1, in.length() - 2);
71 return true;
72 }
73
74 if (in.length() > 1 && in[0] == '{' && in[in.length() - 1] == '}') {
75 CHECK(in.length() >= 4); // Needs at least one byte
76 type_ = BYTES;
77 return parseBytesString(in.substr(1, in.length() - 2), value_bytes_);
78 }
79
80 unsigned tmp = 0;
81 if (ParseUint(in.c_str(), &tmp)) {
82 type_ = UNSIGNED;
83 value_unsigned_ = tmp;
84 return true;
85 }
86
87 return false;
88 }
89
parseFromFile(const std::string & file_name)90 void ConfigFile::parseFromFile(const std::string& file_name) {
91 string config;
92 bool config_read = ReadFileToString(file_name, &config);
93 CHECK(config_read);
94 LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
95 parseFromString(config);
96 }
97
parseFromString(const std::string & config)98 void ConfigFile::parseFromString(const std::string& config) {
99 stringstream ss(config);
100 string line;
101 while (getline(ss, line)) {
102 line = Trim(line);
103 if (line.empty()) continue;
104 if (line.at(0) == '#') continue;
105 if (line.at(0) == 0) continue;
106
107 auto search = line.find('=');
108 CHECK(search != string::npos);
109
110 string key(Trim(line.substr(0, search)));
111 string value_string(Trim(line.substr(search + 1, string::npos)));
112
113 ConfigValue value;
114 bool value_parsed = value.parseFromString(value_string);
115 CHECK(value_parsed);
116 CHECK(!hasKey(key));
117 values_.emplace(key, value);
118
119 LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string;
120 }
121 }
122
hasKey(const std::string & key)123 bool ConfigFile::hasKey(const std::string& key) {
124 return values_.count(key) != 0;
125 }
126
getValue(const std::string & key)127 ConfigValue& ConfigFile::getValue(const std::string& key) {
128 auto search = values_.find(key);
129 CHECK(search != values_.end());
130 return search->second;
131 }
132
getString(const std::string & key)133 std::string ConfigFile::getString(const std::string& key) {
134 return getValue(key).getString();
135 }
136
getUnsigned(const std::string & key)137 unsigned ConfigFile::getUnsigned(const std::string& key) {
138 return getValue(key).getUnsigned();
139 }
140
getBytes(const std::string & key)141 std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) {
142 return getValue(key).getBytes();
143 }
144
clear()145 void ConfigFile::clear() { values_.clear(); }
146