COMP: Win32 fixes

This commit is contained in:
Andy Cedilnik 2005-09-16 09:08:40 -04:00
parent df4f999457
commit 30b67a8bfe
2 changed files with 64 additions and 31 deletions

View File

@ -25,6 +25,10 @@
#include <ctype.h> // for isspace #include <ctype.h> // for isspace
#include <stdio.h> #include <stdio.h>
#ifdef WIN32
# include <windows.h>
#endif
#ifdef KWSYS_IOS_USE_ANSI #ifdef KWSYS_IOS_USE_ANSI
# define VTK_IOS_NOCREATE # define VTK_IOS_NOCREATE
#else #else
@ -64,9 +68,16 @@ public:
void SetTopLevel(const char* tl); void SetTopLevel(const char* tl);
const char* GetTopLevel() { return m_TopLevel.c_str(); } const char* GetTopLevel() { return m_TopLevel.c_str(); }
//! Read from local or global scope. On Windows this mean from local machine
// or local user. On unix this will read from $HOME/.Projectrc or
// /etc/Project
void SetGlobalScope(bool b);
bool GetGlobalScope();
protected: protected:
bool m_Changed; bool m_Changed;
kwsys_stl::string m_TopLevel; kwsys_stl::string m_TopLevel;
bool m_GlobalScope;
#ifdef WIN32 #ifdef WIN32
HKEY HKey; HKEY HKey;
@ -91,7 +102,6 @@ Registry::Registry(Registry::RegistryType registryType)
{ {
m_Opened = false; m_Opened = false;
m_Locked = false; m_Locked = false;
m_GlobalScope = false;
this->Helper = 0; this->Helper = 0;
this->Helper = new RegistryHelper(registryType); this->Helper = new RegistryHelper(registryType);
} }
@ -108,6 +118,18 @@ Registry::~Registry()
delete this->Helper; delete this->Helper;
} }
//----------------------------------------------------------------------------
void Registry::SetGlobalScope(bool b)
{
this->Helper->SetGlobalScope(b);
}
//----------------------------------------------------------------------------
bool Registry::GetGlobalScope()
{
return this->Helper->GetGlobalScope();
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool Registry::Open(const char *toplevel, bool Registry::Open(const char *toplevel,
const char *subkey, int readonly) const char *subkey, int readonly)
@ -330,6 +352,7 @@ RegistryHelper::RegistryHelper(Registry::RegistryType registryType)
m_SubKey = ""; m_SubKey = "";
m_SubKeySpecified = false; m_SubKeySpecified = false;
m_Empty = true; m_Empty = true;
m_GlobalScope = false;
m_RegistryType = registryType; m_RegistryType = registryType;
} }
@ -344,7 +367,7 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
int readonly) int readonly)
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
HKEY scope = HKEY_CURRENT_USER; HKEY scope = HKEY_CURRENT_USER;
if ( this->GetGlobalScope() ) if ( this->GetGlobalScope() )
@ -352,32 +375,31 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
scope = HKEY_LOCAL_MACHINE; scope = HKEY_LOCAL_MACHINE;
} }
int res = 0; int res = 0;
ostrstream str; kwsys_ios::ostringstream str;
DWORD dwDummy; DWORD dwDummy;
str << "Software\\Kitware\\" << toplevel << "\\" << subkey << ends; str << "Software\\Kitware\\" << toplevel << "\\" << subkey;
if ( readonly == vtkKWRegistryUtilities::READONLY ) if ( readonly == Registry::READONLY )
{ {
res = ( RegOpenKeyEx(scope, str.str(), res = ( RegOpenKeyEx(scope, str.str().c_str(),
0, KEY_READ, &this->HKey) == ERROR_SUCCESS ); 0, KEY_READ, &this->HKey) == ERROR_SUCCESS );
} }
else else
{ {
res = ( RegCreateKeyEx(scope, str.str(), res = ( RegCreateKeyEx(scope, str.str().c_str(),
0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, 0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS ); NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS );
} }
str.rdbuf()->freeze(0); return (res != 0);
return res;
} }
#endif #endif
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
{ {
int res = 0; bool res = false;
int cc; int cc;
kwsys_ios::ostringstream str; kwsys_ios::ostringstream str;
if ( !getenv("HOME") ) if ( !getenv("HOME") )
{ {
return 0; return false;
} }
str << getenv("HOME") << "/." << toplevel << "rc"; str << getenv("HOME") << "/." << toplevel << "rc";
if ( readonly == Registry::READWRITE ) if ( readonly == Registry::READWRITE )
@ -385,7 +407,7 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app ); kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
if ( ofs.fail() ) if ( ofs.fail() )
{ {
return 0; return false;
} }
ofs.close(); ofs.close();
} }
@ -393,15 +415,15 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in VTK_IOS_NOCREATE); kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in VTK_IOS_NOCREATE);
if ( !ifs ) if ( !ifs )
{ {
return 0; return false;
} }
if ( ifs->fail()) if ( ifs->fail())
{ {
delete ifs; delete ifs;
return 0; return false;
} }
res = 1; res = true;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
while( !ifs->fail() ) while( !ifs->fail() )
{ {
@ -448,11 +470,11 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
bool RegistryHelper::Close() bool RegistryHelper::Close()
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
int res; int res;
res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS ); res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
return res; return (res != 0);
} }
#else #else
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
@ -520,15 +542,15 @@ bool RegistryHelper::ReadValue(const char *skey, char *value)
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
int res = 1; int res = 1;
DWORD dwType, dwSize; DWORD dwType, dwSize;
dwType = REG_SZ; dwType = REG_SZ;
dwSize = BUFFER_SIZE; dwSize = BUFFER_SIZE;
res = ( RegQueryValueEx(this->HKey, key, NULL, &dwType, res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
(BYTE *)value, &dwSize) == ERROR_SUCCESS ); (BYTE *)value, &dwSize) == ERROR_SUCCESS );
return res; return (res != 0);
} }
#else #else
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
@ -557,11 +579,11 @@ bool RegistryHelper::ReadValue(const char *skey, char *value)
bool RegistryHelper::DeleteKey(const char* key) bool RegistryHelper::DeleteKey(const char* key)
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
int res = 1; int res = 1;
res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS ); res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS );
return res; return (res != 0);
} }
#else #else
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
@ -578,11 +600,11 @@ bool RegistryHelper::DeleteKey(const char* key)
bool RegistryHelper::DeleteValue(const char *skey) bool RegistryHelper::DeleteValue(const char *skey)
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
int res = 1; int res = 1;
res = ( RegDeleteValue( this->HKey, key ) == ERROR_SUCCESS ); res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
return res; return (res != 0);
} }
#else #else
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
@ -604,14 +626,14 @@ bool RegistryHelper::DeleteValue(const char *skey)
bool RegistryHelper::SetValue(const char *skey, const char *value) bool RegistryHelper::SetValue(const char *skey, const char *value)
{ {
#ifdef WIN32 #ifdef WIN32
if ( m_RegistryType == Registry::RegistryType::WIN32) if ( m_RegistryType == Registry::WIN32_REGISTRY)
{ {
int res = 1; int res = 1;
DWORD len = (DWORD)(value ? strlen(value) : 0); DWORD len = (DWORD)(value ? strlen(value) : 0);
res = ( RegSetValueEx(this->HKey, key, 0, REG_SZ, res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
(CONST BYTE *)(const char *)value, (CONST BYTE *)(const char *)value,
len+1) == ERROR_SUCCESS ); len+1) == ERROR_SUCCESS );
return res; return (res != 0);
} }
#else #else
if ( m_RegistryType == Registry::UNIX_REGISTRY ) if ( m_RegistryType == Registry::UNIX_REGISTRY )
@ -688,4 +710,16 @@ char *RegistryHelper::Strip(char *str)
return nstr; return nstr;
} }
//----------------------------------------------------------------------------
void RegistryHelper::SetGlobalScope(bool b)
{
m_GlobalScope = b;
}
//----------------------------------------------------------------------------
bool RegistryHelper::GetGlobalScope()
{
return m_GlobalScope;
}
} // namespace KWSYS_NAMESPACE } // namespace KWSYS_NAMESPACE

View File

@ -75,8 +75,8 @@ public:
// /etc/Project // /etc/Project
void GlobalScopeOn() { this->SetGlobalScope(1); } void GlobalScopeOn() { this->SetGlobalScope(1); }
void GlobalScopeOff() { this->SetGlobalScope(0); } void GlobalScopeOff() { this->SetGlobalScope(0); }
void SetGlobalScope(bool b) { m_GlobalScope = b; } void SetGlobalScope(bool b);
bool GetGlobalScope() { return m_GlobalScope; } bool GetGlobalScope();
// Set or get the toplevel registry key. // Set or get the toplevel registry key.
void SetTopLevel(const char* tl); void SetTopLevel(const char* tl);
@ -102,7 +102,6 @@ private:
bool m_Opened; bool m_Opened;
bool m_Locked; bool m_Locked;
bool m_GlobalScope;
}; // End Class: Registry }; // End Class: Registry
} // namespace @KWSYS_NAMESPACE@ } // namespace @KWSYS_NAMESPACE@