added configuration file reader

This commit is contained in:
darkeye 2000-11-08 17:29:50 +00:00
parent 17de54a6bd
commit d63b1eacc4
7 changed files with 714 additions and 4 deletions

View File

@ -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 <iterator>
#include <iostream.h>
#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<const string, ConfigSection> element( section, cSection);
pair<TableType::iterator, bool> 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
------------------------------------------------------------------------------*/

164
darkice/trunk/src/Config.h Normal file
View File

@ -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 <hash_map>
#include <string>
#include <iostream.h>
#include "Referable.h"
#include "ConfigSection.h"
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/*------------------------------------------------------------------------------
*
*----------------------------------------------------------------------------*/
class Config : public virtual Referable
{
private:
typedef hash_map<string, ConfigSection> 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
------------------------------------------------------------------------------*/

View File

@ -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 <iterator>
#include <iostream.h>
#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<const string, string> element( key, value);
pair<TableType::iterator, bool> 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
------------------------------------------------------------------------------*/

View File

@ -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 <hash_map>
#include <string>
#include "Referable.h"
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/*------------------------------------------------------------------------------
*
*----------------------------------------------------------------------------*/
class ConfigSection : public virtual Referable
{
private:
typedef hash_map<string, string> 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
------------------------------------------------------------------------------*/

View File

@ -4,7 +4,7 @@
Tyrell DarkIce
File : darkice.cpp
File : DarkIce.cpp
Version : $Revision$
Author : $Author$
Location : $Source$
@ -69,9 +69,13 @@
#endif
#include <hash_map>
#include <string>
#include <iostream.h>
#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

View File

@ -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

View File

@ -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
------------------------------------------------------------------------------*/