This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Preference File Reader
  Submitted by



This Code Of The Day is a preference file reader. I've seen several different implementations of preference readers, but I've never been satisfied with there functionality. So, last year I wrote my own. I wanted to have something that would allow me to read from .reg files, ini files, memory blocks, or any other medium. This utility also needed to work on several platforms PC, Mac, PS2, and others. On top of all this, I wanted some cool features such as type safety, iterators, and abstraction from the storage medium to name a few. So, without further a due here is my implementation.

I provided three different versions of Preference storage objects.
  • CFilePref for reading and writing file based preference data such as .ini and .reg files.

  • CRegistryPref for reading and writing windows registry based preference data.

  • CMemoryPref for reading and writing to preference data in memory (this version is very basic, but one could easily be made to read and write binary data to specific memory locations such as flash memory. I've done it)


  • NOTES:
  • The GetValue methods are type safe, if you try to get a value using the wrong type the call will fail.
  • The SetValue methods will not create a value, they will only set an existing value. The call will fail if the value does not exist.
  • The CreateValue methods will not modify existing values, if the value exists the call will fail.
  • These rules apply to key methods as well.
  • All methods work on the current working key. The only exceptions to this rule are the SetKey method and the Save methods.
  • This is a very simple example of how to use the pref objects, the use of each object is exactly the same except for load and save calls.
    //     create a new preference file
    CFilePref filePref;

    filePref.CreateKey("MyRoot"); filePref.SetKey("MyRoot"); filePref.CreateValue("Some string", "strMyString"); filePref.Save("C:\\example.prf");

    // load an existing preference file CFilePref filePref;

    if(filePref.Load("C:\\example.prf")) { std::string strMyString;

    filePref.GetValue(strMyString, "strMyString"); }

    // open a file and save it to the registry CFilePref filePref;

    if(filePref.Load("C:\\example.prf")) { CRegistryPref regPref;

    regPref.CreateKey("HKEY_CURRENT_USER\\Software\\PreferenceTest"); regPref.SetKey("HKEY_CURRENT_USER\\Software\\PreferenceTest"); regPref.Insert(filePref); regPref.Save("HKEY_CURRENT_USER\\Software\\PreferenceTest"); }

    // Generic use of preference data void LoadPreferences( IPreferenceStore &refPrefData) { if(refPrefData.PushKey("MySubKey")) { refPrefData.GetValue(m_nMyIntVal, "nMyIntVal"); refPrefData.PopKey(); } }



    Feel free to use this source in any way you see fit, just don't forget to credit its author. that would be me :-)


    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/defines.h] - (1,042 bytes)

    #if !defined( __DEFINES_H_ )
    #define __DEFINES_H_

    #include "ParserTable.h"

    typedef __int8 int8; typedef unsigned __int8 uint8;

    typedef __int16 int16; typedef unsigned __int16 uint16;

    typedef int int32; typedef unsigned int uint32;

    typedef float float32; typedef double float64;

    #define GET_PREFIX(_ind_) \ ms_ppszValuePrefix[_ind_]

    #define DECLARE_PREFIX_TABLE() \ static const char *ms_ppszValuePrefix[]

    #define IMPLEMENT_PREFIX_TABLE(_class_) \ const char * _class_::ms_ppszValuePrefix[] = \ { \ "string:\"", \ "int:", \ "dword:", \ "float:", \ "vector3:", \ "vector4:", \ "rgbacolor:", \ "hex:", \ "" \ };

    #define GET_POSTFIX(_ind_) \ ms_ppszValuePostfix[_ind_]

    #define DECLARE_POSTFIX_TABLE() \ static const char *ms_ppszValuePostfix[]

    #define IMPLEMENT_POSTFIX_TABLE(_class_) \ const char * _class_::ms_ppszValuePostfix[] = \ { \ "\"", \ "", \ "", \ "", \ "", \ "", \ "", \ "", \ };

    #endif // __DEFINES_H_

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/FilePref.cpp] - (11,213 bytes)

    /////////////////////////////////////////////////////////////////////////////
    //      created:        1/24/2002
    //      filename:       FilePref.cpp
    //      author:         Eli Pulsifer
    //
    //      purpose:        
    
    #include "stdafx.h"
    #include "defines.h"
    #include "FilePref.h"
    #include <io.h>

    using namespace PreferenceStore;

    IMPLEMENT_PREFIX_TABLE(CFilePref) IMPLEMENT_POSTFIX_TABLE(CFilePref) IMPLEMENT_PARSER_TABLE(CFilePref)

    /////////////////////////////////////////////////////////////////////////////////////////////////// CFilePref::CFilePref() : m_hFile(NULL) { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// CFilePref::~CFilePref() { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::LoadFromFile // Description : Loads preference data from a file // Return type : void // Argument : FILE * hFile // bool CFilePref::Load( const std::string & strPathName ) { bool bRet = false; m_hFile = fopen(strPathName.c_str(), "r"); if(m_hFile) { size_t nSize = _filelength(m_hFile->_file); char * pBuffer = new char[nSize + 1];

    fseek(m_hFile, 0, 0);

    nSize = fread(pBuffer, sizeof(char), nSize, m_hFile); // pad with a linefeed incase there is no linefeed at the end of the file pBuffer[nSize] = et_lf_endl;

    LoadPreffDataToTree(pBuffer, nSize + 1); SetKey(""); fclose(m_hFile); m_hFile = NULL; delete pBuffer; bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::SaveToFile // Description : Loads preference data to a file // Return type : void // Argument : FILE * hFile // bool CFilePref::Save( const std::string & strPathName ) { bool bRet = false;

    m_hFile = fopen( strPathName.c_str(), "w"); if(m_hFile) { SaveTree(RootKey()); fclose(m_hFile); m_hFile = NULL; bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::Save // Description : // Return type : bool // Argument : const std::string & strPathName // Argument : IPreferenceStore * pPreferences // bool CFilePref::Save( const std::string & strPathName, IPreferenceStore * pPreferences ) { bool bRet = false;

    m_hFile = fopen( strPathName.c_str(), "w"); if(m_hFile) { SaveTree(pPreferences->CurrentKey()); fclose(m_hFile); m_hFile = NULL; bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::OnSaveKey // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey & refKey // bool CFilePref::OnSaveKey( const std::string strPath, const PreferenceStore::SKey & refKey ) const { static std::string strBuffer; bool bRet = true;

    if(!refKey.Destroy()) { strBuffer = ((!strPath.empty()) ? "\n" : ""); strBuffer += "[" + strPath + "]\n"; bRet = strBuffer.size() == fwrite(strBuffer.c_str(), sizeof(char), strBuffer.size(), m_hFile); }

    return(bRet); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::OnSaveValue // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey & refKey // Argument : const SPreferenceValue & refValueName // Argument : const SPreferenceValue & refValue // bool CFilePref::OnSaveValue( const std::string strPath, const PreferenceStore::SKey & refKey, const PreferenceStore::SPreferenceValue & refValueName, const PreferenceStore::SPreferenceValue & refValue ) const { static std::string strBuffer; bool bRet = true;

    if(!refValue.Destroy()) { strBuffer = '"' + refValueName.GetDataAsText() + '"'; strBuffer += "="; strBuffer += GET_PREFIX(refValue.GetType()); strBuffer += refValue.GetDataAsText(); strBuffer += GET_POSTFIX(refValue.GetType()); strBuffer += "\n"; bRet = strBuffer.size() == fwrite(strBuffer.c_str(), sizeof(char), strBuffer.size(), m_hFile); }

    return(bRet); }



    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::LoadPreffDataToTree // Description : // Return type : void // Argument : char * pSourceData // Argument : uint32 nSourceLength // void CFilePref::LoadPreffDataToTree( char * pSourceData, size_t nSourceLength ) { uint32 nPos = 0; parser_state theState = et_seeking_key_name; SKey *pKey = NULL; std::string m_strKeyName; std::string strValueName; std::string strValue; bool bComment = false;

    while(nPos < nSourceLength) { switch(theState) { case et_comment: switch(sm_mapCode[pSourceData[nPos]]) { case et_cr_endl: case et_lf_endl: theState = theState.LastState(); break; default: break; } break; case et_seeking_key_name: // search for the start of a key name if(et_key_name_delim_start == sm_mapCode[pSourceData[nPos]]) { m_strKeyName.erase(); theState = et_reading_key_name; } else if(et_comment_delim == pSourceData[nPos]) { theState = et_comment; } break;

    case et_reading_key_name: // read the key name switch(sm_mapCode[pSourceData[nPos]]) { case et_cr_endl: case et_lf_endl: case et_key_name_delim_start: case et_value_delim: assert(false); break; case et_key_name_delim_end: theState = et_seeking_value_name; Trim(m_strKeyName); if(!CreateKeys(pKey, m_strKeyName, RootKey(), false)) assert(false); break; case et_print_char: m_strKeyName += pSourceData[nPos]; default: break; } break;

    case et_seeking_value_name: // search for the next value or key name switch(sm_mapCode[pSourceData[nPos]]) { case et_cr_endl: case et_lf_endl: break; case et_key_name_delim_start: m_strKeyName.erase(); theState = et_reading_key_name; break; case et_value_delim: strValueName.erase(); strValue.erase(); theState = et_reading_value; break; default: if(et_comment_delim == pSourceData[nPos]) { theState = et_comment; } else { // dont lose the first character nPos--; strValueName.erase(); theState = et_reading_value_name; } break; } break;

    case et_reading_value_name: // reade the value name switch(sm_mapCode[pSourceData[nPos]]) { case et_cr_endl: case et_lf_endl: theState = et_seeking_value_name; break; case et_key_name_delim_start: case et_key_name_delim_end: assert(false); break; case et_print_char: strValueName += pSourceData[nPos]; break; case et_value_delim: Trim(strValueName); Trim(strValueName, "\""); theState = et_reading_value; break; default: break; } break;

    case et_reading_value: // read data switch(sm_mapCode[pSourceData[nPos]]) { case et_cr_endl: case et_lf_endl: { SPreferenceValue objValue; Trim(strValue); ConvertToValue(objValue, strValue); pKey->AddValue(strValueName, objValue); theState = et_seeking_value_name; strValue.erase(); } break; case et_key_name_delim_start: case et_key_name_delim_end: case et_value_delim: case et_print_char: strValue += pSourceData[nPos]; break; default: break; } break; }; nPos++; } }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::ConvertToValue // Description : Converts the data in strData to an SPreferenceValue object // Return type : void // Argument : SPreferenceValue & refValue // Argument : const std::string & strData // void CFilePref::ConvertToValue( PreferenceStore::SPreferenceValue & refValue, const std::string & strData ) { static std::string strBuffer;

    if(!strData.empty()) { if(strData[0] == '\"') { strBuffer = strData; Trim(strBuffer, "\""); refValue.SetData(strBuffer); } else { char *pBuffer = new char[strData.size() + 1]; char *pTemp; std::string strToken; std::string strValue; SPreferenceValue::ValueType eType = SPreferenceValue::kUndefinedType;

    strcpy(pBuffer, strData.c_str());

    pTemp = strtok(pBuffer, ":"); _strlwr(pTemp); strToken = pTemp; strToken += ':';

    if(0 == strncmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kStringType), strToken.size())) eType = SPreferenceValue::kStringType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kUnsignedIntegerType))) eType = SPreferenceValue::kUnsignedIntegerType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kBinaryType))) eType = SPreferenceValue::kBinaryType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kSignedIntegerType))) eType = SPreferenceValue::kSignedIntegerType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kFloatType))) eType = SPreferenceValue::kFloatType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kVector3Type))) eType = SPreferenceValue::kVector3Type; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kVector4Type))) eType = SPreferenceValue::kVector4Type; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kRGBAColorType))) eType = SPreferenceValue::kRGBAColorType; if(eType == SPreferenceValue::kUndefinedType) { eType = SPreferenceValue::kStringType; strValue = strData.c_str(); } else { strValue = strData.substr(strToken.size()); }

    if(eType == SPreferenceValue::kStringType) Trim(strValue, "\"");

    refValue.SetRawData(strValue.empty() ? "" : strValue, eType); delete[] pBuffer; } } refValue.SetDirty(false); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CFilePref::Trim // Description : Removes any leading and trailing values in strRemove from strToTrim // Return type : void // Argument : std::string & strToTrim // Argument : const std::string & strRemove // void CFilePref::Trim( std::string & strToTrim, const std::string & strRemove /*= " "*/ ) { std::string::size_type nBegin = strToTrim.find_first_not_of(strRemove); std::string::size_type nEnd = strToTrim.find_last_not_of(strRemove); if(std::string::npos == nBegin) { strToTrim.erase(); } else { nEnd = std::string::npos == nEnd ? 0 : nEnd; strToTrim = strToTrim.substr( nBegin, nEnd - nBegin + 1); } }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/FilePref.h] - (2,619 bytes)

    /////////////////////////////////////////////////////////////////////////////
    //      created:        1/24/2002
    //      filename:       FilePref.h
    //      author:         Eli Pulsifer
    //
    //      purpose:			
    
    #if !defined(_FILEPREF_H)
    #define _FILEPREF_H

    #if _MSC_VER > 1000 #pragma once

    #pragma warning ( disable : 4786 ) #endif // _MSC_VER > 1000 #include "PreferenceStore.h" #include <iostream.h>

    class CFilePref : public PreferenceStore::CPreferenceStore { public: enum parser_code { et_print_char, et_nonprint_char, et_escape_char = '\\', et_key_name_delim_start = '[', et_key_name_delim_end = ']', et_value_delim = '=', et_cr_endl = '\r', et_lf_endl = '\n', et_comment_delim = ';' }; public:

    private: enum state { et_seeking_key_name, et_reading_key_name, et_seeking_value_name, et_reading_value_name, et_reading_value, et_comment };

    struct parser_state { parser_state(state eState) : m_eState(eState), m_eLastState(eState) {} parser_state(const parser_state & theState) : m_eState(theState.m_eState), m_eLastState(theState.m_eLastState) {} void SetState(state eState) { m_eLastState = m_eState; m_eState = eState; } state State(void) const { return m_eState; } state LastState(void) const { return m_eLastState; } operator state() const { return m_eState; } const parser_state & operator = ( state eNewState ) { SetState(eNewState); return *this; } private: state m_eState; state m_eLastState; }; public:

    CFilePref(); virtual ~CFilePref();

    // Load and save functions bool Load( const std::string & strPathName ); bool Save( const std::string & strPathName ); bool Save( const std::string & strPathName, IPreferenceStore * pPreferences );

    private: bool OnSaveKey( const std::string strPath, const PreferenceStore::SKey & refKey ) const; bool OnSaveValue( const std::string strPath, const PreferenceStore::SKey & refKey, const PreferenceStore::SPreferenceValue & refValueName, const PreferenceStore::SPreferenceValue & refValue ) const; void LoadPreffDataToTree( char * pSourceData, size_t m_nSourceLength ); void Trim( std::string & strToTrim, const std::string & strRemove = " " ); void ConvertToValue( PreferenceStore::SPreferenceValue & refValue, const std::string & strData );

    DECLARE_PARSER_TABLE(); DECLARE_PREFIX_TABLE(); DECLARE_POSTFIX_TABLE();

    #if defined( _MSC_VER ) mutable FILE * m_hFile; #elif defined( __MWERKS__ ) mutable std::FILE * m_hFile; #endif

    };

    #endif // !defined(_FILEPREF_H)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/MemoryPref.cpp] - (849 bytes)

    // MemoryPref.cpp: implementation of the CMemoryPref class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "defines.h"
    #include "MemoryPref.h"

    ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// using namespace PreferenceStore;

    CMemoryPref::CMemoryPref() {

    }

    CMemoryPref::~CMemoryPref() {

    } bool CMemoryPref::OnSaveKey(const std::string strPath, const PreferenceStore::SKey& refKey) const { return false; } bool CMemoryPref::OnSaveValue(const std::string strPath, const PreferenceStore::SKey& refKey, const PreferenceStore::SPreferenceValue& refValueName, const PreferenceStore::SPreferenceValue& refValue) const { return false; }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/MemoryPref.h] - (917 bytes)

    // MemoryPref.h: interface for the CMemoryPref class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_MEMORYPREF_H__9C80332A_85B6_463B_9120_72BE2CCA7193__INCLUDED_)
    #define AFX_MEMORYPREF_H__9C80332A_85B6_463B_9120_72BE2CCA7193__INCLUDED_

    #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "PreferenceStore.h"

    class CMemoryPref : public PreferenceStore::CPreferenceStore { public: CMemoryPref(); virtual ~CMemoryPref();

    protected: virtual bool OnSaveKey(const std::string strPath, const PreferenceStore::SKey& refKey) const; virtual bool OnSaveValue(const std::string strPath, const PreferenceStore::SKey& refKey, const PreferenceStore::SPreferenceValue& refValueName, const PreferenceStore::SPreferenceValue& refValue) const; };

    #endif // !defined(AFX_MEMORYPREF_H__9C80332A_85B6_463B_9120_72BE2CCA7193__INCLUDED_)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/ParserTable.h] - (2,676 bytes)

    #if !defined (DECLARE_PARSER_TABLE)

    #define DECLARE_PARSER_TABLE() static parser_code sm_mapCode[]

    #define IMPLEMENT_PARSER_TABLE( _parser_name_ ) \ _parser_name_::parser_code _parser_name_::sm_mapCode[] = \ {\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_lf_endl,\ et_nonprint_char,\ et_nonprint_char,\ et_cr_endl,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_nonprint_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_value_delim,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_key_name_delim_start,\ et_print_char,\ et_key_name_delim_end,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_print_char,\ et_nonprint_char,\ };

    #endif // !defined(DECLARE_PARSER_TABLE)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/preference.cpp] - (1,765 bytes)

    // preference.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "FilePref.h"
    #include "RegistryPref.h"

    int _tmain(int argc, _TCHAR* argv[]) { CFilePref filePref; std::string strValue;

    filePref.CreateKey("MyRoot"); filePref.SetKey("MyRoot"); filePref.CreateKey("MySubKey1"); filePref.CreateKey("MySubKey2");

    filePref.CreateValue(0.7f, "myFloat"); filePref.CreateValue(-7, "myInt"); filePref.CreateValue(0xFFFF000F, "myUint"); filePref.CreateValue("Hello World", "myString"); filePref.CreateValue(D3DXVECTOR3(0.0f, 0.0f, 1.0f), "myVector3"); filePref.CreateValue(D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f), "myVector4"); filePref.CreateValue(D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f), "myColor"); filePref.CreateValue((void*)"Some Binary Data", 16, "myBinary"); filePref.SetKey("MyRoot\\MySubKey1"); filePref.CreateValue(D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f), "myColor"); filePref.SetKey("MyRoot\\MySubKey2"); filePref.CreateValue(D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f), "myColor");

    filePref.DestroyKey("MyRoot\\MySubKey1");

    filePref.Save("C:\\test.prf");

    // CRegistryPref regPref; // filePref.SetKey(""); // regPref.CreateKey("HKEY_CURRENT_USER\\Software\\Preference"); // regPref.SetKey("HKEY_CURRENT_USER\\Software\\Preference"); // regPref.Insert(filePref); // regPref.Save("HKEY_CURRENT_USER\\Software\\Preference"); filePref.DestroyKey("MyRoot"); filePref.Purge(); filePref.GetValue(strValue, "myString"); filePref.SetKey("MyRoot");

    filePref.Load("C:\\Program Files\\Intel\\VTune50\\vtune.reg"); filePref.SetKey("HKEY_CURRENT_USER\\Software\\VB and VBA Program Settings\\VTune 4.0\\Globals"); filePref.GetValue(strValue, "GridFontName");

    return 0; }


    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/PreferenceStore.cpp] - (38,664 bytes)

    /////////////////////////////////////////////////////////////////////////////
    //      created:        1/24/2002
    //      filename:       PreferenceStore.cpp
    //      author:         Eli Pulsifer
    //
    //      purpose:        
    
    #include "stdafx.h"
    #include "defines.h"
    #include "PreferenceStore.h"
    #include <algorithm>

    using namespace PreferenceStore;

    bool IsValueDestroyed(const std::pair<SPreferenceValue, SPreferenceValue>& p) { return p.second.Destroy(); }; bool IsKeyDestroyed(const std::pair<std::string, SKey*>& p) { return p.second->Destroy(); };

    template <class AssociativeContainer, class Predicate> void remove_if(AssociativeContainer& C, Predicate p) { typedef typename AssociativeContainer::iterator iterator;

    iterator cur = C.begin(); const iterator last = C.end();

    while ((cur = std::find_if(cur, last, p)) != last) { iterator tmp = cur++; C.erase(tmp); } }

    ////////////////////////////////////////////////////////////////////// // SKey implementation ////////////////////////////////////////////////////////////////////// SKey::SKey(SKey* pParent, const std::string& strName, bool bDirty) : m_pParentKey(pParent) , m_bDirty(bDirty) , m_bDestroy(false) , m_strKeyName(strName) { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// const std::string & SKey::Name() const { return m_strKeyName; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// void SKey::SetDirty(bool bDirty) { m_bDirty = bDirty; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// bool SKey::Dirty() const { return m_bDirty; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// void SKey::SetDestroy(bool bDestroy) { bDestroy ? m_bDirty = m_bDestroy = bDestroy : m_bDestroy = bDestroy; }

    bool SKey::Destroy() const { return m_bDestroy; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// void SKey::AddValue(const std::string& strValueName, const SPreferenceValue& refValue, bool bNameDirty) { SPreferenceValue valueName;

    valueName.SetData(strValueName); valueName.SetDirty(bNameDirty); m_mapData.insert(valuemap::value_type(strValueName, refValue)); }

    ////////////////////////////////////////////////////////////////////// // Function name : NumSubKeys // Description : // Return type : uint32 void // size_t SKey::NumSubKeys() const { return m_mapSubKeys.size(); }

    ////////////////////////////////////////////////////////////////////// // Function name : NumValues // Description : // Return type : uint32 void // size_t SKey::NumValues() const { return m_mapData.size(); }

    ////////////////////////////////////////////////////////////////////// // Key Iterator implementation ////////////////////////////////////////////////////////////////////// IPreferenceStore::KeyIterator::KeyIterator() { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::KeyIterator::KeyIterator(keymap::iterator iKey) { m_iKey = iKey; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::KeyIterator::operator std::string() const { return (*m_iKey).second->Name(); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// const IPreferenceStore::KeyIterator& IPreferenceStore::KeyIterator::operator ++(int) { m_iKey++; return *this; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::KeyIterator::operator == (const IPreferenceStore::KeyIterator &iKey) const { return m_iKey == iKey.m_iKey; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::KeyIterator::operator != (const IPreferenceStore::KeyIterator &iKey) const { return m_iKey != iKey.m_iKey; }

    ////////////////////////////////////////////////////////////////////// // Value Iterator implementation ////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::ValueIterator() { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::ValueIterator(valuemap::iterator iValue) { m_iValue = iValue; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::operator SPreferenceValue() const { return (*m_iValue).second; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::operator std::string() const { return (*m_iValue).first.GetDataAsText(); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// const IPreferenceStore::ValueIterator& IPreferenceStore::ValueIterator::operator ++(int) { m_iValue++; return *this; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::operator == (const IPreferenceStore::ValueIterator &iValue) const { return m_iValue == iValue.m_iValue; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// IPreferenceStore::ValueIterator::operator != (const IPreferenceStore::ValueIterator &iValue) const { return m_iValue != iValue.m_iValue; }

    ////////////////////////////////////////////////////////////////////// // CPreferenceStore implementation ////////////////////////////////////////////////////////////////////// CPreferenceStore::CPreferenceStore() : m_pRootKey(NULL) , m_etValueNameType(SPreferenceValue::kStringType) , m_pCurrentKey(NULL) , m_bAutoDelete(true) { m_pRootKey = new SKey(NULL, "", false); m_pCurrentKey = m_pRootKey; }

    ///////////////////////////////////////////////////////////////////////////// CPreferenceStore::CPreferenceStore(SKey & rootKey) : m_pRootKey(NULL) , m_etValueNameType(SPreferenceValue::kStringType) , m_pCurrentKey(NULL) , m_bAutoDelete(false) { m_pRootKey = &rootKey; m_pCurrentKey = m_pRootKey; }

    ///////////////////////////////////////////////////////////////////////////// CPreferenceStore::~CPreferenceStore() { if(m_bAutoDelete) { FreeSubkeys(*m_pRootKey); delete m_pRootKey; } m_pCurrentKey = NULL; m_pRootKey = NULL; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::FreeSubkeys // Description : Destroyes all the subkeys of pKey // Return type : void // Argument : SKey * pKey // void CPreferenceStore::FreeSubkeys(PreferenceStore::SKey& refKey) { while(refKey.m_mapSubKeys.begin() != refKey.m_mapSubKeys.end()) { FreeSubkeys(*(*refKey.m_mapSubKeys.begin()).second); delete (*refKey.m_mapSubKeys.begin()).second; refKey.m_mapSubKeys.erase(refKey.m_mapSubKeys.begin()); } }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::DestroyValuesAndSubkeys // Description : // Return type : void // Argument : SKey * pKey // void CPreferenceStore::DestroyValuesAndSubkeys(PreferenceStore::SKey& refKey) { keymap::const_iterator iK; valuemap::iterator iV;

    // mark all sub keys for (iK = refKey.m_mapSubKeys.begin() ; iK != refKey.m_mapSubKeys.end() ; iK++) { DestroyValuesAndSubkeys(*(*iK).second);

    (*iK).second->SetDirty(); (*iK).second->SetDestroy(); }

    // mark all values for (iV = refKey.m_mapData.begin() ; iV != refKey.m_mapData.end() ; iV++) { (*iV).second.SetDestroy(); } }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateKeys // Description : Creates a key tree that matches the privide path std::string using m_pParentKey as the root // Return type : SKey * // Argument : const std::string& m_strKeyName // Argument : SKey& refParentKey // bool CPreferenceStore::CreateKeys(SKey *& pNewKey, const std::string& m_strKeyName, SKey& refParentKey, bool bDirty) { bool bRet = false; std::string strNewKeyName = m_strKeyName.substr(0, m_strKeyName.find('\\')); SKey *pCurrentKey = NULL;

    if(!strNewKeyName.empty()) { keymap::iterator i = refParentKey.m_mapSubKeys.find(strNewKeyName);

    // if no key is found create a new one if(refParentKey.m_mapSubKeys.end() == i) { pCurrentKey = new SKey(&refParentKey, strNewKeyName, bDirty); refParentKey.m_mapSubKeys.insert(keymap::value_type(strNewKeyName, pCurrentKey)); pNewKey = pCurrentKey; bRet = true; } // if one exists clear the destroy flag if its set else { pCurrentKey = (*i).second; if((*i).second->Destroy()) { pCurrentKey->SetDestroy(false); pNewKey = pCurrentKey; bRet = true; } } // if this is not the last key, create the sub keys if(strNewKeyName != m_strKeyName) { bRet = CreateKeys(pNewKey, m_strKeyName.substr(m_strKeyName.find('\\') + 1, m_strKeyName.size() - 1), *pCurrentKey); } }

    return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetPathToKey // Description : Gets the path to the provided key // Return type : void // Argument : std::string& strPath // Argument : const SKey& refKey // void CPreferenceStore::GetPathToKey(std::string& strPath, const SKey& refKey) { if(refKey.m_pParentKey) { BuildPathToKey(strPath, *refKey.m_pParentKey); strPath += refKey.m_strKeyName; } }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::BuildPathToKey // Description : Recursively builds the path to the provided key // Return type : void // Argument : std::string& strPath // Argument : const SKey& refKey // void CPreferenceStore::BuildPathToKey(std::string& strPath, const SKey& refKey) { if(refKey.m_pParentKey) { BuildPathToKey(strPath, *refKey.m_pParentKey); strPath += refKey.m_strKeyName + '\\'; } } /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::FindKey // Description : Finds the key that mathces the path / name m_strKeyName // Return type : SKey * // Argument : const std::string& m_strKeyName // Argument : SKey * m_pParentKey // bool CPreferenceStore::FindKey(PreferenceStore::SKey *& pKey, std::string& strName, const std::string& m_strKeyName, PreferenceStore::SKey& refParentKey) { std::string strNewKeyName = m_strKeyName.substr(0, m_strKeyName.find('\\')); bool bResult = false;

    if(!strNewKeyName.empty()) { keymap::iterator i = refParentKey.m_mapSubKeys.find(strNewKeyName); // if we find the key and it has not been marked to destroy continue our search if(refParentKey.m_mapSubKeys.end() != i && !(*i).second->Destroy()) { if(strNewKeyName != m_strKeyName) { bResult = FindKey(pKey, strName, m_strKeyName.substr(m_strKeyName.find('\\') + 1, m_strKeyName.size() - 1), *(*i).second); } else { pKey = (*i).second; strName = m_strKeyName; bResult = true; } } }

    return(bResult); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetKey // Description : Sets the current working key (location in the key tree) // Return type : bool // Argument : const std::string& m_strKeyName // bool CPreferenceStore::SetKey(const std::string& m_strKeyName) { std::string strActiveKeyName; SKey * pNewActiveKey; bool bRet = false; if(m_strKeyName.empty()) { m_pCurrentKey = m_pRootKey; bRet = true; } else { if(FindKey(pNewActiveKey, strActiveKeyName, m_strKeyName, *m_pRootKey)) { m_pCurrentKey = pNewActiveKey; bRet = true; } }

    return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::PushKey // Description : // Return type : bool // Argument : const std::string& m_strKeyName // bool CPreferenceStore::PushKey(const std::string& m_strKeyName) { bool bRet = false;

    keymap::iterator i = m_pCurrentKey->m_mapSubKeys.find(m_strKeyName); if(i != m_pCurrentKey->m_mapSubKeys.end() ) { m_pCurrentKey = (*i).second; bRet = true; }

    return bRet; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::PopKey // Description : // Return type : bool // Argument : void // bool CPreferenceStore::PopKey(void) { bool bRet = false;

    if(m_pCurrentKey != m_pRootKey) { m_pCurrentKey = m_pCurrentKey->m_pParentKey; bRet = true; } return bRet; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::IsChildKey // Description : // Return type : bool // Argument : const std::string& m_strKeyName // bool CPreferenceStore::IsChildKey(const std::string& m_strKeyName) { bool bRet = false;

    keymap::iterator i = m_pCurrentKey->m_mapSubKeys.find(m_strKeyName); bRet = i != m_pCurrentKey->m_mapSubKeys.end();

    return bRet; } /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateKey // Description : Creates a key tree that matches the privide path std::string using the main root key as the root // Return type : bool // Argument : const std::string& m_strKeyName // bool CPreferenceStore::CreateKey(const std::string& m_strKeyName) { SKey *pNewKey; return(CreateKeys(pNewKey, m_strKeyName, *m_pCurrentKey)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetKey // Description : Gets the name of the current working key // Return type : bool // Argument : std::string& m_strKeyName // bool CPreferenceStore::GetKey(std::string& m_strKeyName) const { if(m_pRootKey != m_pCurrentKey) m_strKeyName = m_pCurrentKey->m_strKeyName;

    return m_pRootKey != m_pCurrentKey; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : Gets the value associated with strValueName in the current working key // Return type : bool // Argument : std::string& strValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(std::string& strValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(strValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : Sets the value associated with strValueName in the current working key // Return type : bool // Argument : const std::string& strValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(const std::string& strValue, const std::string& strValueName) { SPreferenceValue objValue(strValue);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : Gets the value associated with strValueName in the current working key // Return type : bool // Argument : uint32& dwValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(uint32& dwValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(dwValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : Sets the value associated with strValueName in the current working key // Return type : bool // Argument : uint32 dwValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(uint32 dwValue, const std::string& strValueName) { SPreferenceValue objValue(dwValue);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : Gets the value associated with strValueName in the current working key // Return type : bool // Argument : void * pValue // Argument : uint32& nLength // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(void * pValue, uint32& nLength, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(pValue, nLength); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : Sets the value associated with strValueName in the current working key // Return type : bool // Argument : const void * pValue // Argument : uint32 nLength // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(const void * pValue, uint32 nLength, const std::string& strValueName) { SPreferenceValue objValue(pValue, nLength);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : // Return type : bool // Argument : int32& nValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(int32& nValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(nValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : // Return type : bool // Argument : int32 nValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(int32 nValue, const std::string& strValueName) { SPreferenceValue objValue(nValue);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : // Return type : bool // Argument : float32& fValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(float32& fValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(fValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : // Return type : bool // Argument : float32 fValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(float32 fValue, const std::string& strValueName) { SPreferenceValue objValue(fValue);

    return(SetValue(objValue, strValueName)); }



    #if defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : // Return type : bool // Argument : D3DXVECTOR3 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(D3DXVECTOR3 &vecValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(vecValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : // Return type : bool // Argument : const D3DXVECTOR3 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName) { SPreferenceValue objValue(vecValue);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : // Return type : bool // Argument : D3DXVECTOR4 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(D3DXVECTOR4 &vecValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(vecValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : // Return type : bool // Argument : const D3DXVECTOR4 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(const D3DXVECTOR4 &vecValue, const std::string& strValueName) { SPreferenceValue objValue(vecValue);

    return(SetValue(objValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : // Return type : bool // Argument : D3DXCOLOR &colorValue // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(D3DXCOLOR &colorValue, const std::string& strValueName) const { SPreferenceValue objValue; bool bRet = false; if(GetValue(objValue, strValueName)) { bRet = objValue.GetData(colorValue); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : // Return type : bool // Argument : const D3DXCOLOR &colorValue // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(const D3DXCOLOR &colorValue, const std::string& strValueName) { SPreferenceValue objValue(colorValue);

    return(SetValue(objValue, strValueName)); } #endif // defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : Creates a value with the name strValueName in the current working key // Return type : bool // Argument : const std::string& strValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(const std::string& strValue, const std::string& strValueName) { SPreferenceValue objNewValue(strValue);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : Creates a value with the name strValueName in the current working key // Return type : bool // Argument : uint32 dwValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(uint32 dwValue, const std::string& strValueName) { SPreferenceValue objNewValue(dwValue);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : // Return type : bool // Argument : float32 fValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(float32 fValue, const std::string& strValueName) { SPreferenceValue objNewValue(fValue);

    return(CreateValue(objNewValue, strValueName)); }

    #if defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : // Return type : bool // Argument : const D3DXVECTOR3 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName) { SPreferenceValue objNewValue(vecValue);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : // Return type : bool // Argument : const D3DXVECTOR4 &vecValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(const D3DXVECTOR4 &vecValue, const std::string& strValueName) { SPreferenceValue objNewValue(vecValue);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : // Return type : bool // Argument : const D3DXCOLOR &colorValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(const D3DXCOLOR &colorValue, const std::string& strValueName) { SPreferenceValue objNewValue(colorValue);

    return(CreateValue(objNewValue, strValueName)); } #endif // defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : // Return type : bool // Argument : int32 nValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(int32 nValue, const std::string& strValueName) { SPreferenceValue objNewValue(nValue);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : Creates a value with the name strValueName in the current working key // Return type : bool // Argument : const void * pValue // Argument : uint32 nLength // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(const void * pValue, uint32 nLength, const std::string& strValueName) { SPreferenceValue objNewValue(pValue, nLength);

    return(CreateValue(objNewValue, strValueName)); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::DestroyValue // Description : Destroy the value with the name strValueName in the current working key // Return type : bool // Argument : const std::string& strValueName // bool CPreferenceStore::DestroyValue(const std::string& strValueName) { bool bRet = false; if(m_pRootKey != m_pCurrentKey) { valuemap::iterator i = m_pCurrentKey->m_mapData.find(strValueName); // if the value exists and has not been marked to destroy we can set the destroy flag bRet = (i != m_pCurrentKey->m_mapData.end()) && !(*i).second.Destroy();

    if(bRet) { (*i).second.SetDestroy(); // m_pCurrentKey->m_mapData.erase(i); } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::ValueBegin // Description : // Return type : IPreferenceStore::ValueIterator // IPreferenceStore::ValueIterator CPreferenceStore::ValueBegin() const { return ValueIterator(m_pCurrentKey->m_mapData.begin()); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::ValueEnd // Description : // Return type : IPreferenceStore::ValueIterator // IPreferenceStore::ValueIterator CPreferenceStore::ValueEnd() const { return ValueIterator(m_pCurrentKey->m_mapData.end()); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::DestroyKey // Description : Destroys the the named key and all sub keys // Return type : bool // Argument : const std::string& m_strKeyName // bool CPreferenceStore::DestroyKey(const std::string& m_strKeyName) { std::string strKeyToDestroyName; SKey * pKeyToDestroy;

    // mark the key to destroy if we can find it and its not the root bool bRet = FindKey(pKeyToDestroy, strKeyToDestroyName, m_strKeyName, *m_pRootKey) ? (pKeyToDestroy != m_pRootKey) : false;

    if(bRet) { pKeyToDestroy->SetDirty(); pKeyToDestroy->SetDestroy(); DestroyValuesAndSubkeys(*pKeyToDestroy); }

    return(bRet); }

    ////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::NumSubKeys // Description : // Return type : uint32 // size_t CPreferenceStore::NumSubKeys() const { return m_pCurrentKey->NumSubKeys(); }

    ////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::NumValues // Description : // Return type : uint32 // size_t CPreferenceStore::NumValues() const { return m_pCurrentKey->NumValues(); }

    ////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::KeyBegin // Description : // Return type : IPreferenceStore::KeyIterator // IPreferenceStore::KeyIterator CPreferenceStore::KeyBegin() const { return KeyIterator(m_pCurrentKey->m_mapSubKeys.begin()); }

    ////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::KeyEnd // Description : // Return type : IPreferenceStore::KeyIterator // IPreferenceStore::KeyIterator CPreferenceStore::KeyEnd() const { return KeyIterator(m_pCurrentKey->m_mapSubKeys.end()); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SetValue // Description : Sets an SPreferenceValue object in the working key if it exist // Return type : bool // Argument : const std::string& strValue // Argument : value_type eType // Argument : const std::string& strValueName // bool CPreferenceStore::SetValue(SPreferenceValue& refValue, const std::string& strValueName) { bool bRet = false;

    if(m_pRootKey != m_pCurrentKey) { valuemap::iterator i = m_pCurrentKey->m_mapData.find(strValueName); // if the value exists and has not been marked to destroy we can set the value bRet = (i != m_pCurrentKey->m_mapData.end()) && !(*i).second.Destroy();

    if(bRet) { (*i).second = refValue; } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::GetValue // Description : Gets an SPreferenceValue object from the working key if it exist // Return type : bool // Argument : std::string& strValue // Argument : value_type eType // Argument : const std::string& strValueName // bool CPreferenceStore::GetValue(SPreferenceValue& refValue, const std::string& strValueName) const { bool bRet = false;

    if(m_pRootKey != m_pCurrentKey) { valuemap::iterator i = m_pCurrentKey->m_mapData.find(strValueName); // if the value exists and has not been marked to destroy we can get the value bRet = (i != m_pCurrentKey->m_mapData.end()) && !(*i).second.Destroy();

    if(bRet) { refValue = (*i).second; } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CreateValue // Description : Adds an SPreferenceValue object to the working key if it does not already exist // Return type : bool // Argument : SPreferenceValue& refValue // Argument : const std::string& strValueName // bool CPreferenceStore::CreateValue(SPreferenceValue& refValue, const std::string& strValueName) { bool bRet = false;

    if(m_pRootKey != m_pCurrentKey) { valuemap::iterator i = m_pCurrentKey->m_mapData.find(strValueName); // if the value does not exist or has been marked to destroy we can add the new value bRet = (i == m_pCurrentKey->m_mapData.end()) || (*i).second.Destroy();

    if(bRet) { m_pCurrentKey->m_mapData[SPreferenceValue(strValueName, m_etValueNameType)] = refValue; } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::CurrentKey // Description : // Return type : SKey & // Argument : void // SKey& CPreferenceStore::CurrentKey() { return(*m_pCurrentKey); } /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::RootKey // Description : // Return type : SKey & // Argument : void // SKey& CPreferenceStore::RootKey() { return(*m_pRootKey); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::SaveTree // Description : // Return type : void // Argument : SKey * pKey // Argument : const std::string& m_strKeyName // bool CPreferenceStore::SaveTree(SKey& refKey) { bool bRet = true; std::string strPath; valuemap::iterator iV; keymap::iterator iK;

    // if key is marked destroy, dont save if(refKey.Destroy()) return true; GetPathToKey(strPath, refKey);

    // Save the Key if(!strPath.empty()) { bRet = OnSaveKey(strPath, refKey); }

    if(bRet) { // Save the Value name / value pairs for (iV = refKey.m_mapData.begin() ; bRet && iV != refKey.m_mapData.end() ; iV++) { if(!(*iV).second.Destroy()) { bRet = OnSaveValue(strPath, refKey, (*iV).first, (*iV).second); } }

    // Recurs sub keys for (iK = refKey.m_mapSubKeys.begin() ; bRet && iK != refKey.m_mapSubKeys.end() ; iK++) { bRet = SaveTree(*(*iK).second); } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::Purge // Description : // Return type : void // void CPreferenceStore::Purge() { Purge(m_pRootKey); } void CPreferenceStore::Purge(SKey* pKey) { keymap::iterator iK;

    // Execute the destroy on marked keys and values remove_if(pKey->m_mapSubKeys, IsKeyDestroyed); remove_if(pKey->m_mapData, IsValueDestroyed);

    // Recurs to sub keys for (iK = pKey->m_mapSubKeys.begin() ; iK != pKey->m_mapSubKeys.end() ; ++iK) { Purge((*iK).second); } } /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::OnSaveKey // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey& refKey // bool CPreferenceStore::OnSaveKey(const std::string strPath, const SKey& refKey) const { return false; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::OnSaveValue // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey& refKey // Argument : const SPreferenceValue& refValueName // Argument : const SPreferenceValue& refValue // bool CPreferenceStore::OnSaveValue(const std::string strPath, const SKey& refKey, const SPreferenceValue& refValueName, const SPreferenceValue& refValue) const { return false; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::Insert // Description : // Return type : bool // Argument : IPreferenceStore &ref // bool CPreferenceStore::Insert(IPreferenceStore &ref) { return Insert(&ref.CurrentKey()); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::Insert // Description : // Return type : bool // Argument : PreferenceStore::SKey* pKey // bool CPreferenceStore::Insert(PreferenceStore::SKey* pKey) { valuemap::iterator iV; keymap::iterator iK;

    for(iV = pKey->m_mapData.begin() ; iV != pKey->m_mapData.end() ; ++iV) { if(!(*iV).second.Destroy()) CreateValue((*iV).second, static_cast<const char*>((*iV).first)); }

    for (iK = pKey->m_mapSubKeys.begin() ; iK != pKey->m_mapSubKeys.end() ; ++iK) { if(!(*iK).second->Destroy()) { CreateKey((*iK).first); PushKey((*iK).first); Insert((*iK).second); PopKey(); } } return true; }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/PreferenceStore.h] - (10,127 bytes)

    /////////////////////////////////////////////////////////////////////////////
    //      created:        1/24/2002
    //      filename:       PreferenceStore.h
    //      author:         Eli Pulsifer
    //
    //      purpose:        
    
    #if !defined _PREFERENCESTORE_H
    #define _PREFERENCESTORE_H

    #if _MSC_VER > 1000 #pragma once #pragma warning ( disable : 4786 ) #endif // _MSC_VER > 1000 #include "PreferenceValue.h" #include <map>

    namespace PreferenceStore { class CPreferenceStore; struct SKey;

    typedef std::map<SPreferenceValue, SPreferenceValue> valuemap; typedef std::map<std::string, SKey*> keymap;

    struct SKey { friend CPreferenceStore; SKey(SKey* pParent, const std::string& strName, bool bDirty);

    const std::string & Name() const; bool Dirty() const; bool Destroy() const; void AddValue(const std::string& strValueName, const SPreferenceValue& refValue, bool bNameDirty = false); size_t NumSubKeys() const; size_t NumValues() const;

    protected: void SetDirty(bool bDirty = true); void SetDestroy(bool bDestroy = true);

    valuemap m_mapData; keymap m_mapSubKeys; std::string m_strKeyName; SKey* m_pParentKey; bool m_bDirty; bool m_bDestroy; }; }

    struct IPreferenceStore { struct KeyIterator { KeyIterator(); KeyIterator(PreferenceStore::keymap::iterator iKey); operator std::string() const; const KeyIterator& operator ++( int ); operator == (const KeyIterator &iKey) const; operator != (const KeyIterator &iKey) const; private: PreferenceStore::keymap::iterator m_iKey; };

    struct ValueIterator { ValueIterator(); ValueIterator(PreferenceStore::valuemap::iterator iValue); operator std::string() const; operator PreferenceStore::SPreferenceValue() const; const ValueIterator& operator ++( int ); operator == (const ValueIterator &iValue) const; operator != (const ValueIterator &iValue) const; private: PreferenceStore::valuemap::iterator m_iValue; }; virtual ~IPreferenceStore(){}

    // Key manipulation functions virtual bool GetKey(std::string& m_strKeyName) const = 0; virtual bool SetKey(const std::string& m_strKeyName) = 0; virtual bool PushKey(const std::string& m_strKeyName) = 0; virtual bool PopKey(void) = 0; virtual bool IsChildKey(const std::string& m_strKeyName) = 0; virtual bool CreateKey(const std::string& m_strKeyName) = 0; virtual bool DestroyKey(const std::string& m_strKeyName) = 0; virtual void Purge() = 0; virtual size_t NumSubKeys() const = 0; virtual size_t NumValues() const = 0; virtual KeyIterator KeyBegin() const = 0; virtual KeyIterator KeyEnd() const = 0; virtual bool Insert(IPreferenceStore &ref) = 0;

    // Value manipulation functions virtual bool GetValue(std::string& strValue, const std::string& strValueName) const = 0; virtual bool SetValue(const std::string& strValue, const std::string& strValueName) = 0; virtual bool CreateValue(const std::string& strValue, const std::string& strValueName) = 0;

    virtual bool GetValue(uint32& dwValue, const std::string& strValueName) const = 0; virtual bool SetValue(uint32 dwValue, const std::string& strValueName) = 0; virtual bool CreateValue(uint32 dwValue, const std::string& strValueName) = 0;

    virtual bool GetValue(int32& nValue, const std::string& strValueName) const = 0; virtual bool SetValue(int32 nValue, const std::string& strValueName) = 0; virtual bool CreateValue(int32 nValue, const std::string& strValueName) = 0;

    virtual bool GetValue(float32& fValue, const std::string& strValueName) const = 0; virtual bool SetValue(float32 fValue, const std::string& strValueName) = 0; virtual bool CreateValue(float32 fValue, const std::string& strValueName) = 0; virtual bool GetValue(PreferenceStore::SPreferenceValue& refValue, const std::string& strValueName) const = 0; virtual bool SetValue(PreferenceStore::SPreferenceValue& refValue, const std::string& strValueName) = 0; virtual bool CreateValue(PreferenceStore::SPreferenceValue& refValue, const std::string& strValueName) = 0;

    // DirectX math and color types #if defined(DIRECT3D_VERSION) virtual bool GetValue(D3DXVECTOR4& vValue, const std::string& strValueName) const = 0; virtual bool SetValue(const D3DXVECTOR4 &vValue, const std::string& strValueName) = 0; virtual bool CreateValue(const D3DXVECTOR4 &vValue, const std::string& strValueName) = 0; virtual bool GetValue(D3DXVECTOR3 &vecValue, const std::string& strValueName) const = 0; virtual bool SetValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName) = 0; virtual bool CreateValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName) = 0; virtual bool GetValue(D3DXCOLOR &colorValue, const std::string& strValueName) const = 0; virtual bool SetValue(const D3DXCOLOR &colorValue, const std::string& strValueName) = 0; virtual bool CreateValue(const D3DXCOLOR &colorValue, const std::string& strValueName) = 0; #endif // defined(DIRECT3D_VERSION) virtual bool GetValue(void * pValue, uint32& nLength, const std::string& strValueName) const = 0; virtual bool SetValue(const void * pValue, uint32 nLength, const std::string& strValueName) = 0; virtual bool CreateValue(const void * pValue, uint32 nLength, const std::string& strValueName) = 0;

    virtual bool DestroyValue(const std::string& strValueName) = 0;

    virtual ValueIterator ValueBegin() const = 0; virtual ValueIterator ValueEnd() const = 0;

    virtual PreferenceStore::SKey& CurrentKey(void) = 0; virtual PreferenceStore::SKey& RootKey(void) = 0; };

    namespace PreferenceStore { class CPreferenceStore: public IPreferenceStore { public: CPreferenceStore(); CPreferenceStore(PreferenceStore::SKey & rootKey); virtual ~CPreferenceStore();

    // Key manipulation functions bool GetKey(std::string& m_strKeyName) const; bool SetKey(const std::string& m_strKeyName); bool PushKey(const std::string& m_strKeyName); bool PopKey(void); bool IsChildKey(const std::string& m_strKeyName); bool CreateKey(const std::string& m_strKeyName); bool DestroyKey(const std::string& m_strKeyName); void Purge(); size_t NumSubKeys() const; size_t NumValues() const; KeyIterator KeyBegin() const; KeyIterator KeyEnd() const; bool Insert(IPreferenceStore &ref);

    // Value manipulation functions bool GetValue(std::string& strValue, const std::string& strValueName) const; bool SetValue(const std::string& strValue, const std::string& strValueName); bool CreateValue(const std::string& strValue, const std::string& strValueName);

    bool GetValue(uint32& dwValue, const std::string& strValueName) const; bool SetValue(uint32 dwValue, const std::string& strValueName); bool CreateValue(uint32 dwValue, const std::string& strValueName);

    bool GetValue(int32& nValue, const std::string& strValueName) const; bool SetValue(int32 nValue, const std::string& strValueName); bool CreateValue(int32 nValue, const std::string& strValueName);

    bool GetValue(float32& fValue, const std::string& strValueName) const; bool SetValue(float32 fValue, const std::string& strValueName); bool CreateValue(float32 fValue, const std::string& strValueName);

    // DirectX math and color types #if defined(DIRECT3D_VERSION) bool GetValue(D3DXVECTOR4& vValue, const std::string& strValueName) const; bool SetValue(const D3DXVECTOR4 &vValue, const std::string& strValueName); bool CreateValue(const D3DXVECTOR4 &vValue, const std::string& strValueName); bool GetValue(D3DXVECTOR3 &vecValue, const std::string& strValueName) const; bool SetValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName); bool CreateValue(const D3DXVECTOR3 &vecValue, const std::string& strValueName);

    bool GetValue(D3DXCOLOR &colorValue, const std::string& strValueName) const; bool SetValue(const D3DXCOLOR &colorValue, const std::string& strValueName); bool CreateValue(const D3DXCOLOR &colorValue, const std::string& strValueName); #endif // defined(DIRECT3D_VERSION) bool GetValue(void * pValue, uint32& nLength, const std::string& strValueName) const; bool SetValue(const void * pValue, uint32 nLength, const std::string& strValueName); bool CreateValue(const void * pValue, uint32 nLength, const std::string& strValueName);

    bool GetValue(SPreferenceValue& refValue, const std::string& strValueName) const; bool SetValue(SPreferenceValue& refValue, const std::string& strValueName); bool CreateValue(SPreferenceValue& refValue, const std::string& strValueName);

    bool DestroyValue(const std::string& strValueName);

    virtual ValueIterator ValueBegin() const; virtual ValueIterator ValueEnd() const;

    protected:

    bool SaveTree(PreferenceStore::SKey& refKey); virtual bool OnSaveKey(const std::string strPath, const PreferenceStore::SKey& refKey) const; virtual bool OnSaveValue(const std::string strPath, const PreferenceStore::SKey& refKey, const SPreferenceValue& refValueName, const SPreferenceValue& refValue) const; void Purge(PreferenceStore::SKey* pKey); bool Insert(PreferenceStore::SKey* pKey);

    bool CreateKeys(PreferenceStore::SKey *& pNewKey, const std::string& m_strKeyName, PreferenceStore::SKey& refParentKey, bool bDirty = true); void GetPathToKey(std::string& strPath, const PreferenceStore::SKey& refKey); void BuildPathToKey(std::string& strPath, const PreferenceStore::SKey& refKey); void FreeSubkeys(PreferenceStore::SKey& refKey); void DestroyValuesAndSubkeys(PreferenceStore::SKey& refKey); bool FindKey(PreferenceStore::SKey *& pKey, std::string& strName, const std::string& m_strKeyName, PreferenceStore::SKey& refParentKey);

    PreferenceStore::SKey & RootKey(void); PreferenceStore::SKey & CurrentKey(void);

    private: bool m_bAutoDelete; PreferenceStore::SKey* m_pRootKey; PreferenceStore::SKey* m_pCurrentKey; std::string m_strCurrentKeyName; SPreferenceValue::ValueType m_etValueNameType; }; } #endif // !defined(_PREFERENCESTORE_H)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/PreferenceValue.cpp] - (22,573 bytes)

    ///////////////////////////////////////////////////////////////////////////////
    //      created:        8/13/2001
    //      filename:       PreferenceValue.cpp
    //      author:         Eli Pulsifer
    //
    
    #include "stdafx.h"
    #include "defines.h"
    #include "PreferenceValue.h"
    #include "assert.h"

    using namespace PreferenceStore;

    SPreferenceValue::SPreferenceValue(const SPreferenceValue& value) : m_eType(value.m_eType) , m_pRawData(NULL) , m_nLength(0) , m_bDirty(value.m_bDirty) , m_bDestroy(value.m_bDestroy) { // copy the value data, if necessary if (value.m_nLength > 0) { AllocData(value.m_nLength); memcpy(m_pRawData, value.m_pRawData, m_nLength); } }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(bool isDirty) : m_eType(kUndefinedType) , m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(const std::string& stringData, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(stringData); } /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(const void* buffer, uint32 bufferLength, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(buffer, bufferLength); }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(uint32 integerValue, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(integerValue); }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(int32 integerValue, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(integerValue); }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(float floatValue, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(floatValue); }

    /////////////////////////////////////////////////////////////////////////////// #if defined(DIRECT3D_VERSION) SPreferenceValue::SPreferenceValue(const D3DXVECTOR3 &vecData, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(vecData); } /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(const D3DXVECTOR4 &vecValue, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(vecValue); } SPreferenceValue::SPreferenceValue(const D3DXCOLOR &colorData, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetData(colorData); } #endif // defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::SPreferenceValue(const std::string& data, ValueType dataType, bool isDirty) : m_pRawData(NULL) , m_nLength(0) , m_bDirty(isDirty) , m_bDestroy(false) { SetRawData(data, dataType); }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::~SPreferenceValue() { FreeData(); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves std::string data if present // Return type : bool // Argument : std::string & strData // bool SPreferenceValue::GetData(std::string& stringData) const { bool isValid = (kStringType == m_eType);

    if (isValid) stringData = reinterpret_cast<char*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to std::string and applies std::string data // Return type : void // Argument : const std::string & strData // void SPreferenceValue::SetData(const std::string& stringData) { FreeData();

    m_eType = kStringType; AllocData(stringData.size() + 1); strcpy(reinterpret_cast<char*>(m_pRawData), stringData.c_str()); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : char* // Description : // Return type : SPreferenceValue::operator const // SPreferenceValue::operator const char*() const { return reinterpret_cast<char*>(m_pRawData); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves uint32 data if present // Return type : bool // Argument : uint32 & dwData // bool SPreferenceValue::GetData(uint32& integerData) const { bool isValid = (kUnsignedIntegerType == m_eType);

    if (isValid) integerData = *reinterpret_cast<uint32*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to uint32 and applies uint32 data // Return type : void // Argument : uint32 dwData // void SPreferenceValue::SetData(uint32 integerData) { FreeData();

    m_eType = kUnsignedIntegerType; AllocData(sizeof(uint32)); *reinterpret_cast<uint32*>(m_pRawData) = integerData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves int32 data if present // Return type : bool // Argument : uint32 & dwData // bool SPreferenceValue::GetData(int32& integerData) const { bool isValid = (kSignedIntegerType == m_eType);

    if (isValid) integerData = *reinterpret_cast<int32*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : int32 // Description : // Return type : SPreferenceValue::operator // SPreferenceValue::operator int32() const { assert(kFloatType == m_eType); return *reinterpret_cast<int32*>(m_pRawData); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to uint32 and applies uint32 data // Return type : void // Argument : uint32 dwData // void SPreferenceValue::SetData(float floatData) { FreeData();

    m_eType = kFloatType; AllocData(sizeof(float)); *reinterpret_cast<float*>(m_pRawData) = floatData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves int32 data if present // Return type : bool // Argument : uint32 & dwData // bool SPreferenceValue::GetData(float & floatData) const { bool isValid = (kFloatType == m_eType);

    if (isValid) floatData = *reinterpret_cast<float*>(m_pRawData);

    return isValid; }

    ////////////////////////////////////////////////////////////////////// // Function name : float32 // Description : // Return type : SPreferenceValue::operator // SPreferenceValue::operator float32() const { assert(kFloatType == m_eType); return *reinterpret_cast<float32*>(m_pRawData); }

    #if defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves D3DXVECTOR3 data if present // Return type : bool // Argument : D3DXVECTOR3 & vecData // bool SPreferenceValue::GetData(D3DXVECTOR3 &vecData) const { bool isValid = (kVector3Type == m_eType);

    if (isValid) vecData = *reinterpret_cast<D3DXVECTOR3*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to D3DXVECTOR3 and applies D3DXVECTOR3 data // Return type : void // Argument : const D3DXVECTOR3 & vecData // void SPreferenceValue::SetData(const D3DXVECTOR3 & vecData) { FreeData();

    m_eType = kVector3Type; AllocData(sizeof(D3DXVECTOR3)); *reinterpret_cast<D3DXVECTOR3*>(m_pRawData) = vecData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves D3DXVECTOR4 data if present // Return type : bool // Argument : D3DXVECTOR4 & vecData // bool SPreferenceValue::GetData(D3DXVECTOR4 &vecData) const { bool isValid = (kVector4Type == m_eType);

    if (isValid) vecData = *reinterpret_cast<D3DXVECTOR4*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to D3DXVECTOR4 and applies D3DXVECTOR4 data // Return type : void // Argument : const D3DXVECTOR4 & vecData // void SPreferenceValue::SetData(const D3DXVECTOR4 & vecData) { FreeData();

    m_eType = kVector4Type; AllocData(sizeof(D3DXVECTOR4)); *reinterpret_cast<D3DXVECTOR4*>(m_pRawData) = vecData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : D3DXVECTOR4 // Description : // Return type : SPreferenceValue::operator // SPreferenceValue::operator D3DXVECTOR4() const { assert(kVector4Type == m_eType); return *reinterpret_cast<D3DXVECTOR4*>(m_pRawData); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves D3DXVECTOR3 data if present // Return type : bool // Argument : D3DXVECTOR3 & vecData // bool SPreferenceValue::GetData(D3DXCOLOR &colorData) const { bool isValid = (kRGBAColorType == m_eType);

    if (isValid) colorData = *reinterpret_cast<D3DXCOLOR*>(m_pRawData);

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to D3DXVECTOR3 and applies D3DXVECTOR3 data // Return type : void // Argument : const D3DXVECTOR3 & vecData // void SPreferenceValue::SetData(const D3DXCOLOR &colorData) { FreeData();

    m_eType = kRGBAColorType; AllocData(sizeof(D3DXCOLOR)); *reinterpret_cast<D3DXCOLOR*>(m_pRawData) = colorData; } #endif // defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to int32 and applies uint32 data // Return type : void // Argument : uint32 dwData // void SPreferenceValue::SetData(int32 integerData) { FreeData();

    m_eType = kSignedIntegerType; AllocData(sizeof(int32)); *reinterpret_cast<int32*>(m_pRawData) = integerData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetData // Description : Retrieves binary data if present // Return type : bool // Argument : void * pData // Argument : uint32 nLength // bool SPreferenceValue::GetData(void* buffer, uint32& bufferLength) const { bool isValid = (kBinaryType == m_eType);

    if (isValid) { uint32 nLengthToGet = m_nLength > bufferLength ? bufferLength : m_nLength; memcpy(buffer, m_pRawData, nLengthToGet); bufferLength = nLengthToGet; }

    return isValid; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetData // Description : Sets type to binary and applies binary data // Return type : void // Argument : const void * pData // Argument : uint32 nLength // void SPreferenceValue::SetData(const void* buffer, uint32 bufferLength) { FreeData();

    m_eType = kBinaryType; AllocData(bufferLength); memcpy(m_pRawData, buffer, bufferLength); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::GetDataAsText // Description : Retrieves the contained data as a formated std::string // Return type : std::string // Argument : void // std::string SPreferenceValue::GetDataAsText() const { std::string stringData;

    switch (m_eType) { case kStringType: GetData(stringData); break;

    case kUnsignedIntegerType: { uint32 integerData; char pszBuffer[11]; GetData(integerData); sprintf(pszBuffer, "%1X", integerData); stringData = pszBuffer; } break;

    case kSignedIntegerType: { int32 integerData; char pszBuffer[11]; GetData(integerData); sprintf(pszBuffer, "%1d", integerData); stringData = pszBuffer; } break;

    case kFloatType: { float floatData; char pszBuffer[43]; GetData(floatData); sprintf(pszBuffer, "%0.4f", floatData); stringData = pszBuffer; } break;

    case kBinaryType: ConvertToString(stringData, m_pRawData, m_nLength); break; #if defined(DIRECT3D_VERSION) case kVector3Type: { D3DXVECTOR3 vecData; char pszBuffer[128]; GetData(vecData); sprintf(pszBuffer, "%f, %f, %f", vecData.x, vecData.y, vecData.z); stringData = pszBuffer; } break;

    case kVector4Type: { D3DXVECTOR4 vecData; char pszBuffer[128]; GetData(vecData); sprintf(pszBuffer, "%f, %f, %f, %f", vecData.x, vecData.y, vecData.z, vecData.w); stringData = pszBuffer; } break;

    case kRGBAColorType: { D3DXCOLOR colorData; char pszBuffer[128]; GetData(colorData); sprintf(pszBuffer, "%f, %f, %f, %f", colorData.r, colorData.g, colorData.b, colorData.a); stringData = pszBuffer; } break; #endif // defined(DIRECT3D_VERSION) case kUndefinedType: { char* pszBuffer = new char[m_nLength + 1]; memcpy(pszBuffer, m_pRawData, m_nLength); pszBuffer[m_nLength] = '\0'; stringData = pszBuffer; delete pszBuffer; } }

    return stringData; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::SetRawData // Description : Sets the type and contained data from a formatted std::string // Return type : void // Argument : const std::string & strData // Argument : ValueType eType // void SPreferenceValue::SetRawData(const std::string& data, ValueType dataType) { switch (dataType) { case kStringType: SetData(data); break;

    case kUnsignedIntegerType: SetData((uint32)strtoul(data.c_str(), NULL, 16)); break; case kSignedIntegerType: SetData((int32)strtol(data.c_str(), NULL, 10)); break; case kFloatType: SetData((float)strtod(data.c_str(), NULL)); break;

    #if defined(DIRECT3D_VERSION) case kVector3Type: { D3DXVECTOR3 vecData; ConvertToD3DXVECTOR3(vecData, data); SetData(vecData); } break; case kVector4Type: { D3DXVECTOR4 vecData; ConvertToD3DXVECTOR4(vecData, data); SetData(vecData); } break; case kRGBAColorType: { D3DXCOLOR colorData; ConvertToD3DXCOLOR(colorData, data); SetData(colorData); } break; #endif // defined(DIRECT3D_VERSION) case kBinaryType: { int nByteCount = 1; int nPos; char* pszBuffer = _strdup(data.c_str()); // count the bytes for(nPos = 0; pszBuffer[nPos] != '\0' ; ++nPos) { if (',' == pszBuffer[nPos]) nByteCount++; }

    FreeData(); m_eType = dataType; AllocData(nByteCount);

    char* pszToken = strtok(pszBuffer, ",");

    nPos = 0;

    // read each byte, convert to bin and set the dest memory value while (pszToken != NULL) { m_pRawData[nPos] = pszToken[0] - 0x30; m_pRawData[nPos] <<= 4; m_pRawData[nPos] = pszToken[1] - 0x30; pszToken = strtok(NULL, ","); } // return the number of bytes set in the buffer } break;

    case kUndefinedType: { FreeData(); AllocData(data.size() + 1); strcpy(reinterpret_cast<char*>(m_pRawData), data.c_str()); } break; } }

    /////////////////////////////////////////////////////////////////////////////// SPreferenceValue::ValueType SPreferenceValue::GetType() const { return m_eType; }

    /////////////////////////////////////////////////////////////////////////////// size_t SPreferenceValue::GetSize() const { return m_nLength; }

    /////////////////////////////////////////////////////////////////////////////// void SPreferenceValue::SetDirty(bool isDirty) { m_bDirty = isDirty; }

    /////////////////////////////////////////////////////////////////////////////// bool SPreferenceValue::IsDirty() const { return m_bDirty; }

    /////////////////////////////////////////////////////////////////////////////// void SPreferenceValue::SetDestroy(bool bDestroy) { m_bDirty = m_bDestroy = bDestroy; }

    /////////////////////////////////////////////////////////////////////////////// bool SPreferenceValue::Destroy() const { return m_bDestroy; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::operator < // Description : Binary less than compare // Return type : bool SPreferenceValue::operator // Argument : const SPreferenceValue & refValue // bool SPreferenceValue::operator < (const SPreferenceValue& rhs) const { bool bRet;

    if(m_nLength < rhs.m_nLength) bRet = true; else if(m_nLength > rhs.m_nLength) bRet = false; else bRet = 0 > memcmp(m_pRawData, rhs.m_pRawData, m_nLength);

    return bRet; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::operator = // Description : Assignment // Return type : const SPreferenceValue // Argument : const SPreferenceValue & refValue // const SPreferenceValue& SPreferenceValue::operator = (const SPreferenceValue& rhs) { FreeData(); m_eType = rhs.m_eType; AllocData(rhs.m_nLength); memcpy(m_pRawData, rhs.m_pRawData, m_nLength); m_bDirty = rhs.m_bDirty; m_bDestroy = rhs.m_bDestroy; return *this; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::ConvertToString // Description : Converts bytes pointed to by pValue to a hex std::string representation // Return type : void // Argument : std::string & strValue // Argument : const void * pValue // Argument : uint32 nLength // void SPreferenceValue::ConvertToString(std::string& stringValue, const void* buffer, size_t bufferLength) const { static char pszBuffer[3]; uint32 nPos;

    stringValue.erase();

    if(bufferLength) { stringValue.reserve((bufferLength * 2) + bufferLength - 1);

    for (nPos = 0; nPos < bufferLength; ++nPos) { _itoa((reinterpret_cast<unsigned char*>(const_cast<void*>(buffer)))[nPos], pszBuffer, 16); stringValue += pszBuffer; stringValue += ','; } stringValue.resize(stringValue.size() - 1); } }

    /////////////////////////////////////////////////////////////////////////////// // Function name : ConvertToFloatArray // Description : // Return type : void // Argument : float *pVals // Argument : uint32 nVals // Argument : const std::string& stringValue // void ConvertToFloatArray(float *pVals, uint32 nVals, const std::string& stringValue) { char* pszBuffer = _strdup(stringValue.c_str()); char* pszVal = NULL; uint32 iVal = 0;

    pszVal = strtok(pszBuffer, ","); while(pszVal && iVal < nVals) { pVals[iVal] = static_cast<float>(atof(pszVal)); iVal++; pszVal = strtok(NULL, ","); } free(pszBuffer); }

    #if defined(DIRECT3D_VERSION)

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::ConvertToD3DXVECTOR3 // Description : // Return type : void // Argument : D3DXVECTOR3 &vecData // Argument : const std::string& stringValue // void SPreferenceValue::ConvertToD3DXVECTOR3(D3DXVECTOR3 &vecData, const std::string& stringValue) const { ConvertToFloatArray(&vecData.x, 3, stringValue); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::ConvertToD3DXVECTOR4 // Description : // Return type : void // Argument : D3DXVECTOR4 &vecData // Argument : const std::string& stringValue // void SPreferenceValue::ConvertToD3DXVECTOR4(D3DXVECTOR4 &vecData, const std::string& stringValue) const { ConvertToFloatArray(&vecData.x, 4, stringValue); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::ConvertToD3DXCOLOR // Description : // Return type : void // Argument : D3DXCOLOE &colorData // Argument : const std::string& stringValue // void SPreferenceValue::ConvertToD3DXCOLOR(D3DXCOLOR &colorData, const std::string& stringValue) const { ConvertToFloatArray(&colorData.r, 4, stringValue); } #endif // defined(DIRECT3D_VERSION) /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::FreeData // Description : // Return type : void // void SPreferenceValue::FreeData() { delete m_pRawData; m_pRawData = NULL; m_nLength = 0; }

    /////////////////////////////////////////////////////////////////////////////// // Function name : SPreferenceValue::AllocData // Description : // Return type : void // Argument : uint32 dataSize // void SPreferenceValue::AllocData(size_t dataSize) { m_nLength = dataSize; m_pRawData = new uint8[m_nLength]; }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/PreferenceValue.h] - (3,665 bytes)

    /////////////////////////////////////////////////////////////////////////////
    //      created:        1/24/2002
    //      filename:       PreferenceValue.h
    //      author:         Eli Pulsifer
    //
    //      purpose:        
    
    #if !defined(_PREFERENCEVALUE_H)
    #define _PREFERENCEVALUE_H

    #if _MSC_VER > 1000 #pragma once #pragma warning ( disable : 4786 ) #endif // _MSC_VER > 1000 #include "defines.h" #include <string>

    namespace PreferenceStore {

    struct SPreferenceValue { public: enum ValueType { kStringType, kSignedIntegerType, kUnsignedIntegerType, kFloatType, kVector3Type, kVector4Type, kRGBAColorType, kBinaryType, kUndefinedType };

    SPreferenceValue(const SPreferenceValue& value); SPreferenceValue(bool isDirty = true); SPreferenceValue(const std::string& stringData, bool isDirty = true); SPreferenceValue(uint32 integerValue, bool isDirty = true); SPreferenceValue(int32 integerValue, bool isDirty = true); SPreferenceValue(float32 floatValue, bool isDirty = true); SPreferenceValue(const void* buffer, uint32 bufferLength, bool isDirty = true); SPreferenceValue(const std::string& data, ValueType dataType, bool isDirty = true);

    #if defined(DIRECT3D_VERSION) SPreferenceValue(const D3DXVECTOR4 &vecValue, bool isDirty = true); SPreferenceValue(const D3DXVECTOR3 &vectorData, bool isDirty = true); SPreferenceValue(const D3DXCOLOR &colorData, bool isDirty = true); #endif // defined(DIRECT3D_VERSION) ~SPreferenceValue();

    bool GetData(std::string& stringData) const; void SetData(const std::string& stringData); operator const char*() const;

    bool GetData(int32& integerData) const; void SetData(int32 integerData); operator int32() const;

    bool GetData(uint32& integerData) const; void SetData(uint32 integerData); operator uint32() const;

    bool GetData(float32& floatData) const; void SetData(float32 floatData); operator float32() const;

    bool GetData(void* buffer, uint32& bufferLength) const; void SetData(const void* buffer, uint32 bufferLength);

    #if defined(DIRECT3D_VERSION) bool GetData(D3DXVECTOR4 &vectorData) const; void SetData(const D3DXVECTOR4 &vectorData); operator D3DXVECTOR4() const;

    bool GetData(D3DXVECTOR3 &vectorData) const; void SetData(const D3DXVECTOR3 &vectorData); operator D3DXVECTOR3();

    bool GetData(D3DXCOLOR &colorData) const; void SetData(const D3DXCOLOR &colorData); operator D3DXCOLOR(); #endif // defined(DIRECT3D_VERSION) std::string GetDataAsText() const; void SetRawData(const std::string& data, ValueType dataType);

    ValueType GetType() const; size_t GetSize() const; void SetDirty(bool isDirty = true); bool IsDirty() const; void SetDestroy(bool bDestroy = true); bool Destroy() const;

    bool operator < (const SPreferenceValue& rhs) const; const SPreferenceValue& operator = (const SPreferenceValue& rhs);

    private: void ConvertToString(std::string& stringValue, const void* buffer, size_t bufferLength) const; #if defined(DIRECT3D_VERSION) void ConvertToD3DXVECTOR3(D3DXVECTOR3 &vectorData, const std::string& stringValue) const; void ConvertToD3DXVECTOR4(D3DXVECTOR4 &vecData, const std::string& stringValue) const; void ConvertToD3DXCOLOR(D3DXCOLOR &colorData, const std::string& stringValue) const; #endif // defined(DIRECT3D_VERSION) void FreeData(); void AllocData(size_t dataSize);

    ValueType m_eType; size_t m_nLength; uint8 *m_pRawData; bool m_bDirty; bool m_bDestroy; }; }

    #endif // !defined(_PREFERENCEVALUE_H)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/RegistryPref.cpp] - (5,995 bytes)

    // RegistryPreferenceStore.cpp: implementation of the CRegistryPref class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "defines.h"
    #include "RegistryPref.h"

    using namespace PreferenceStore;

    ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CRegistryPref::CRegistryPref() {

    }

    CRegistryPref::~CRegistryPref() {

    }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::Load // Description : // Return type : bool // Argument : const std::string & strPathName // bool CRegistryPref::Load( const std::string & strPathName ) { bool bRet(false); CRegistryKey theRootKey; if(theRootKey.Open(strPathName)) { SKey * pRootKey; CreateKeys(pRootKey, strPathName, RootKey(), false); EnumSubKeys(theRootKey, pRootKey); theRootKey.Close(); bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::Save // Description : // Return type : bool // Argument : const std::string & strPathName // bool CRegistryPref::Save( const std::string & strPathName ) { bool bRet(false); std::string strLastActiveKey;

    BuildPathToKey(strLastActiveKey, CurrentKey());

    strLastActiveKey = strLastActiveKey.substr(0, strLastActiveKey.find_last_of('\\'));

    if(SetKey(strPathName)) { bRet = SaveTree(CurrentKey()); SetKey(strLastActiveKey); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : Save // Description : // Return type : bool // Argument : const std::string & strPathName IPreferenceStore * pPreferences // bool CRegistryPref::Save( const std::string & strPathName, IPreferenceStore * pPreferences ) { bool bRet(false);

    std::string strLastActiveKey; pPreferences->GetKey(strLastActiveKey);

    if(pPreferences->SetKey(strPathName)) { bRet = SaveTree(pPreferences->CurrentKey()); pPreferences->SetKey(strLastActiveKey); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::EnumSubKeys // Description : // Return type : void // Argument : const CRegistryKey & refParentKey // Argument : SKey * pParentPreffKey // void CRegistryPref::EnumSubKeys( const CRegistryKey & refParentKey, PreferenceStore::SKey * pParentPreffKey ) { CRegistryKey::enum_context * pContext = NULL; CRegistryKey theSubKey;

    refParentKey.CreateEnumContext(pContext);

    EnumValues(refParentKey, pParentPreffKey);

    while(refParentKey.EnumSubKeys(theSubKey, pContext)) { SKey *pKey;

    if(CreateKeys(pKey, theSubKey.Name(), *pParentPreffKey, false)) { EnumSubKeys(theSubKey, pKey); theSubKey.Close(); } }

    refParentKey.DestroyEnumContext(pContext); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::EnumValues // Description : // Return type : void // Argument : const CRegistryKey & refKey // Argument : const CRegistryKey::enum_context * pContext // Argument : SKey * pParentPreffKey // void CRegistryPref::EnumValues( const CRegistryKey & refKey, PreferenceStore::SKey * pParentPreffKey ) { CRegistryKey::enum_context * pContext = NULL; CRegistryValue objRegValue; std::string strName; SPreferenceValue objValue; SPreferenceValue objValueName;

    refKey.CreateEnumContext(pContext);

    while(refKey.EnumValues(objRegValue, strName, pContext)) { objRegValue.GetValue(objValue); objValue.SetDirty(false); pParentPreffKey->AddValue(objRegValue.GetName(), objValue, false); }

    refKey.DestroyEnumContext(pContext); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::OnSaveKey // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey & refKey // bool CRegistryPref::OnSaveKey( const std::string strPath, const PreferenceStore::SKey & refKey ) const { bool bRet = true;

    if(refKey.Dirty()) { CRegistryKey theRegKey; if(refKey.Destroy()) { bRet = theRegKey.Open(strPath); if(bRet) { bRet = theRegKey.Destroy(); } } else { bRet = theRegKey.Create(strPath); if(bRet) { theRegKey.Close(); } } }

    return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryPref::OnSaveValue // Description : // Return type : bool // Argument : const std::string strPath // Argument : const SKey & refKey // Argument : const SPreferenceValue & refValueName // Argument : const SPreferenceValue & refValue // bool CRegistryPref::OnSaveValue( const std::string strPath, const PreferenceStore::SKey & refKey, const PreferenceStore::SPreferenceValue & refValueName, const PreferenceStore::SPreferenceValue & refValue ) const { bool bRet = true;

    if (refValue.IsDirty()) { CRegistryKey theRegKey;

    bRet = theRegKey.Open(strPath); if(bRet) { std::string strValueName; CRegistryValue theRegValue;

    refValueName.GetData(strValueName); if(refValue.Destroy()) { bRet = theRegValue.Open(theRegKey, strValueName); if(bRet) { bRet = theRegValue.Destroy(); } } else { bRet = theRegValue.Create(theRegKey, strValueName, refValue); } } }

    return(bRet); }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/RegistryPref.h] - (1,360 bytes)

    // RegistryPref.h: interface for the CRegistryPref class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_REGISTRYPREF_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_)
    #define AFX_REGISTRYPREF_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_

    #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "PreferenceStore.h" #include "WinRegistry.h"

    class CRegistryPref : public PreferenceStore::CPreferenceStore { public: CRegistryPref(); virtual ~CRegistryPref();

    // Load and save functions bool Load( const std::string & strPathName ); bool Save( const std::string & strPathName ); bool Save( const std::string & strPathName, IPreferenceStore * pPreferences );

    protected: bool OnSaveKey( const std::string strPath, const PreferenceStore::SKey & refKey ) const; bool OnSaveValue( const std::string strPath, const PreferenceStore::SKey & refKey, const PreferenceStore::SPreferenceValue & refValueName, const PreferenceStore::SPreferenceValue & refValue ) const; void EnumSubKeys( const CRegistryKey & refParentKey, PreferenceStore::SKey * pParentPreffKey ); void EnumValues( const CRegistryKey & refKey, PreferenceStore::SKey * pParentPreffKey );

    private: };

    #endif // !defined(AFX_REGISTRYPREF_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_)

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/stdafx.cpp] - (297 bytes)

    // stdafx.cpp : source file that includes just the standard includes
    // preference.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information
    
    #include "stdafx.h"

    // TODO: reference any additional headers you need in STDAFX.H // and not in this file

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/stdafx.h] - (415 bytes)

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    
    #pragma once

    #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #include <stdio.h> #include <tchar.h> #include <assert.h> #include <D3dx8math.h>

    // TODO: reference additional headers your program requires here

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/WinRegistry.cpp] - (19,797 bytes)

    // WinRegistry.cpp: implementation of the CRegistryKey and CRegistryValue classes.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "defines.h"
    #include "WinRegistry.h"

    using namespace PreferenceStore;

    IMPLEMENT_PREFIX_TABLE(CRegistryValue) IMPLEMENT_POSTFIX_TABLE(CRegistryValue)

    #define REG_SUCCESS(_arg_) (ERROR_SUCCESS==(_arg_))

    CRegistryKey::CRegKeyInfo::CRegKeyInfo() : m_dwSubKeys(0) , m_dwMaxSubKeyLen(0) , m_dwMaxClassLen(0) , m_dwValues(0) , m_dwMaxValueNameLen(0) , m_dwMaxValueLen(0) , m_dwSecurityDescriptor(0) { memset(&m_ftLastWriteTime, 0, sizeof(m_ftLastWriteTime)); }

    CRegistryKey::CRegKeyInfo::CRegKeyInfo(HKEY hKey) { DWORD dwClassLen = 0; DWORD dwRet = RegQueryInfoKey(hKey, NULL, &dwClassLen, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

    if(ERROR_SUCCESS == dwRet || ERROR_MORE_DATA == dwRet) { dwClassLen++; m_strClass.reserve(dwClassLen); if(SUCCEEDED(RegQueryInfoKey(hKey, &m_strClass[0], &dwClassLen, NULL, &m_dwSubKeys, &m_dwMaxSubKeyLen, &m_dwMaxClassLen, &m_dwValues, &m_dwMaxValueNameLen, &m_dwMaxValueLen, &m_dwSecurityDescriptor, &m_ftLastWriteTime))) { m_dwMaxSubKeyLen++; m_dwMaxClassLen++; m_dwMaxValueNameLen++; } } }

    CRegistryKey::CRegKeyInfo::~CRegKeyInfo() { }

    const CRegistryKey::CRegKeyInfo & CRegistryKey::CRegKeyInfo::operator = (const CRegKeyInfo & refToCopy) { m_strClass = refToCopy.m_strClass; m_dwSubKeys = refToCopy.m_dwSubKeys; m_dwMaxSubKeyLen = refToCopy.m_dwMaxSubKeyLen; m_dwMaxClassLen = refToCopy.m_dwMaxClassLen; m_dwValues = refToCopy.m_dwValues; m_dwMaxValueNameLen = refToCopy.m_dwMaxValueNameLen; m_dwMaxValueLen = refToCopy.m_dwMaxValueLen; m_dwSecurityDescriptor = refToCopy.m_dwSecurityDescriptor; memcpy(&m_ftLastWriteTime, &refToCopy.m_ftLastWriteTime, sizeof(FILETIME)); return(*this); }



    CRegistryKey::CRegistryKey() : m_hKey(NULL) { }

    CRegistryKey::~CRegistryKey() { }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Open // Description : // Return type : bool // Argument : const std::string & strPath // Argument : DWORD dwAccess // bool CRegistryKey::Open(const std::string & strPath, DWORD dwAccess) // dwAccess = KEY_ALL_ACCESS { bool bRet = false; HKEY hKeyRoot; std::string strSubKey; ExtractKeyHandleAndSubKey( hKeyRoot, strSubKey, strPath );

    if(Open(hKeyRoot, strSubKey, dwAccess)) { m_theKeyInfo = CRegKeyInfo(m_hKey); m_strPath = strPath; ExtractKeyName(m_strKeyName, strPath); bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Close // Description : // Return type : void // Argument : void // void CRegistryKey::Close() { ::RegCloseKey(m_hKey); m_hKey = NULL; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Open // Description : // Return type : bool // Argument : HKEY hKey // Argument : const std::string & strSubKey // Argument : DWORD dwAccess // bool CRegistryKey::Open(HKEY hKey, const std::string & strSubKey, DWORD dwAccess) { return( REG_SUCCESS(::RegOpenKeyEx(hKey, strSubKey.c_str(), 0, dwAccess, &m_hKey))); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Create // Description : // Return type : bool // Argument : const std::string & strPath // Argument : DWORD dwAccess // Argument : DWORD dwOptions // bool CRegistryKey::Create(const std::string & strPath, DWORD dwAccess, DWORD dwOptions) { bool bRet = false; HKEY hKeyRoot; std::string strSubKey;

    ExtractKeyHandleAndSubKey( hKeyRoot, strSubKey, strPath ); if(Create(hKeyRoot, strSubKey, dwAccess, dwOptions)) { m_theKeyInfo = CRegKeyInfo(m_hKey); ExtractKeyName(m_strKeyName, strPath); m_strPath = strPath; bRet = true; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Create // Description : // Return type : bool // Argument : HKEY hKey // Argument : const std::string & strSubKey // Argument : DWORD dwAccess // Argument : DWORD dwOptions // bool CRegistryKey::Create(HKEY hKey, const std::string & strSubKey, DWORD dwAccess, DWORD dwOptions) { DWORD dwDisposition;

    return(REG_SUCCESS(::RegCreateKeyEx(hKey, strSubKey.c_str(), 0, "", dwOptions, dwAccess, NULL, &m_hKey, &dwDisposition))); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::Destroy // Description : // Return type : bool // Argument : void // bool CRegistryKey::Destroy( void ) { bool bRet = REG_SUCCESS(::RegDeleteKey(m_hKey, "")); m_hKey = NULL; return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::GetSubKey // Description : // Return type : bool // Argument : CRegistryKey & refKey // Argument : const std::string & strSubKey // Argument : DWORD dwAccess // bool CRegistryKey::GetSubKey( CRegistryKey & refKey, const std::string & strSubKey, DWORD dwAccess ) { bool bRet = refKey.Open(m_hKey, strSubKey, dwAccess); if(bRet) { refKey.m_theKeyInfo = CRegKeyInfo(m_hKey); refKey.m_strPath = m_strPath + '\\' + strSubKey; ExtractKeyName(refKey.m_strKeyName, strSubKey); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::EnumSubKeys // Description : // Return type : bool // Argument : CRegistryKey & refKey // Argument : enum_context *& pContext // Argument : DWORD dwAccess // bool CRegistryKey::EnumSubKeys( CRegistryKey & refKey, CRegistryKey::enum_context *& pContext, DWORD dwAccess ) const { bool bRet(false);

    DWORD dwSubkeyLen = pContext->nBufferLen; // Initialize the key info and buffers DWORD dwRet = ::RegEnumKeyEx(m_hKey, pContext->dwCurrent, pContext->pszNameBuffer, &dwSubkeyLen, NULL, NULL, NULL, NULL);

    if(REG_SUCCESS(dwRet)) { pContext->dwCurrent++; if(refKey.Open(m_hKey, pContext->pszNameBuffer, dwAccess)) { ExtractKeyName(refKey.m_strKeyName, pContext->pszNameBuffer); refKey.m_strPath = m_strPath + '\\' + pContext->pszNameBuffer; refKey.m_theKeyInfo = CRegKeyInfo(refKey); bRet = true; } // else // Error exception } // else if(ERROR_NO_MORE_ITEMS != dwRet) // Error exception return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::EnumValues // Description : // Return type : bool // Argument : CRegistryValue & refValue // Argument : std::string & strName // Argument : enum_context *& pContext // bool CRegistryKey::EnumValues( CRegistryValue & refValue, std::string & strName, CRegistryKey::enum_context *& pContext ) const { bool bRet = false;

    if(m_theKeyInfo.m_dwValues) { bRet = refValue.EnumValue(*this, pContext->dwCurrent, pContext); } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::CreateEnumContext // Description : // Return type : void // Argument : enum_context *& pContext // void CRegistryKey::CreateEnumContext( CRegistryKey::enum_context *& pContext ) const { pContext = new enum_context; pContext->nBufferLen = m_theKeyInfo.m_dwMaxSubKeyLen > m_theKeyInfo.m_dwMaxValueNameLen ? m_theKeyInfo.m_dwMaxSubKeyLen : m_theKeyInfo.m_dwMaxValueNameLen; pContext->pszNameBuffer = new char[pContext->nBufferLen]; pContext->dwCurrent = 0; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::DestroyEnumContext // Description : // Return type : void // Argument : enum_context *& pContext // void CRegistryKey::DestroyEnumContext( CRegistryKey::enum_context *& pContext ) const { delete pContext->pszNameBuffer; delete pContext; pContext = NULL; }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::ExtractKeyHandleAndSubKey // Description : // Return type : void // Argument : HKEY & hKey // Argument : std::string & strSubKey // Argument : const std::string & strPath // void CRegistryKey::ExtractKeyHandleAndSubKey(HKEY & hKey, std::string & strSubKey, const std::string & strPath) const { std::string m_strKeyName = strPath.substr(0, strPath.find('\\'));

    hKey = NULL;

    if(0 == _stricmp("HKEY_CLASSES_ROOT", m_strKeyName.c_str())) hKey = HKEY_CLASSES_ROOT; else if(0 == _stricmp("HKEY_CURRENT_CONFIG", m_strKeyName.c_str())) hKey = HKEY_CURRENT_CONFIG; else if(0 == _stricmp("HKEY_CURRENT_USER", m_strKeyName.c_str())) hKey = HKEY_CURRENT_USER; else if(0 == _stricmp("HKEY_LOCAL_MACHINE", m_strKeyName.c_str())) hKey = HKEY_LOCAL_MACHINE; else if(0 == _stricmp("HKEY_USERS", m_strKeyName.c_str())) hKey = HKEY_USERS; else if(0 == _stricmp("HKEY_PERFORMANCE_DATA", m_strKeyName.c_str())) hKey = HKEY_PERFORMANCE_DATA; else if(0 == _stricmp("HKEY_DYN_DATA", m_strKeyName.c_str())) hKey = HKEY_DYN_DATA;

    if(hKey && std::string::npos != strPath.find('\\')) strSubKey = strPath.substr(strPath.find('\\') + 1, strPath.size() - 1); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryKey::ExtractKeyName // Description : // Return type : void // Argument : std::string & m_strKeyName // Argument : const std::string & strPath // void CRegistryKey::ExtractKeyName(std::string & m_strKeyName, const std::string & strPath) const { m_strKeyName = strPath.substr(strPath.rfind('\\') + 1, strPath.size()); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : & CRegistryKey::operator = // Description : // Return type : const CRegistryKey // Argument : const CRegistryKey & refKeyToCopy // const CRegistryKey & CRegistryKey::operator = (const CRegistryKey & refKeyToCopy) { m_hKey = refKeyToCopy.m_hKey; m_strKeyName = refKeyToCopy.m_strKeyName; m_strPath = refKeyToCopy.m_strPath; m_theKeyInfo = refKeyToCopy.m_theKeyInfo; return(*this); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryValue::Open // Description : // Return type : bool // Argument : CRegistryKey & refKey // Argument : const std::string & strName // bool CRegistryValue::Open(CRegistryKey & refKey, const std::string & strName) { bool bRet = false; m_theRegKey = refKey; m_strName = strName; DWORD dwType; DWORD dwValueLen = m_theRegKey.GetKeyInfo().m_dwMaxValueLen + 1; BYTE * pValue = new BYTE[dwValueLen];

    if(REG_SUCCESS(::RegQueryValueEx(m_theRegKey, m_strName.c_str(), 0, &dwType, pValue, &dwValueLen))) { switch(dwType) { case REG_SZ: m_theValue.SetData(std::string((const char*)pValue)); bRet = true; break; case REG_DWORD: m_theValue.SetData((uint32)*(DWORD*)pValue); bRet = true; break; case REG_BINARY: m_theValue.SetData(pValue, dwValueLen); bRet = true; break; default: // Error exception break; } } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryValue::Create // Description : // Return type : bool // Argument : CRegistryKey & refKey // Argument : const std::string & strName // Argument : const SPreferenceValue & refValue // bool CRegistryValue::Create(CRegistryKey & refKey, const std::string & strName, const SPreferenceValue & refValue) { bool bRet = false; m_theRegKey = refKey; m_strName = strName; if(SetValue(refValue)) { bRet = true; } else { m_strName.erase(); } return(bRet); }

    bool CRegistryValue::EnumValue(const CRegistryKey & refKey, size_t nIndex, CRegistryKey::enum_context * pContext /*= NULL*/) { bool bRet = false; DWORD dwNameLen; char * pszNameBuffer; DWORD dwDataLen = refKey.GetKeyInfo().m_dwMaxValueLen; DWORD dwType;

    if(pContext) { dwNameLen = pContext->nBufferLen; pszNameBuffer = pContext->pszNameBuffer; } else { dwNameLen = refKey.GetKeyInfo().m_dwMaxValueNameLen; pszNameBuffer = new char[dwNameLen]; }

    BYTE * pData = new BYTE[dwDataLen];

    if(REG_SUCCESS(::RegEnumValue(refKey, static_cast<DWORD>(nIndex), pszNameBuffer, &dwNameLen, NULL, &dwType, pData, &dwDataLen))) { pContext->dwCurrent++; m_strName = pszNameBuffer; switch(dwType) { case REG_BINARY: m_theValue.SetData((void*)pData, dwDataLen); bRet = true; break; case REG_DWORD: m_theValue.SetData((uint32)*((DWORD*)pData)); bRet = true; break; case REG_SZ: ConvertToValue(m_theValue, (char*)pData); bRet = true; break; default: // Error exception break; } } delete pData;

    return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryValue::Set // Description : // Return type : bool // Argument : const SPreferenceValue & refValue // bool CRegistryValue::SetValue(const SPreferenceValue & refValue) { bool bRet = false;

    m_theValue = refValue; switch(refValue.GetType()) { case SPreferenceValue::kStringType: { std::string strValue; refValue.GetData(strValue);

    bRet = REG_SUCCESS(::RegSetValueEx(m_theRegKey, m_strName.c_str(), 0, REG_SZ, (BYTE*)strValue.c_str(), static_cast<DWORD>(strValue.size()))); } break; case SPreferenceValue::kUnsignedIntegerType: { DWORD dwValue; refValue.GetData((uint32&)dwValue);

    bRet = REG_SUCCESS(::RegSetValueEx(m_theRegKey, m_strName.c_str(), 0, REG_DWORD, (BYTE*)&dwValue, sizeof(DWORD))); } break; case SPreferenceValue::kBinaryType: { size_t nDataLen = refValue.GetSize(); BYTE * pData = new BYTE[nDataLen]; refValue.GetData(pData, nDataLen);

    bRet = REG_SUCCESS(::RegSetValueEx(m_theRegKey, m_strName.c_str(), 0, REG_BINARY, pData, static_cast<DWORD>(nDataLen) )); delete pData; } break; case SPreferenceValue::kSignedIntegerType: case SPreferenceValue::kFloatType: case SPreferenceValue::kVector3Type: case SPreferenceValue::kVector4Type: case SPreferenceValue::kRGBAColorType: { std::string strValue = refValue.GetDataAsText();

    strValue = std::string(GET_PREFIX(refValue.GetType())) + strValue + GET_POSTFIX(refValue.GetType());

    bRet = REG_SUCCESS(::RegSetValueEx(m_theRegKey, m_strName.c_str(), 0, REG_SZ, (BYTE*)strValue.c_str(), static_cast<DWORD>(strValue.size()))); } break; } return(bRet); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryValue::Destroy // Description : // Return type : bool // Argument : void // bool CRegistryValue::Destroy(void) { bool bRet = REG_SUCCESS(::RegDeleteValue(m_theRegKey, m_strName.c_str())); m_strName.erase(); return(bRet); }

    /////////////////////////////////////////////////////////////////////////////// // Function name : CRegistryValue::ConvertToValue // Description : // Return type : void // Argument : SPreferenceValue & refValue // Argument : const std::string & strData // void CRegistryValue::ConvertToValue( SPreferenceValue & refValue, const std::string & strData ) const { static std::string strBuffer;

    if(!strData.empty()) { if(strData[0] == '\"') { strBuffer = strData; Trim(strBuffer, "\""); refValue.SetData(strBuffer); } else { char *pBuffer = new char[strData.size() + 1]; char *pTemp; std::string strToken; std::string strValue; SPreferenceValue::ValueType eType = SPreferenceValue::kUndefinedType;

    strcpy(pBuffer, strData.c_str());

    pTemp = strtok(pBuffer, ":"); _strlwr(pTemp); strToken = pTemp; strToken += ':';

    if(0 == strncmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kStringType), strToken.size())) eType = SPreferenceValue::kStringType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kUnsignedIntegerType))) eType = SPreferenceValue::kUnsignedIntegerType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kBinaryType))) eType = SPreferenceValue::kBinaryType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kSignedIntegerType))) eType = SPreferenceValue::kSignedIntegerType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kFloatType))) eType = SPreferenceValue::kFloatType; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kVector3Type))) eType = SPreferenceValue::kVector3Type; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kVector4Type))) eType = SPreferenceValue::kVector4Type; else if(0 == strcmp(strToken.c_str(), GET_PREFIX(SPreferenceValue::kRGBAColorType))) eType = SPreferenceValue::kRGBAColorType; if(eType == SPreferenceValue::kUndefinedType) { eType = SPreferenceValue::kStringType; strValue = strData.c_str(); } else { strValue = strData.substr(strToken.size()); }

    if(eType == SPreferenceValue::kStringType) Trim(strValue, "\"");

    refValue.SetRawData(strValue.empty() ? "" : strValue, eType); delete[] pBuffer; } } refValue.SetDirty(false); }

    /////////////////////////////////////////////////////////////////////////////////////////////////// // Function name : CPreferenceStore::Trim // Description : Removes any leading and trailing values in strRemove from strToTrim // Return type : void // Argument : std::string & strToTrim // Argument : const std::string & strRemove // void CRegistryValue::Trim( std::string & strToTrim, const std::string & strRemove /*= " "*/ ) const { std::string::size_type nBegin = strToTrim.find_first_not_of(strRemove); std::string::size_type nEnd = strToTrim.find_last_not_of(strRemove); if(std::string::npos == nBegin) { strToTrim.erase(); } else { nEnd = std::string::npos == nEnd ? 0 : nEnd; strToTrim = strToTrim.substr( nBegin, nEnd - nBegin + 1); } }

    Currently browsing [preference.zip] (28,981 bytes) - [preference/preference/WinRegistry.h] - (3,875 bytes)

    // WinRegistry.h: interface for the CRegistryKey and CRegistryValue classes.
    //
    //////////////////////////////////////////////////////////////////////
    
    #if !defined(AFX_WINREGISTRY_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_)
    #define AFX_WINREGISTRY_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_

    #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <string> #include <windows.h> #include "PreferenceValue.h"

    class CRegistryValue;

    class CRegistryKey { public: class CRegKeyInfo { public: CRegKeyInfo(); CRegKeyInfo(HKEY hKey); ~CRegKeyInfo();

    const CRegKeyInfo & operator = ( const CRegKeyInfo & refToCopy);

    std::string m_strClass; DWORD m_dwSubKeys; // buffer for number of subkeys DWORD m_dwMaxSubKeyLen; // buffer for longest subkey name length DWORD m_dwMaxClassLen; // buffer for longest class string length DWORD m_dwValues; // buffer for number of value entries DWORD m_dwMaxValueNameLen; // buffer for longest value name length DWORD m_dwMaxValueLen; // buffer for longest value data length DWORD m_dwSecurityDescriptor; // buffer for security descriptor length FILETIME m_ftLastWriteTime; // buffer for last write time };

    struct enum_context { DWORD dwCurrent; char *pszNameBuffer; DWORD nBufferLen; };

    CRegistryKey(); virtual ~CRegistryKey();

    bool Open(const std::string & strPath, DWORD dwAccess = KEY_ALL_ACCESS); void Close(); bool Create( const std::string & strPath, DWORD dwAccess = KEY_ALL_ACCESS, DWORD dwOptions = REG_OPTION_NON_VOLATILE ); bool Destroy();

    bool GetSubKey( CRegistryKey & refKey, const std::string & strSubKey, DWORD dwAccess = KEY_ALL_ACCESS ); bool EnumSubKeys( CRegistryKey & refKey, enum_context *& pContext, DWORD dwAccess = KEY_ALL_ACCESS ) const; bool EnumValues( CRegistryValue & refValue, std::string & strName, enum_context *& pContext ) const; void CreateEnumContext( enum_context *& pContext ) const; void DestroyEnumContext( enum_context *& pContext ) const; const std::string & Name(void) {return(m_strKeyName);} operator HKEY () const {return(m_hKey);} const CRegistryKey & operator = (const CRegistryKey & refKeyToCopy); const CRegKeyInfo & GetKeyInfo() const {return(m_theKeyInfo);}

    protected: bool Create(HKEY hKey, const std::string & strSubKey, DWORD dwAccess, DWORD dwOptions); bool Open(HKEY hKey, const std::string & strSubKey, DWORD dwAccess); void ExtractKeyHandleAndSubKey(HKEY & hKey, std::string & strSubKey, const std::string & strPath) const; void ExtractKeyName(std::string & m_strKeyName, const std::string & strPath) const; HKEY m_hKey; std::string m_strKeyName; std::string m_strPath; CRegKeyInfo m_theKeyInfo; };

    class CRegistryValue { public: CRegistryValue(){} virtual ~CRegistryValue(){}

    bool Open(CRegistryKey & refKey, const std::string & strName); bool Create(CRegistryKey & refKey, const std::string & strName, const PreferenceStore::SPreferenceValue & refValue); bool EnumValue(const CRegistryKey & refKey, size_t nIndex, CRegistryKey::enum_context * pContext = NULL); bool Destroy(void);

    void GetValue( PreferenceStore::SPreferenceValue & refValue ) {refValue = m_theValue;} bool SetValue(const PreferenceStore::SPreferenceValue & refValue); const std::string & GetName(void) {return(m_strName);}

    protected: void ConvertToValue( PreferenceStore::SPreferenceValue & refValue, const std::string & strData ) const; void Trim( std::string & strToTrim, const std::string & strRemove = " " ) const;

    DECLARE_PREFIX_TABLE(); DECLARE_POSTFIX_TABLE();

    private: CRegistryKey m_theRegKey; std::string m_strName; PreferenceStore::SPreferenceValue m_theValue; };

    #endif // !defined(AFX_WINREGISTRY_H__82DB35FD_FDCD_4B7C_B9D1_8AA4CDE4530E__INCLUDED_)

    The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

     

    Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
    Please read our Terms, Conditions, and Privacy information.