Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 1 | /* |
Steve Reinhardt | ad8b963 | 2005-06-05 05:16:00 -0400 | [diff] [blame] | 2 | * Copyright (c) 2001-2005 The Regents of The University of Michigan |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 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. |
| 15 | * |
| 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. |
Ali Saidi | cb0cf2d | 2006-05-31 19:26:56 -0400 | [diff] [blame] | 27 | * |
| 28 | * Authors: Nathan Binkert |
| 29 | * Steve Reinhardt |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 30 | */ |
| 31 | |
Brandon Potter | 7a8dda4 | 2016-11-09 14:27:37 -0600 | [diff] [blame] | 32 | #include "base/inifile.hh" |
| 33 | |
Andreas Hansson | b6aa6d5 | 2012-04-14 05:43:31 -0400 | [diff] [blame] | 34 | #include <algorithm> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 35 | #include <fstream> |
| 36 | #include <iostream> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 37 | #include <string> |
Nathan Binkert | 39a0556 | 2011-04-15 10:44:06 -0700 | [diff] [blame] | 38 | #include <vector> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 39 | |
Steve Reinhardt | 25693e9 | 2003-10-10 11:09:00 -0700 | [diff] [blame] | 40 | #include "base/str.hh" |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | IniFile::IniFile() |
| 45 | {} |
| 46 | |
| 47 | IniFile::~IniFile() |
| 48 | { |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 49 | SectionTable::iterator i = table.begin(); |
| 50 | SectionTable::iterator end = table.end(); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 51 | |
| 52 | while (i != end) { |
| 53 | delete (*i).second; |
| 54 | ++i; |
| 55 | } |
| 56 | } |
| 57 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 58 | bool |
| 59 | IniFile::load(const string &file) |
| 60 | { |
| 61 | ifstream f(file.c_str()); |
| 62 | |
| 63 | if (!f.is_open()) |
| 64 | return false; |
| 65 | |
| 66 | return load(f); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | const string & |
| 71 | IniFile::Entry::getValue() const |
| 72 | { |
| 73 | referenced = true; |
| 74 | return value; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void |
| 79 | IniFile::Section::addEntry(const std::string &entryName, |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 80 | const std::string &value, |
| 81 | bool append) |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 82 | { |
| 83 | EntryTable::iterator ei = table.find(entryName); |
| 84 | |
| 85 | if (ei == table.end()) { |
| 86 | // new entry |
| 87 | table[entryName] = new Entry(value); |
| 88 | } |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 89 | else if (append) { |
| 90 | // append new reult to old entry |
| 91 | ei->second->appendValue(value); |
| 92 | } |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 93 | else { |
| 94 | // override old entry |
| 95 | ei->second->setValue(value); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 100 | bool |
| 101 | IniFile::Section::add(const std::string &assignment) |
| 102 | { |
| 103 | string::size_type offset = assignment.find('='); |
Steve Reinhardt | 21c7ee1 | 2003-10-23 18:51:12 -0700 | [diff] [blame] | 104 | if (offset == string::npos) { |
| 105 | // no '=' found |
| 106 | cerr << "Can't parse .ini line " << assignment << endl; |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 107 | return false; |
Steve Reinhardt | 21c7ee1 | 2003-10-23 18:51:12 -0700 | [diff] [blame] | 108 | } |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 109 | |
| 110 | // if "+=" rather than just "=" then append value |
| 111 | bool append = (assignment[offset-1] == '+'); |
| 112 | |
| 113 | string entryName = assignment.substr(0, append ? offset-1 : offset); |
| 114 | string value = assignment.substr(offset + 1); |
| 115 | |
| 116 | eat_white(entryName); |
| 117 | eat_white(value); |
| 118 | |
| 119 | addEntry(entryName, value, append); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 124 | IniFile::Entry * |
| 125 | IniFile::Section::findEntry(const std::string &entryName) const |
| 126 | { |
| 127 | referenced = true; |
| 128 | |
| 129 | EntryTable::const_iterator ei = table.find(entryName); |
| 130 | |
| 131 | return (ei == table.end()) ? NULL : ei->second; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | IniFile::Section * |
| 136 | IniFile::addSection(const string §ionName) |
| 137 | { |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 138 | SectionTable::iterator i = table.find(sectionName); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 139 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 140 | if (i != table.end()) { |
| 141 | return i->second; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 142 | } |
| 143 | else { |
| 144 | // new entry |
| 145 | Section *sec = new Section(); |
| 146 | table[sectionName] = sec; |
| 147 | return sec; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | IniFile::Section * |
| 153 | IniFile::findSection(const string §ionName) const |
| 154 | { |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 155 | SectionTable::const_iterator i = table.find(sectionName); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 156 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 157 | return (i == table.end()) ? NULL : i->second; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | // Take string of the form "<section>:<parameter>=<value>" and add to |
| 162 | // database. Return true if successful, false if parse error. |
| 163 | bool |
| 164 | IniFile::add(const string &str) |
| 165 | { |
| 166 | // find ':' |
| 167 | string::size_type offset = str.find(':'); |
| 168 | if (offset == string::npos) // no ':' found |
| 169 | return false; |
| 170 | |
| 171 | string sectionName = str.substr(0, offset); |
| 172 | string rest = str.substr(offset + 1); |
| 173 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 174 | eat_white(sectionName); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 175 | Section *s = addSection(sectionName); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 176 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 177 | return s->add(rest); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool |
| 181 | IniFile::load(istream &f) |
| 182 | { |
| 183 | Section *section = NULL; |
| 184 | |
| 185 | while (!f.eof()) { |
| 186 | f >> ws; // Eat whitespace |
| 187 | if (f.eof()) { |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | string line; |
| 192 | getline(f, line); |
| 193 | if (line.size() == 0) |
| 194 | continue; |
| 195 | |
| 196 | eat_end_white(line); |
| 197 | int last = line.size() - 1; |
| 198 | |
| 199 | if (line[0] == '[' && line[last] == ']') { |
| 200 | string sectionName = line.substr(1, last - 1); |
| 201 | eat_white(sectionName); |
| 202 | section = addSection(sectionName); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | if (section == NULL) |
| 207 | continue; |
| 208 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 209 | if (!section->add(line)) |
| 210 | return false; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | bool |
| 217 | IniFile::find(const string §ionName, const string &entryName, |
| 218 | string &value) const |
| 219 | { |
| 220 | Section *section = findSection(sectionName); |
| 221 | if (section == NULL) |
| 222 | return false; |
| 223 | |
| 224 | Entry *entry = section->findEntry(entryName); |
| 225 | if (entry == NULL) |
| 226 | return false; |
| 227 | |
| 228 | value = entry->getValue(); |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool |
Andreas Sandberg | 18135ce | 2016-10-04 11:22:16 +0100 | [diff] [blame] | 234 | IniFile::entryExists(const string §ionName, const string &entryName) const |
| 235 | { |
| 236 | Section *section = findSection(sectionName); |
| 237 | |
| 238 | if (!section) |
| 239 | return false; |
| 240 | else |
| 241 | return section->findEntry(entryName); |
| 242 | } |
| 243 | |
| 244 | bool |
Steve Reinhardt | 2ac0543 | 2003-11-02 21:49:15 -0800 | [diff] [blame] | 245 | IniFile::sectionExists(const string §ionName) const |
| 246 | { |
| 247 | return findSection(sectionName) != NULL; |
| 248 | } |
| 249 | |
| 250 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 251 | bool |
| 252 | IniFile::Section::printUnreferenced(const string §ionName) |
| 253 | { |
| 254 | bool unref = false; |
| 255 | bool search_unref_entries = false; |
| 256 | vector<string> unref_ok_entries; |
| 257 | |
| 258 | Entry *entry = findEntry("unref_entries_ok"); |
| 259 | if (entry != NULL) { |
| 260 | tokenize(unref_ok_entries, entry->getValue(), ' '); |
| 261 | if (unref_ok_entries.size()) { |
| 262 | search_unref_entries = true; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | for (EntryTable::iterator ei = table.begin(); |
| 267 | ei != table.end(); ++ei) { |
| 268 | const string &entryName = ei->first; |
Andreas Hansson | c10098f | 2013-02-19 05:56:06 -0500 | [diff] [blame] | 269 | entry = ei->second; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 270 | |
| 271 | if (entryName == "unref_section_ok" || |
| 272 | entryName == "unref_entries_ok") |
| 273 | { |
| 274 | continue; |
| 275 | } |
| 276 | |
| 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())) |
| 281 | { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | cerr << "Parameter " << sectionName << ":" << entryName |
| 286 | << " not referenced." << endl; |
| 287 | unref = true; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return unref; |
| 292 | } |
| 293 | |
| 294 | |
Andrew Bardsley | b2c2e67 | 2014-09-20 17:17:47 -0400 | [diff] [blame] | 295 | void |
| 296 | IniFile::getSectionNames(vector<string> &list) const |
| 297 | { |
| 298 | for (SectionTable::const_iterator i = table.begin(); |
| 299 | i != table.end(); ++i) |
| 300 | { |
| 301 | list.push_back((*i).first); |
| 302 | } |
| 303 | } |
| 304 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 305 | bool |
| 306 | IniFile::printUnreferenced() |
| 307 | { |
| 308 | bool unref = false; |
| 309 | |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 310 | for (SectionTable::iterator i = table.begin(); |
| 311 | i != table.end(); ++i) { |
| 312 | const string §ionName = i->first; |
| 313 | Section *section = i->second; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 314 | |
| 315 | if (!section->isReferenced()) { |
| 316 | if (section->findEntry("unref_section_ok") == NULL) { |
| 317 | cerr << "Section " << sectionName << " not referenced." |
| 318 | << endl; |
| 319 | unref = true; |
| 320 | } |
| 321 | } |
| 322 | else { |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 323 | if (section->printUnreferenced(sectionName)) { |
| 324 | unref = true; |
| 325 | } |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | return unref; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | void |
| 334 | IniFile::Section::dump(const string §ionName) |
| 335 | { |
| 336 | for (EntryTable::iterator ei = table.begin(); |
| 337 | ei != table.end(); ++ei) { |
| 338 | cout << sectionName << ": " << (*ei).first << " => " |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 339 | << (*ei).second->getValue() << "\n"; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | IniFile::dump() |
| 345 | { |
Steve Reinhardt | 5159241 | 2003-10-21 21:24:34 -0700 | [diff] [blame] | 346 | for (SectionTable::iterator i = table.begin(); |
| 347 | i != table.end(); ++i) { |
| 348 | i->second->dump(i->first); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 349 | } |
| 350 | } |