• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONFIG_FILE_H_
16 #define CONFIG_FILE_H_
17 
18 #include <cassert>
19 #include <iosfwd>
20 #include <map>
21 #include <string>
22 #include <vector>
23 
24 
25 namespace header_checker {
26 namespace utils {
27 
28 
29 class ConfigSection {
30  public:
31   using MapType = std::map<std::string, bool>;
32   using const_iterator = MapType::const_iterator;
33 
34 
35  public:
36   ConfigSection() = default;
37   ConfigSection(ConfigSection &&) = default;
38   ConfigSection &operator=(ConfigSection &&) = default;
39 
HasProperty(const std::string & name)40   bool HasProperty(const std::string &name) const {
41     return map_.find(name) != map_.end();
42   }
43 
GetProperty(const std::string & name)44   bool GetProperty(const std::string &name) const {
45     auto &&it = map_.find(name);
46     if (it == map_.end()) {
47       return false;
48     }
49     return it->second;
50   }
51 
GetIgnoredLinkerSetKeys()52   const std::vector<std::string> &GetIgnoredLinkerSetKeys() const {
53     return ignored_linker_set_keys_;
54   }
55 
56   bool operator[](const std::string &name) const { return GetProperty(name); }
57 
begin()58   const_iterator begin() const { return map_.begin(); }
59 
end()60   const_iterator end() const { return map_.end(); }
61 
62  private:
63   ConfigSection(const ConfigSection &) = delete;
64   ConfigSection &operator=(const ConfigSection &) = delete;
65 
66 
67  private:
68   MapType map_;
69   std::vector<std::string> ignored_linker_set_keys_;
70 
71   friend class ConfigFile;
72 };
73 
74 
75 class ConfigFile {
76  public:
77   using MapType = std::map<std::pair<std::string, std::string>, ConfigSection>;
78   using const_iterator = MapType::const_iterator;
79 
80 
81  public:
82   ConfigFile() = default;
83   ConfigFile(ConfigFile &&) = default;
84   ConfigFile &operator=(ConfigFile &&) = default;
85 
86   bool Load(const std::string &path);
87   bool Load(std::istream &istream);
88 
HasSection(const std::string & section_name,const std::string & version)89   bool HasSection(const std::string &section_name,
90                   const std::string &version) const {
91     return map_.find({section_name, version}) != map_.end();
92   }
93 
GetSection(const std::string & section_name,const std::string & version)94   const ConfigSection &GetSection(const std::string &section_name,
95                                   const std::string &version) const {
96     auto &&it = map_.find({section_name, version});
97     assert(it != map_.end());
98     return it->second;
99   }
100 
101   bool HasGlobalSection();
102 
103   const ConfigSection &GetGlobalSection();
104 
HasProperty(const std::string & section_name,const std::string & version,const std::string & property_name)105   bool HasProperty(const std::string &section_name, const std::string &version,
106                    const std::string &property_name) const {
107     auto &&it = map_.find({section_name, version});
108     if (it == map_.end()) {
109       return false;
110     }
111     return it->second.HasProperty(property_name);
112   }
113 
GetProperty(const std::string & section_name,const std::string & version,const std::string & property_name)114   bool GetProperty(const std::string &section_name, const std::string &version,
115                    const std::string &property_name) const {
116     auto &&it = map_.find({section_name, version});
117     if (it == map_.end()) {
118       return false;
119     }
120     return it->second.GetProperty(property_name);
121   }
122 
begin()123   const_iterator begin() const { return map_.begin(); }
124 
end()125   const_iterator end() const { return map_.end(); }
126 
127  private:
128   ConfigFile(const ConfigFile &) = delete;
129   ConfigFile &operator=(const ConfigFile &) = delete;
130 
131 
132  private:
133   MapType map_;
134 };
135 
136 
137 }  // namespace utils
138 }  // namespace header_checker
139 
140 
141 #endif  // CONFIG_FILE_H_
142