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 SectionTable::iterator i
= table
.begin();
63 SectionTable::iterator end
= table
.end();
74 IniFile::loadCPP(const string
&file
, vector
<char *> &cppArgs
)
78 // Open the file just to verify that we can. Otherwise if the
79 // file doesn't exist or has bad permissions the user will get
80 // confusing errors from cpp/g++.
81 ifstream
tmpf(file
.c_str());
92 char tempfile
[] = "/tmp/configXXXXXX";
93 fd
[0] = fd
[1] = mkstemp(tempfile
);
102 char filename
[FILENAME_MAX
];
103 string::size_type i
= file
.copy(filename
, sizeof(filename
) - 1);
106 int arg_count
= cppArgs
.size();
108 char **args
= new char *[arg_count
+ 20];
111 args
[nextArg
++] = "g++";
112 args
[nextArg
++] = "-E";
113 args
[nextArg
++] = "-P";
114 args
[nextArg
++] = "-nostdinc";
115 args
[nextArg
++] = "-nostdinc++";
116 args
[nextArg
++] = "-x";
117 args
[nextArg
++] = "c++";
118 args
[nextArg
++] = "-undef";
120 for (int i
= 0; i
< arg_count
; i
++)
121 args
[nextArg
++] = cppArgs
[i
];
123 args
[nextArg
++] = filename
;
124 args
[nextArg
++] = NULL
;
126 close(STDOUT_FILENO
);
127 if (dup2(fd
[1], STDOUT_FILENO
) == -1)
136 waitpid(pid
, &retval
, 0);
138 // check for normal completion of CPP
139 if (!WIFEXITED(retval
) || WEXITSTATUS(retval
) != 0)
145 lseek(fd
[0], 0, SEEK_SET
);
151 using namespace __gnu_cxx
;
152 stdio_filebuf
<char> fbuf(fd
[0], ios_base::in
, true,
153 static_cast<stdio_filebuf
<char>::int_type
>(BUFSIZ
));
155 if (fbuf
.is_open()) {
175 IniFile::load(const string
&file
)
177 ifstream
f(file
.c_str());
187 IniFile::Entry::getValue() const
195 IniFile::Section::addEntry(const std::string
&entryName
,
196 const std::string
&value
,
199 EntryTable::iterator ei
= table
.find(entryName
);
201 if (ei
== table
.end()) {
203 table
[entryName
] = new Entry(value
);
206 // append new reult to old entry
207 ei
->second
->appendValue(value
);
210 // override old entry
211 ei
->second
->setValue(value
);
217 IniFile::Section::add(const std::string
&assignment
)
219 string::size_type offset
= assignment
.find('=');
220 if (offset
== string::npos
) // no '=' found
223 // if "+=" rather than just "=" then append value
224 bool append
= (assignment
[offset
-1] == '+');
226 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
227 string value
= assignment
.substr(offset
+ 1);
229 eat_white(entryName
);
232 addEntry(entryName
, value
, append
);
238 IniFile::Section::findEntry(const std::string
&entryName
) const
242 EntryTable::const_iterator ei
= table
.find(entryName
);
244 return (ei
== table
.end()) ? NULL
: ei
->second
;
249 IniFile::addSection(const string
§ionName
)
251 SectionTable::iterator i
= table
.find(sectionName
);
253 if (i
!= table
.end()) {
258 Section
*sec
= new Section();
259 table
[sectionName
] = sec
;
266 IniFile::findSection(const string
§ionName
) const
268 SectionTable::const_iterator i
= table
.find(sectionName
);
270 return (i
== table
.end()) ? NULL
: i
->second
;
274 // Take string of the form "<section>:<parameter>=<value>" and add to
275 // database. Return true if successful, false if parse error.
277 IniFile::add(const string
&str
)
280 string::size_type offset
= str
.find(':');
281 if (offset
== string::npos
) // no ':' found
284 string sectionName
= str
.substr(0, offset
);
285 string rest
= str
.substr(offset
+ 1);
287 eat_white(sectionName
);
288 Section
*s
= addSection(sectionName
);
294 IniFile::load(istream
&f
)
296 Section
*section
= NULL
;
299 f
>> ws
; // Eat whitespace
306 if (line
.size() == 0)
310 int last
= line
.size() - 1;
312 if (line
[0] == '[' && line
[last
] == ']') {
313 string sectionName
= line
.substr(1, last
- 1);
314 eat_white(sectionName
);
315 section
= addSection(sectionName
);
322 if (!section
->add(line
))
330 IniFile::find(const string
§ionName
, const string
&entryName
,
333 Section
*section
= findSection(sectionName
);
337 Entry
*entry
= section
->findEntry(entryName
);
341 value
= entry
->getValue();
347 IniFile::findDefault(const string
&_section
, const string
&entry
,
350 string section
= _section
;
351 while (!find(section
, entry
, value
)) {
352 if (!find(section
, "default", section
))
361 IniFile::Section::printUnreferenced(const string
§ionName
)
364 bool search_unref_entries
= false;
365 vector
<string
> unref_ok_entries
;
367 Entry
*entry
= findEntry("unref_entries_ok");
369 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
370 if (unref_ok_entries
.size()) {
371 search_unref_entries
= true;
375 for (EntryTable::iterator ei
= table
.begin();
376 ei
!= table
.end(); ++ei
) {
377 const string
&entryName
= ei
->first
;
378 Entry
*entry
= ei
->second
;
380 if (entryName
== "unref_section_ok" ||
381 entryName
== "unref_entries_ok")
386 if (!entry
->isReferenced()) {
387 if (search_unref_entries
&&
388 (std::find(unref_ok_entries
.begin(), unref_ok_entries
.end(),
389 entryName
) != unref_ok_entries
.end()))
394 cerr
<< "Parameter " << sectionName
<< ":" << entryName
395 << " not referenced." << endl
;
405 IniFile::printUnreferenced()
409 for (SectionTable::iterator i
= table
.begin();
410 i
!= table
.end(); ++i
) {
411 const string
§ionName
= i
->first
;
412 Section
*section
= i
->second
;
414 if (!section
->isReferenced()) {
415 if (section
->findEntry("unref_section_ok") == NULL
) {
416 cerr
<< "Section " << sectionName
<< " not referenced."
422 if (section
->printUnreferenced(sectionName
)) {
433 IniFile::Section::dump(const string
§ionName
)
435 for (EntryTable::iterator ei
= table
.begin();
436 ei
!= table
.end(); ++ei
) {
437 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
438 << (*ei
).second
->getValue() << "\n";
445 for (SectionTable::iterator i
= table
.begin();
446 i
!= table
.end(); ++i
) {
447 i
->second
->dump(i
->first
);