ENH: More handling of unix versus windows registry

This commit is contained in:
Andy Cedilnik 2005-09-16 08:38:12 -04:00
parent 0b30d23085
commit df4f999457
1 changed files with 136 additions and 103 deletions

View File

@ -441,70 +441,78 @@ bool RegistryHelper::Open(const char *toplevel, const char *subkey,
delete ifs;
return res;
}
return false;
}
//----------------------------------------------------------------------------
bool RegistryHelper::Close()
{
#ifdef WIN32
int res;
res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
return res;
#else
int res = 0;
if ( !m_Changed )
if ( m_RegistryType == Registry::RegistryType::WIN32)
{
int res;
res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
return res;
}
#else
if ( m_RegistryType == Registry::UNIX_REGISTRY )
{
int res = 0;
if ( !m_Changed )
{
this->EntriesMap.erase(
this->EntriesMap.begin(),
this->EntriesMap.end());
m_Empty = 1;
this->SetSubKey(0);
return 1;
}
kwsys_ios::ostringstream str;
if ( !getenv("HOME") )
{
return 0;
}
str << getenv("HOME") << "/." << this->GetTopLevel() << "rc";
kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
if ( !ofs )
{
return 0;
}
if ( ofs->fail())
{
delete ofs;
return 0;
}
*ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
<< "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
<< "# coments and empty lines will be deleted. Every line has" << kwsys_ios::endl
<< "# to be in format: " << kwsys_ios::endl
<< "# key = value" << kwsys_ios::endl
<< "#" << kwsys_ios::endl;
if ( !this->EntriesMap.empty() )
{
RegistryHelper::StringToStringMap::iterator it;
for ( it = this->EntriesMap.begin();
it != this->EntriesMap.end();
++ it )
{
*ofs << it->first.c_str() << " = " << it->second.c_str()<< kwsys_ios::endl;
}
}
this->EntriesMap.erase(
this->EntriesMap.begin(),
this->EntriesMap.end());
m_Empty = 1;
this->SetSubKey(0);
return 1;
}
kwsys_ios::ostringstream str;
if ( !getenv("HOME") )
{
return 0;
}
str << getenv("HOME") << "/." << this->GetTopLevel() << "rc";
kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
if ( !ofs )
{
return 0;
}
if ( ofs->fail())
{
ofs->close();
delete ofs;
return 0;
res = 1;
this->SetSubKey(0);
m_Empty = 1;
return res;
}
*ofs << "# This file is automatically generated by the application" << kwsys_ios::endl
<< "# If you change any lines or add new lines, note that all" << kwsys_ios::endl
<< "# coments and empty lines will be deleted. Every line has" << kwsys_ios::endl
<< "# to be in format: " << kwsys_ios::endl
<< "# key = value" << kwsys_ios::endl
<< "#" << kwsys_ios::endl;
if ( !this->EntriesMap.empty() )
{
RegistryHelper::StringToStringMap::iterator it;
for ( it = this->EntriesMap.begin();
it != this->EntriesMap.end();
++ it )
{
*ofs << it->first.c_str() << " = " << it->second.c_str()<< kwsys_ios::endl;
}
}
this->EntriesMap.erase(
this->EntriesMap.begin(),
this->EntriesMap.end());
ofs->close();
delete ofs;
res = 1;
this->SetSubKey(0);
m_Empty = 1;
return res;
#endif
return false;
}
//----------------------------------------------------------------------------
@ -512,92 +520,118 @@ bool RegistryHelper::ReadValue(const char *skey, char *value)
{
#ifdef WIN32
int res = 1;
DWORD dwType, dwSize;
dwType = REG_SZ;
dwSize = BUFFER_SIZE;
res = ( RegQueryValueEx(this->HKey, key, NULL, &dwType,
(BYTE *)value, &dwSize) == ERROR_SUCCESS );
return res;
if ( m_RegistryType == Registry::RegistryType::WIN32)
{
int res = 1;
DWORD dwType, dwSize;
dwType = REG_SZ;
dwSize = BUFFER_SIZE;
res = ( RegQueryValueEx(this->HKey, key, NULL, &dwType,
(BYTE *)value, &dwSize) == ERROR_SUCCESS );
return res;
}
#else
int res = 0;
char *key = this->CreateKey( skey );
if ( !key )
if ( m_RegistryType == Registry::UNIX_REGISTRY )
{
return 0;
int res = 0;
char *key = this->CreateKey( skey );
if ( !key )
{
return 0;
}
RegistryHelper::StringToStringMap::iterator it
= this->EntriesMap.find(key);
if ( it != this->EntriesMap.end() )
{
strcpy(value, it->second.c_str());
res = 1;
}
delete [] key;
return res;
}
RegistryHelper::StringToStringMap::iterator it
= this->EntriesMap.find(key);
if ( it != this->EntriesMap.end() )
{
strcpy(value, it->second.c_str());
res = 1;
}
delete [] key;
return res;
#endif
return false;
}
//----------------------------------------------------------------------------
bool RegistryHelper::DeleteKey(const char* key)
{
#ifdef WIN32
int res = 1;
res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS );
return res;
if ( m_RegistryType == Registry::RegistryType::WIN32)
{
int res = 1;
res = ( RegDeleteKey( this->HKey, key ) == ERROR_SUCCESS );
return res;
}
#else
(void)key;
int res = 0;
return res;
if ( m_RegistryType == Registry::UNIX_REGISTRY )
{
(void)key;
int res = 0;
return res;
}
#endif
return false;
}
//----------------------------------------------------------------------------
bool RegistryHelper::DeleteValue(const char *skey)
{
#ifdef WIN32
int res = 1;
res = ( RegDeleteValue( this->HKey, key ) == ERROR_SUCCESS );
return res;
#else
char *key = this->CreateKey( skey );
if ( !key )
if ( m_RegistryType == Registry::RegistryType::WIN32)
{
return 0;
int res = 1;
res = ( RegDeleteValue( this->HKey, key ) == ERROR_SUCCESS );
return res;
}
#else
if ( m_RegistryType == Registry::UNIX_REGISTRY )
{
char *key = this->CreateKey( skey );
if ( !key )
{
return 0;
}
this->EntriesMap.erase(key);
delete [] key;
return 1;
}
this->EntriesMap.erase(key);
delete [] key;
return 1;
#endif
return false;
}
//----------------------------------------------------------------------------
bool RegistryHelper::SetValue(const char *skey, const char *value)
{
#ifdef WIN32
int res = 1;
DWORD len = (DWORD)(value ? strlen(value) : 0);
res = ( RegSetValueEx(this->HKey, key, 0, REG_SZ,
(CONST BYTE *)(const char *)value,
len+1) == ERROR_SUCCESS );
return res;
#else
char *key = this->CreateKey( skey );
if ( !key )
if ( m_RegistryType == Registry::RegistryType::WIN32)
{
return 0;
int res = 1;
DWORD len = (DWORD)(value ? strlen(value) : 0);
res = ( RegSetValueEx(this->HKey, key, 0, REG_SZ,
(CONST BYTE *)(const char *)value,
len+1) == ERROR_SUCCESS );
return res;
}
#else
if ( m_RegistryType == Registry::UNIX_REGISTRY )
{
char *key = this->CreateKey( skey );
if ( !key )
{
return 0;
}
this->EntriesMap[key] = value;
delete [] key;
return 1;
}
this->EntriesMap[key] = value;
delete [] key;
return 1;
#endif
return false;
}
//----------------------------------------------------------------------------
char *RegistryHelper::CreateKey( const char *key )
{
#ifdef WIN32
#else
char *newkey;
if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
{
@ -607,7 +641,6 @@ char *RegistryHelper::CreateKey( const char *key )
newkey = new char[ len+1 ] ;
::sprintf(newkey, "%s\\%s", this->m_SubKey.c_str(), key);
return newkey;
#endif
}
void RegistryHelper::SetSubKey(const char* sk)