inifile.hh (2665:a124942bacb8) inifile.hh (5543:3af77710f397)
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 <vector>
39
40#include "base/hashmap.hh"
41
42/**
43 * @file
44 * Declaration of IniFile object.
45 * @todo Change comments to match documentation style.
46 */
47
48///
49/// This class represents the contents of a ".ini" file.
50///
51/// It's basically a two level lookup table: a set of named sections,
52/// where each section is a set of key/value pairs. Section names,
53/// keys, and values are all uninterpreted strings.
54///
55class IniFile
56{
57 protected:
58
59 ///
60 /// A single key/value pair.
61 ///
62 class Entry
63 {
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 <vector>
39
40#include "base/hashmap.hh"
41
42/**
43 * @file
44 * Declaration of IniFile object.
45 * @todo Change comments to match documentation style.
46 */
47
48///
49/// This class represents the contents of a ".ini" file.
50///
51/// It's basically a two level lookup table: a set of named sections,
52/// where each section is a set of key/value pairs. Section names,
53/// keys, and values are all uninterpreted strings.
54///
55class IniFile
56{
57 protected:
58
59 ///
60 /// A single key/value pair.
61 ///
62 class Entry
63 {
64 std::string value; ///< The entry value.
65 mutable bool referenced; ///< Has this entry been used?
64 std::string value; ///< The entry value.
65 mutable bool referenced; ///< Has this entry been used?
66
67 public:
68 /// Constructor.
69 Entry(const std::string &v)
70 : value(v), referenced(false)
71 {
72 }
73
74 /// Has this entry been used?
75 bool isReferenced() { return referenced; }
76
77 /// Fetch the value.
78 const std::string &getValue() const;
79
80 /// Set the value.
81 void setValue(const std::string &v) { value = v; }
82
83 /// Append the given string to the value. A space is inserted
84 /// between the existing value and the new value. Since this
85 /// operation is typically used with values that are
86 /// space-separated lists of tokens, this keeps the tokens
87 /// separate.
88 void appendValue(const std::string &v) { value += " "; value += v; }
89 };
90
91 ///
92 /// A section.
93 ///
94 class Section
95 {
96 /// EntryTable type. Map of strings to Entry object pointers.
97 typedef m5::hash_map<std::string, Entry *> EntryTable;
98
66
67 public:
68 /// Constructor.
69 Entry(const std::string &v)
70 : value(v), referenced(false)
71 {
72 }
73
74 /// Has this entry been used?
75 bool isReferenced() { return referenced; }
76
77 /// Fetch the value.
78 const std::string &getValue() const;
79
80 /// Set the value.
81 void setValue(const std::string &v) { value = v; }
82
83 /// Append the given string to the value. A space is inserted
84 /// between the existing value and the new value. Since this
85 /// operation is typically used with values that are
86 /// space-separated lists of tokens, this keeps the tokens
87 /// separate.
88 void appendValue(const std::string &v) { value += " "; value += v; }
89 };
90
91 ///
92 /// A section.
93 ///
94 class Section
95 {
96 /// EntryTable type. Map of strings to Entry object pointers.
97 typedef m5::hash_map<std::string, Entry *> EntryTable;
98
99 EntryTable table; ///< Table of entries.
100 mutable bool referenced; ///< Has this section been used?
99 EntryTable table; ///< Table of entries.
100 mutable bool referenced; ///< Has this section been used?
101
102 public:
103 /// Constructor.
104 Section()
105 : table(), referenced(false)
106 {
107 }
108
109 /// Has this section been used?
110 bool isReferenced() { return referenced; }
111
112 /// Add an entry to the table. If an entry with the same name
113 /// already exists, the 'append' parameter is checked If true,
114 /// the new value will be appended to the existing entry. If
115 /// false, the new value will replace the existing entry.
116 void addEntry(const std::string &entryName, const std::string &value,
117 bool append);
118
119 /// Add an entry to the table given a string assigment.
120 /// Assignment should be of the form "param=value" or
121 /// "param+=value" (for append). This funciton parses the
122 /// assignment statment and calls addEntry().
123 /// @retval True for success, false if parse error.
124 bool add(const std::string &assignment);
125
126 /// Find the entry with the given name.
127 /// @retval Pointer to the entry object, or NULL if none.
128 Entry *findEntry(const std::string &entryName) const;
129
130 /// Print the unreferenced entries in this section to cerr.
131 /// Messages can be suppressed using "unref_section_ok" and
132 /// "unref_entries_ok".
133 /// @param sectionName Name of this section, for use in output message.
134 /// @retval True if any entries were printed.
135 bool printUnreferenced(const std::string &sectionName);
136
137 /// Print the contents of this section to cout (for debugging).
138 void dump(const std::string &sectionName);
139 };
140
141 /// SectionTable type. Map of strings to Section object pointers.
142 typedef m5::hash_map<std::string, Section *> SectionTable;
143
144 protected:
145 /// Hash of section names to Section object pointers.
146 SectionTable table;
147
148 /// Look up section with the given name, creating a new section if
149 /// not found.
150 /// @retval Pointer to section object.
151 Section *addSection(const std::string &sectionName);
152
153 /// Look up section with the given name.
154 /// @retval Pointer to section object, or NULL if not found.
155 Section *findSection(const std::string &sectionName) const;
156
157 public:
158 /// Constructor.
159 IniFile();
160
161 /// Destructor.
162 ~IniFile();
163
164 /// Load parameter settings from given istream. This is a helper
165 /// function for load(string) and loadCPP(), which open a file
166 /// and then pass it here.
167 /// @retval True if successful, false if errors were encountered.
168 bool load(std::istream &f);
169
170 /// Load the specified file, passing it through the C preprocessor.
171 /// Parameter settings found in the file will be merged with any
172 /// already defined in this object.
173 /// @param file The path of the file to load.
174 /// @param cppFlags Vector of extra flags to pass to cpp.
175 /// @retval True if successful, false if errors were encountered.
176 bool loadCPP(const std::string &file, std::vector<char *> &cppFlags);
177
178 /// Load the specified file.
179 /// Parameter settings found in the file will be merged with any
180 /// already defined in this object.
181 /// @param file The path of the file to load.
182 /// @retval True if successful, false if errors were encountered.
183 bool load(const std::string &file);
184
185 /// Take string of the form "<section>:<parameter>=<value>" or
186 /// "<section>:<parameter>+=<value>" and add to database.
187 /// @retval True if successful, false if parse error.
188 bool add(const std::string &s);
189
190 /// Find value corresponding to given section and entry names.
191 /// Value is returned by reference in 'value' param.
192 /// @retval True if found, false if not.
193 bool find(const std::string &section, const std::string &entry,
194 std::string &value) const;
195
196 /// Determine whether the named section exists in the .ini file.
197 /// Note that the 'Section' class is (intentionally) not public,
198 /// so all clients can do is get a bool that says whether there
199 /// are any values in that section or not.
200 /// @return True if the section exists.
201 bool sectionExists(const std::string &section) const;
202
203 /// Print unreferenced entries in object. Iteratively calls
204 /// printUnreferend() on all the constituent sections.
205 bool printUnreferenced();
206
207 /// Dump contents to cout. For debugging.
208 void dump();
209};
210
211#endif // __INIFILE_HH__
101
102 public:
103 /// Constructor.
104 Section()
105 : table(), referenced(false)
106 {
107 }
108
109 /// Has this section been used?
110 bool isReferenced() { return referenced; }
111
112 /// Add an entry to the table. If an entry with the same name
113 /// already exists, the 'append' parameter is checked If true,
114 /// the new value will be appended to the existing entry. If
115 /// false, the new value will replace the existing entry.
116 void addEntry(const std::string &entryName, const std::string &value,
117 bool append);
118
119 /// Add an entry to the table given a string assigment.
120 /// Assignment should be of the form "param=value" or
121 /// "param+=value" (for append). This funciton parses the
122 /// assignment statment and calls addEntry().
123 /// @retval True for success, false if parse error.
124 bool add(const std::string &assignment);
125
126 /// Find the entry with the given name.
127 /// @retval Pointer to the entry object, or NULL if none.
128 Entry *findEntry(const std::string &entryName) const;
129
130 /// Print the unreferenced entries in this section to cerr.
131 /// Messages can be suppressed using "unref_section_ok" and
132 /// "unref_entries_ok".
133 /// @param sectionName Name of this section, for use in output message.
134 /// @retval True if any entries were printed.
135 bool printUnreferenced(const std::string &sectionName);
136
137 /// Print the contents of this section to cout (for debugging).
138 void dump(const std::string &sectionName);
139 };
140
141 /// SectionTable type. Map of strings to Section object pointers.
142 typedef m5::hash_map<std::string, Section *> SectionTable;
143
144 protected:
145 /// Hash of section names to Section object pointers.
146 SectionTable table;
147
148 /// Look up section with the given name, creating a new section if
149 /// not found.
150 /// @retval Pointer to section object.
151 Section *addSection(const std::string &sectionName);
152
153 /// Look up section with the given name.
154 /// @retval Pointer to section object, or NULL if not found.
155 Section *findSection(const std::string &sectionName) const;
156
157 public:
158 /// Constructor.
159 IniFile();
160
161 /// Destructor.
162 ~IniFile();
163
164 /// Load parameter settings from given istream. This is a helper
165 /// function for load(string) and loadCPP(), which open a file
166 /// and then pass it here.
167 /// @retval True if successful, false if errors were encountered.
168 bool load(std::istream &f);
169
170 /// Load the specified file, passing it through the C preprocessor.
171 /// Parameter settings found in the file will be merged with any
172 /// already defined in this object.
173 /// @param file The path of the file to load.
174 /// @param cppFlags Vector of extra flags to pass to cpp.
175 /// @retval True if successful, false if errors were encountered.
176 bool loadCPP(const std::string &file, std::vector<char *> &cppFlags);
177
178 /// Load the specified file.
179 /// Parameter settings found in the file will be merged with any
180 /// already defined in this object.
181 /// @param file The path of the file to load.
182 /// @retval True if successful, false if errors were encountered.
183 bool load(const std::string &file);
184
185 /// Take string of the form "<section>:<parameter>=<value>" or
186 /// "<section>:<parameter>+=<value>" and add to database.
187 /// @retval True if successful, false if parse error.
188 bool add(const std::string &s);
189
190 /// Find value corresponding to given section and entry names.
191 /// Value is returned by reference in 'value' param.
192 /// @retval True if found, false if not.
193 bool find(const std::string &section, const std::string &entry,
194 std::string &value) const;
195
196 /// Determine whether the named section exists in the .ini file.
197 /// Note that the 'Section' class is (intentionally) not public,
198 /// so all clients can do is get a bool that says whether there
199 /// are any values in that section or not.
200 /// @return True if the section exists.
201 bool sectionExists(const std::string &section) const;
202
203 /// Print unreferenced entries in object. Iteratively calls
204 /// printUnreferend() on all the constituent sections.
205 bool printUnreferenced();
206
207 /// Dump contents to cout. For debugging.
208 void dump();
209};
210
211#endif // __INIFILE_HH__