xref: /aosp_15_r20/system/nfc/utils/include/config.h (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
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   explicit ConfigValue(std::vector<int8_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