Add context-sensitive Git History item
[git-cheetah/kirill.git] / registry.c
blobdd639787ea52abae5bbc479f1480c106cac0fab4
1 #include <windows.h>
2 #include "registry.h"
4 /* uses get_registry_path to replace patterns */
5 HRESULT create_reg_entries(const HKEY root, reg_value const info[])
7 HRESULT result;
8 int i;
10 for (i = 0; NULL != info[i].path; ++i) {
11 char path[MAX_REGISTRY_PATH];
12 char name[MAX_REGISTRY_PATH], *regname = NULL;
13 char value [MAX_REGISTRY_PATH], *regvalue = NULL;
15 HKEY key;
16 DWORD disp;
18 get_registry_path(info[i].path, path);
19 result = RegCreateKeyEx(root, path,
20 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
21 &key, &disp);
22 if (ERROR_SUCCESS != result)
23 return (result);
25 regname = get_registry_path(info[i].name, name);
26 regvalue = get_registry_path(info[i].value, value);
29 * regname can legitimately be NULL,
30 * but if value is NULL, it's just a key
32 if (NULL != regvalue) {
33 char *endptr;
34 DWORD dwValue = strtoul(regvalue, &endptr, 10);
35 if (endptr && !*endptr)
36 result = RegSetValueEx(key, regname, 0,
37 REG_DWORD,
38 (LPBYTE)&dwValue,
39 sizeof(dwValue));
40 else
41 result = RegSetValueEx(key, regname, 0,
42 REG_SZ,
43 (LPBYTE)regvalue,
44 (DWORD)strlen(regvalue));
47 RegCloseKey(key);
48 if (ERROR_SUCCESS != result)
49 return (result);
52 return ERROR_SUCCESS;
55 static inline HRESULT mask_errors(HRESULT const result)
57 switch (result) {
58 case ERROR_FILE_NOT_FOUND: return ERROR_SUCCESS;
61 return result;
64 HRESULT delete_reg_entries(HKEY const root, reg_value const info[])
66 HRESULT result;
67 int i = 0;
69 /* count items in the array */
70 while (NULL != info[i].path)
71 i++;
72 /* at this point, i is the __count__, so
73 make it an offset to the last element */
74 i--;
76 /* walk the array backwards (we're at the terminating triple-null) */
77 do {
78 char path[MAX_REGISTRY_PATH];
79 HKEY key;
81 i--;
83 get_registry_path(info[i].path, path);
85 if (info[i].name || info[i].value) {
86 /* delete the value */
88 char name[MAX_REGISTRY_PATH], *regname = NULL;
90 result = mask_errors(RegOpenKeyEx(root, path,
91 0, KEY_WRITE, &key));
92 if (ERROR_SUCCESS != result)
93 return result;
96 * some of our errors are masked (e.g. not found)
97 * don't work on this key if we could not open it
99 if (NULL == key)
100 continue;
102 regname = get_registry_path(info[i].name, name);
103 result = mask_errors(RegDeleteValue(key, regname));
105 RegCloseKey(key);
106 } else /* not the value, delete the key */
107 result = mask_errors(RegDeleteKey(root, path));
109 if (ERROR_SUCCESS != result)
110 return (result);
111 } while (i);
113 return ERROR_SUCCESS;