Main Page   Class Hierarchy   Compound List   File List   Compound Members  

resConfigRegistryUnix.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2001-2002 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/resmgt/resConfigRegistryUnix.h>
00020 #include <freecloth/resmgt/rcShdPtr.h>
00021 
00022 #include <freecloth/base/baStringUtil.h>
00023 #include <freecloth/base/stdlib.h>
00024 
00025 ////////////////////////////////////////////////////////////////////////////////
00026 // LOCAL DECLARATIONS
00027 
00028 FREECLOTH_NAMESPACE_START
00029 
00030 ////////////////////////////////////////////////////////////////////////////////
00031 // CLASS ResConfigRegistryW
00032 
00033 //------------------------------------------------------------------------------
00034 RCShdPtr<ResConfigRegistryW> ResConfigRegistryW::create(
00035     const String& groupName
00036 ) {
00037     //DGFX_TRACE_ENTER( "ResConfigRegistryW::create" );
00038 
00039     // FIXME: get rid of spaces
00040     const String rcfile( BaStringUtil::toLower(
00041         String( ::getenv( "HOME" ) ) + "/." + groupName + "rc"
00042     ) );
00043 
00044     return RCShdPtr<ResConfigRegistryW>(
00045         new ResConfigRegistryWUnix( rcfile )
00046     );
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////////////////
00050 // CLASS ResConfigRegistryR
00051 
00052 //------------------------------------------------------------------------------
00053 RCShdPtr<ResConfigRegistryR> ResConfigRegistryR::create(
00054     const String& groupName
00055 ) {
00056     //DGFX_TRACE_ENTER( "ResConfigRegistryR::create " << groupName );
00057     const String rcfile( BaStringUtil::toLower(
00058         String( ::getenv( "HOME" ) ) + "/." + groupName + "rc"
00059     ) );
00060 
00061     return RCShdPtr<ResConfigRegistryR>(
00062         new ResConfigRegistryRUnix( rcfile )
00063     );
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////////////////
00067 // CLASS ResConfigRegistryWUnix
00068 
00069 //------------------------------------------------------------------------------
00070 
00071 ResConfigRegistryWUnix::ResConfigRegistryWUnix(
00072     const String& filename
00073 ) : out( filename.c_str() )
00074 {
00075 }
00076 
00077 //------------------------------------------------------------------------------
00078 
00079 ResConfigRegistryWUnix::~ResConfigRegistryWUnix()
00080 {
00081 }
00082 
00083 //------------------------------------------------------------------------------
00084 
00085 void ResConfigRegistryWUnix::writeString(
00086     const String& key,
00087     const String& value
00088 ) {
00089     out << key << "=" << value << std::endl;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////////////////
00093 // CLASS ResConfigRegistryRUnix
00094 
00095 //------------------------------------------------------------------------------
00096 
00097 ResConfigRegistryRUnix::ResConfigRegistryRUnix(
00098     const String& filename
00099 ) {
00100     //DGFX_TRACE_ENTER( "ResConfigRegistryRUnix ctor " << filename );
00101     std::ifstream in( filename.c_str() );
00102 
00103     while ( in.good() ) {
00104         char buf[ 1024 ];
00105         in.getline( buf, sizeof( buf ) );
00106         if ( buf[ 0 ] != '#' ) {
00107             const String s( buf );
00108             UInt32 equalsPos = s.find( '=' );
00109             String key;
00110             String value;
00111             if ( equalsPos != String::npos ) {
00112                 const char* whitespace = " \t";
00113                 UInt32 nonWSPos;
00114                 nonWSPos = s.find_last_not_of( whitespace, equalsPos - 1 );
00115                 if ( nonWSPos != String::npos ) {
00116                     key = s.substr( 0, nonWSPos+1 );
00117                 }
00118                 nonWSPos = s.find_first_not_of( whitespace, equalsPos + 1 );
00119                 if ( nonWSPos != String::npos ) {
00120                     value = s.substr( nonWSPos );
00121                 }
00122             }
00123             if (key.length() > 0) {
00124                 _keys[ key ] = value;
00125             }
00126         }
00127         // TODO: store dead keys
00128     }
00129 }
00130 
00131 //------------------------------------------------------------------------------
00132 
00133 ResConfigRegistryRUnix::~ResConfigRegistryRUnix()
00134 {
00135 }
00136 
00137 //------------------------------------------------------------------------------
00138 
00139 bool ResConfigRegistryRUnix::hasKey(
00140     const String& key
00141 ) const {
00142     //DGFX_TRACE_ENTER( "ResConfigRegistryRUnix::hasKey" );
00143     //DGFX_TRACE( "key = " << key );
00144     std::map<String,String>::const_iterator it;
00145     it = _keys.find( key );
00146     //DGFX_TRACE( "answer = " << ( it != _keys.end() ) );
00147     return it != _keys.end();
00148 }
00149 
00150 //------------------------------------------------------------------------------
00151 
00152 String ResConfigRegistryRUnix::readString(
00153     const String& key,
00154     const String& def
00155 ) const {
00156     std::map<String,String>::const_iterator it;
00157     it = _keys.find( key );
00158     if ( it != _keys.end() ) {
00159         return it->second;
00160     }
00161     else {
00162         return def;
00163     }
00164 }
00165 
00166 FREECLOTH_NAMESPACE_END

Generated on Wed Apr 23 15:58:52 2003 for Freecloth by doxygen1.3-rc3