00001 /*------------------------------------------------------------------------------ 00002 00003 Copyright (c) 2000 Tyrell Corporation. All rights reserved. 00004 00005 Tyrell Config 00006 00007 File : DarkIceConfig.cpp 00008 Version : $Revision: 1.1 $ 00009 Author : $Author: darkeye $ 00010 Location : $Source: /cvsroot/darkice/darkice/src/DarkIceConfig.cpp,v $ 00011 00012 Copyright notice: 00013 00014 This program is free software; you can redistribute it and/or 00015 modify it under the terms of the GNU General Public License 00016 as published by the Free Software Foundation; either version 2 00017 of the License, or (at your option) any later version. 00018 00019 This program is distributed in the hope that it will be useful, 00020 but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License 00025 along with this program; if not, write to the Free Software 00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00027 00028 ------------------------------------------------------------------------------*/ 00029 00030 /* ============================================================ include files */ 00031 00032 #ifdef HAVE_CONFIG_H 00033 #include "config.h" 00034 #endif 00035 00036 #include <iterator> 00037 00038 #include <iostream> 00039 00040 00041 #include "DarkIceConfig.h" 00042 00043 00044 /* =================================================== local data structures */ 00045 00046 00047 /* ================================================ local constants & macros */ 00048 00049 /*------------------------------------------------------------------------------ 00050 * File identity 00051 *----------------------------------------------------------------------------*/ 00052 static const char fileid[] = "$Id: DarkIceConfig.cpp,v 1.1 2005/04/04 08:36:17 darkeye Exp $"; 00053 00054 00055 /*------------------------------------------------------------------------------ 00056 * Max line size 00057 *----------------------------------------------------------------------------*/ 00058 #define LINE_SIZE 256 00059 00060 00061 /*------------------------------------------------------------------------------ 00062 * string containing all white space characters 00063 *----------------------------------------------------------------------------*/ 00064 #define WHITE_SPACE_STR " \t" 00065 00066 00067 /* =============================================== local function prototypes */ 00068 00069 00070 /* ============================================================= module code */ 00071 00072 /*------------------------------------------------------------------------------ 00073 * Get a value for a key 00074 *----------------------------------------------------------------------------*/ 00075 const ConfigSection * 00076 Config :: get ( const char * key ) const throw ( Exception ) 00077 { 00078 if ( !key ) { 00079 throw Exception( __FILE__, __LINE__, "no key"); 00080 } 00081 00082 TableType::const_iterator it = table.find( key); 00083 if ( it == table.end() ) { 00084 return 0; 00085 } 00086 return &(it->second); 00087 } 00088 00089 00090 /*------------------------------------------------------------------------------ 00091 * Add a configuration line 00092 *----------------------------------------------------------------------------*/ 00093 bool 00094 Config :: addLine ( const char * line ) throw ( Exception ) 00095 { 00096 if ( !line ) { 00097 throw Exception( __FILE__, __LINE__, "no line"); 00098 } 00099 00100 std::string::size_type ix; 00101 std::string str( line); 00102 00103 /* delete everything after the first # */ 00104 if ( (ix = str.find( '#')) != str.npos ) { 00105 str.erase( ix); 00106 } 00107 /* eat up all white space from the front */ 00108 if ( (ix = str.find_first_not_of( WHITE_SPACE_STR)) != str.npos ) { 00109 str.erase( 0, ix); 00110 } 00111 /* eat up all white space from the end */ 00112 if ( (ix = str.find_last_not_of( WHITE_SPACE_STR)) != str.npos ) { 00113 str.erase( ix + 1); 00114 } 00115 00116 if ( !str.length() ) { 00117 return true; 00118 } 00119 if ( str[0] == '[' && str[str.size()-1] == ']' ) { 00120 // a new section starts 00121 00122 std::string section( str, 1, str.size()-2); 00123 ConfigSection cSection; 00124 std::pair<const std::string, ConfigSection> 00125 element( section, cSection); 00126 std::pair<TableType::iterator, bool> res; 00127 00128 res = table.insert( element); 00129 00130 currentSection = section; 00131 return res.second; 00132 } else { 00133 // it's a line for the current section 00134 00135 TableType::iterator it = table.find( currentSection); 00136 if ( it == table.end() ) { 00137 throw Exception( __FILE__, __LINE__, "no current section"); 00138 } 00139 00140 return it->second.addLine( line); 00141 } 00142 } 00143 00144 00145 /*------------------------------------------------------------------------------ 00146 * Add a configuration line 00147 *----------------------------------------------------------------------------*/ 00148 void 00149 Config :: read ( std::istream & is ) throw ( Exception ) 00150 { 00151 char line[LINE_SIZE]; 00152 unsigned int num; 00153 00154 for ( num = 0; !is.fail() && !is.eof(); ++num ) { 00155 is.getline( line, LINE_SIZE); 00156 if ( is.eof() ) { 00157 break; 00158 } else if ( is.fail() ) { 00159 throw Exception( __FILE__, __LINE__, "line too long", num); 00160 } 00161 00162 addLine( line); 00163 } 00164 } 00165 00166 00167 00168 /*------------------------------------------------------------------------------ 00169 00170 $Source: /cvsroot/darkice/darkice/src/DarkIceConfig.cpp,v $ 00171 00172 $Log: DarkIceConfig.cpp,v $ 00173 Revision 1.1 2005/04/04 08:36:17 darkeye 00174 commited changes to enable Jack support 00175 thanks to Nicholas J. Humfrey, njh@ecs.soton.ac.uk 00176 00177 Revision 1.7 2002/05/28 12:35:41 darkeye 00178 code cleanup: compiles under gcc-c++ 3.1, using -pedantic option 00179 00180 Revision 1.6 2001/10/19 09:20:09 darkeye 00181 config file now may contain tabs also as white space 00182 00183 Revision 1.5 2001/09/09 11:26:43 darkeye 00184 full line comments skipped earlier: commens allowed before the first secion 00185 00186 Revision 1.4 2001/09/05 20:11:15 darkeye 00187 removed dependency on locally stored SGI STL header files 00188 now compiler-supplied C++ library STL header files are used 00189 compiles under GNU C++ 3 00190 hash_map (an SGI extension to STL) replaced with map 00191 std:: namespace prefix added to all STL class references 00192 00193 Revision 1.3 2001/08/30 17:25:56 darkeye 00194 renamed configure.h to config.h 00195 00196 Revision 1.2 2000/11/13 18:46:50 darkeye 00197 added kdoc-style documentation comments 00198 00199 Revision 1.1 2000/11/08 17:29:50 darkeye 00200 added configuration file reader 00201 00202 Revision 1.2 2000/11/05 14:08:27 darkeye 00203 changed builting to an automake / autoconf environment 00204 00205 Revision 1.1.1.1 2000/11/05 10:05:49 darkeye 00206 initial version 00207 00208 00209 ------------------------------------------------------------------------------*/ 00210