gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
45namespace gem5
46{
47
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() const { 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() const { 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);
129 const Entry *findEntry(const std::string &entryName) const;
130
136 bool printUnreferenced(const std::string &sectionName) const;
137
139 void dump(const std::string &sectionName) const;
140
141 EntryTable::const_iterator begin() const;
142 EntryTable::const_iterator end() const;
143 };
144
146 typedef std::unordered_map<std::string, Section> SectionTable;
147
148 protected:
151
155 Section *addSection(const std::string &sectionName);
156
159 Section *findSection(const std::string &sectionName);
160 const Section *findSection(const std::string &sectionName) const;
161
162 public:
164 IniFile();
165
170 bool load(std::istream &f);
171
177 bool load(const std::string &file);
178
182 bool add(const std::string &s);
183
187 bool find(const std::string &section, const std::string &entry,
188 std::string &value) const;
189
193 bool entryExists(const std::string &section,
194 const std::string &entry) const;
195
201 bool sectionExists(const std::string &section) const;
202
205
208 bool printUnreferenced() const;
209
211 void dump();
212
214 using VisitSectionCallback = std::function<void(
215 const std::string&, const std::string&)>;
216
218 void visitSection(const std::string &sectionName, VisitSectionCallback cb);
219};
220
221} // namespace gem5
222
223#endif // __INIFILE_HH__
A single key/value pair.
Definition inifile.hh:63
std::string value
The entry value.
Definition inifile.hh:64
bool isReferenced() const
Has this entry been used?
Definition inifile.hh:75
const std::string & getValue() const
Fetch the value.
Definition inifile.cc:58
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
EntryTable table
Table of entries.
Definition inifile.hh:99
std::unordered_map< std::string, Entry > EntryTable
EntryTable type. Map of strings to Entry object pointers.
Definition inifile.hh:97
void addEntry(const std::string &entryName, const std::string &value, bool append)
Add an entry to the table.
Definition inifile.cc:66
EntryTable::const_iterator end() const
Definition inifile.cc:345
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:88
EntryTable::const_iterator begin() const
Definition inifile.cc:339
bool referenced
Has this section been used?
Definition inifile.hh:100
bool isReferenced() const
Has this section been used?
Definition inifile.hh:110
Entry * findEntry(const std::string &entryName)
Find the entry with the given name.
Definition inifile.cc:112
This class represents the contents of a ".ini" file.
Definition inifile.hh:56
bool add(const std::string &s)
Take string of the form "<section>:<parameter>=<value>" or "<section>:<parameter>+=<value>" and add t...
Definition inifile.cc:154
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:207
void visitSection(const std::string &sectionName, VisitSectionCallback cb)
Iterate over key/value pairs of the given section.
Definition inifile.cc:351
std::unordered_map< std::string, Section > SectionTable
SectionTable type. Map of strings to Section object pointers.
Definition inifile.hh:146
std::function< void( const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
Definition inifile.hh:214
SectionTable table
Hash of section names to Section object pointers.
Definition inifile.hh:150
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:224
void getSectionNames(std::vector< std::string > &list) const
Push all section names into the given vector.
Definition inifile.cc:286
bool load(std::istream &f)
Load parameter settings from given istream.
Definition inifile.cc:171
void dump()
Dump contents to cout. For debugging.
Definition inifile.cc:330
bool printUnreferenced() const
Print unreferenced entries in object.
Definition inifile.cc:295
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:130
bool sectionExists(const std::string &section) const
Determine whether the named section exists in the .ini file.
Definition inifile.cc:236
Section * findSection(const std::string &sectionName)
Look up section with the given name.
Definition inifile.cc:136
STL vector class.
Definition stl.hh:37
Bitfield< 28 > v
Definition misc_types.hh:54
Bitfield< 4 > s
Bitfield< 6 > f
Definition misc_types.hh:68
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0