2 * Copyright (c) 2001-2004 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.
32 #include <sys/signal.h>
33 #include <sys/types.h>
48 #include "base/inifile.hh"
49 #include "base/str.hh"
58 SectionTable::iterator i
= table
.begin();
59 SectionTable::iterator end
= table
.end();
70 IniFile::loadCPP(const string
&file
, vector
<char *> &cppArgs
)
72 // Open the file just to verify that we can. Otherwise if the
73 // file doesn't exist or has bad permissions the user will get
74 // confusing errors from cpp/g++.
75 ifstream
tmpf(file
.c_str());
82 char *cfile
= strcpy(new char[file
.size() + 1], file
.c_str());
83 char *dir
= dirname(cfile
);
89 dir_arg
= new char[arg
.size() + 1];
90 strcpy(dir_arg
, arg
.c_str());
95 char tempfile
[] = "/tmp/configXXXXXX";
96 int tmp_fd
= mkstemp(tempfile
);
104 char filename
[FILENAME_MAX
];
105 string::size_type i
= file
.copy(filename
, sizeof(filename
) - 1);
108 int arg_count
= cppArgs
.size();
110 char **args
= new char *[arg_count
+ 20];
113 args
[nextArg
++] = "g++";
114 args
[nextArg
++] = "-E";
115 args
[nextArg
++] = "-P";
116 args
[nextArg
++] = "-nostdinc";
117 args
[nextArg
++] = "-nostdinc++";
118 args
[nextArg
++] = "-x";
119 args
[nextArg
++] = "c++";
120 args
[nextArg
++] = "-undef";
122 for (int i
= 0; i
< arg_count
; i
++)
123 args
[nextArg
++] = cppArgs
[i
];
126 args
[nextArg
++] = dir_arg
;
128 args
[nextArg
++] = filename
;
129 args
[nextArg
++] = NULL
;
131 close(STDOUT_FILENO
);
132 if (dup2(tmp_fd
, STDOUT_FILENO
) == -1)
141 waitpid(pid
, &retval
, 0);
145 // check for normal completion of CPP
146 if (!WIFEXITED(retval
) || WEXITSTATUS(retval
) != 0)
153 status
= load(tempfile
);
162 IniFile::load(const string
&file
)
164 ifstream
f(file
.c_str());
174 IniFile::Entry::getValue() const
182 IniFile::Section::addEntry(const std::string
&entryName
,
183 const std::string
&value
,
186 EntryTable::iterator ei
= table
.find(entryName
);
188 if (ei
== table
.end()) {
190 table
[entryName
] = new Entry(value
);
193 // append new reult to old entry
194 ei
->second
->appendValue(value
);
197 // override old entry
198 ei
->second
->setValue(value
);
204 IniFile::Section::add(const std::string
&assignment
)
206 string::size_type offset
= assignment
.find('=');
207 if (offset
== string::npos
) {
209 cerr
<< "Can't parse .ini line " << assignment
<< endl
;
213 // if "+=" rather than just "=" then append value
214 bool append
= (assignment
[offset
-1] == '+');
216 string entryName
= assignment
.substr(0, append
? offset
-1 : offset
);
217 string value
= assignment
.substr(offset
+ 1);
219 eat_white(entryName
);
222 addEntry(entryName
, value
, append
);
228 IniFile::Section::findEntry(const std::string
&entryName
) const
232 EntryTable::const_iterator ei
= table
.find(entryName
);
234 return (ei
== table
.end()) ? NULL
: ei
->second
;
239 IniFile::addSection(const string
§ionName
)
241 SectionTable::iterator i
= table
.find(sectionName
);
243 if (i
!= table
.end()) {
248 Section
*sec
= new Section();
249 table
[sectionName
] = sec
;
256 IniFile::findSection(const string
§ionName
) const
258 SectionTable::const_iterator i
= table
.find(sectionName
);
260 return (i
== table
.end()) ? NULL
: i
->second
;
264 // Take string of the form "<section>:<parameter>=<value>" and add to
265 // database. Return true if successful, false if parse error.
267 IniFile::add(const string
&str
)
270 string::size_type offset
= str
.find(':');
271 if (offset
== string::npos
) // no ':' found
274 string sectionName
= str
.substr(0, offset
);
275 string rest
= str
.substr(offset
+ 1);
277 eat_white(sectionName
);
278 Section
*s
= addSection(sectionName
);
284 IniFile::load(istream
&f
)
286 Section
*section
= NULL
;
289 f
>> ws
; // Eat whitespace
296 if (line
.size() == 0)
300 int last
= line
.size() - 1;
302 if (line
[0] == '[' && line
[last
] == ']') {
303 string sectionName
= line
.substr(1, last
- 1);
304 eat_white(sectionName
);
305 section
= addSection(sectionName
);
312 if (!section
->add(line
))
320 IniFile::find(const string
§ionName
, const string
&entryName
,
323 Section
*section
= findSection(sectionName
);
327 Entry
*entry
= section
->findEntry(entryName
);
331 value
= entry
->getValue();
337 IniFile::findDefault(const string
&_section
, const string
&entry
,
340 string section
= _section
;
341 while (!findAppend(section
, entry
, value
)) {
342 if (!find(section
, "default", section
)) {
351 IniFile::findAppend(const string
&_section
, const string
&entry
,
354 string section
= _section
;
360 if (find(section
, entry
, val
)) {
371 } while (find(section
, "append", section
));
378 IniFile::sectionExists(const string
§ionName
) const
380 return findSection(sectionName
) != NULL
;
385 IniFile::Section::printUnreferenced(const string
§ionName
)
388 bool search_unref_entries
= false;
389 vector
<string
> unref_ok_entries
;
391 Entry
*entry
= findEntry("unref_entries_ok");
393 tokenize(unref_ok_entries
, entry
->getValue(), ' ');
394 if (unref_ok_entries
.size()) {
395 search_unref_entries
= true;
399 for (EntryTable::iterator ei
= table
.begin();
400 ei
!= table
.end(); ++ei
) {
401 const string
&entryName
= ei
->first
;
402 Entry
*entry
= ei
->second
;
404 if (entryName
== "unref_section_ok" ||
405 entryName
== "unref_entries_ok")
410 if (!entry
->isReferenced()) {
411 if (search_unref_entries
&&
412 (std::find(unref_ok_entries
.begin(), unref_ok_entries
.end(),
413 entryName
) != unref_ok_entries
.end()))
418 cerr
<< "Parameter " << sectionName
<< ":" << entryName
419 << " not referenced." << endl
;
429 IniFile::printUnreferenced()
433 for (SectionTable::iterator i
= table
.begin();
434 i
!= table
.end(); ++i
) {
435 const string
§ionName
= i
->first
;
436 Section
*section
= i
->second
;
438 if (!section
->isReferenced()) {
439 if (section
->findEntry("unref_section_ok") == NULL
) {
440 cerr
<< "Section " << sectionName
<< " not referenced."
446 if (section
->printUnreferenced(sectionName
)) {
457 IniFile::Section::dump(const string
§ionName
)
459 for (EntryTable::iterator ei
= table
.begin();
460 ei
!= table
.end(); ++ei
) {
461 cout
<< sectionName
<< ": " << (*ei
).first
<< " => "
462 << (*ei
).second
->getValue() << "\n";
469 for (SectionTable::iterator i
= table
.begin();
470 i
!= table
.end(); ++i
) {
471 i
->second
->dump(i
->first
);