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