1 /* 2 * Copyright 2017 The Android Open Source Project 3 * Copyright 2018-2019 NXP 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 #pragma once 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 class ConfigValue { 24 public: 25 enum Type { UNSIGNED, STRING, BYTES }; 26 27 ConfigValue(); 28 ConfigValue(std::string); 29 ConfigValue(unsigned); 30 ConfigValue(std::vector<uint8_t>); 31 Type getType() const; 32 std::string getString() const; 33 unsigned getUnsigned() const; 34 std::vector<uint8_t> getBytes() const; 35 36 bool parseFromString(std::string in); 37 38 private: 39 Type type_; 40 std::string value_string_; 41 unsigned value_unsigned_; 42 std::vector<uint8_t> value_bytes_; 43 }; 44 45 class ConfigFile { 46 public: 47 void parseFromFile(const std::string& file_name); 48 void parseFromString(const std::string& config); 49 void addConfig(const std::string& config, ConfigValue& value); 50 51 bool hasKey(const std::string& key); 52 std::string getString(const std::string& key); 53 unsigned getUnsigned(const std::string& key); 54 std::vector<uint8_t> getBytes(const std::string& key); 55 56 bool isEmpty(); 57 void clear(); 58 59 private: 60 ConfigValue& getValue(const std::string& key); 61 62 std::map<std::string, ConfigValue> values_; 63 }; 64