• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ConfigValue(std::string value)46 ConfigValue::ConfigValue(std::string value) {
47   // Don't allow empty strings
48   CHECK(!(value.empty()));
49   type_ = STRING;
50   value_string_ = value;
51 }
52 
ConfigValue(unsigned value)53 ConfigValue::ConfigValue(unsigned value) {
54   type_ = UNSIGNED;
55   value_unsigned_ = value;
56 }
57 
ConfigValue(std::vector<uint8_t> value)58 ConfigValue::ConfigValue(std::vector<uint8_t> value) {
59   CHECK(!(value.empty()));
60   type_ = BYTES;
61   value_bytes_ = value;
62 }
63 
getType() const64 ConfigValue::Type ConfigValue::getType() const { return type_; }
65 
getString() const66 std::string ConfigValue::getString() const {
67   CHECK(type_ == STRING);
68   return value_string_;
69 };
70 
getUnsigned() const71 unsigned ConfigValue::getUnsigned() const {
72   CHECK(type_ == UNSIGNED);
73   return value_unsigned_;
74 };
75 
getBytes() const76 std::vector<uint8_t> ConfigValue::getBytes() const {
77   CHECK(type_ == BYTES);
78   return value_bytes_;
79 };
80 
parseFromString(std::string in)81 bool 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] == '}') {
90     CHECK(in.length() >= 4);  // Needs at least one byte
91     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 
addConfig(const std::string & key,ConfigValue & value)105 void ConfigFile::addConfig(const std::string& key, ConfigValue& value) {
106   CHECK(!hasKey(key));
107   values_.emplace(key, value);
108 }
109 
parseFromFile(const std::string & file_name)110 void ConfigFile::parseFromFile(const std::string& file_name) {
111   string config;
112   bool config_read = ReadFileToString(file_name, &config);
113   CHECK(config_read);
114   LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
115   parseFromString(config);
116 }
117 
parseFromString(const std::string & config)118 void 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);
136     addConfig(key, value);
137 
138     LOG(INFO) << "ConfigFile - [" << key << "] = " << value_string;
139   }
140 }
141 
hasKey(const std::string & key)142 bool ConfigFile::hasKey(const std::string& key) {
143   return values_.count(key) != 0;
144 }
145 
getValue(const std::string & key)146 ConfigValue& ConfigFile::getValue(const std::string& key) {
147   auto search = values_.find(key);
148   CHECK(search != values_.end());
149   return search->second;
150 }
151 
getString(const std::string & key)152 std::string ConfigFile::getString(const std::string& key) {
153   return getValue(key).getString();
154 }
155 
getUnsigned(const std::string & key)156 unsigned ConfigFile::getUnsigned(const std::string& key) {
157   return getValue(key).getUnsigned();
158 }
159 
getBytes(const std::string & key)160 std::vector<uint8_t> ConfigFile::getBytes(const std::string& key) {
161   return getValue(key).getBytes();
162 }
163 
isEmpty()164 bool ConfigFile::isEmpty() { return values_.empty(); }
clear()165 void ConfigFile::clear() { values_.clear(); }
166