gem5  v22.1.0.0
inifile.cc
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 #include "base/inifile.hh"
30 
31 #include <algorithm>
32 #include <fstream>
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 
37 #include "base/str.hh"
38 
39 namespace gem5
40 {
41 
43 {}
44 
46 {
47  SectionTable::iterator i = table.begin();
48  SectionTable::iterator end = table.end();
49 
50  while (i != end) {
51  delete (*i).second;
52  ++i;
53  }
54 }
55 
56 bool
57 IniFile::load(const std::string &file)
58 {
59  std::ifstream f(file.c_str());
60 
61  if (!f.is_open())
62  return false;
63 
64  return load(f);
65 }
66 
67 
68 const std::string &
70 {
71  referenced = true;
72  return value;
73 }
74 
75 
76 void
77 IniFile::Section::addEntry(const std::string &entryName,
78  const std::string &value,
79  bool append)
80 {
81  EntryTable::iterator ei = table.find(entryName);
82 
83  if (ei == table.end()) {
84  // new entry
85  table[entryName] = new Entry(value);
86  }
87  else if (append) {
88  // append new reult to old entry
89  ei->second->appendValue(value);
90  }
91  else {
92  // override old entry
93  ei->second->setValue(value);
94  }
95 }
96 
97 
98 bool
99 IniFile::Section::add(const std::string &assignment)
100 {
101  std::string::size_type offset = assignment.find('=');
102  if (offset == std::string::npos) {
103  // no '=' found
104  std::cerr << "Can't parse .ini line " << assignment << std::endl;
105  return false;
106  }
107 
108  // if "+=" rather than just "=" then append value
109  bool append = (assignment[offset-1] == '+');
110 
111  std::string entryName = assignment.substr(0, append ? offset-1 : offset);
112  std::string value = assignment.substr(offset + 1);
113 
114  eat_white(entryName);
115  eat_white(value);
116 
117  addEntry(entryName, value, append);
118  return true;
119 }
120 
121 
123 IniFile::Section::findEntry(const std::string &entryName) const
124 {
125  referenced = true;
126 
127  EntryTable::const_iterator ei = table.find(entryName);
128 
129  return (ei == table.end()) ? NULL : ei->second;
130 }
131 
132 
134 IniFile::addSection(const std::string &sectionName)
135 {
136  SectionTable::iterator i = table.find(sectionName);
137 
138  if (i != table.end()) {
139  return i->second;
140  }
141  else {
142  // new entry
143  Section *sec = new Section();
144  table[sectionName] = sec;
145  return sec;
146  }
147 }
148 
149 
151 IniFile::findSection(const std::string &sectionName) const
152 {
153  SectionTable::const_iterator i = table.find(sectionName);
154 
155  return (i == table.end()) ? NULL : i->second;
156 }
157 
158 
159 // Take string of the form "<section>:<parameter>=<value>" and add to
160 // database. Return true if successful, false if parse error.
161 bool
162 IniFile::add(const std::string &str)
163 {
164  // find ':'
165  std::string::size_type offset = str.find(':');
166  if (offset == std::string::npos) // no ':' found
167  return false;
168 
169  std::string sectionName = str.substr(0, offset);
170  std::string rest = str.substr(offset + 1);
171 
172  eat_white(sectionName);
173  Section *s = addSection(sectionName);
174 
175  return s->add(rest);
176 }
177 
178 bool
179 IniFile::load(std::istream &f)
180 {
181  Section *section = NULL;
182 
183  while (!f.eof()) {
184  f >> std::ws; // Eat whitespace
185  if (f.eof()) {
186  break;
187  }
188 
189  std::string line;
190  getline(f, line);
191  if (line.size() == 0)
192  continue;
193 
194  eat_end_white(line);
195  int last = line.size() - 1;
196 
197  if (line[0] == '[' && line[last] == ']') {
198  std::string sectionName = line.substr(1, last - 1);
199  eat_white(sectionName);
200  section = addSection(sectionName);
201  continue;
202  }
203 
204  if (section == NULL)
205  continue;
206 
207  if (!section->add(line))
208  return false;
209  }
210 
211  return true;
212 }
213 
214 bool
215 IniFile::find(const std::string &sectionName, const std::string &entryName,
216  std::string &value) const
217 {
218  Section *section = findSection(sectionName);
219  if (section == NULL)
220  return false;
221 
222  Entry *entry = section->findEntry(entryName);
223  if (entry == NULL)
224  return false;
225 
226  value = entry->getValue();
227 
228  return true;
229 }
230 
231 bool
232 IniFile::entryExists(const std::string &sectionName,
233  const std::string &entryName) const
234 {
235  Section *section = findSection(sectionName);
236 
237  if (!section)
238  return false;
239  else
240  return section->findEntry(entryName);
241 }
242 
243 bool
244 IniFile::sectionExists(const std::string &sectionName) const
245 {
246  return findSection(sectionName) != NULL;
247 }
248 
249 
250 bool
251 IniFile::Section::printUnreferenced(const std::string &sectionName)
252 {
253  bool unref = false;
254  bool search_unref_entries = false;
255  std::vector<std::string> unref_ok_entries;
256 
257  Entry *entry = findEntry("unref_entries_ok");
258  if (entry != NULL) {
259  tokenize(unref_ok_entries, entry->getValue(), ' ');
260  if (unref_ok_entries.size()) {
261  search_unref_entries = true;
262  }
263  }
264 
265  for (EntryTable::iterator ei = table.begin();
266  ei != table.end(); ++ei) {
267  const std::string &entryName = ei->first;
268  entry = ei->second;
269 
270  if (entryName == "unref_section_ok" ||
271  entryName == "unref_entries_ok")
272  {
273  continue;
274  }
275 
276  if (!entry->isReferenced()) {
277  if (search_unref_entries &&
278  (std::find(unref_ok_entries.begin(), unref_ok_entries.end(),
279  entryName) != unref_ok_entries.end()))
280  {
281  continue;
282  }
283 
284  std::cerr << "Parameter " << sectionName << ":" << entryName
285  << " not referenced." << std::endl;
286  unref = true;
287  }
288  }
289 
290  return unref;
291 }
292 
293 
294 void
296 {
297  for (SectionTable::const_iterator i = table.begin();
298  i != table.end(); ++i)
299  {
300  list.push_back((*i).first);
301  }
302 }
303 
304 bool
306 {
307  bool unref = false;
308 
309  for (SectionTable::iterator i = table.begin();
310  i != table.end(); ++i) {
311  const std::string &sectionName = i->first;
312  Section *section = i->second;
313 
314  if (!section->isReferenced()) {
315  if (section->findEntry("unref_section_ok") == NULL) {
316  std::cerr << "Section " << sectionName << " not referenced."
317  << std::endl;
318  unref = true;
319  }
320  }
321  else {
322  if (section->printUnreferenced(sectionName)) {
323  unref = true;
324  }
325  }
326  }
327 
328  return unref;
329 }
330 
331 
332 void
333 IniFile::Section::dump(const std::string &sectionName)
334 {
335  for (EntryTable::iterator ei = table.begin();
336  ei != table.end(); ++ei) {
337  std::cout << sectionName << ": " << (*ei).first << " => "
338  << (*ei).second->getValue() << "\n";
339  }
340 }
341 
342 void
344 {
345  for (SectionTable::iterator i = table.begin();
346  i != table.end(); ++i) {
347  i->second->dump(i->first);
348  }
349 }
350 
351 IniFile::Section::EntryTable::const_iterator
353 {
354  return table.begin();
355 }
356 
357 IniFile::Section::EntryTable::const_iterator
359 {
360  return table.end();
361 }
362 
363 void
364 IniFile::visitSection(const std::string &sectionName,
366 {
367  const auto& section = *table.at(sectionName);
368  for (const auto& pair : section) {
369  cb(pair.first, pair.second->getValue());
370  }
371 }
372 
373 } // namespace gem5
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
bool referenced
Has this entry been used?
Definition: inifile.hh:65
bool isReferenced()
Has this section been used?
Definition: inifile.hh:110
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
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
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
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
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
Declaration of IniFile object.
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 56 > f
Definition: pagetable.hh:53
unsigned int size_type
Definition: types.hh:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
Definition: str.cc:68
void eat_end_white(std::string &s)
Definition: str.hh:59
void eat_white(std::string &s)
Definition: str.hh:67
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