-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
I think this patch has the potential to improve code readability if improved a bit.
Am 2014-10-09 10:55, schrieb Jonathan Vollebregt:
+#define MAX_ROOT_KEY_NAME_LENGTH 20 +#define NUM_ROOT_KEYS 5
+static const WCHAR short_HKEY_name[NUM_ROOT_KEYS][5] = { ... +};
+static const WCHAR long_HKEY_name[NUM_ROOT_KEYS][MAX_ROOT_KEY_NAME_LENGTH] = { ... +};
+static const HKEY HKEYs[NUM_ROOT_KEYS] = { ... +};
Why not use one table instead of 3 separate arrays with magically matching indices?
Something like static const struct { HKEY key; const WCHAR *long_name; const WCHAR *short_name; } keys[] = { {HKEY_LOCAL_MACHINE, {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0}, {'H','K','L','M',0}}, ... };
static HKEY path_get_rootkey(const WCHAR *path) { unsigned int i; for (i = 0; i < sizeof(keys) / sizeof(*keys); i++) { if (!strcmpi(keys[i].short_name, name) || !strcmpi(keys[i].long_name, name)) return keys[i].key; } return NULL; }
if (strncmpiW(path, short_HKEY_name[i], strlenW(short_HKEY_name[i])) == 0 ||
strncmpiW(path, long_HKEY_name[i], strlenW(long_HKEY_name[i])) == 0)
I don't think strncmpi(x, y, strlen(y)) has any advantage over strcmpi(x, y). strlen just counts the number of characters to the first 0 byte, where strcmpi would stop anyway.
Are you sure there is not some other behavior of CompareStringW that str(n)cmpi does not have? I have not found any, but I am not sure about the CP_ACP part.
The original code probably has a bug if the user passes a string like HKEY_LOCAL_MACHINE_GARBAGE. It's worth writing a test for this.
+static HKEY path_get_rootkey(const WCHAR *path) +{
- if (path_get_rootkey_name(path))
return HKEYs[(path_get_rootkey_name(path) - long_HKEY_name[0]) / MAX_ROOT_KEY_NAME_LENGTH];
This searches the array twice.
- path = strchrW(path, '\');
- if (!path)
return k;
I expect that this causes trouble with RegCloseKey.