Main Page   Class Hierarchy   Compound List   File List   Compound Members  

resConfigRegistryWindows.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/resConfigRegistryWindows.h>
00020 #include <freecloth/resmgt/rcShdPtr.h>
00021 
00022 ////////////////////////////////////////////////////////////////////////////////
00023 // LOCAL DECLARATIONS
00024 
00025 FREECLOTH_NAMESPACE_START
00026     const String BASE_PATH = "Software\\Winamp\\Plugins";
00027 };
00028 
00029 namespace freecloth {
00030 
00031 ////////////////////////////////////////////////////////////////////////////////
00032 // CLASS ResConfigRegistryW
00033 
00034 //------------------------------------------------------------------------------
00035 RCShdPtr<ResConfigRegistryW> ResConfigRegistryW::create(
00036     const String& groupName
00037 ) {
00038     return RCShdPtr<ResConfigRegistryW>(
00039         new ResConfigRegistryWWindows( BASE_PATH + "\\" + groupName )
00040     );
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////////////////
00044 // CLASS ResConfigRegistryR
00045 
00046 //------------------------------------------------------------------------------
00047 RCShdPtr<ResConfigRegistryR> ResConfigRegistryR::create(
00048     const String& groupName
00049 ) {
00050     return RCShdPtr<ResConfigRegistryR>(
00051         new ResConfigRegistryRWindows( BASE_PATH + "\\" + groupName )
00052     );
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////////////////
00056 // CLASS ResConfigRegistryWWindows
00057 
00058 //------------------------------------------------------------------------------
00059 
00060 ResConfigRegistryWWindows::ResConfigRegistryWWindows(
00061     const String& keyName
00062 ) {
00063     HRESULT result;
00064     
00065     result = ::RegCreateKeyEx(
00066         HKEY_CURRENT_USER,
00067         keyName.c_str(),
00068         0,
00069         NULL,
00070         0,
00071         KEY_WRITE,
00072         NULL,
00073         &_hkey,
00074         NULL
00075     );
00076 
00077     DGFX_ASSERT( result == 0 );
00078 }
00079 
00080 //------------------------------------------------------------------------------
00081 
00082 ResConfigRegistryWWindows::~ResConfigRegistryWWindows()
00083 {
00084     ::RegCloseKey( _hkey );
00085 }
00086 
00087 //------------------------------------------------------------------------------
00088 
00089 void ResConfigRegistryWWindows::writeString(
00090     const String& key,
00091     const String& value
00092 ) {
00093     HRESULT result;
00094     result = ::RegSetValueEx(
00095         _hkey,
00096         key.c_str(),
00097         0,
00098         REG_SZ,
00099         reinterpret_cast<const BYTE*>( value.c_str() ),
00100         value.length()
00101     );
00102     DGFX_ASSERT( result == 0 );
00103 }
00104 
00105 //------------------------------------------------------------------------------
00106 
00107 void ResConfigRegistryWWindows::writeUInt32(
00108     const String& key,
00109     UInt32 value
00110 ) {
00111     HRESULT result;
00112     result = ::RegSetValueEx(
00113         _hkey,
00114         key.c_str(),
00115         0,
00116         REG_DWORD,
00117         reinterpret_cast<const BYTE*>( &value ),
00118         sizeof( value )
00119     );
00120     DGFX_ASSERT( result == 0 );
00121 }
00122 
00123 //------------------------------------------------------------------------------
00124 
00125 void ResConfigRegistryWWindows::writeFloat(
00126     const String& key,
00127     Float value
00128 ) {
00129     writeUInt32( key, *reinterpret_cast<UInt32*>( &value ) );
00130 }
00131 
00132 ////////////////////////////////////////////////////////////////////////////////
00133 // CLASS ResConfigRegistryRWindows
00134 
00135 //------------------------------------------------------------------------------
00136 
00137 ResConfigRegistryRWindows::ResConfigRegistryRWindows(
00138     const String& keyName
00139 ) {
00140     HRESULT result;
00141 
00142     result = ::RegOpenKeyEx(
00143         HKEY_CURRENT_USER,
00144         keyName.c_str(),
00145         0,
00146         KEY_READ,
00147         &_hkey
00148     );
00149 
00150     // Key not found
00151     if ( result != 0 ) {
00152         _hkey = NULL;
00153     }
00154 }
00155 
00156 //------------------------------------------------------------------------------
00157 
00158 ResConfigRegistryRWindows::~ResConfigRegistryRWindows()
00159 {
00160     ::RegCloseKey( _hkey );
00161 }
00162 
00163 //------------------------------------------------------------------------------
00164 
00165 bool ResConfigRegistryRWindows::hasKey(
00166     const String& key
00167 ) const {
00168     bool retVal = false;
00169 
00170     if ( _hkey != NULL ) {
00171         DWORD size;
00172         DWORD type;
00173         HRESULT result = ::RegQueryValueEx(
00174             _hkey, key.c_str(), 0, &type, NULL, &size
00175         );
00176         if ( result == 0 ) {
00177             retVal = true;
00178         }
00179     }
00180     return retVal;
00181 }
00182 
00183 //------------------------------------------------------------------------------
00184 
00185 String ResConfigRegistryRWindows::readString(
00186     const String& key,
00187     const String& def
00188 ) const {
00189     String retVal = def;
00190 
00191     DWORD size = 0;
00192     DWORD type;
00193     HRESULT result = ::RegQueryValueEx(
00194         _hkey, key.c_str(), 0, &type, NULL, &size
00195     );
00196     if ( result == 0 && type == REG_SZ ) {
00197         char* buf = new char[ size + 1 ];
00198         HRESULT result = ::RegQueryValueEx(
00199             _hkey, key.c_str(), 0, &type, reinterpret_cast<BYTE*>( buf ), &size
00200         );
00201         if ( result == 0 ) {
00202             retVal = buf;
00203         }
00204         delete[] buf;
00205     }
00206 
00207     return retVal;
00208 }
00209 
00210 //------------------------------------------------------------------------------
00211 
00212 UInt32 ResConfigRegistryRWindows::readUInt32(
00213     const String& key,
00214     UInt32 def
00215 ) const {
00216     UInt32 retVal = def;
00217 
00218     if ( _hkey != NULL ) {
00219         DWORD value;
00220         DWORD size = sizeof( value );
00221         DWORD type;
00222         HRESULT result = ::RegQueryValueEx(
00223             _hkey,
00224             key.c_str(),
00225             0,
00226             &type,
00227             reinterpret_cast<BYTE*>( &value ),
00228             &size
00229         );
00230         if ( result == 0 && type == REG_DWORD ) {
00231             retVal = value;
00232         }
00233     }
00234     return retVal;
00235 }
00236 
00237 //------------------------------------------------------------------------------
00238 
00239 Float ResConfigRegistryRWindows::readFloat(
00240     const String& key,
00241     Float def
00242 ) const {
00243     DGFX_ASSERT( sizeof(Float) == sizeof(UInt32) );
00244 
00245     UInt32 retVal = readUInt32( key, *reinterpret_cast<UInt32*>( &def ) );
00246     return *reinterpret_cast<Float*>( &retVal );
00247 }
00248 
00249 FREECLOTH_NAMESPACE_END

Generated on Fri May 2 16:51:13 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002