inifile.hh revision 10385
12SN/A/* 21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 322SN/A#ifndef __INIFILE_HH__ 332SN/A#define __INIFILE_HH__ 342SN/A 352SN/A#include <fstream> 362SN/A#include <list> 372SN/A#include <string> 382SN/A#include <vector> 392SN/A 4056SN/A#include "base/hashmap.hh" 412SN/A 42229SN/A/** 43229SN/A * @file 44229SN/A * Declaration of IniFile object. 45229SN/A * @todo Change comments to match documentation style. 46229SN/A */ 47160SN/A 48160SN/A/// 49160SN/A/// This class represents the contents of a ".ini" file. 50160SN/A/// 51160SN/A/// It's basically a two level lookup table: a set of named sections, 52160SN/A/// where each section is a set of key/value pairs. Section names, 53160SN/A/// keys, and values are all uninterpreted strings. 54160SN/A/// 552SN/Aclass IniFile 562SN/A{ 572SN/A protected: 58160SN/A 59160SN/A /// 60160SN/A /// A single key/value pair. 61160SN/A /// 622SN/A class Entry 632SN/A { 645543Ssaidi@eecs.umich.edu std::string value; ///< The entry value. 655543Ssaidi@eecs.umich.edu mutable bool referenced; ///< Has this entry been used? 662SN/A 672SN/A public: 68160SN/A /// Constructor. 692SN/A Entry(const std::string &v) 702SN/A : value(v), referenced(false) 712SN/A { 722SN/A } 732SN/A 74160SN/A /// Has this entry been used? 752SN/A bool isReferenced() { return referenced; } 762SN/A 77160SN/A /// Fetch the value. 782SN/A const std::string &getValue() const; 792SN/A 80160SN/A /// Set the value. 812SN/A void setValue(const std::string &v) { value = v; } 82160SN/A 83160SN/A /// Append the given string to the value. A space is inserted 84160SN/A /// between the existing value and the new value. Since this 85160SN/A /// operation is typically used with values that are 86160SN/A /// space-separated lists of tokens, this keeps the tokens 87160SN/A /// separate. 88160SN/A void appendValue(const std::string &v) { value += " "; value += v; } 892SN/A }; 902SN/A 91160SN/A /// 92160SN/A /// A section. 93160SN/A /// 942SN/A class Section 952SN/A { 96160SN/A /// EntryTable type. Map of strings to Entry object pointers. 972SN/A typedef m5::hash_map<std::string, Entry *> EntryTable; 982SN/A 995543Ssaidi@eecs.umich.edu EntryTable table; ///< Table of entries. 1005543Ssaidi@eecs.umich.edu mutable bool referenced; ///< Has this section been used? 1012SN/A 1022SN/A public: 103160SN/A /// Constructor. 1042SN/A Section() 1052SN/A : table(), referenced(false) 1062SN/A { 1072SN/A } 1082SN/A 109160SN/A /// Has this section been used? 1102SN/A bool isReferenced() { return referenced; } 1112SN/A 112160SN/A /// Add an entry to the table. If an entry with the same name 113160SN/A /// already exists, the 'append' parameter is checked If true, 114160SN/A /// the new value will be appended to the existing entry. If 115160SN/A /// false, the new value will replace the existing entry. 116160SN/A void addEntry(const std::string &entryName, const std::string &value, 117160SN/A bool append); 118160SN/A 119160SN/A /// Add an entry to the table given a string assigment. 120160SN/A /// Assignment should be of the form "param=value" or 121160SN/A /// "param+=value" (for append). This funciton parses the 122160SN/A /// assignment statment and calls addEntry(). 123160SN/A /// @retval True for success, false if parse error. 124160SN/A bool add(const std::string &assignment); 125160SN/A 126160SN/A /// Find the entry with the given name. 127160SN/A /// @retval Pointer to the entry object, or NULL if none. 1282SN/A Entry *findEntry(const std::string &entryName) const; 1292SN/A 130160SN/A /// Print the unreferenced entries in this section to cerr. 131160SN/A /// Messages can be suppressed using "unref_section_ok" and 132160SN/A /// "unref_entries_ok". 133160SN/A /// @param sectionName Name of this section, for use in output message. 134160SN/A /// @retval True if any entries were printed. 1352SN/A bool printUnreferenced(const std::string §ionName); 136160SN/A 137160SN/A /// Print the contents of this section to cout (for debugging). 1382SN/A void dump(const std::string §ionName); 1392SN/A }; 1402SN/A 141160SN/A /// SectionTable type. Map of strings to Section object pointers. 142160SN/A typedef m5::hash_map<std::string, Section *> SectionTable; 1432SN/A 1442SN/A protected: 145160SN/A /// Hash of section names to Section object pointers. 146160SN/A SectionTable table; 1472SN/A 148160SN/A /// Look up section with the given name, creating a new section if 149160SN/A /// not found. 150160SN/A /// @retval Pointer to section object. 1512SN/A Section *addSection(const std::string §ionName); 152160SN/A 153160SN/A /// Look up section with the given name. 154160SN/A /// @retval Pointer to section object, or NULL if not found. 1552SN/A Section *findSection(const std::string §ionName) const; 1562SN/A 1572SN/A public: 158160SN/A /// Constructor. 1592SN/A IniFile(); 160160SN/A 161160SN/A /// Destructor. 1622SN/A ~IniFile(); 1632SN/A 1641046SN/A /// Load parameter settings from given istream. This is a helper 1651046SN/A /// function for load(string) and loadCPP(), which open a file 1661046SN/A /// and then pass it here. 1671046SN/A /// @retval True if successful, false if errors were encountered. 1681046SN/A bool load(std::istream &f); 1691046SN/A 170160SN/A /// Load the specified file. 171160SN/A /// Parameter settings found in the file will be merged with any 172160SN/A /// already defined in this object. 173160SN/A /// @param file The path of the file to load. 174160SN/A /// @retval True if successful, false if errors were encountered. 1752SN/A bool load(const std::string &file); 1762SN/A 177160SN/A /// Take string of the form "<section>:<parameter>=<value>" or 178160SN/A /// "<section>:<parameter>+=<value>" and add to database. 179160SN/A /// @retval True if successful, false if parse error. 1802SN/A bool add(const std::string &s); 1812SN/A 182160SN/A /// Find value corresponding to given section and entry names. 183160SN/A /// Value is returned by reference in 'value' param. 184160SN/A /// @retval True if found, false if not. 1852SN/A bool find(const std::string §ion, const std::string &entry, 1862SN/A std::string &value) const; 187160SN/A 188304SN/A /// Determine whether the named section exists in the .ini file. 189304SN/A /// Note that the 'Section' class is (intentionally) not public, 190304SN/A /// so all clients can do is get a bool that says whether there 191304SN/A /// are any values in that section or not. 192304SN/A /// @return True if the section exists. 193304SN/A bool sectionExists(const std::string §ion) const; 194304SN/A 19510385SAndrew.Bardsley@arm.com /// Push all section names into the given vector 19610385SAndrew.Bardsley@arm.com void getSectionNames(std::vector<std::string> &list) const; 19710385SAndrew.Bardsley@arm.com 198160SN/A /// Print unreferenced entries in object. Iteratively calls 199160SN/A /// printUnreferend() on all the constituent sections. 2002SN/A bool printUnreferenced(); 2012SN/A 202160SN/A /// Dump contents to cout. For debugging. 2032SN/A void dump(); 2042SN/A}; 2052SN/A 2062SN/A#endif // __INIFILE_HH__ 207