• 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 /******************************************************************************
17  *
18  *  The original Work has been changed by NXP.
19  *
20  *  Copyright 2019,2023 NXP
21  *
22  *  Licensed under the Apache License, Version 2.0 (the "License");
23  *  you may not use this file except in compliance with the License.
24  *  You may obtain a copy of the License at
25  *
26  *  http://www.apache.org/licenses/LICENSE-2.0
27  *
28  *  Unless required by applicable law or agreed to in writing, software
29  *  distributed under the License is distributed on an "AS IS" BASIS,
30  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  *  See the License for the specific language governing permissions and
32  *  limitations under the License.
33  *
34  ******************************************************************************/
35 #include "config.h"
36 
37 #include <android-base/file.h>
38 #include <android-base/logging.h>
39 #include <android-base/parseint.h>
40 #include <android-base/strings.h>
41 
42 using namespace ::std;
43 using namespace ::android::base;
44 
45 namespace {
46 
parseBytesString(std::string in,std::vector<uint8_t> & out)47 bool parseBytesString(std::string in, std::vector<uint8_t>& out) {
48   vector<string> values = Split(in, ":");
49   if (values.size() == 0) return false;
50   for (const string& value : values) {
51     if (value.length() != 2) return false;
52     uint8_t tmp = 0;
53     string hexified = "0x";
54     hexified.append(value);
55     if (!ParseUint(hexified.c_str(), &tmp)) return false;
56     out.push_back(tmp);
57   }
58   return true;
59 }
60 
61 }  // namespace
62 
ConfigValue()63 ConfigValue::ConfigValue() {
64   value_unsigned_ = 0;
65   type_ = UNSIGNED;
66 }
67 
getType() const68 ConfigValue::Type ConfigValue::getType() const { return type_; }
69 
getString() const70 std::string ConfigValue::getString() const {
71   CHECK(type_ == STRING);
72   return value_string_;
73 };
74 
getUnsigned() const75 unsigned ConfigValue::getUnsigned() const {
76   CHECK(type_ == UNSIGNED);
77   return value_unsigned_;
78 };
79 
getBytes() const80 std::vector<uint8_t> ConfigValue::getBytes() const {
81   CHECK(type_ == BYTES);
82   return value_bytes_;
83 };
84 
parseFromString(std::string in)85 bool ConfigValue::parseFromString(std::string in) {
86   if (in.length() > 1 && in[0] == '"' && in[in.length() - 1] == '"') {
87     CHECK(in.length() > 2);  // Don't allow empty strings
88     type_ = STRING;
89     value_string_ = in.substr(1, in.length() - 2);
90     return true;
91   }
92 
93   if (in.length() > 1 && in[0] == '{' && in[in.length() - 1] == '}') {
94     CHECK(in.length() >= 4);  // Needs at least one byte
95     type_ = BYTES;
96     return parseBytesString(in.substr(1, in.length() - 2), value_bytes_);
97   }
98 
99   unsigned tmp = 0;
100   if (ParseUint(in.c_str(), &tmp)) {
101     type_ = UNSIGNED;
102     value_unsigned_ = tmp;
103     return true;
104   }
105 
106   return false;
107 }
108 
parseFromFile(const std::string & file_name)109 void ConfigFile::parseFromFile(const std::string& file_name) {
110   string config;
111   bool config_read = ReadFileToString(file_name, &config);
112   CHECK(config_read);
113   LOG(INFO) << "ConfigFile - Parsing file '" << file_name << "'";
114   parseFromString(config);
115 }
116 
parseFromString(const std::string & config)117 void ConfigFile::parseFromString(const std::string& config) {
118   stringstream ss(config);
119   string line;
120   while (getline(ss, line)) {
121     line = Trim(line);
122     if (line.empty()) continue;
123     if (line.at(0) == '#') continue;
124     if (line.at(0) == 0) continue;
125 
126     auto search = line.find('=');
127     CHECK(search != string::npos);
128 
129     string key(Trim(line.substr(0, search)));
130     string value_string(Trim(line.substr(search + 1, string::npos)));
131 
132     ConfigValue value;
133     bool value_parsed = value.parseFromString(value_string);
134     CHECK(value_parsed);
135     CHECK(!hasKey(key));
136     values_.emplace(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   CHECK(values_.find(key) != values_.end());
148   auto search = values_.find(key);
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