2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
37 #include "base/inifile.hh"
38 #include "base/str.hh"
47 SectionTable::iterator i
= table
.begin();
48 SectionTable::iterator end
= table
.end();
57 IniFile::load(const string
&file
)
59 ifstream
f(file
.c_str());
69 IniFile::Entry::getValue() const
77 IniFile::Section::addEntry(const std::string
&entryName
,
78 const std::string
&value
,
81 EntryTable::iterator ei
= table
.find(entryName
);
83 if (ei
== table
.end()) {
85 table
[entryName
] = new Entry(value
);
88 // append new reult to old entry
89 ei
->second
->appendValue(value
);
93 ei
->second
->setValue(value
);
99 IniFile::Section::add(const std::string
&assignment
)
101 string::size_type offset
= assignment
.find('=');
102 if (offset
== string::npos
) {
104 cerr
<< "Can't parse .ini line " << assignment
<< endl
;
108 // if "+=" rather than just "=" then append value
109 bool append
= (assignment
[offset
-1] == '+');
111 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
112 string value
= assignment
.substr(offset
+ 1);
114 eat_white(entryName
);
117 addEntry(entryName
, value
, append
);
123 IniFile::Section::findEntry(const std::string
&entryName
) const
127 EntryTable::const_iterator ei
= table
.find(entryName
);
129 return (ei
== table
.end()) ? NULL
: ei
->second
;
134 IniFile::addSection(const string
§ionName
)
136 SectionTable::iterator i
= table
.find(sectionName
);
138 if (i
!= table
.end()) {
143 Section
*sec
= new Section();
144 table
[sectionName
] = sec
;
151 IniFile::findSection(const string
§ionName
) const
153 SectionTable::const_iterator i
= table
.find(sectionName
);
155 return (i
== table
.end()) ? NULL
: i
->second
;
159 // Take string of the form "<section>:<parameter>=<value>" and add to
160 // database. Return true if successful, false if parse error.
162 IniFile::add(const string
&str
)
165 string::size_type offset
= str
.find(':');
166 if (offset
== string::npos
) // no ':' found
169 string sectionName
= str
.substr(0, offset
);
170 string rest
= str
.substr(offset
+ 1);
172 eat_white(sectionName
);
173 Section
*s
= addSection(sectionName
);
179 IniFile::load(istream
&f
)
181 Section
*section
= NULL
;
184 f
>> ws
; // Eat whitespace
191 if (line
.size() == 0)
195 int last
= line
.size() - 1;
197 if (line
[0] == '[' && line
[last
] == ']') {
198 string sectionName
= line
.substr(1, last
- 1);
199 eat_white(sectionName
);
200 section
= addSection(sectionName
);
207 if (!section
->add(line
))
215 IniFile::find(const string
§ionName
, const string
&entryName
,
218 Section
*section
= findSection(sectionName
);
222 Entry
*entry
= section
->findEntry(entryName
);
226 value
= entry
->getValue();
232 IniFile::sectionExists(const string
§ionName
) const
234 return findSection(sectionName
) != NULL
;
239 IniFile::Section::printUnreferenced(const string
§ionName
)
242 bool search_unref_entries
= false;
243 vector
<string
> unref_ok_entries
;
245 Entry
*entry
= findEntry("unref_entries_ok");
247 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
248 if (unref_ok_entries
.size()) {
249 search_unref_entries
= true;
253 for (EntryTable::iterator ei
= table
.begin();
254 ei
!= table
.end(); ++ei
) {
255 const string
&entryName
= ei
->first
;
256 Entry
*entry
= ei
->second
;
258 if (entryName
== "unref_section_ok" ||
259 entryName
== "unref_entries_ok")
264 if (!entry
->isReferenced()) {
265 if (search_unref_entries
&&
266 (std::find(unref_ok_entries
.begin(), unref_ok_entries
.end(),
267 entryName
) != unref_ok_entries
.end()))
272 cerr
<< "Parameter " << sectionName
<< ":" << entryName
273 << " not referenced." << endl
;
283 IniFile::printUnreferenced()
287 for (SectionTable::iterator i
= table
.begin();
288 i
!= table
.end(); ++i
) {
289 const string
§ionName
= i
->first
;
290 Section
*section
= i
->second
;
292 if (!section
->isReferenced()) {
293 if (section
->findEntry("unref_section_ok") == NULL
) {
294 cerr
<< "Section " << sectionName
<< " not referenced."
300 if (section
->printUnreferenced(sectionName
)) {
311 IniFile::Section::dump(const string
§ionName
)
313 for (EntryTable::iterator ei
= table
.begin();
314 ei
!= table
.end(); ++ei
) {
315 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
316 << (*ei
).second
->getValue() << "\n";
323 for (SectionTable::iterator i
= table
.begin();
324 i
!= table
.end(); ++i
) {
325 i
->second
->dump(i
->first
);