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 Type getType() const; 28 std::string getString() const; 29 unsigned getUnsigned() const; 30 std::vector<uint8_t> getBytes() const; 31 32 bool parseFromString(std::string in); 33 34 private: 35 Type type_; 36 std::string value_string_; 37 unsigned value_unsigned_; 38 std::vector<uint8_t> value_bytes_; 39 }; 40 41 class ConfigFile { 42 public: 43 void parseFromFile(const std::string& file_name); 44 void parseFromString(const std::string& config); 45 46 bool hasKey(const std::string& key); 47 std::string getString(const std::string& key); 48 unsigned getUnsigned(const std::string& key); 49 std::vector<uint8_t> getBytes(const std::string& key); 50 51 void clear(); 52 53 private: 54 ConfigValue& getValue(const std::string& key); 55 56 std::map<std::string, ConfigValue> values_; 57 }; 58