From d63b1eacc4166e0319e83b7bc3162395b5cc05e5 Mon Sep 17 00:00:00 2001 From: darkeye Date: Wed, 8 Nov 2000 17:29:50 +0000 Subject: [PATCH] added configuration file reader --- darkice/trunk/src/Config.cpp | 186 ++++++++++++++++++++++++++++ darkice/trunk/src/Config.h | 164 ++++++++++++++++++++++++ darkice/trunk/src/ConfigSection.cpp | 174 ++++++++++++++++++++++++++ darkice/trunk/src/ConfigSection.h | 148 ++++++++++++++++++++++ darkice/trunk/src/DarkIce.cpp | 31 ++++- darkice/trunk/src/Makefile.am | 6 + darkice/trunk/src/main.cpp | 9 +- 7 files changed, 714 insertions(+), 4 deletions(-) create mode 100644 darkice/trunk/src/Config.cpp create mode 100644 darkice/trunk/src/Config.h create mode 100644 darkice/trunk/src/ConfigSection.cpp create mode 100644 darkice/trunk/src/ConfigSection.h diff --git a/darkice/trunk/src/Config.cpp b/darkice/trunk/src/Config.cpp new file mode 100644 index 0000000..c942149 --- /dev/null +++ b/darkice/trunk/src/Config.cpp @@ -0,0 +1,186 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2000 Tyrell Corporation. All rights reserved. + + Tyrell Config + + File : Config.cpp + Version : $Revision$ + Author : $Author$ + Location : $Source$ + + Abstract : + + A configuration file representation. The file is of the syntax: + + # this is a whole line comment + key = value + an ugly key name = long value # this end is a comment too + + also empty lines are ignored and all white space is removed + from the front and end of keys / values + + Copyright notice: + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include + + +#include "Config.h" + + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +/*------------------------------------------------------------------------------ + * File identity + *----------------------------------------------------------------------------*/ +static const char fileid[] = "$Id$"; + + +/*------------------------------------------------------------------------------ + * Max line size + *----------------------------------------------------------------------------*/ +#define LINE_SIZE 256 + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Get a value for a key + *----------------------------------------------------------------------------*/ +const ConfigSection * +Config :: get ( const char * key ) const throw ( Exception ) +{ + if ( !key ) { + throw Exception( __FILE__, __LINE__, "no key"); + } + + TableType::const_iterator it = table.find( key); + if ( it == table.end() ) { + return 0; + } + return &(it->second); +} + + +/*------------------------------------------------------------------------------ + * Add a configuration line + *----------------------------------------------------------------------------*/ +bool +Config :: addLine ( const char * line ) throw ( Exception ) +{ + if ( !line ) { + throw Exception( __FILE__, __LINE__, "no line"); + } + + string::size_type ix; + string str( line); + + /* delete everything after the first # */ + if ( (ix = str.find( '#')) != str.npos ) { + str.erase( ix); + } + /* eat up all white space from the front */ + if ( (ix = str.find_first_not_of( ' ')) != str.npos ) { + str.erase( 0, ix); + } + /* eat up all white space from the end */ + if ( (ix = str.find_last_not_of( ' ')) != str.npos ) { + str.erase( ix + 1); + } + + if ( str[0] == '[' && str[str.size()-1] == ']' ) { + // a new section starts + + string section( str, 1, str.size()-2); + ConfigSection cSection; + pair element( section, cSection); + pair res; + + res = table.insert( element); + + currentSection = section; + return res.second; + } else { + // it's a line for the current section + + TableType::iterator it = table.find( currentSection); + if ( it == table.end() ) { + throw Exception( __FILE__, __LINE__, "no current section"); + } + + return it->second.addLine( line); + } +} + + +/*------------------------------------------------------------------------------ + * Add a configuration line + *----------------------------------------------------------------------------*/ +void +Config :: read ( istream & is ) throw ( Exception ) +{ + char line[LINE_SIZE]; + unsigned int num; + + for ( num = 0; !is.fail() && !is.eof(); ++num ) { + is.getline( line, LINE_SIZE); + if ( is.eof() ) { + break; + } else if ( is.fail() ) { + throw Exception( __FILE__, __LINE__, "line too long", num); + } + + addLine( line); + } +} + + + +/*------------------------------------------------------------------------------ + + $Source$ + + $Log$ + Revision 1.1 2000/11/08 17:29:50 darkeye + added configuration file reader + + Revision 1.2 2000/11/05 14:08:27 darkeye + changed builting to an automake / autoconf environment + + Revision 1.1.1.1 2000/11/05 10:05:49 darkeye + initial version + + +------------------------------------------------------------------------------*/ + diff --git a/darkice/trunk/src/Config.h b/darkice/trunk/src/Config.h new file mode 100644 index 0000000..74fcc47 --- /dev/null +++ b/darkice/trunk/src/Config.h @@ -0,0 +1,164 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2000 Tyrell Corporation. All rights reserved. + + Tyrell Config + + File : Config.h + Version : $Revision$ + Author : $Author$ + Location : $Source$ + + Abstract : + + A configuration file representation. The file is of the syntax: + + [section1] + # this is a whole line comment + key = value + an ugly key name = long value # this end is a comment too + + [section2] + # this is a whole line comment in section 2 + key = value + an ugly key name = long value # this end is a comment too + + + also empty lines are ignored and all white space is removed + from the front and end of keys / values + + Copyright notice: + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. + +------------------------------------------------------------------------------*/ +#ifndef CONFIG_H +#define CONFIG_H + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#include +#include + +#include + +#include "Referable.h" +#include "ConfigSection.h" + + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/*------------------------------------------------------------------------------ + * + *----------------------------------------------------------------------------*/ +class Config : public virtual Referable +{ + private: + + typedef hash_map TableType; + TableType table; + + + string currentSection; + + + protected: + + + public: + + inline + Config ( void ) throw ( Exception ) + { + } + + + inline virtual + ~Config ( void ) throw ( Exception ) + { + } + + +/* + + inline + Config ( const Config & di ) throw ( Exception ) + { + } + + + inline Config & + operator= ( const Config * di ) throw ( Exception ) + { + } +*/ + + inline virtual void + reset ( void ) throw ( Exception ) + { + table.clear(); + currentSection = ""; + } + + + virtual bool + addLine ( const char * line ) throw ( Exception ); + + + virtual void + read ( istream & is ) throw ( Exception ); + + + virtual const ConfigSection * + get ( const char * key ) const throw ( Exception ); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + + +#endif /* CONFIG_H */ + + +/*------------------------------------------------------------------------------ + + $Source$ + + $Log$ + Revision 1.1 2000/11/08 17:29:50 darkeye + added configuration file reader + + Revision 1.1.1.1 2000/11/05 10:05:50 darkeye + initial version + + +------------------------------------------------------------------------------*/ + diff --git a/darkice/trunk/src/ConfigSection.cpp b/darkice/trunk/src/ConfigSection.cpp new file mode 100644 index 0000000..52ae4e2 --- /dev/null +++ b/darkice/trunk/src/ConfigSection.cpp @@ -0,0 +1,174 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2000 Tyrell Corporation. All rights reserved. + + Tyrell ConfigSection + + File : ConfigSection.cpp + Version : $Revision$ + Author : $Author$ + Location : $Source$ + + Abstract : + + A configuration file representation. The file is of the syntax: + + # this is a whole line comment + key = value + an ugly key name = long value # this end is a comment too + + also empty lines are ignored and all white space is removed + from the front and end of keys / values + + Copyright notice: + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include + + +#include "ConfigSection.h" + + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +/*------------------------------------------------------------------------------ + * File identity + *----------------------------------------------------------------------------*/ +static const char fileid[] = "$Id$"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Add a key / value pair + *----------------------------------------------------------------------------*/ +bool +ConfigSection :: add ( const char * key, + const char * value ) throw ( Exception ) +{ + if ( !key || !value ) { + throw Exception( __FILE__, __LINE__, "no key or value"); + } + + pair element( key, value); + pair res; + + res = table.insert( element); + + return res.second; +} + + +/*------------------------------------------------------------------------------ + * Get a value for a key + *----------------------------------------------------------------------------*/ +const char * +ConfigSection :: get ( const char * key ) const throw ( Exception ) +{ + if ( !key ) { + throw Exception( __FILE__, __LINE__, "no key"); + } + + TableType::const_iterator it = table.find( key); + if ( it == table.end() ) { + return 0; + } + return it->second.c_str(); +} + + +/*------------------------------------------------------------------------------ + * Add a configuration line + *----------------------------------------------------------------------------*/ +bool +ConfigSection :: addLine ( const char * line ) throw ( Exception ) +{ + if ( !line ) { + throw Exception( __FILE__, __LINE__, "no line"); + } + + string::size_type ix; + string str( line); + + /* delete everything after the first # */ + if ( (ix = str.find( '#')) != str.npos ) { + str.erase( ix); + } + /* eat up all white space from the front */ + if ( (ix = str.find_first_not_of( ' ')) != str.npos ) { + str.erase( 0, ix); + } + /* eat up all white space from the end */ + if ( (ix = str.find_last_not_of( ' ')) != str.npos ) { + str.erase( ix + 1); + } + + /* find the '=' delimiter between key and value */ + if ( (ix = str.find( '=')) == str.npos ) { + return false; + } + + string key( str, 0, ix); + string value( str, ix + 1); + + /* eat up all white space from the front of value */ + if ( (ix = value.find_first_not_of( ' ')) != value.npos ) { + value.erase( 0, ix); + } + /* eat up all white space from the end of key */ + if ( (ix = key.find_last_not_of( ' ')) != key.npos ) { + key.erase( ix + 1); + } + + /* now add the new key / value pair */ + return add( key.c_str(), value.c_str()); +} + + +/*------------------------------------------------------------------------------ + + $Source$ + + $Log$ + Revision 1.1 2000/11/08 17:29:50 darkeye + added configuration file reader + + Revision 1.2 2000/11/05 14:08:27 darkeye + changed builting to an automake / autoconf environment + + Revision 1.1.1.1 2000/11/05 10:05:49 darkeye + initial version + + +------------------------------------------------------------------------------*/ + diff --git a/darkice/trunk/src/ConfigSection.h b/darkice/trunk/src/ConfigSection.h new file mode 100644 index 0000000..1776c82 --- /dev/null +++ b/darkice/trunk/src/ConfigSection.h @@ -0,0 +1,148 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2000 Tyrell Corporation. All rights reserved. + + Tyrell ConfigSection + + File : ConfigSection.h + Version : $Revision$ + Author : $Author$ + Location : $Source$ + + Abstract : + + A configuration file representation. The file is of the syntax: + + # this is a whole line comment + key = value + an ugly key name = long value # this end is a comment too + + also empty lines are ignored and all white space is removed + from the front and end of keys / values + + Copyright notice: + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. + +------------------------------------------------------------------------------*/ +#ifndef CONFIG_SECTION_H +#define CONFIG_SECTION_H + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#include +#include + +#include "Referable.h" + + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/*------------------------------------------------------------------------------ + * + *----------------------------------------------------------------------------*/ +class ConfigSection : public virtual Referable +{ + private: + + typedef hash_map TableType; + TableType table; + + + void + init ( void ) throw ( Exception ); + + + protected: + + + public: + + inline + ConfigSection ( void ) throw ( Exception ) + { + } + + + inline virtual + ~ConfigSection ( void ) throw ( Exception ) + { + } + + +/* + + inline + ConfigSection ( const ConfigSection & di ) throw ( Exception ) + { + } + + + inline ConfigSection & + operator= ( const ConfigSection * di ) throw ( Exception ) + { + } +*/ + + virtual bool + add ( const char * key, + const char * value ) throw ( Exception ); + + + virtual const char * + get ( const char * key ) const throw ( Exception ); + + + virtual bool + addLine ( const char * line ) throw ( Exception ); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + + +#endif /* CONFIG_SECTION_H */ + + +/*------------------------------------------------------------------------------ + + $Source$ + + $Log$ + Revision 1.1 2000/11/08 17:29:50 darkeye + added configuration file reader + + Revision 1.1.1.1 2000/11/05 10:05:50 darkeye + initial version + + +------------------------------------------------------------------------------*/ + diff --git a/darkice/trunk/src/DarkIce.cpp b/darkice/trunk/src/DarkIce.cpp index f15c644..6517b48 100644 --- a/darkice/trunk/src/DarkIce.cpp +++ b/darkice/trunk/src/DarkIce.cpp @@ -4,7 +4,7 @@ Tyrell DarkIce - File : darkice.cpp + File : DarkIce.cpp Version : $Revision$ Author : $Author$ Location : $Source$ @@ -69,9 +69,13 @@ #endif +#include +#include + #include +#include "Config.h" #include "DarkIce.h" @@ -210,7 +214,29 @@ DarkIce :: run ( void ) throw ( Exception ) pid_t pid; cout << "DarkIce" << endl << endl << flush; +/* + Config config; + const ConfigSection * cs; + const char * str; + config.read( cin); + + cs = config.get( "first"); + if ( cs ) { + str = cs->get( "blahblah"); + cout << str << endl; + str = cs->get( "ejj"); + cout << str << endl; + str = cs->get( "ojj"); + cout << str << endl; + } + + cs = config.get( "second"); + if ( cs ) { + str = cs->get( "blahblah"); + cout << str << endl; + } +*/ init(); cout << "init OK" << endl << flush; @@ -254,6 +280,9 @@ DarkIce :: run ( void ) throw ( Exception ) $Source$ $Log$ + Revision 1.3 2000/11/08 17:29:50 darkeye + added configuration file reader + Revision 1.2 2000/11/05 14:08:27 darkeye changed builting to an automake / autoconf environment diff --git a/darkice/trunk/src/Makefile.am b/darkice/trunk/src/Makefile.am index adbd0ba..75d9b13 100644 --- a/darkice/trunk/src/Makefile.am +++ b/darkice/trunk/src/Makefile.am @@ -1,4 +1,6 @@ bin_PROGRAMS = darkice +INCLUDES = -I../stl +CXXFLAGS = -g darkice_SOURCES = AudioEncoder.h\ AudioSource.h\ @@ -36,5 +38,9 @@ darkice_SOURCES = AudioEncoder.h\ TcpSocket.h\ Util.cpp\ Util.h\ + ConfigSection.h\ + ConfigSection.cpp\ + Config.h\ + Config.cpp\ main.cpp diff --git a/darkice/trunk/src/main.cpp b/darkice/trunk/src/main.cpp index 8a816ae..71e48bc 100644 --- a/darkice/trunk/src/main.cpp +++ b/darkice/trunk/src/main.cpp @@ -73,7 +73,7 @@ main ( di->run(); } catch ( Exception & e ) { - cout << "Exception: " << e << endl << flush; + cout << "DarkIce: " << e << endl << flush; } return res; @@ -85,8 +85,11 @@ main ( $Source$ $Log$ - Revision 1.1 2000/11/05 10:05:52 darkeye - Initial revision + Revision 1.2 2000/11/08 17:29:50 darkeye + added configuration file reader + + Revision 1.1.1.1 2000/11/05 10:05:52 darkeye + initial version ------------------------------------------------------------------------------*/