gem5  v22.1.0.0
inifile.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __INIFILE_HH__
30 #define __INIFILE_HH__
31 
32 #include <fstream>
33 #include <functional>
34 #include <list>
35 #include <string>
36 #include <unordered_map>
37 #include <vector>
38 
45 namespace gem5
46 {
47 
55 class IniFile
56 {
57  protected:
58 
62  class Entry
63  {
64  std::string value;
65  mutable bool referenced;
66 
67  public:
69  Entry(const std::string &v)
70  : value(v), referenced(false)
71  {
72  }
73 
75  bool isReferenced() { return referenced; }
76 
78  const std::string &getValue() const;
79 
81  void setValue(const std::string &v) { value = v; }
82 
88  void appendValue(const std::string &v) { value += " "; value += v; }
89  };
90 
94  class Section
95  {
97  typedef std::unordered_map<std::string, Entry *> EntryTable;
98 
100  mutable bool referenced;
101 
102  public:
105  : table(), referenced(false)
106  {
107  }
108 
110  bool isReferenced() { return referenced; }
111 
116  void addEntry(const std::string &entryName, const std::string &value,
117  bool append);
118 
124  bool add(const std::string &assignment);
125 
128  Entry *findEntry(const std::string &entryName) const;
129 
135  bool printUnreferenced(const std::string &sectionName);
136 
138  void dump(const std::string &sectionName);
139 
140  EntryTable::const_iterator begin() const;
141  EntryTable::const_iterator end() const;
142  };
143 
145  typedef std::unordered_map<std::string, Section *> SectionTable;
146 
147  protected:
150 
154  Section *addSection(const std::string &sectionName);
155 
158  Section *findSection(const std::string &sectionName) const;
159 
160  public:
162  IniFile();
163 
165  ~IniFile();
166 
171  bool load(std::istream &f);
172 
178  bool load(const std::string &file);
179 
183  bool add(const std::string &s);
184 
188  bool find(const std::string &section, const std::string &entry,
189  std::string &value) const;
190 
194  bool entryExists(const std::string &section,
195  const std::string &entry) const;
196 
202  bool sectionExists(const std::string &section) const;
203 
206 
209  bool printUnreferenced();
210 
212  void dump();
213 
215  using VisitSectionCallback = std::function<void(
216  const std::string&, const std::string&)>;
217 
219  void visitSection(const std::string &sectionName, VisitSectionCallback cb);
220 };
221 
222 } // namespace gem5
223 
224 #endif // __INIFILE_HH__
A single key/value pair.
Definition: inifile.hh:63
std::string value
The entry value.
Definition: inifile.hh:64
const std::string & getValue() const
Fetch the value.
Definition: inifile.cc:69
bool isReferenced()
Has this entry been used?
Definition: inifile.hh:75
Entry(const std::string &v)
Constructor.
Definition: inifile.hh:69
void setValue(const std::string &v)
Set the value.
Definition: inifile.hh:81
bool referenced
Has this entry been used?
Definition: inifile.hh:65
void appendValue(const std::string &v)
Append the given string to the value.
Definition: inifile.hh:88
bool isReferenced()
Has this section been used?
Definition: inifile.hh:110
EntryTable table
Table of entries.
Definition: inifile.hh:99
void addEntry(const std::string &entryName, const std::string &value, bool append)
Add an entry to the table.
Definition: inifile.cc:77
EntryTable::const_iterator end() const
Definition: inifile.cc:358
Section()
Constructor.
Definition: inifile.hh:104
bool add(const std::string &assignment)
Add an entry to the table given a string assigment.
Definition: inifile.cc:99
EntryTable::const_iterator begin() const
Definition: inifile.cc:352
bool printUnreferenced(const std::string &sectionName)
Print the unreferenced entries in this section to cerr.
Definition: inifile.cc:251
bool referenced
Has this section been used?
Definition: inifile.hh:100
std::unordered_map< std::string, Entry * > EntryTable
EntryTable type. Map of strings to Entry object pointers.
Definition: inifile.hh:97
void dump(const std::string &sectionName)
Print the contents of this section to cout (for debugging).
Definition: inifile.cc:333
Entry * findEntry(const std::string &entryName) const
Find the entry with the given name.
Definition: inifile.cc:123
This class represents the contents of a ".ini" file.
Definition: inifile.hh:56
std::function< void(const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
Definition: inifile.hh:216
bool add(const std::string &s)
Take string of the form "<section>:<parameter>=<value>" or "<section>:<parameter>+=<value>" and add t...
Definition: inifile.cc:162
bool find(const std::string &section, const std::string &entry, std::string &value) const
Find value corresponding to given section and entry names.
Definition: inifile.cc:215
void visitSection(const std::string &sectionName, VisitSectionCallback cb)
Iterate over key/value pairs of the given section.
Definition: inifile.cc:364
~IniFile()
Destructor.
Definition: inifile.cc:45
Section * findSection(const std::string &sectionName) const
Look up section with the given name.
Definition: inifile.cc:151
std::unordered_map< std::string, Section * > SectionTable
SectionTable type. Map of strings to Section object pointers.
Definition: inifile.hh:145
SectionTable table
Hash of section names to Section object pointers.
Definition: inifile.hh:149
bool entryExists(const std::string &section, const std::string &entry) const
Determine whether the entry exists within named section exists in the .ini file.
Definition: inifile.cc:232
void getSectionNames(std::vector< std::string > &list) const
Push all section names into the given vector.
Definition: inifile.cc:295
bool load(std::istream &f)
Load parameter settings from given istream.
Definition: inifile.cc:179
void dump()
Dump contents to cout. For debugging.
Definition: inifile.cc:343
bool printUnreferenced()
Print unreferenced entries in object.
Definition: inifile.cc:305
IniFile()
Constructor.
Definition: inifile.cc:42
Section * addSection(const std::string &sectionName)
Look up section with the given name, creating a new section if not found.
Definition: inifile.cc:134
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition: inifile.cc:244
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 0 > v
Definition: pagetable.hh:65
Bitfield< 56 > f
Definition: pagetable.hh:53
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
static scfx_rep_node * list
Definition: scfx_rep.cc:336

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1