File: regstorage.cpp

package info (click to toggle)
einstein 2.0.dfsg.2-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,240 kB
  • ctags: 1,653
  • sloc: cpp: 10,424; makefile: 79; sh: 1
file content (131 lines) | stat: -rw-r--r-- 3,582 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifdef WIN32                    // Win32 only
#include "regstorage.h"
#include "unicode.h"


RegistryStorage::RegistryStorage()
{
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                "SOFTWARE\\Flowix Games\\Einstein\\2.0",
                0, KEY_READ, &globalKey))
        globalKey = NULL;
    if (RegOpenKeyEx(HKEY_CURRENT_USER, 
                "SOFTWARE\\Flowix Games\\Einstein\\2.0",
                0, KEY_READ | KEY_WRITE, &userKey))
    {
        if (RegCreateKeyEx(HKEY_CURRENT_USER, 
                    "SOFTWARE\\Flowix Games\\Einstein\\2.0", 0, NULL, 
                    REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE,
                    NULL, &userKey, NULL))
            userKey = NULL;
    }
}

RegistryStorage::~RegistryStorage()
{
    if (globalKey)
        RegCloseKey(globalKey);
    if (userKey)
        RegCloseKey(userKey);
}

int RegistryStorage::get(const std::wstring &name, int dflt)
{
    std::string uname(toUtf8(name));
    
    if (globalKey) {
        DWORD data;
        DWORD size = sizeof(data);
        DWORD type;
        if (! RegQueryValueEx(globalKey, uname.c_str(), NULL, &type,
                    (BYTE*)&data, &size))
            if (type == REG_DWORD)
                return data;
    }
    
    if (userKey) {
        DWORD data;
        DWORD size = sizeof(data);
        DWORD type;
        if (! RegQueryValueEx(userKey, uname.c_str(), NULL, &type,
                    (BYTE*)&data, &size))
            if (type == REG_DWORD)
                return data;
    }
    
    return dflt;
}

std::wstring RegistryStorage::get(const std::wstring &name, const std::wstring &dflt)
{
    std::string uname(toUtf8(name));
    
    if (globalKey) {
        DWORD size = 0;
        DWORD type;
        if (! RegQueryValueEx(globalKey, uname.c_str(), NULL, &type,
                    NULL, &size)) 
        {
            if ((type == REG_SZ) && (size > 0)) {
                char *data = new char[size + 1];
                if (! RegQueryValueEx(globalKey, uname.c_str(), NULL, &type,
                            (BYTE*)data, &size)) 
                {
                    std::wstring s(fromUtf8(data));
                    delete[] data;
                    return s;
                }
                delete[] data;
            } else
                return L"";
        }
    }
    
    if (userKey) {
        DWORD size = 0;
        DWORD type;
        if (! RegQueryValueEx(userKey, uname.c_str(), NULL, &type,
                    NULL, &size)) 
        {
            if ((type == REG_SZ) && (size > 0)) {
                char *data = new char[size];
                if (! RegQueryValueEx(userKey, uname.c_str(), NULL, &type,
                            (BYTE*)data, &size)) 
                {
                    std::wstring s(fromUtf8(data));
                    delete[] data;
                    return s;
                }
                delete[] data;
            } else
                return L"";
        }
    }
    
    return dflt;
}

void RegistryStorage::set(const std::wstring &name, int value)
{
    std::string uname(toUtf8(name));
    
    if (userKey) {
        DWORD data = value;
        RegSetValueEx(userKey, uname.c_str(), 0, REG_DWORD, 
                (BYTE*)&data, sizeof(data));
    }
}

void RegistryStorage::set(const std::wstring &name, const std::wstring &value)
{
    std::string uname(toUtf8(name));
    std::string uval(toUtf8(value));

    if (userKey)
        RegSetValueEx(userKey, uname.c_str(), 0, REG_SZ, 
                (BYTE*)uval.c_str(), (uval.length() + 1) * sizeof(char));
}


#endif