[PATCH v4 2/8] reg.exe: Add path_get_key() to remove boilerplate

Stefan Dösinger stefandoesinger at gmail.com
Sun Oct 12 08:58:20 CDT 2014


-----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.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJUOoj8AAoJEN0/YqbEcdMwBI8P/1CgdHX0yt2Z6iZqn61fA1wt
M89EKguHgXoQmorrtpMYtZJNoMd6f0kIv4yH+corcZwz01iGAuu4Zq7v3grD3SIz
uv6S+QNiysw8wT5tTSFVu+DiGuxQXreb1BryjMXgmoOLbmPLPbcYX+yEj1DaiRqg
gh1hXYpVoqCjKGed8Wukk+Cq481azQz41e2vUqd2pP8mux0BToWaBU8H9HufTxYg
RyHYaKksstGhQLINm2FOAtq2S8eZN5TRdELcTEuQoDiE6c6YNIepLLJ5f6MiH4lt
6Biq2CUeIOhkqEWjWGzGmfom2rBq1lcSWuLnV9N7coRKdCOC+wWpeEvUBjvFTqMW
JcbdZxusE3Rp80scp7YFnUBVXfOlQr4IofKkNeJkVUjoAIGOuSAcr6pHnO8KTpMI
K/9nkFb/cOF8IQi6Gc3PxJxXYBBQFw578aeNpUk6QEmimXcyBvmCe/K2Oki1Qa7x
CoU7MIMvtE1oSL2kN5o4yzJ+YrIZ4CGfg8yhvl9M54tWHvLMLe+Bw+5pMefwgDXP
QIYCdKKIWf+hsTOJCYHLgD5wU8euvTvcgmBklI5cGivwLQqrLfZMucmnsG4jmXH4
pLdZwJsC1XXRRej13YofPzfRSZbK3pcjGwpiq7sxJTrWH05pTm+kBOGId1ChelcS
jW7y6q/ecery5/13atxt
=PXew
-----END PGP SIGNATURE-----



More information about the wine-devel mailing list