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

Generated on Fri Jun 9 2017 13:03:40 for gem5 by doxygen 1.8.6