2009-10-02 01:21:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2005-09-16 16:20:48 +04:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2005-09-16 16:20:48 +04:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2005-09-16 16:20:48 +04:00
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
#include KWSYS_HEADER(Registry.hxx)
|
|
|
|
|
|
|
|
#include KWSYS_HEADER(Configure.hxx)
|
|
|
|
#include KWSYS_HEADER(ios/iostream)
|
|
|
|
#include KWSYS_HEADER(stl/string)
|
|
|
|
#include KWSYS_HEADER(stl/map)
|
|
|
|
#include KWSYS_HEADER(ios/iostream)
|
|
|
|
#include KWSYS_HEADER(ios/fstream)
|
|
|
|
#include KWSYS_HEADER(ios/sstream)
|
2006-03-06 21:43:58 +03:00
|
|
|
// Work-around CMake dependency scanning limitation. This must
|
|
|
|
// duplicate the above list of headers.
|
|
|
|
#if 0
|
|
|
|
# include "Registry.hxx.in"
|
|
|
|
# include "Configure.hxx.in"
|
|
|
|
# include "kwsys_stl.hxx.in"
|
|
|
|
# include "kwsys_stl_string.hxx.in"
|
|
|
|
# include "kwsys_stl_map.hxx.in"
|
|
|
|
# include "kwsys_ios_iostream.h.in"
|
|
|
|
# include "kwsys_ios_fstream.h.in"
|
|
|
|
# include "kwsys_ios_sstream.h.in"
|
|
|
|
#endif
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
#include <ctype.h> // for isspace
|
|
|
|
#include <stdio.h>
|
2006-08-01 22:45:32 +04:00
|
|
|
#include <string.h> /* strlen, strncpy */
|
|
|
|
#include <stdlib.h> /* getenv */
|
2005-09-16 16:20:48 +04:00
|
|
|
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
namespace KWSYS_NAMESPACE
|
|
|
|
{
|
|
|
|
class RegistryHelper {
|
|
|
|
public:
|
|
|
|
RegistryHelper(Registry::RegistryType registryType);
|
|
|
|
virtual ~RegistryHelper();
|
|
|
|
|
|
|
|
// Read a value from the registry.
|
2005-09-16 21:38:25 +04:00
|
|
|
virtual bool ReadValue(const char *key, const char **value);
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
// Delete a key from the registry.
|
|
|
|
virtual bool DeleteKey(const char *key);
|
|
|
|
|
|
|
|
// Delete a value from a given key.
|
|
|
|
virtual bool DeleteValue(const char *key);
|
|
|
|
|
|
|
|
// Set value in a given key.
|
2006-03-10 00:40:00 +03:00
|
|
|
virtual bool SetValue(const char *key, const char *value);
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
// Open the registry at toplevel/subkey.
|
2006-03-10 00:40:00 +03:00
|
|
|
virtual bool Open(const char *toplevel, const char *subkey,
|
2005-09-16 16:20:48 +04:00
|
|
|
int readonly);
|
|
|
|
|
2006-03-10 00:40:00 +03:00
|
|
|
// Close the registry.
|
2005-09-16 16:20:48 +04:00
|
|
|
virtual bool Close();
|
|
|
|
|
|
|
|
// Set the value of changed
|
|
|
|
void SetChanged(bool b) { m_Changed = b; }
|
|
|
|
void SetTopLevel(const char* tl);
|
|
|
|
const char* GetTopLevel() { return m_TopLevel.c_str(); }
|
|
|
|
|
2005-09-16 17:08:40 +04:00
|
|
|
//! Read from local or global scope. On Windows this mean from local machine
|
2006-03-10 00:40:00 +03:00
|
|
|
// or local user. On unix this will read from $HOME/.Projectrc or
|
2005-09-16 17:08:40 +04:00
|
|
|
// /etc/Project
|
|
|
|
void SetGlobalScope(bool b);
|
|
|
|
bool GetGlobalScope();
|
|
|
|
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string EncodeKey(const char* str);
|
|
|
|
kwsys_stl::string EncodeValue(const char* str);
|
|
|
|
kwsys_stl::string DecodeValue(const char* str);
|
|
|
|
|
2005-09-16 16:20:48 +04:00
|
|
|
protected:
|
|
|
|
bool m_Changed;
|
2006-03-10 00:40:00 +03:00
|
|
|
kwsys_stl::string m_TopLevel;
|
2005-09-16 17:08:40 +04:00
|
|
|
bool m_GlobalScope;
|
2005-09-16 16:20:48 +04:00
|
|
|
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 16:20:48 +04:00
|
|
|
HKEY HKey;
|
|
|
|
#endif
|
|
|
|
// Strip trailing and ending spaces.
|
|
|
|
char *Strip(char *str);
|
|
|
|
void SetSubKey(const char* sk);
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string CreateKey(const char *key);
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
typedef kwsys_stl::map<kwsys_stl::string, kwsys_stl::string> StringToStringMap;
|
|
|
|
StringToStringMap EntriesMap;
|
|
|
|
kwsys_stl::string m_SubKey;
|
|
|
|
bool m_Empty;
|
|
|
|
bool m_SubKeySpecified;
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string m_HomeDirectory;
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
Registry::RegistryType m_RegistryType;
|
|
|
|
};
|
|
|
|
|
2005-09-16 17:15:52 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2005-09-16 18:09:38 +04:00
|
|
|
#define Registry_BUFFER_SIZE 8192
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
Registry::Registry(Registry::RegistryType registryType)
|
|
|
|
{
|
|
|
|
m_Opened = false;
|
|
|
|
m_Locked = false;
|
|
|
|
this->Helper = 0;
|
|
|
|
this->Helper = new RegistryHelper(registryType);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
Registry::~Registry()
|
|
|
|
{
|
|
|
|
if ( m_Opened )
|
|
|
|
{
|
|
|
|
kwsys_ios::cerr << "Registry::Close should be "
|
|
|
|
"called here. The registry is not closed."
|
|
|
|
<< kwsys_ios::endl;
|
|
|
|
}
|
|
|
|
delete this->Helper;
|
|
|
|
}
|
|
|
|
|
2005-09-16 17:08:40 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void Registry::SetGlobalScope(bool b)
|
|
|
|
{
|
|
|
|
this->Helper->SetGlobalScope(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool Registry::GetGlobalScope()
|
|
|
|
{
|
|
|
|
return this->Helper->GetGlobalScope();
|
|
|
|
}
|
|
|
|
|
2005-09-16 16:20:48 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool Registry::Open(const char *toplevel,
|
|
|
|
const char *subkey, int readonly)
|
|
|
|
{
|
|
|
|
bool res = false;
|
|
|
|
if ( m_Locked )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
if ( m_Opened )
|
|
|
|
{
|
|
|
|
if ( !this->Close() )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !toplevel || !*toplevel )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
kwsys_ios::cerr << "Registry::Opened() Toplevel not defined"
|
|
|
|
<< kwsys_ios::endl;
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
2006-03-10 00:40:00 +03:00
|
|
|
if ( isspace(toplevel[0]) ||
|
2005-09-16 16:20:48 +04:00
|
|
|
isspace(toplevel[strlen(toplevel)-1]) )
|
|
|
|
{
|
|
|
|
kwsys_ios::cerr << "Toplevel has to start with letter or number and end"
|
|
|
|
" with one" << kwsys_ios::endl;
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
res = this->Helper->Open(toplevel, subkey, readonly);
|
|
|
|
if ( readonly != Registry::READONLY )
|
|
|
|
{
|
|
|
|
m_Locked = true;
|
|
|
|
}
|
2006-03-10 00:40:00 +03:00
|
|
|
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
m_Opened = true;
|
|
|
|
this->Helper->SetTopLevel(toplevel);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool Registry::Close()
|
|
|
|
{
|
|
|
|
bool res = false;
|
|
|
|
if ( m_Opened )
|
|
|
|
{
|
|
|
|
res = this->Helper->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
m_Opened = false;
|
|
|
|
m_Locked = false;
|
|
|
|
this->Helper->SetChanged(false);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2006-03-10 00:40:00 +03:00
|
|
|
bool Registry::ReadValue(const char *subkey,
|
|
|
|
const char *key,
|
2005-09-16 21:38:25 +04:00
|
|
|
const char **value)
|
2006-03-10 00:40:00 +03:00
|
|
|
{
|
2006-07-13 00:21:18 +04:00
|
|
|
bool res = false;
|
2006-03-10 00:40:00 +03:00
|
|
|
bool open = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( ! value )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2006-10-03 19:55:34 +04:00
|
|
|
*value = 0;
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( !m_Opened )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
if ( !this->Open(this->GetTopLevel(), subkey,
|
2005-09-16 16:20:48 +04:00
|
|
|
Registry::READONLY) )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
res = this->Helper->ReadValue(key, value);
|
|
|
|
|
|
|
|
if ( open )
|
|
|
|
{
|
|
|
|
if ( !this->Close() )
|
|
|
|
{
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool Registry::DeleteKey(const char *subkey, const char *key)
|
|
|
|
{
|
2006-07-13 00:21:18 +04:00
|
|
|
bool res = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
bool open = false;
|
|
|
|
if ( !m_Opened )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
if ( !this->Open(this->GetTopLevel(), subkey,
|
2005-09-16 16:20:48 +04:00
|
|
|
Registry::READWRITE) )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = this->Helper->DeleteKey(key);
|
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
this->Helper->SetChanged(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( open )
|
|
|
|
{
|
|
|
|
if ( !this->Close() )
|
|
|
|
{
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool Registry::DeleteValue(const char *subkey, const char *key)
|
|
|
|
{
|
2006-07-13 00:21:18 +04:00
|
|
|
bool res = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
bool open = false;
|
|
|
|
if ( !m_Opened )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
if ( !this->Open(this->GetTopLevel(), subkey,
|
2005-09-16 16:20:48 +04:00
|
|
|
Registry::READWRITE) )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = this->Helper->DeleteValue(key);
|
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
this->Helper->SetChanged(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( open )
|
|
|
|
{
|
|
|
|
if ( !this->Close() )
|
|
|
|
{
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2006-03-10 00:40:00 +03:00
|
|
|
bool Registry::SetValue(const char *subkey, const char *key,
|
2005-09-16 16:20:48 +04:00
|
|
|
const char *value)
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
bool res = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
bool open = false;
|
|
|
|
if ( !m_Opened )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
if ( !this->Open(this->GetTopLevel(), subkey,
|
2005-09-16 16:20:48 +04:00
|
|
|
Registry::READWRITE) )
|
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = this->Helper->SetValue( key, value );
|
|
|
|
if ( res )
|
|
|
|
{
|
|
|
|
this->Helper->SetChanged(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( open )
|
|
|
|
{
|
|
|
|
if ( !this->Close() )
|
|
|
|
{
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* Registry::GetTopLevel()
|
|
|
|
{
|
|
|
|
return this->Helper->GetTopLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void Registry::SetTopLevel(const char* tl)
|
|
|
|
{
|
|
|
|
this->Helper->SetTopLevel(tl);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void RegistryHelper::SetTopLevel(const char* tl)
|
|
|
|
{
|
|
|
|
if ( tl )
|
|
|
|
{
|
|
|
|
m_TopLevel = tl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_TopLevel = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
RegistryHelper::RegistryHelper(Registry::RegistryType registryType)
|
|
|
|
{
|
|
|
|
m_Changed = false;
|
|
|
|
m_TopLevel = "";
|
|
|
|
m_SubKey = "";
|
|
|
|
m_SubKeySpecified = false;
|
|
|
|
m_Empty = true;
|
2005-09-16 17:08:40 +04:00
|
|
|
m_GlobalScope = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
m_RegistryType = registryType;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
RegistryHelper::~RegistryHelper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool RegistryHelper::Open(const char *toplevel, const char *subkey,
|
|
|
|
int readonly)
|
2006-03-10 00:40:00 +03:00
|
|
|
{
|
2006-01-17 18:21:45 +03:00
|
|
|
this->EntriesMap.clear();
|
2005-09-20 00:19:57 +04:00
|
|
|
m_Empty = 1;
|
|
|
|
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
|
|
|
HKEY scope = HKEY_CURRENT_USER;
|
|
|
|
if ( this->GetGlobalScope() )
|
|
|
|
{
|
|
|
|
scope = HKEY_LOCAL_MACHINE;
|
|
|
|
}
|
|
|
|
int res = 0;
|
2005-09-16 17:08:40 +04:00
|
|
|
kwsys_ios::ostringstream str;
|
2005-09-16 16:20:48 +04:00
|
|
|
DWORD dwDummy;
|
2005-09-16 17:08:40 +04:00
|
|
|
str << "Software\\Kitware\\" << toplevel << "\\" << subkey;
|
|
|
|
if ( readonly == Registry::READONLY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
res = ( RegOpenKeyEx(scope, str.str().c_str(),
|
2005-09-16 16:20:48 +04:00
|
|
|
0, KEY_READ, &this->HKey) == ERROR_SUCCESS );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-09-16 17:08:40 +04:00
|
|
|
res = ( RegCreateKeyEx(scope, str.str().c_str(),
|
2006-03-10 00:40:00 +03:00
|
|
|
0, "", REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE,
|
|
|
|
NULL, &this->HKey, &dwDummy) == ERROR_SUCCESS );
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-17 16:50:10 +04:00
|
|
|
if ( res != 0 )
|
|
|
|
{
|
|
|
|
this->SetSubKey( subkey );
|
|
|
|
}
|
2005-09-16 17:08:40 +04:00
|
|
|
return (res != 0);
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 17:08:40 +04:00
|
|
|
bool res = false;
|
2005-09-16 16:20:48 +04:00
|
|
|
int cc;
|
|
|
|
kwsys_ios::ostringstream str;
|
2005-09-16 21:38:25 +04:00
|
|
|
const char* homeDirectory;
|
|
|
|
if ( (homeDirectory = getenv("HOME")) == 0 )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( (homeDirectory = getenv("USERPROFILE")) == 0 )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
return false;
|
2005-09-16 21:38:25 +04:00
|
|
|
}
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 21:38:25 +04:00
|
|
|
m_HomeDirectory = homeDirectory;
|
2006-01-17 18:21:45 +03:00
|
|
|
str << m_HomeDirectory.c_str() << "/." << toplevel << "rc";
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( readonly == Registry::READWRITE )
|
|
|
|
{
|
|
|
|
kwsys_ios::ofstream ofs( str.str().c_str(), kwsys_ios::ios::out|kwsys_ios::ios::app );
|
|
|
|
if ( ofs.fail() )
|
|
|
|
{
|
2005-09-16 17:08:40 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
ofs.close();
|
|
|
|
}
|
|
|
|
|
2005-09-16 17:15:52 +04:00
|
|
|
kwsys_ios::ifstream *ifs = new kwsys_ios::ifstream(str.str().c_str(), kwsys_ios::ios::in
|
|
|
|
#ifndef KWSYS_IOS_USE_ANSI
|
|
|
|
| kwsys_ios::ios::nocreate
|
|
|
|
#endif
|
|
|
|
);
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( !ifs )
|
|
|
|
{
|
2005-09-16 17:08:40 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
if ( ifs->fail())
|
|
|
|
{
|
|
|
|
delete ifs;
|
2005-09-16 17:08:40 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
2005-09-16 17:08:40 +04:00
|
|
|
res = true;
|
2005-09-16 18:09:38 +04:00
|
|
|
char buffer[Registry_BUFFER_SIZE];
|
2005-09-16 16:20:48 +04:00
|
|
|
while( !ifs->fail() )
|
|
|
|
{
|
2005-09-16 18:09:38 +04:00
|
|
|
ifs->getline(buffer, Registry_BUFFER_SIZE);
|
2005-09-16 16:20:48 +04:00
|
|
|
if ( ifs->fail() || ifs->eof() )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *line = this->Strip(buffer);
|
|
|
|
if ( *line == '#' || *line == 0 )
|
|
|
|
{
|
|
|
|
// Comment
|
|
|
|
continue;
|
2006-03-10 00:40:00 +03:00
|
|
|
}
|
2005-09-16 16:20:48 +04:00
|
|
|
int linelen = static_cast<int>(strlen(line));
|
|
|
|
for ( cc = 0; cc < linelen; cc++ )
|
|
|
|
{
|
|
|
|
if ( line[cc] == '=' )
|
|
|
|
{
|
|
|
|
char *key = new char[ cc+1 ];
|
|
|
|
strncpy( key, line, cc );
|
|
|
|
key[cc] = 0;
|
|
|
|
char *value = line + cc + 1;
|
|
|
|
char *nkey = this->Strip(key);
|
|
|
|
char *nvalue = this->Strip(value);
|
2005-09-16 21:38:25 +04:00
|
|
|
this->EntriesMap[nkey] = this->DecodeValue(nvalue);
|
2005-09-16 16:20:48 +04:00
|
|
|
m_Empty = 0;
|
|
|
|
delete [] key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifs->close();
|
|
|
|
this->SetSubKey( subkey );
|
|
|
|
delete ifs;
|
|
|
|
return res;
|
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool RegistryHelper::Close()
|
|
|
|
{
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 16:38:12 +04:00
|
|
|
int res;
|
2006-03-10 00:40:00 +03:00
|
|
|
res = ( RegCloseKey(this->HKey) == ERROR_SUCCESS );
|
2005-09-16 17:08:40 +04:00
|
|
|
return (res != 0);
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 17:15:52 +04:00
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 16:38:12 +04:00
|
|
|
if ( !m_Changed )
|
|
|
|
{
|
|
|
|
this->SetSubKey(0);
|
2005-09-16 17:20:28 +04:00
|
|
|
return true;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
2005-09-16 16:20:48 +04:00
|
|
|
|
2005-09-16 16:38:12 +04:00
|
|
|
kwsys_ios::ostringstream str;
|
2006-01-17 18:21:45 +03:00
|
|
|
str << m_HomeDirectory.c_str() << "/." << this->GetTopLevel() << "rc";
|
2005-09-16 16:38:12 +04:00
|
|
|
kwsys_ios::ofstream *ofs = new kwsys_ios::ofstream(str.str().c_str(), kwsys_ios::ios::out);
|
|
|
|
if ( !ofs )
|
|
|
|
{
|
2005-09-16 17:20:28 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
if ( ofs->fail())
|
|
|
|
{
|
|
|
|
delete ofs;
|
2005-09-16 17:20:28 +04:00
|
|
|
return false;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
|
|
|
*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
|
2005-09-16 21:38:25 +04:00
|
|
|
<< "# comments and empty lines will be deleted. Every line has" << kwsys_ios::endl
|
2005-09-16 16:38:12 +04:00
|
|
|
<< "# 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 )
|
|
|
|
{
|
2005-09-16 21:56:09 +04:00
|
|
|
*ofs << it->first.c_str() << " = " << this->EncodeValue(it->second.c_str()).c_str() << kwsys_ios::endl;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
|
|
|
}
|
2006-01-17 18:21:45 +03:00
|
|
|
this->EntriesMap.clear();
|
2005-09-16 16:38:12 +04:00
|
|
|
ofs->close();
|
|
|
|
delete ofs;
|
|
|
|
this->SetSubKey(0);
|
|
|
|
m_Empty = 1;
|
2005-09-16 17:20:28 +04:00
|
|
|
return true;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-09-16 21:38:25 +04:00
|
|
|
bool RegistryHelper::ReadValue(const char *skey, const char **value)
|
2005-09-16 16:20:48 +04:00
|
|
|
|
|
|
|
{
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string key = this->CreateKey( skey );
|
|
|
|
if ( key.empty() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-10 00:40:00 +03:00
|
|
|
DWORD dwType, dwSize;
|
2005-09-16 16:38:12 +04:00
|
|
|
dwType = REG_SZ;
|
2005-09-16 21:38:25 +04:00
|
|
|
char buffer[1024]; // Replace with RegQueryInfoKey
|
2005-09-17 16:50:10 +04:00
|
|
|
dwSize = sizeof(buffer);
|
2006-03-10 00:40:00 +03:00
|
|
|
int res = ( RegQueryValueEx(this->HKey, skey, NULL, &dwType,
|
2005-09-16 21:38:25 +04:00
|
|
|
(BYTE *)buffer, &dwSize) == ERROR_SUCCESS );
|
|
|
|
if ( !res )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->EntriesMap[key] = buffer;
|
|
|
|
RegistryHelper::StringToStringMap::iterator it
|
|
|
|
= this->EntriesMap.find(key);
|
|
|
|
*value = it->second.c_str();
|
|
|
|
return true;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 17:15:52 +04:00
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 17:20:28 +04:00
|
|
|
bool res = false;
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string key = this->CreateKey( skey );
|
|
|
|
if ( key.empty() )
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
2005-09-16 17:20:28 +04:00
|
|
|
return false;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
2005-09-16 21:38:25 +04:00
|
|
|
|
2005-09-16 16:38:12 +04:00
|
|
|
RegistryHelper::StringToStringMap::iterator it
|
|
|
|
= this->EntriesMap.find(key);
|
|
|
|
if ( it != this->EntriesMap.end() )
|
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
*value = it->second.c_str();
|
2005-09-16 17:20:28 +04:00
|
|
|
res = true;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
|
|
|
return res;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-09-16 21:38:25 +04:00
|
|
|
bool RegistryHelper::DeleteKey(const char* skey)
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
int res = ( RegDeleteKey( this->HKey, skey ) == ERROR_SUCCESS );
|
2005-09-16 17:08:40 +04:00
|
|
|
return (res != 0);
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
2005-09-16 17:15:52 +04:00
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string key = this->CreateKey( skey );
|
|
|
|
if ( key.empty() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->EntriesMap.erase(key);
|
|
|
|
return true;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool RegistryHelper::DeleteValue(const char *skey)
|
|
|
|
{
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
2005-09-22 17:58:03 +04:00
|
|
|
int res = ( RegDeleteValue( this->HKey, skey ) == ERROR_SUCCESS );
|
2005-09-16 17:08:40 +04:00
|
|
|
return (res != 0);
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
2005-09-16 17:15:52 +04:00
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string key = this->CreateKey( skey );
|
|
|
|
if ( key.empty() )
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
2005-09-16 17:20:28 +04:00
|
|
|
return false;
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
|
|
|
this->EntriesMap.erase(key);
|
2005-09-16 17:20:28 +04:00
|
|
|
return true;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool RegistryHelper::SetValue(const char *skey, const char *value)
|
|
|
|
{
|
2005-09-16 17:15:52 +04:00
|
|
|
#ifdef _WIN32
|
2005-09-16 17:08:40 +04:00
|
|
|
if ( m_RegistryType == Registry::WIN32_REGISTRY)
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
|
|
|
DWORD len = (DWORD)(value ? strlen(value) : 0);
|
2006-03-10 00:40:00 +03:00
|
|
|
int res = ( RegSetValueEx(this->HKey, skey, 0, REG_SZ,
|
|
|
|
(CONST BYTE *)(const char *)value,
|
2005-09-16 16:38:12 +04:00
|
|
|
len+1) == ERROR_SUCCESS );
|
2005-09-16 17:08:40 +04:00
|
|
|
return (res != 0);
|
2005-09-16 16:38:12 +04:00
|
|
|
}
|
2005-09-16 17:15:52 +04:00
|
|
|
#endif
|
2005-09-16 21:38:25 +04:00
|
|
|
if ( m_RegistryType == Registry::FILE_REGISTRY )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string key = this->CreateKey( skey );
|
|
|
|
if ( key.empty() )
|
2005-09-16 16:38:12 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-16 21:56:09 +04:00
|
|
|
this->EntriesMap[key] = value;
|
2005-09-16 16:38:12 +04:00
|
|
|
return 1;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 16:38:12 +04:00
|
|
|
return false;
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_stl::string RegistryHelper::CreateKey( const char *key )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
|
|
|
if ( !m_SubKeySpecified || m_SubKey.empty() || !key )
|
|
|
|
{
|
2005-09-16 21:38:25 +04:00
|
|
|
return "";
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
2005-09-16 21:38:25 +04:00
|
|
|
kwsys_ios::ostringstream ostr;
|
2006-01-17 18:21:45 +03:00
|
|
|
ostr << this->EncodeKey(this->m_SubKey.c_str()).c_str()
|
|
|
|
<< "\\" << this->EncodeKey(key).c_str();
|
2005-09-16 21:38:25 +04:00
|
|
|
return ostr.str();
|
2005-09-16 16:20:48 +04:00
|
|
|
}
|
|
|
|
|
2005-09-16 21:38:25 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2005-09-16 16:20:48 +04:00
|
|
|
void RegistryHelper::SetSubKey(const char* sk)
|
|
|
|
{
|
|
|
|
if ( !sk )
|
|
|
|
{
|
|
|
|
m_SubKey = "";
|
|
|
|
m_SubKeySpecified = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_SubKey = sk;
|
|
|
|
m_SubKeySpecified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
char *RegistryHelper::Strip(char *str)
|
|
|
|
{
|
2006-07-13 17:07:41 +04:00
|
|
|
int cc;
|
2006-03-30 22:49:56 +04:00
|
|
|
size_t len;
|
2005-09-16 16:20:48 +04:00
|
|
|
char *nstr;
|
|
|
|
if ( !str )
|
|
|
|
{
|
|
|
|
return NULL;
|
2006-03-10 00:40:00 +03:00
|
|
|
}
|
2005-09-16 16:20:48 +04:00
|
|
|
len = strlen(str);
|
|
|
|
nstr = str;
|
2006-07-13 17:07:41 +04:00
|
|
|
for( cc=0; cc < static_cast<int>(len); cc++ )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
|
|
|
if ( !isspace( *nstr ) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nstr ++;
|
|
|
|
}
|
2006-07-13 17:07:41 +04:00
|
|
|
for( cc= static_cast<int>(strlen(nstr))-1; cc>=0; cc-- )
|
2005-09-16 16:20:48 +04:00
|
|
|
{
|
|
|
|
if ( !isspace( nstr[cc] ) )
|
|
|
|
{
|
|
|
|
nstr[cc+1] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nstr;
|
|
|
|
}
|
|
|
|
|
2005-09-16 17:08:40 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void RegistryHelper::SetGlobalScope(bool b)
|
|
|
|
{
|
|
|
|
m_GlobalScope = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool RegistryHelper::GetGlobalScope()
|
|
|
|
{
|
|
|
|
return m_GlobalScope;
|
|
|
|
}
|
|
|
|
|
2005-09-16 21:38:25 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
kwsys_stl::string RegistryHelper::EncodeKey(const char* str)
|
|
|
|
{
|
|
|
|
kwsys_ios::ostringstream ostr;
|
|
|
|
while ( *str )
|
|
|
|
{
|
|
|
|
switch ( *str )
|
|
|
|
{
|
2005-09-16 21:56:09 +04:00
|
|
|
case '%': case '=': case '\n': case '\r': case '\t':
|
2005-09-16 21:38:25 +04:00
|
|
|
char buffer[4];
|
|
|
|
sprintf(buffer, "%%%02X", *str);
|
|
|
|
ostr << buffer;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ostr << *str;
|
|
|
|
}
|
|
|
|
str ++;
|
|
|
|
}
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
kwsys_stl::string RegistryHelper::EncodeValue(const char* str)
|
|
|
|
{
|
|
|
|
kwsys_ios::ostringstream ostr;
|
|
|
|
while ( *str )
|
|
|
|
{
|
|
|
|
switch ( *str )
|
|
|
|
{
|
2006-03-10 00:40:00 +03:00
|
|
|
case '%': case '=': case '\n': case '\r': case '\t':
|
2005-09-16 21:38:25 +04:00
|
|
|
char buffer[4];
|
|
|
|
sprintf(buffer, "%%%02X", *str);
|
|
|
|
ostr << buffer;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ostr << *str;
|
|
|
|
}
|
|
|
|
str ++;
|
|
|
|
}
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
kwsys_stl::string RegistryHelper::DecodeValue(const char* str)
|
|
|
|
{
|
|
|
|
kwsys_ios::ostringstream ostr;
|
|
|
|
while ( *str )
|
|
|
|
{
|
2005-09-20 00:19:57 +04:00
|
|
|
unsigned int val;
|
2005-09-16 21:38:25 +04:00
|
|
|
switch ( *str )
|
|
|
|
{
|
|
|
|
case '%':
|
|
|
|
if ( *(str+1) && *(str+2) && sscanf(str+1, "%x", &val) == 1 )
|
|
|
|
{
|
2006-03-10 20:01:29 +03:00
|
|
|
ostr << static_cast<char>(val);
|
2005-09-16 21:38:25 +04:00
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ostr << *str;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ostr << *str;
|
|
|
|
}
|
|
|
|
str ++;
|
|
|
|
}
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
2005-09-16 16:20:48 +04:00
|
|
|
} // namespace KWSYS_NAMESPACE
|