Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

ConfigSection.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell ConfigSection
00006 
00007    File     : ConfigSection.cpp
00008    Version  : $Revision: 1.8 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/ConfigSection.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 "ConfigSection.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: ConfigSection.cpp,v 1.8 2002/05/28 12:35:41 darkeye Exp $";
00053 
00054 
00055 /*------------------------------------------------------------------------------
00056  *  string containing all white space characters
00057  *----------------------------------------------------------------------------*/
00058 #define WHITE_SPACE_STR      " \t"
00059 
00060 
00061 /* ===============================================  local function prototypes */
00062 
00063 
00064 /* =============================================================  module code */
00065 
00066 /*------------------------------------------------------------------------------
00067  *  Add a key / value pair
00068  *----------------------------------------------------------------------------*/
00069 bool
00070 ConfigSection :: add (   const char    * key,
00071                          const char    * value )            throw ( Exception )
00072 {
00073     if ( !key || !value ) {
00074         throw Exception( __FILE__, __LINE__, "no key or value");
00075     }
00076 
00077     std::pair<const std::string, std::string>   element( key, value);
00078     std::pair<TableType::iterator, bool>        res;
00079 
00080     res = table.insert( element);
00081 
00082     return res.second;
00083 }
00084         
00085         
00086 /*------------------------------------------------------------------------------
00087  *  Get a value for a key
00088  *----------------------------------------------------------------------------*/
00089 const char *
00090 ConfigSection :: get (  const char    * key ) const         throw ( Exception )
00091 {
00092     if ( !key ) {
00093         throw Exception( __FILE__, __LINE__, "no key");
00094     }
00095 
00096     TableType::const_iterator   it = table.find( key);
00097     if ( it == table.end() ) {
00098         return 0;
00099     }
00100     return it->second.c_str();
00101 }
00102 
00103 
00104 /*------------------------------------------------------------------------------
00105  *  Get a value for a key, in the key does not exist, throw an exception
00106  *----------------------------------------------------------------------------*/
00107 const char *
00108 ConfigSection :: getForSure (   const char    * key,
00109                                 const char    * message1,
00110                                 const char    * message2,
00111                                 int             code ) const
00112                                                         throw ( Exception )
00113 {
00114     const char      * value;
00115     
00116     if ( !(value = get( key)) ) {
00117         throw Exception( __FILE__, __LINE__, key, message1, message2, code);
00118     }
00119 
00120     return value;
00121 }
00122 
00123 
00124 /*------------------------------------------------------------------------------
00125  *  Add a configuration line
00126  *----------------------------------------------------------------------------*/
00127 bool
00128 ConfigSection :: addLine (  const char    * line )          throw ( Exception )
00129 {
00130     if ( !line ) {
00131         throw Exception( __FILE__, __LINE__, "no line");
00132     }
00133 
00134     std::string::size_type  ix;
00135     std::string             str( line);
00136 
00137     /* delete everything after the first # */
00138     if ( (ix = str.find( '#')) != str.npos ) {
00139         str.erase( ix);
00140     }
00141     /* eat up all white space from the front */
00142     if ( (ix = str.find_first_not_of( WHITE_SPACE_STR)) != str.npos ) {
00143         str.erase( 0, ix);
00144     }
00145     /* eat up all white space from the end */
00146     if ( (ix = str.find_last_not_of( WHITE_SPACE_STR)) != str.npos ) {
00147         str.erase( ix + 1);
00148     }
00149     if ( !str.length() ) {
00150         return true;
00151     }
00152 
00153     /* find the '=' delimiter between key and value */
00154     if ( (ix = str.find( '=')) == str.npos ) {
00155         return false;
00156     }
00157 
00158     std::string     key( str, 0, ix);
00159     std::string     value( str, ix + 1);
00160 
00161     /* eat up all white space from the front of value */
00162     if ( (ix = value.find_first_not_of( WHITE_SPACE_STR)) != value.npos ) {
00163         value.erase( 0, ix);
00164     }
00165     /* eat up all white space from the end of key */
00166     if ( (ix = key.find_last_not_of( WHITE_SPACE_STR)) != key.npos ) {
00167         key.erase( ix + 1);
00168     }
00169 
00170     /* now add the new key / value pair */
00171     return add( key.c_str(), value.c_str());
00172 }
00173 
00174 
00175 /*------------------------------------------------------------------------------
00176  
00177   $Source: /cvsroot/darkice/darkice/src/ConfigSection.cpp,v $
00178 
00179   $Log: ConfigSection.cpp,v $
00180   Revision 1.8  2002/05/28 12:35:41  darkeye
00181   code cleanup: compiles under gcc-c++ 3.1, using -pedantic option
00182 
00183   Revision 1.7  2001/10/19 09:20:09  darkeye
00184   config file now may contain tabs also as white space
00185 
00186   Revision 1.6  2001/09/09 11:26:43  darkeye
00187   full line comments skipped earlier: commens allowed before the first secion
00188 
00189   Revision 1.5  2001/09/05 20:11:15  darkeye
00190   removed dependency on locally stored SGI STL header files
00191   now compiler-supplied C++ library STL header files are used
00192   compiles under GNU C++ 3
00193   hash_map (an SGI extension to STL) replaced with map
00194   std:: namespace prefix added to all STL class references
00195 
00196   Revision 1.4  2001/08/30 17:25:56  darkeye
00197   renamed configure.h to config.h
00198 
00199   Revision 1.3  2000/11/13 18:46:50  darkeye
00200   added kdoc-style documentation comments
00201 
00202   Revision 1.2  2000/11/09 22:08:17  darkeye
00203   added function getForSure
00204 
00205   Revision 1.1  2000/11/08 17:29:50  darkeye
00206   added configuration file reader
00207 
00208   Revision 1.2  2000/11/05 14:08:27  darkeye
00209   changed builting to an automake / autoconf environment
00210 
00211   Revision 1.1.1.1  2000/11/05 10:05:49  darkeye
00212   initial version
00213 
00214   
00215 ------------------------------------------------------------------------------*/
00216 

Generated on Fri May 19 15:36:48 2006 for DarkIce by  doxygen 1.4.4