blob: 8850c95d3df5f79f2a6b2a294529096187fbb14d [file] [log] [blame]
Andre Eisenbach38003282017-11-17 16:52:05 -08001/*
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
23using namespace ::std;
24using namespace ::android::base;
25
26namespace {
27
28bool 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 Kandoif4064152018-02-28 16:27:41 -080044ConfigValue::ConfigValue() {}
45
46ConfigValue::ConfigValue(std::string value) {
47 // Don't allow empty strings
48 CHECK(!(value.empty()));
49 type_ = STRING;
50 value_string_ = value;
51}
52
53ConfigValue::ConfigValue(unsigned value) {
54 type_ = UNSIGNED;
55 value_unsigned_ = value;
56}
57
58ConfigValue::ConfigValue(std::vector<uint8_t> value) {
59 CHECK(!(value.empty()));
60 type_ = BYTES;
61 value_bytes_ = value;
62}
63
Andre Eisenbach38003282017-11-17 16:52:05 -080064ConfigValue::Type ConfigValue::getType() const { return type_; }
65
66std::string ConfigValue::getString() const {
67 CHECK(type_ == STRING);
68 return value_string_;
69};
70
71unsigned ConfigValue::getUnsigned() const {
72 CHECK(type_ == UNSIGNED);
73 return value_unsigned_;
74};
75
76std::vector<uint8_t> ConfigValue::getBytes() const {
77 CHECK(type_ == BYTES);
78 return value_bytes_;
79};
80
81bool 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 Nakashima832d5552017-12-19 12:10:29 +090090 CHECK(in.length() >= 4); // Needs at least one byte
Andre Eisenbach38003282017-11-17 16:52:05 -080091 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 Kandoif4064152018-02-28 16:27:41 -0800105void ConfigFile::addConfig(const std::string& key, ConfigValue& value) {
106 CHECK(!hasKey(key));
107 values_.emplace(key, value);
108}
109
Andre Eisenbach38003282017-11-17 16:52:05 -0800110void ConfigFile::parseFromFile(const std::string& file_name) {
111 string config;
112 bool config_read = ReadFileToString(file_name, &config);
113 CHECK(config_read);
Andre Eisenbach9c2d4312017-11-27 13:35:33 -0800114 LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
Andre Eisenbach38003282017-11-17 16:52:05 -0800115 parseFromString(config);
116}
117
118void 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 Kandoif4064152018-02-28 16:27:41 -0800136 addConfig(key, value);
Andre Eisenbach9c2d4312017-11-27 13:35:33 -0800137
138 LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string;
Andre Eisenbach38003282017-11-17 16:52:05 -0800139 }
140}
141
142bool ConfigFile::hasKey(const std::string& key) {
143 return values_.count(key) != 0;
144}
145
146ConfigValue& ConfigFile::getValue(const std::string& key) {
147 auto search = values_.find(key);
148 CHECK(search != values_.end());
149 return search->second;
150}
151
152std::string ConfigFile::getString(const std::string& key) {
153 return getValue(key).getString();
154}
155
156unsigned ConfigFile::getUnsigned(const std::string& key) {
157 return getValue(key).getUnsigned();
158}
159
160std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) {
161 return getValue(key).getBytes();
162}
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800163
Ruchi Kandoiaf716282018-03-13 14:31:11 -0700164bool ConfigFile::isEmpty() { return values_.empty(); }
Andre Eisenbacha34d7d12017-11-27 13:16:18 -0800165void ConfigFile::clear() { values_.clear(); }