Standardize License information
[openafs.git] / src / WINNT / afsapplib / settings.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include <windows.h>
16 #include <winerror.h>
17 #include <WINNT/TaLocale.h>
18 #include <WINNT/settings.h>
19
20
21 /*
22  * ROUTINES ___________________________________________________________________
23  *
24  */
25
26 BOOL RestoreSettings (HKEY hkParent,
27                       LPCTSTR pszBase,
28                       LPCTSTR pszValue,
29                       PVOID pStructure,
30                       size_t cbStructure,
31                       WORD wVerExpected)
32 {
33    BOOL rc = FALSE;
34
35    size_t cbStored;
36    if ((cbStored = GetRegValueSize (hkParent, pszBase, pszValue)) != 0)
37       {
38       if (cbStored >= sizeof(WORD)+cbStructure)
39          {
40          PVOID pStructureFinal;
41          if ((pStructureFinal = (PVOID)Allocate (cbStored)) != NULL)
42             {
43             if (GetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, cbStored))
44                {
45                WORD wVerStored = *(LPWORD)pStructureFinal;
46
47                if ( (HIBYTE(wVerStored) == HIBYTE(wVerExpected)) &&
48                     (LOBYTE(wVerStored) >= LOBYTE(wVerExpected)) )
49                   {
50                   memcpy (pStructure, &((LPBYTE)pStructureFinal)[ sizeof(WORD) ], cbStructure);
51                   rc = TRUE;
52                   }
53                }
54
55             Free (pStructureFinal);
56             }
57          }
58       }
59
60    return rc;
61 }
62
63
64 BOOL StoreSettings (HKEY hkParent,
65                     LPCTSTR pszBase,
66                     LPCTSTR pszValue,
67                     PVOID pStructure,
68                     size_t cbStructure,
69                     WORD wVersion)
70 {
71    BOOL rc = FALSE;
72
73    PVOID pStructureFinal;
74    if ((pStructureFinal = (PVOID)Allocate (sizeof(WORD) + cbStructure)) != NULL)
75       {
76       *(LPWORD)pStructureFinal = wVersion;
77       memcpy (&((LPBYTE)pStructureFinal)[ sizeof(WORD) ], pStructure, cbStructure);
78
79       rc = SetBinaryRegValue (hkParent, pszBase, pszValue, pStructureFinal, sizeof(WORD) + cbStructure);
80
81       Free (pStructureFinal);
82       }
83
84    return rc;
85 }
86
87
88 void EraseSettings (HKEY hkParent, LPCTSTR pszBase, LPCTSTR pszValue)
89 {
90    HKEY hk;
91    if (RegOpenKey (hkParent, pszBase, &hk) == 0)
92       {
93       RegDeleteValue (hk, pszValue);
94       RegCloseKey (hk);
95       }
96 }
97
98
99 BOOL GetBinaryRegValue (HKEY hk,
100                         LPCTSTR pszBase,
101                         LPCTSTR pszValue,
102                         PVOID pData,
103                         size_t cbData)
104 {
105    BOOL rc = FALSE;
106
107    HKEY hkFinal;
108    if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
109       {
110       DWORD dwType;
111       DWORD dwSize = (DWORD)cbData;
112
113       if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, (LPBYTE)pData, &dwSize) == ERROR_SUCCESS)
114          rc = TRUE;
115
116       RegCloseKey (hk);
117       }
118
119    return rc;
120 }
121
122
123 size_t GetRegValueSize (HKEY hk,
124                         LPCTSTR pszBase,
125                         LPCTSTR pszValue)
126 {
127    size_t cb = 0;
128
129    HKEY hkFinal;
130    if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
131       {
132       DWORD dwType;
133       DWORD dwSize = 0;
134
135       if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
136          {
137          cb = (size_t)dwSize;
138          }
139
140       RegCloseKey (hk);
141       }
142
143    return cb;
144 }
145
146
147 BOOL SetBinaryRegValue (HKEY hk,
148                         LPCTSTR pszBase,
149                         LPCTSTR pszValue,
150                         PVOID pData,
151                         size_t cbData)
152 {
153    BOOL rc = FALSE;
154
155    HKEY hkFinal;
156    if (RegCreateKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
157       {
158       DWORD dwSize = (DWORD)cbData;
159
160       if (RegSetValueEx (hkFinal, pszValue, NULL, REG_BINARY, (LPBYTE)pData, dwSize) == ERROR_SUCCESS)
161          rc = TRUE;
162
163       RegCloseKey (hk);
164       }
165
166    return rc;
167 }
168
169
170 BOOL GetMultiStringRegValue (HKEY hk,
171                              LPCTSTR pszBase,
172                              LPCTSTR pszValue,
173                              LPTSTR *pmszData)
174 {
175    BOOL rc = FALSE;
176
177    HKEY hkFinal;
178    if (RegOpenKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
179       {
180       DWORD dwSize = 0;
181       DWORD dwType = 0;
182       RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, NULL, &dwSize);
183
184       if ((dwType == REG_MULTI_SZ) && (dwSize != 0))
185          {
186          *pmszData = AllocateString (dwSize);
187
188          if (RegQueryValueEx (hkFinal, pszValue, NULL, &dwType, (LPBYTE)*pmszData, &dwSize) == ERROR_SUCCESS)
189             rc = TRUE;
190          else
191             {
192             FreeString (*pmszData);
193             *pmszData = NULL;
194             }
195          }
196
197       RegCloseKey (hk);
198       }
199
200    return rc;
201 }
202
203
204 BOOL SetMultiStringRegValue (HKEY hk,
205                              LPCTSTR pszBase,
206                              LPCTSTR pszValue,
207                              LPCTSTR mszData)
208 {
209    BOOL rc = FALSE;
210
211    HKEY hkFinal;
212    if (RegCreateKey (hk, pszBase, &hkFinal) == ERROR_SUCCESS)
213       {
214       DWORD dwSize = sizeof(TCHAR);
215       for (LPCTSTR psz = mszData; psz && *psz; psz += 1+lstrlen(psz))
216          dwSize += sizeof(TCHAR) * (1+lstrlen(psz));
217
218       if (RegSetValueEx (hkFinal, pszValue, NULL, REG_MULTI_SZ, (LPBYTE)mszData, dwSize) == ERROR_SUCCESS)
219          rc = TRUE;
220
221       RegCloseKey (hk);
222       }
223
224    return rc;
225 }
226
227
228 // Under Windows NT, RegDeleteKey() is not recursive--under Windows 95,
229 // it is.  Sigh.  This routine works recursively on either OS.
230 //
231 BOOL RegDeltreeKey (HKEY hk, LPTSTR pszKey)
232 {
233    HKEY hkSub;
234    if (RegOpenKey (hk, pszKey, &hkSub) == 0)
235       {
236       TCHAR szFound[ MAX_PATH ];
237       while (RegEnumKey (hkSub, 0, szFound, MAX_PATH) == 0)
238          {
239          if (!RegDeltreeKey (hkSub, szFound))
240             {
241             RegCloseKey (hkSub);
242             return FALSE;
243             }
244          }
245
246       RegCloseKey (hkSub);
247       }
248
249    if (RegDeleteKey (hk, pszKey) != 0)
250       return FALSE;
251
252    return TRUE;
253 }
254