1/* 2 * Copyright (c) 2001-2005 The Regents of The University of Michigan 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.
| 1/* 2 * Copyright (c) 2001-2005 The Regents of The University of Michigan 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.
|
27 */ 28 29#ifndef __INIFILE_HH__ 30#define __INIFILE_HH__ 31 32#include <fstream> 33#include <list> 34#include <string> 35#include <vector> 36 37#include "base/hashmap.hh" 38 39/** 40 * @file 41 * Declaration of IniFile object. 42 * @todo Change comments to match documentation style. 43 */ 44 45/// 46/// This class represents the contents of a ".ini" file. 47/// 48/// It's basically a two level lookup table: a set of named sections, 49/// where each section is a set of key/value pairs. Section names, 50/// keys, and values are all uninterpreted strings. 51/// 52class IniFile 53{ 54 protected: 55 56 /// 57 /// A single key/value pair. 58 /// 59 class Entry 60 { 61 std::string value; ///< The entry value. 62 mutable bool referenced; ///< Has this entry been used? 63 64 public: 65 /// Constructor. 66 Entry(const std::string &v) 67 : value(v), referenced(false) 68 { 69 } 70 71 /// Has this entry been used? 72 bool isReferenced() { return referenced; } 73 74 /// Fetch the value. 75 const std::string &getValue() const; 76 77 /// Set the value. 78 void setValue(const std::string &v) { value = v; } 79 80 /// Append the given string to the value. A space is inserted 81 /// between the existing value and the new value. Since this 82 /// operation is typically used with values that are 83 /// space-separated lists of tokens, this keeps the tokens 84 /// separate. 85 void appendValue(const std::string &v) { value += " "; value += v; } 86 }; 87 88 /// 89 /// A section. 90 /// 91 class Section 92 { 93 /// EntryTable type. Map of strings to Entry object pointers. 94 typedef m5::hash_map<std::string, Entry *> EntryTable; 95 96 EntryTable table; ///< Table of entries. 97 mutable bool referenced; ///< Has this section been used? 98 99 public: 100 /// Constructor. 101 Section() 102 : table(), referenced(false) 103 { 104 } 105 106 /// Has this section been used? 107 bool isReferenced() { return referenced; } 108 109 /// Add an entry to the table. If an entry with the same name 110 /// already exists, the 'append' parameter is checked If true, 111 /// the new value will be appended to the existing entry. If 112 /// false, the new value will replace the existing entry. 113 void addEntry(const std::string &entryName, const std::string &value, 114 bool append); 115 116 /// Add an entry to the table given a string assigment. 117 /// Assignment should be of the form "param=value" or 118 /// "param+=value" (for append). This funciton parses the 119 /// assignment statment and calls addEntry(). 120 /// @retval True for success, false if parse error. 121 bool add(const std::string &assignment); 122 123 /// Find the entry with the given name. 124 /// @retval Pointer to the entry object, or NULL if none. 125 Entry *findEntry(const std::string &entryName) const; 126 127 /// Print the unreferenced entries in this section to cerr. 128 /// Messages can be suppressed using "unref_section_ok" and 129 /// "unref_entries_ok". 130 /// @param sectionName Name of this section, for use in output message. 131 /// @retval True if any entries were printed. 132 bool printUnreferenced(const std::string §ionName); 133 134 /// Print the contents of this section to cout (for debugging). 135 void dump(const std::string §ionName); 136 }; 137 138 /// SectionTable type. Map of strings to Section object pointers. 139 typedef m5::hash_map<std::string, Section *> SectionTable; 140 141 protected: 142 /// Hash of section names to Section object pointers. 143 SectionTable table; 144 145 /// Look up section with the given name, creating a new section if 146 /// not found. 147 /// @retval Pointer to section object. 148 Section *addSection(const std::string §ionName); 149 150 /// Look up section with the given name. 151 /// @retval Pointer to section object, or NULL if not found. 152 Section *findSection(const std::string §ionName) const; 153 154 public: 155 /// Constructor. 156 IniFile(); 157 158 /// Destructor. 159 ~IniFile(); 160 161 /// Load parameter settings from given istream. This is a helper 162 /// function for load(string) and loadCPP(), which open a file 163 /// and then pass it here. 164 /// @retval True if successful, false if errors were encountered. 165 bool load(std::istream &f); 166 167 /// Load the specified file, passing it through the C preprocessor. 168 /// Parameter settings found in the file will be merged with any 169 /// already defined in this object. 170 /// @param file The path of the file to load. 171 /// @param cppFlags Vector of extra flags to pass to cpp. 172 /// @retval True if successful, false if errors were encountered. 173 bool loadCPP(const std::string &file, std::vector<char *> &cppFlags); 174 175 /// Load the specified file. 176 /// Parameter settings found in the file will be merged with any 177 /// already defined in this object. 178 /// @param file The path of the file to load. 179 /// @retval True if successful, false if errors were encountered. 180 bool load(const std::string &file); 181 182 /// Take string of the form "<section>:<parameter>=<value>" or 183 /// "<section>:<parameter>+=<value>" and add to database. 184 /// @retval True if successful, false if parse error. 185 bool add(const std::string &s); 186 187 /// Find value corresponding to given section and entry names. 188 /// Value is returned by reference in 'value' param. 189 /// @retval True if found, false if not. 190 bool find(const std::string §ion, const std::string &entry, 191 std::string &value) const; 192 193 /// Determine whether the named section exists in the .ini file. 194 /// Note that the 'Section' class is (intentionally) not public, 195 /// so all clients can do is get a bool that says whether there 196 /// are any values in that section or not. 197 /// @return True if the section exists. 198 bool sectionExists(const std::string §ion) const; 199 200 /// Print unreferenced entries in object. Iteratively calls 201 /// printUnreferend() on all the constituent sections. 202 bool printUnreferenced(); 203 204 /// Dump contents to cout. For debugging. 205 void dump(); 206}; 207 208#endif // __INIFILE_HH__
| 30 */ 31 32#ifndef __INIFILE_HH__ 33#define __INIFILE_HH__ 34 35#include <fstream> 36#include <list> 37#include <string> 38#include <vector> 39 40#include "base/hashmap.hh" 41 42/** 43 * @file 44 * Declaration of IniFile object. 45 * @todo Change comments to match documentation style. 46 */ 47 48/// 49/// This class represents the contents of a ".ini" file. 50/// 51/// It's basically a two level lookup table: a set of named sections, 52/// where each section is a set of key/value pairs. Section names, 53/// keys, and values are all uninterpreted strings. 54/// 55class IniFile 56{ 57 protected: 58 59 /// 60 /// A single key/value pair. 61 /// 62 class Entry 63 { 64 std::string value; ///< The entry value. 65 mutable bool referenced; ///< Has this entry been used? 66 67 public: 68 /// Constructor. 69 Entry(const std::string &v) 70 : value(v), referenced(false) 71 { 72 } 73 74 /// Has this entry been used? 75 bool isReferenced() { return referenced; } 76 77 /// Fetch the value. 78 const std::string &getValue() const; 79 80 /// Set the value. 81 void setValue(const std::string &v) { value = v; } 82 83 /// Append the given string to the value. A space is inserted 84 /// between the existing value and the new value. Since this 85 /// operation is typically used with values that are 86 /// space-separated lists of tokens, this keeps the tokens 87 /// separate. 88 void appendValue(const std::string &v) { value += " "; value += v; } 89 }; 90 91 /// 92 /// A section. 93 /// 94 class Section 95 { 96 /// EntryTable type. Map of strings to Entry object pointers. 97 typedef m5::hash_map<std::string, Entry *> EntryTable; 98 99 EntryTable table; ///< Table of entries. 100 mutable bool referenced; ///< Has this section been used? 101 102 public: 103 /// Constructor. 104 Section() 105 : table(), referenced(false) 106 { 107 } 108 109 /// Has this section been used? 110 bool isReferenced() { return referenced; } 111 112 /// Add an entry to the table. If an entry with the same name 113 /// already exists, the 'append' parameter is checked If true, 114 /// the new value will be appended to the existing entry. If 115 /// false, the new value will replace the existing entry. 116 void addEntry(const std::string &entryName, const std::string &value, 117 bool append); 118 119 /// Add an entry to the table given a string assigment. 120 /// Assignment should be of the form "param=value" or 121 /// "param+=value" (for append). This funciton parses the 122 /// assignment statment and calls addEntry(). 123 /// @retval True for success, false if parse error. 124 bool add(const std::string &assignment); 125 126 /// Find the entry with the given name. 127 /// @retval Pointer to the entry object, or NULL if none. 128 Entry *findEntry(const std::string &entryName) const; 129 130 /// Print the unreferenced entries in this section to cerr. 131 /// Messages can be suppressed using "unref_section_ok" and 132 /// "unref_entries_ok". 133 /// @param sectionName Name of this section, for use in output message. 134 /// @retval True if any entries were printed. 135 bool printUnreferenced(const std::string §ionName); 136 137 /// Print the contents of this section to cout (for debugging). 138 void dump(const std::string §ionName); 139 }; 140 141 /// SectionTable type. Map of strings to Section object pointers. 142 typedef m5::hash_map<std::string, Section *> SectionTable; 143 144 protected: 145 /// Hash of section names to Section object pointers. 146 SectionTable table; 147 148 /// Look up section with the given name, creating a new section if 149 /// not found. 150 /// @retval Pointer to section object. 151 Section *addSection(const std::string §ionName); 152 153 /// Look up section with the given name. 154 /// @retval Pointer to section object, or NULL if not found. 155 Section *findSection(const std::string §ionName) const; 156 157 public: 158 /// Constructor. 159 IniFile(); 160 161 /// Destructor. 162 ~IniFile(); 163 164 /// Load parameter settings from given istream. This is a helper 165 /// function for load(string) and loadCPP(), which open a file 166 /// and then pass it here. 167 /// @retval True if successful, false if errors were encountered. 168 bool load(std::istream &f); 169 170 /// Load the specified file, passing it through the C preprocessor. 171 /// Parameter settings found in the file will be merged with any 172 /// already defined in this object. 173 /// @param file The path of the file to load. 174 /// @param cppFlags Vector of extra flags to pass to cpp. 175 /// @retval True if successful, false if errors were encountered. 176 bool loadCPP(const std::string &file, std::vector<char *> &cppFlags); 177 178 /// Load the specified file. 179 /// Parameter settings found in the file will be merged with any 180 /// already defined in this object. 181 /// @param file The path of the file to load. 182 /// @retval True if successful, false if errors were encountered. 183 bool load(const std::string &file); 184 185 /// Take string of the form "<section>:<parameter>=<value>" or 186 /// "<section>:<parameter>+=<value>" and add to database. 187 /// @retval True if successful, false if parse error. 188 bool add(const std::string &s); 189 190 /// Find value corresponding to given section and entry names. 191 /// Value is returned by reference in 'value' param. 192 /// @retval True if found, false if not. 193 bool find(const std::string §ion, const std::string &entry, 194 std::string &value) const; 195 196 /// Determine whether the named section exists in the .ini file. 197 /// Note that the 'Section' class is (intentionally) not public, 198 /// so all clients can do is get a bool that says whether there 199 /// are any values in that section or not. 200 /// @return True if the section exists. 201 bool sectionExists(const std::string §ion) const; 202 203 /// Print unreferenced entries in object. Iteratively calls 204 /// printUnreferend() on all the constituent sections. 205 bool printUnreferenced(); 206 207 /// Dump contents to cout. For debugging. 208 void dump(); 209}; 210 211#endif // __INIFILE_HH__
|