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
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  LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
90  parseFromString(config);
91}
92
93void ConfigFile::parseFromString(const std::string& config) {
94  stringstream ss(config);
95  string line;
96  while (getline(ss, line)) {
97    line = Trim(line);
98    if (line.empty()) continue;
99    if (line.at(0) == '#') continue;
100    if (line.at(0) == 0) continue;
101
102    auto search = line.find('=');
103    CHECK(search != string::npos);
104
105    string key(Trim(line.substr(0, search)));
106    string value_string(Trim(line.substr(search + 1, string::npos)));
107
108    ConfigValue value;
109    bool value_parsed = value.parseFromString(value_string);
110    CHECK(value_parsed);
111    CHECK(!hasKey(key));
112    values_.emplace(key, value);
113
114    LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string;
115  }
116}
117
118bool ConfigFile::hasKey(const std::string& key) {
119  return values_.count(key) != 0;
120}
121
122ConfigValue& ConfigFile::getValue(const std::string& key) {
123  auto search = values_.find(key);
124  CHECK(search != values_.end());
125  return search->second;
126}
127
128std::string ConfigFile::getString(const std::string& key) {
129  return getValue(key).getString();
130}
131
132unsigned ConfigFile::getUnsigned(const std::string& key) {
133  return getValue(key).getUnsigned();
134}
135
136std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) {
137  return getValue(key).getBytes();
138}
139
140void ConfigFile::clear() { values_.clear(); }
141