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

Jonathan Vollebregt jnvsor at gmail.com
Thu Oct 9 03:55:08 CDT 2014


---
 programs/reg/reg.c | 130 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 76 insertions(+), 54 deletions(-)

diff --git a/programs/reg/reg.c b/programs/reg/reg.c
index 181d889..415ee32 100644
--- a/programs/reg/reg.c
+++ b/programs/reg/reg.c
@@ -20,6 +20,33 @@
 #include <wine/unicode.h>
 #include "reg.h"
 
+#define MAX_ROOT_KEY_NAME_LENGTH 20
+#define NUM_ROOT_KEYS 5
+
+static const WCHAR short_HKEY_name[NUM_ROOT_KEYS][5] = {
+    {'H','K','L','M',0},
+    {'H','K','C','U',0},
+    {'H','K','C','R',0},
+    {'H','K','U',0},
+    {'H','K','C','C',0},
+};
+
+static const WCHAR long_HKEY_name[NUM_ROOT_KEYS][MAX_ROOT_KEY_NAME_LENGTH] = {
+    {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0},
+    {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0},
+    {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0},
+    {'H','K','E','Y','_','U','S','E','R','S',0},
+    {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0},
+};
+
+static const HKEY HKEYs[NUM_ROOT_KEYS] = {
+    HKEY_LOCAL_MACHINE,
+    HKEY_CURRENT_USER,
+    HKEY_CLASSES_ROOT,
+    HKEY_USERS,
+    HKEY_CURRENT_CONFIG,
+};
+
 static int reg_printfW(const WCHAR *msg, ...)
 {
     va_list va_args;
@@ -67,35 +94,50 @@ static int reg_message(int msg)
     return reg_printfW(formatW, msg_buffer);
 }
 
-static HKEY get_rootkey(LPWSTR key)
+static const WCHAR *path_get_rootkey_name(const WCHAR *path)
+{
+    DWORD i;
+
+    for (i = 0; i < NUM_ROOT_KEYS; i++)
+    {
+        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)
+        {
+            return long_HKEY_name[i];
+        }
+    }
+
+    reg_message(STRING_INVALID_KEY);
+    return NULL;
+}
+
+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];
+    else
+        return NULL;
+}
+
+static HKEY path_get_key(const WCHAR *path)
 {
-    static const WCHAR szHKLM[] = {'H','K','L','M',0};
-    static const WCHAR szHKEY_LOCAL_MACHINE[] = {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',0};
-    static const WCHAR szHKCU[] = {'H','K','C','U',0};
-    static const WCHAR szHKEY_CURRENT_USER[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R',0};
-    static const WCHAR szHKCR[] = {'H','K','C','R',0};
-    static const WCHAR szHKEY_CLASSES_ROOT[] = {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T',0};
-    static const WCHAR szHKU[] = {'H','K','U',0};
-    static const WCHAR szHKEY_USERS[] = {'H','K','E','Y','_','U','S','E','R','S',0};
-    static const WCHAR szHKCC[] = {'H','K','C','C',0};
-    static const WCHAR szHKEY_CURRENT_CONFIG[] = {'H','K','E','Y','_','C','U','R','R','E','N','T','_','C','O','N','F','I','G',0};
-
-    if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKLM,4)==CSTR_EQUAL ||
-        CompareStringW(CP_ACP,NORM_IGNORECASE,key,18,szHKEY_LOCAL_MACHINE,18)==CSTR_EQUAL)
-        return HKEY_LOCAL_MACHINE;
-    else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCU,4)==CSTR_EQUAL ||
-             CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CURRENT_USER,17)==CSTR_EQUAL)
-        return HKEY_CURRENT_USER;
-    else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCR,4)==CSTR_EQUAL ||
-             CompareStringW(CP_ACP,NORM_IGNORECASE,key,17,szHKEY_CLASSES_ROOT,17)==CSTR_EQUAL)
-        return HKEY_CLASSES_ROOT;
-    else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,3,szHKU,3)==CSTR_EQUAL ||
-             CompareStringW(CP_ACP,NORM_IGNORECASE,key,10,szHKEY_USERS,10)==CSTR_EQUAL)
-        return HKEY_USERS;
-    else if (CompareStringW(CP_ACP,NORM_IGNORECASE,key,4,szHKCC,4)==CSTR_EQUAL ||
-             CompareStringW(CP_ACP,NORM_IGNORECASE,key,19,szHKEY_CURRENT_CONFIG,19)==CSTR_EQUAL)
-        return HKEY_CURRENT_CONFIG;
-    else return NULL;
+    HKEY k = path_get_rootkey(path);
+    if (!k)
+        return NULL;
+
+    path = strchrW(path, '\\');
+    if (!path)
+        return k;
+
+    path++;
+
+    if (RegOpenKeyW(k, path, &k) != ERROR_SUCCESS)
+    {
+        reg_message(STRING_CANNOT_FIND);
+        return NULL;
+    }
+
+    return k;
 }
 
 static DWORD get_regtype(LPWSTR type)
@@ -181,12 +223,9 @@ static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
     }
     p++;
 
-    root = get_rootkey(key_name);
+    root = path_get_rootkey(key_name);
     if (!root)
-    {
-        reg_message(STRING_INVALID_KEY);
         return 1;
-    }
 
     if(RegCreateKeyW(root,p,&subkey)!=ERROR_SUCCESS)
     {
@@ -232,28 +271,15 @@ static int reg_add(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
 static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
     BOOL value_all, BOOL force)
 {
-    LPWSTR p;
-    HKEY root,subkey;
+    HKEY subkey = path_get_key(key_name);
 
     static const WCHAR stubW[] = {'D','E','L','E','T','E',
         ' ','-',' ','%','s',' ','%','s',' ','%','d',' ','%','d',' ','%','d','\n'
         ,0};
     reg_printfW(stubW, key_name, value_name, value_empty, value_all, force);
 
-    p = strchrW(key_name,'\\');
-    if (!p)
-    {
-        reg_message(STRING_INVALID_KEY);
-        return 1;
-    }
-    p++;
-
-    root = get_rootkey(key_name);
-    if (!root)
-    {
-        reg_message(STRING_INVALID_KEY);
+    if (!subkey)
         return 1;
-    }
 
     if (value_name && value_empty)
     {
@@ -275,7 +301,9 @@ static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
     /* Delete subtree only if no /v* option is given */
     if (!value_name && !value_empty && !value_all)
     {
-        if (RegDeleteTreeW(root,p)!=ERROR_SUCCESS)
+        HKEY root = path_get_rootkey(key_name);
+
+        if (RegDeleteTreeW(root, strchrW(key_name,'\\')+1) != ERROR_SUCCESS)
         {
             reg_message(STRING_CANNOT_FIND);
             return 1;
@@ -284,12 +312,6 @@ static int reg_delete(WCHAR *key_name, WCHAR *value_name, BOOL value_empty,
         return 0;
     }
 
-    if(RegOpenKeyW(root,p,&subkey)!=ERROR_SUCCESS)
-    {
-        reg_message(STRING_CANNOT_FIND);
-        return 1;
-    }
-
     if (value_all)
     {
         LPWSTR szValue;
-- 
2.1.1




More information about the wine-patches mailing list