2 * Copyright (c) 2003 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.
34 #include <sys/signal.h>
35 #include <sys/types.h>
46 #include <ext/stdio_filebuf.h>
52 #include "base/inifile.hh"
53 #include "base/str.hh"
62 ConfigTable::iterator i
= table
.begin();
63 ConfigTable::iterator end
= table
.end();
74 IniFile::loadCPP(const string
&file
, vector
<char *> &cppArgs
)
82 char tempfile
[] = "/tmp/configXXXXXX";
83 fd
[0] = fd
[1] = mkstemp(tempfile
);
92 char filename
[FILENAME_MAX
];
93 string::size_type i
= file
.copy(filename
, sizeof(filename
) - 1);
96 int arg_count
= cppArgs
.size();
98 char **args
= new char *[arg_count
+ 20];
101 args
[nextArg
++] = "g++";
102 args
[nextArg
++] = "-E";
103 args
[nextArg
++] = "-P";
104 args
[nextArg
++] = "-nostdinc";
105 args
[nextArg
++] = "-nostdinc++";
106 args
[nextArg
++] = "-x";
107 args
[nextArg
++] = "c++";
108 args
[nextArg
++] = "-undef";
110 for (int i
= 0; i
< arg_count
; i
++)
111 args
[nextArg
++] = cppArgs
[i
];
113 args
[nextArg
++] = filename
;
114 args
[nextArg
++] = NULL
;
116 close(STDOUT_FILENO
);
117 if (dup2(fd
[1], STDOUT_FILENO
) == -1)
126 waitpid(pid
, &retval
, 0);
128 // check for normal completion of CPP
129 if (!WIFEXITED(retval
) || WEXITSTATUS(retval
) != 0)
135 lseek(fd
[0], 0, SEEK_SET
);
141 using namespace __gnu_cxx
;
142 stdio_filebuf
<char> fbuf(fd
[0], ios_base::in
, true,
143 static_cast<stdio_filebuf
<char>::int_type
>(BUFSIZ
));
145 if (fbuf
.is_open()) {
165 IniFile::load(const string
&file
)
167 ifstream
f(file
.c_str());
177 IniFile::Entry::getValue() const
185 IniFile::Section::addEntry(const std::string
&entryName
,
186 const std::string
&value
)
188 EntryTable::iterator ei
= table
.find(entryName
);
190 if (ei
== table
.end()) {
192 table
[entryName
] = new Entry(value
);
195 // override old entry
196 ei
->second
->setValue(value
);
202 IniFile::Section::findEntry(const std::string
&entryName
) const
206 EntryTable::const_iterator ei
= table
.find(entryName
);
208 return (ei
== table
.end()) ? NULL
: ei
->second
;
213 IniFile::addSection(const string
§ionName
)
215 ConfigTable::iterator ci
= table
.find(sectionName
);
217 if (ci
!= table
.end()) {
222 Section
*sec
= new Section();
223 table
[sectionName
] = sec
;
230 IniFile::findSection(const string
§ionName
) const
232 ConfigTable::const_iterator ci
= table
.find(sectionName
);
234 return (ci
== table
.end()) ? NULL
: ci
->second
;
238 // Take string of the form "<section>:<parameter>=<value>" and add to
239 // database. Return true if successful, false if parse error.
241 IniFile::add(const string
&str
)
244 string::size_type offset
= str
.find(':');
245 if (offset
== string::npos
) // no ':' found
248 string sectionName
= str
.substr(0, offset
);
249 string rest
= str
.substr(offset
+ 1);
251 offset
= rest
.find('=');
252 if (offset
== string::npos
) // no '='found
255 string entryName
= rest
.substr(0, offset
);
256 string value
= rest
.substr(offset
+ 1);
258 eat_white(sectionName
);
259 eat_white(entryName
);
262 Section
*s
= addSection(sectionName
);
263 s
->addEntry(entryName
, value
);
269 IniFile::load(istream
&f
)
271 Section
*section
= NULL
;
274 f
>> ws
; // Eat whitespace
281 if (line
.size() == 0)
285 int last
= line
.size() - 1;
287 if (line
[0] == '[' && line
[last
] == ']') {
288 string sectionName
= line
.substr(1, last
- 1);
289 eat_white(sectionName
);
290 section
= addSection(sectionName
);
297 string::size_type offset
= line
.find('=');
298 string entryName
= line
.substr(0, offset
);
299 string value
= line
.substr(offset
+ 1);
301 eat_white(entryName
);
304 section
->addEntry(entryName
, value
);
311 IniFile::find(const string
§ionName
, const string
&entryName
,
314 Section
*section
= findSection(sectionName
);
318 Entry
*entry
= section
->findEntry(entryName
);
322 value
= entry
->getValue();
328 IniFile::findDefault(const string
&_section
, const string
&entry
,
331 string section
= _section
;
332 while (!find(section
, entry
, value
)) {
333 if (!find(section
, "default", section
))
342 IniFile::Section::printUnreferenced(const string
§ionName
)
345 bool search_unref_entries
= false;
346 vector
<string
> unref_ok_entries
;
348 Entry
*entry
= findEntry("unref_entries_ok");
350 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
351 if (unref_ok_entries
.size()) {
352 search_unref_entries
= true;
356 for (EntryTable::iterator ei
= table
.begin();
357 ei
!= table
.end(); ++ei
) {
358 const string
&entryName
= ei
->first
;
359 Entry
*entry
= ei
->second
;
361 if (entryName
== "unref_section_ok" ||
362 entryName
== "unref_entries_ok")
367 if (!entry
->isReferenced()) {
368 if (search_unref_entries
&&
369 (std::find(unref_ok_entries
.begin(), unref_ok_entries
.end(),
370 entryName
) != unref_ok_entries
.end()))
375 cerr
<< "Parameter " << sectionName
<< ":" << entryName
376 << " not referenced." << endl
;
386 IniFile::printUnreferenced()
390 for (ConfigTable::iterator ci
= table
.begin();
391 ci
!= table
.end(); ++ci
) {
392 const string
§ionName
= ci
->first
;
393 Section
*section
= ci
->second
;
395 if (!section
->isReferenced()) {
396 if (section
->findEntry("unref_section_ok") == NULL
) {
397 cerr
<< "Section " << sectionName
<< " not referenced."
403 if (section
->printUnreferenced(sectionName
)) {
414 IniFile::Section::dump(const string
§ionName
)
416 for (EntryTable::iterator ei
= table
.begin();
417 ei
!= table
.end(); ++ei
) {
418 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
419 << (*ei
).second
<< "\n";
426 for (ConfigTable::iterator ci
= table
.begin();
427 ci
!= table
.end(); ++ci
) {
428 ci
->second
->dump(ci
->first
);