gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  * Authors: Nathan Binkert
29  * Steve Reinhardt
30  */
31 
32 #ifndef __INIFILE_HH__
33 #define __INIFILE_HH__
34 
35 #include <fstream>
36 #include <list>
37 #include <string>
38 #include <unordered_map>
39 #include <vector>
40 
47 class IniFile
55 {
56  protected:
57 
61  class Entry
62  {
63  std::string value;
64  mutable bool referenced;
65 
66  public:
68  Entry(const std::string &v)
69  : value(v), referenced(false)
70  {
71  }
72 
74  bool isReferenced() { return referenced; }
75 
77  const std::string &getValue() const;
78 
80  void setValue(const std::string &v) { value = v; }
81 
87  void appendValue(const std::string &v) { value += " "; value += v; }
88  };
89 
93  class Section
94  {
96  typedef std::unordered_map<std::string, Entry *> EntryTable;
97 
99  mutable bool referenced;
100 
101  public:
104  : table(), referenced(false)
105  {
106  }
107 
109  bool isReferenced() { return referenced; }
110 
115  void addEntry(const std::string &entryName, const std::string &value,
116  bool append);
117 
123  bool add(const std::string &assignment);
124 
127  Entry *findEntry(const std::string &entryName) const;
128 
134  bool printUnreferenced(const std::string &sectionName);
135 
137  void dump(const std::string &sectionName);
138  };
139 
141  typedef std::unordered_map<std::string, Section *> SectionTable;
142 
143  protected:
146 
150  Section *addSection(const std::string &sectionName);
151 
154  Section *findSection(const std::string &sectionName) const;
155 
156  public:
158  IniFile();
159 
161  ~IniFile();
162 
167  bool load(std::istream &f);
168 
174  bool load(const std::string &file);
175 
179  bool add(const std::string &s);
180 
184  bool find(const std::string &section, const std::string &entry,
185  std::string &value) const;
186 
190  bool entryExists(const std::string &section,
191  const std::string &entry) const;
192 
198  bool sectionExists(const std::string &section) const;
199 
201  void getSectionNames(std::vector<std::string> &list) const;
202 
205  bool printUnreferenced();
206 
208  void dump();
209 };
210 
211 #endif // __INIFILE_HH__
A section.
Definition: inifile.hh:93
Bitfield< 28 > v
Definition: miscregs.hh:1366
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
~IniFile()
Destructor.
Definition: inifile.cc:47
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
std::string value
The entry value.
Definition: inifile.hh:63
std::unordered_map< std::string, Entry * > EntryTable
EntryTable type. Map of strings to Entry object pointers.
Definition: inifile.hh:96
bool isReferenced()
Has this entry been used?
Definition: inifile.hh:74
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
bool referenced
Has this entry been used?
Definition: inifile.hh:64
Section()
Constructor.
Definition: inifile.hh:103
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.
std::unordered_map< std::string, Section * > SectionTable
SectionTable type. Map of strings to Section object pointers.
Definition: inifile.hh:141
const std::string & getValue() const
Fetch the value.
Definition: inifile.cc:71
SectionTable table
Hash of section names to Section object pointers.
Definition: inifile.hh:145
Section * findSection(const std::string &sectionName) const
Look up section with the given name.
Definition: inifile.cc:153
bool referenced
Has this section been used?
Definition: inifile.hh:99
EntryTable table
Table of entries.
Definition: inifile.hh:98
bool isReferenced()
Has this section been used?
Definition: inifile.hh:109
void appendValue(const std::string &v)
Append the given string to the value.
Definition: inifile.hh:87
Entry(const std::string &v)
Constructor.
Definition: inifile.hh:68
void setValue(const std::string &v)
Set the value.
Definition: inifile.hh:80
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
This class represents the contents of a ".ini" file.
Definition: inifile.hh:54
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