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 #pragma once 17 18 #include <map> 19 #include <string> 20 #include <vector> 21 22 class ConfigValue { 23 public: 24 enum Type { UNSIGNED, STRING, BYTES }; 25 26 ConfigValue(); 27 explicit ConfigValue(std::string); 28 explicit ConfigValue(unsigned); 29 explicit ConfigValue(std::vector<uint8_t>); 30 Type getType() const; 31 std::string getString() const; 32 unsigned getUnsigned() const; 33 std::vector<uint8_t> getBytes() const; 34 35 bool parseFromString(std::string in); 36 37 private: 38 Type type_; 39 std::string value_string_; 40 unsigned value_unsigned_; 41 std::vector<uint8_t> value_bytes_; 42 }; 43 44 class ConfigFile { 45 public: 46 void parseFromFile(const std::string& file_name); 47 void parseFromString(const std::string& config); 48 void addConfig(const std::string& config, ConfigValue& value); 49 50 bool hasKey(const std::string& key); 51 std::string getString(const std::string& key); 52 unsigned getUnsigned(const std::string& key); 53 std::vector<uint8_t> getBytes(const std::string& key); 54 55 bool isEmpty(); 56 void clear(); 57 58 private: 59 ConfigValue& getValue(const std::string& key); 60 61 std::map<std::string, ConfigValue> values_; 62 }; 63