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