SOL9 2.0 Class: Profile

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * Profile.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2008/07/01 Modified to inherit class CurrentUserRegistry.
// The following registry is used.
//  "HKEY_CURRENT_USER\\Software\\Antillia\\Application\\[ModuleName]"
// Log output level for a SOL::LogWriter object gotten from the LogWriter::getInstance method
//    is defined in the following key on "Level"
//
//  "HKEY_CURRENT_USER\\Software\\Antillia\\Application\\[ModuleName]\\Log"
//
// 2008/09/13 Added two methods: setFileFolder/getFileFolder.
//
// 2009/06/20 Added two methods for wchar_t* string:
//    bool set(const char* section, char* entry, const wchar_t* wstring, size_t wlen)
//    bool get(const char* section, char* entry, wchar_t* wstring, size_t wlen)

#pragma once

#include <sol\Object.h>
#include <sol\CurrentUserRegistry.h>
#include <sol\Folder.h>

#include <mbstring.h>

#include <sol/StringT.h>
#include <sol/StringConverter.h>

namespace SOL {

class Profile :public CurrentUserRegistry {
private:
    TCHAR  key[SIZE_1KB];
 
public:
    /**
     * Constructor.
     * 
     * Note that the following registry is used to store your profile on
     *this constructor witout argument.
     *
     * HKEY_CURRENT_USER\\Software\\Antillia\\Application\\[ModuleName]"
     */
    Profile(const TCHAR* name=NULL)
        :CurrentUserRegistry()
    {
        memset(key, (TCHAR)0, SizeOf(key));

        wchar_t fileName[_MAX_PATH];
        wchar_t* wname = NULL;
        StringT<TCHAR> tname=_T("");

        //String moduleName = _T("");
        if(name) {
            tname = name;
        }

        else {
            //Get the module name from the current executable program.
            ::GetModuleFileNameW(NULL, fileName, _MAX_PATH);
            wname = ::wcsrchr(fileName, (wchar_t)'\\');
            if (wname) {
                wname++;            
            }
            wchar_t* dot = (wchar_t*)strrchr(wname, '.');
            if (dot) {
                *dot = (TCHAR)0;
            }
            StringConverter converter;
            converter.convert(wname, tname);
        }

        _stprintf_s(key, SizeOf(key), _T("Software\\Antillia\\Application\\%s"), 
            (const TCHAR*)tname);

        //MessageBox(NULL, key, "Profile", MB_OK);
        createKey(key);
    }

public:
    ~Profile()
    {
    }

public:
    int get(const TCHAR* section, const TCHAR* entry, int defvalue)
    {
        int rc = defvalue;
        int value =0;
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        if (getInteger(regKey, entry, value)) {
            rc = value;    
        }
        return rc;
    }

public:
    bool get(__in const TCHAR* section, __in const TCHAR* entry, 
        __out String& value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);
        return getText(regKey, entry, value);
    }

public:
    bool get(__in const TCHAR* section, __in const TCHAR* entry, 
        __out StringT<char>& value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);
        return getText(regKey, entry, value);
    }

public:
    bool get(__in const TCHAR* section, __in const TCHAR* entry, 
        __out StringT<wchar_t>& value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);
        return getText(regKey, entry, value);
    }

public:
    bool get(const TCHAR* section, const TCHAR* entry,
        TCHAR* buff, UINT bufflen)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);
        return getText(regKey, entry, buff, bufflen);
    }

public:
    bool get(const TCHAR* section, const TCHAR* entry, TCHAR* defvalue, 
        TCHAR* buff, UINT bufflen)
    {
        bool rc = false;
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        if (getText(regKey, entry, buff, bufflen)) {
            rc = true;
        } else {
            strcpy_s(buff, bufflen, defvalue);
        }
        return rc;
    }

public:
    bool getLogLevel(int& level)
    {
        bool rc = false;
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, _T("Log"));
        if (getInteger(regKey, _T("Level"), level)) {
            rc = true;
        }
        return rc;
    }

public:
    bool getGeometry(int* x, int* y, int* w, int* h)
    {
        bool rc = false;

          *x = CW_USEDEFAULT;
        *y = CW_USEDEFAULT;
        *w = CW_USEDEFAULT;
        *h = CW_USEDEFAULT;

        
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, _T("Windows"));

        TCHAR geo[128];

        if(getText(regKey, _T("Geometry"), geo, SizeOf(geo) )) {
            _stscanf_s(geo, _T("%d %d %d %d"), x, y, w, h);
            if (*w <=0)  *w =400;
            if (*h <=0)  *h =300; 
            rc = true;
        }
        return rc;
    }

public:
    bool set(const TCHAR* section, const TCHAR* entry, int value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return setInteger(regKey, entry, value);

    }

public:
    bool set(const TCHAR* section, const TCHAR* entry, TCHAR* value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);
        
        return setText(regKey, entry, value);

    }

public:
    bool set(const TCHAR* section, const TCHAR* entry, __in StringT<char>&  value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return setText(regKey, entry, value);

    }
public:
    bool set(const TCHAR* section, const TCHAR* entry, __in StringT<wchar_t>& value)
    {
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return setText(regKey, entry, value);

    }

public:
    //2008/09/14: Added
    // Used to store a selected folder in SOL::FileDialog to registry. 
    bool setFileFolder(const TCHAR* value=_T(".\\"))
    {
        const TCHAR* section = _T("FileFolder");
        const TCHAR* entry   = _T("directory");
        
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return setText(regKey, entry, value);

    }

public:
    //2008/09/14: Added
    // Used to restore a previously selected folder in SOL::FileDialog to registry. 
    bool getFileFolder(TCHAR* value, UINT len)
    {
        const TCHAR* section = _T("FileFolder");
        const TCHAR* entry   = _T("directory");
        
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return getText(regKey, entry, value, len);
    }

public:
    //2009/06/20
    bool set(const TCHAR* section, TCHAR* entry, const wchar_t* wstring, size_t wlen)
    {        
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return setBinary(regKey, entry, (unsigned char*)wstring, (wlen+1)*2);
    }

public:
    //2009/06/20
    bool get(const TCHAR* section, TCHAR* entry, wchar_t* wstring, size_t wlen)
    {        
        TCHAR regKey[SIZE_1KB];
        _stprintf_s(regKey, SizeOf(regKey), _T("%s\\%s"), this->key, section);

        return getBinary(regKey, entry, (unsigned char*)wstring, (wlen+1)*2);
    }

public:
    void setGeometry(int x, int y, int w, int h)
    {
        TCHAR geo[128];
        _stprintf_s(geo, SizeOf(geo), 
                _T("%d %d %d %d"), x, y, w, h);
        set(_T("Windows"), _T("Geometry"), geo);
    }

public:
    void setGeometry(RECT* r)
    {
        TCHAR geo[128];
        _stprintf_s(geo, SizeOf(geo),
            _T("%d %d %d %d"), r->left, r->top,
            r->right - r->left, r->bottom - r->top);
        set(_T("Windows"), _T("Geometry"), geo);
    }
};

}


Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.