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
38 #include "base/inifile.hh"
39 #include "base/str.hh"
48 SectionTable::iterator i
= table
.begin();
49 SectionTable::iterator end
= table
.end();
58 IniFile::load(const string
&file
)
60 ifstream
f(file
.c_str());
70 IniFile::Entry::getValue() const
78 IniFile::Section::addEntry(const std::string
&entryName
,
79 const std::string
&value
,
82 EntryTable::iterator ei
= table
.find(entryName
);
84 if (ei
== table
.end()) {
86 table
[entryName
] = new Entry(value
);
89 // append new reult to old entry
90 ei
->second
->appendValue(value
);
94 ei
->second
->setValue(value
);
100 IniFile::Section::add(const std::string
&assignment
)
102 string::size_type offset
= assignment
.find('=');
103 if (offset
== string::npos
) {
105 cerr
<< "Can't parse .ini line " << assignment
<< endl
;
109 // if "+=" rather than just "=" then append value
110 bool append
= (assignment
[offset
-1] == '+');
112 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
113 string value
= assignment
.substr(offset
+ 1);
115 eat_white(entryName
);
118 addEntry(entryName
, value
, append
);
124 IniFile::Section::findEntry(const std::string
&entryName
) const
128 EntryTable::const_iterator ei
= table
.find(entryName
);
130 return (ei
== table
.end()) ? NULL
: ei
->second
;
135 IniFile::addSection(const string
§ionName
)
137 SectionTable::iterator i
= table
.find(sectionName
);
139 if (i
!= table
.end()) {
144 Section
*sec
= new Section();
145 table
[sectionName
] = sec
;
152 IniFile::findSection(const string
§ionName
) const
154 SectionTable::const_iterator i
= table
.find(sectionName
);
156 return (i
== table
.end()) ? NULL
: i
->second
;
160 // Take string of the form "<section>:<parameter>=<value>" and add to
161 // database. Return true if successful, false if parse error.
163 IniFile::add(const string
&str
)
166 string::size_type offset
= str
.find(':');
167 if (offset
== string::npos
) // no ':' found
170 string sectionName
= str
.substr(0, offset
);
171 string rest
= str
.substr(offset
+ 1);
173 eat_white(sectionName
);
174 Section
*s
= addSection(sectionName
);
180 IniFile::load(istream
&f
)
182 Section
*section
= NULL
;
185 f
>> ws
; // Eat whitespace
192 if (line
.size() == 0)
196 int last
= line
.size() - 1;
198 if (line
[0] == '[' && line
[last
] == ']') {
199 string sectionName
= line
.substr(1, last
- 1);
200 eat_white(sectionName
);
201 section
= addSection(sectionName
);
208 if (!section
->add(line
))
216 IniFile::find(const string
§ionName
, const string
&entryName
,
219 Section
*section
= findSection(sectionName
);
223 Entry
*entry
= section
->findEntry(entryName
);
227 value
= entry
->getValue();
233 IniFile::sectionExists(const string
§ionName
) const
235 return findSection(sectionName
) != NULL
;
240 IniFile::Section::printUnreferenced(const string
§ionName
)
243 bool search_unref_entries
= false;
244 vector
<string
> unref_ok_entries
;
246 Entry
*entry
= findEntry("unref_entries_ok");
248 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
249 if (unref_ok_entries
.size()) {
250 search_unref_entries
= true;
254 for (EntryTable::iterator ei
= table
.begin();
255 ei
!= table
.end(); ++ei
) {
256 const string
&entryName
= ei
->first
;
259 if (entryName
== "unref_section_ok" ||
260 entryName
== "unref_entries_ok")
265 if (!entry
->isReferenced()) {
266 if (search_unref_entries
&&
267 (std::find(unref_ok_entries
.begin(), unref_ok_entries
.end(),
268 entryName
) != unref_ok_entries
.end()))
273 cerr
<< "Parameter " << sectionName
<< ":" << entryName
274 << " not referenced." << endl
;
284 IniFile::printUnreferenced()
288 for (SectionTable::iterator i
= table
.begin();
289 i
!= table
.end(); ++i
) {
290 const string
§ionName
= i
->first
;
291 Section
*section
= i
->second
;
293 if (!section
->isReferenced()) {
294 if (section
->findEntry("unref_section_ok") == NULL
) {
295 cerr
<< "Section " << sectionName
<< " not referenced."
301 if (section
->printUnreferenced(sectionName
)) {
312 IniFile::Section::dump(const string
§ionName
)
314 for (EntryTable::iterator ei
= table
.begin();
315 ei
!= table
.end(); ++ei
) {
316 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
317 << (*ei
).second
->getValue() << "\n";
324 for (SectionTable::iterator i
= table
.begin();
325 i
!= table
.end(); ++i
) {
326 i
->second
->dump(i
->first
);