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
32 #include "base/inifile.hh"
40 #include "base/str.hh"
49 SectionTable::iterator i
= table
.begin();
50 SectionTable::iterator end
= table
.end();
59 IniFile::load(const string
&file
)
61 ifstream
f(file
.c_str());
71 IniFile::Entry::getValue() const
79 IniFile::Section::addEntry(const std::string
&entryName
,
80 const std::string
&value
,
83 EntryTable::iterator ei
= table
.find(entryName
);
85 if (ei
== table
.end()) {
87 table
[entryName
] = new Entry(value
);
90 // append new reult to old entry
91 ei
->second
->appendValue(value
);
95 ei
->second
->setValue(value
);
101 IniFile::Section::add(const std::string
&assignment
)
103 string::size_type offset
= assignment
.find('=');
104 if (offset
== string::npos
) {
106 cerr
<< "Can't parse .ini line " << assignment
<< endl
;
110 // if "+=" rather than just "=" then append value
111 bool append
= (assignment
[offset
-1] == '+');
113 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
114 string value
= assignment
.substr(offset
+ 1);
116 eat_white(entryName
);
119 addEntry(entryName
, value
, append
);
125 IniFile::Section::findEntry(const std::string
&entryName
) const
129 EntryTable::const_iterator ei
= table
.find(entryName
);
131 return (ei
== table
.end()) ? NULL
: ei
->second
;
136 IniFile::addSection(const string
§ionName
)
138 SectionTable::iterator i
= table
.find(sectionName
);
140 if (i
!= table
.end()) {
145 Section
*sec
= new Section();
146 table
[sectionName
] = sec
;
153 IniFile::findSection(const string
§ionName
) const
155 SectionTable::const_iterator i
= table
.find(sectionName
);
157 return (i
== table
.end()) ? NULL
: i
->second
;
161 // Take string of the form "<section>:<parameter>=<value>" and add to
162 // database. Return true if successful, false if parse error.
164 IniFile::add(const string
&str
)
167 string::size_type offset
= str
.find(':');
168 if (offset
== string::npos
) // no ':' found
171 string sectionName
= str
.substr(0, offset
);
172 string rest
= str
.substr(offset
+ 1);
174 eat_white(sectionName
);
175 Section
*s
= addSection(sectionName
);
181 IniFile::load(istream
&f
)
183 Section
*section
= NULL
;
186 f
>> ws
; // Eat whitespace
193 if (line
.size() == 0)
197 int last
= line
.size() - 1;
199 if (line
[0] == '[' && line
[last
] == ']') {
200 string sectionName
= line
.substr(1, last
- 1);
201 eat_white(sectionName
);
202 section
= addSection(sectionName
);
209 if (!section
->add(line
))
217 IniFile::find(const string
§ionName
, const string
&entryName
,
220 Section
*section
= findSection(sectionName
);
224 Entry
*entry
= section
->findEntry(entryName
);
228 value
= entry
->getValue();
234 IniFile::entryExists(const string
§ionName
, const string
&entryName
) const
236 Section
*section
= findSection(sectionName
);
241 return section
->findEntry(entryName
);
245 IniFile::sectionExists(const string
§ionName
) const
247 return findSection(sectionName
) != NULL
;
252 IniFile::Section::printUnreferenced(const string
§ionName
)
255 bool search_unref_entries
= false;
256 vector
<string
> unref_ok_entries
;
258 Entry
*entry
= findEntry("unref_entries_ok");
260 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
261 if (unref_ok_entries
.size()) {
262 search_unref_entries
= true;
266 for (EntryTable::iterator ei
= table
.begin();
267 ei
!= table
.end(); ++ei
) {
268 const string
&entryName
= ei
->first
;
271 if (entryName
== "unref_section_ok" ||
272 entryName
== "unref_entries_ok")
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()))
285 cerr
<< "Parameter " << sectionName
<< ":" << entryName
286 << " not referenced." << endl
;
296 IniFile::getSectionNames(vector
<string
> &list
) const
298 for (SectionTable::const_iterator i
= table
.begin();
299 i
!= table
.end(); ++i
)
301 list
.push_back((*i
).first
);
306 IniFile::printUnreferenced()
310 for (SectionTable::iterator i
= table
.begin();
311 i
!= table
.end(); ++i
) {
312 const string
§ionName
= i
->first
;
313 Section
*section
= i
->second
;
315 if (!section
->isReferenced()) {
316 if (section
->findEntry("unref_section_ok") == NULL
) {
317 cerr
<< "Section " << sectionName
<< " not referenced."
323 if (section
->printUnreferenced(sectionName
)) {
334 IniFile::Section::dump(const string
§ionName
)
336 for (EntryTable::iterator ei
= table
.begin();
337 ei
!= table
.end(); ++ei
) {
338 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
339 << (*ei
).second
->getValue() << "\n";
346 for (SectionTable::iterator i
= table
.begin();
347 i
!= table
.end(); ++i
) {
348 i
->second
->dump(i
->first
);