[3/8] regedit: Remove cruft and make functions static, reordering where necessary to avoid forward declarations.

Francois Gouget fgouget at codeweavers.com
Thu Jun 14 04:42:41 CDT 2007


Remove the command parameter of processRegLines() as it can only be used with processRegEntry().
---

As in patch 1 this makes a bunch of functions static which has the same 
positive consequences (even more so):

This makes cross-file calls much easier to spot, making it easier to 
understand the structure of the code and to figure out the impact of 
changing a given function.

It also removes a bunch of commented out or simply never called 
functions which saves one the trouble of wondering what they're for, or 
whether deleting values really is unimplemented as claimed in some 
comments (doDeleteValue() vs. delete_registry_key() and setValue()).


 programs/regedit/listview.c |    3 -
 programs/regedit/regedit.c  |    2 +-
 programs/regedit/regproc.c  |  400 +++++++++++++------------------------------
 programs/regedit/regproc.h  |   42 +-----
 4 files changed, 126 insertions(+), 321 deletions(-)

diff --git a/programs/regedit/listview.c b/programs/regedit/listview.c
index bd7b0b6..52c2b0c 100644
--- a/programs/regedit/listview.c
+++ b/programs/regedit/listview.c
@@ -154,7 +154,6 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
 
     index = ListView_InsertItem(hwndLV, &item);
     if (index != -1) {
-        /*        LPTSTR pszText = NULL; */
         switch (dwValType) {
         case REG_SZ:
         case REG_EXPAND_SZ:
@@ -169,7 +168,6 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
                 wsprintf(buf, _T("0x%08X (%d)"), *(DWORD*)ValBuf, *(DWORD*)ValBuf);
                 ListView_SetItemText(hwndLV, index, 2, buf);
             }
-            /*            lpsRes = convertHexToDWORDStr(lpbData, dwLen); */
             break;
         case REG_BINARY: {
                 unsigned int i;
@@ -188,7 +186,6 @@ static void AddEntryToList(HWND hwndLV, LPTSTR Name, DWORD dwValType,
             break;
         default:
           {
-            /*            lpsRes = convertHexToHexCSV(lpbData, dwLen); */
             TCHAR szText[128];
             LoadString(hInst, IDS_REGISTRY_VALUE_CANT_DISPLAY, szText, COUNT_OF(szText));
             ListView_SetItemText(hwndLV, index, 2, szText);
diff --git a/programs/regedit/regedit.c b/programs/regedit/regedit.c
index 41ef204..155ecfa 100644
--- a/programs/regedit/regedit.c
+++ b/programs/regedit/regedit.c
@@ -158,7 +158,7 @@ static BOOL PerformRegAction(REGEDIT_ACTION action, LPSTR s)
                     fprintf(stderr,"%s: Can't open file \"%s\"\n", getAppName(), filename);
                     exit(1);
                 }
-                processRegLines(reg_file, doSetValue);
+                processRegLines(reg_file);
                 if (realname)
                 {
                     HeapFree(GetProcessHeap(),0,realname);
diff --git a/programs/regedit/regproc.c b/programs/regedit/regproc.c
index c796d0b..3f55427 100644
--- a/programs/regedit/regproc.c
+++ b/programs/regedit/regproc.c
@@ -68,38 +68,9 @@ if (!(p)) \
 }
 
 /******************************************************************************
- * This is a replacement for strsep which is not portable (missing on Solaris).
- */
-#if 0
-/* DISABLED */
-char* getToken(char** str, const char* delims)
-{
-    char* token;
-
-    if (*str==NULL) {
-        /* No more tokens */
-        return NULL;
-    }
-
-    token=*str;
-    while (**str!='\0') {
-        if (strchr(delims,**str)!=NULL) {
-            **str='\0';
-            (*str)++;
-            return token;
-        }
-        (*str)++;
-    }
-    /* There is no other token */
-    *str=NULL;
-    return token;
-}
-#endif
-
-/******************************************************************************
  * Converts a hex representation of a DWORD into a DWORD.
  */
-DWORD convertHexToDWord(char *str, BYTE *buf)
+static DWORD convertHexToDWord(char *str, BYTE *buf)
 {
     DWORD dw;
     char xbuf[9];
@@ -115,7 +86,7 @@ DWORD convertHexToDWord(char *str, BYTE *buf)
  * Converts a hex comma separated values list into a hex list.
  * The Hex input string must be in exactly the correct form.
  */
-DWORD convertHexCSVToHex(char *str, BYTE *buf, ULONG bufLen)
+static DWORD convertHexCSVToHex(char *str, BYTE *buf, ULONG bufLen)
 {
     char *s = str;  /* Pointer to current */
     char *b = (char*) buf;  /* Pointer to result  */
@@ -161,7 +132,7 @@ DWORD convertHexCSVToHex(char *str, BYTE *buf, ULONG bufLen)
  *
  * Note: Updated based on the algorithm used in 'server/registry.c'
  */
-DWORD getDataType(LPSTR *lpValue, DWORD* parse_type)
+static DWORD getDataType(LPSTR *lpValue, DWORD* parse_type)
 {
     struct data_type { const char *tag; int len; int type; int parse_type; };
 
@@ -202,33 +173,6 @@ DWORD getDataType(LPSTR *lpValue, DWORD* parse_type)
 }
 
 /******************************************************************************
- * Returns an allocated buffer with a cleaned copy (removed the surrounding
- * dbl quotes) of the passed value.
- */
-LPSTR getArg( LPSTR arg)
-{
-    LPSTR tmp = NULL;
-    ULONG len;
-
-    if (arg == NULL)
-        return NULL;
-
-    /*
-     * Get rid of surrounding quotes
-     */
-    len = strlen(arg);
-
-    if( arg[len-1] == '\"' ) arg[len-1] = '\0';
-    if( arg[0]     == '\"' ) arg++;
-
-    tmp = HeapAlloc(GetProcessHeap(), 0, strlen(arg)+1);
-    CHECK_ENOUGH_MEMORY(tmp);
-    strcpy(tmp, arg);
-
-    return tmp;
-}
-
-/******************************************************************************
  * Replaces escape sequences with the characters.
  */
 static void REGPROC_unescape_string(LPSTR str)
@@ -268,7 +212,7 @@ static void REGPROC_unescape_string(LPSTR str)
  * val_name - name of the registry value
  * val_data - registry value data
  */
-HRESULT setValue(LPSTR val_name, LPSTR val_data)
+static HRESULT setValue(LPSTR val_name, LPSTR val_data)
 {
     HRESULT hRes;
     DWORD   dwDataType, dwParseType;
@@ -332,51 +276,51 @@ HRESULT setValue(LPSTR val_name, LPSTR val_data)
 
 
 /******************************************************************************
- * Open the key
+ * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
+ * the key class (what ends before the first '\')
  */
-HRESULT openKey( LPSTR stdInput)
+static HKEY getRegClass(LPSTR lpClass)
 {
-    DWORD   dwDisp;
-    HRESULT hRes;
-
-    /* Sanity checks */
-    if (stdInput == NULL)
-        return ERROR_INVALID_PARAMETER;
-
-    /* Get the registry class */
-    currentKeyClass = getRegClass(stdInput); /* Sets global variable */
-    if (currentKeyClass == (HKEY)ERROR_INVALID_PARAMETER)
-        return (HRESULT)ERROR_INVALID_PARAMETER;
+    LPSTR classNameEnd;
+    LPSTR classNameBeg;
+    unsigned int i;
 
-    /* Get the key name */
-    currentKeyName = getRegKeyName(stdInput); /* Sets global variable */
-    if (currentKeyName == NULL)
-        return ERROR_INVALID_PARAMETER;
+    char  lpClassCopy[KEY_MAX_LEN];
 
-    hRes = RegCreateKeyEx(
-               currentKeyClass,          /* Class     */
-               currentKeyName,           /* Sub Key   */
-               0,                        /* MUST BE 0 */
-               NULL,                     /* object type */
-               REG_OPTION_NON_VOLATILE,  /* option, REG_OPTION_NON_VOLATILE ... */
-               KEY_ALL_ACCESS,           /* access mask, KEY_ALL_ACCESS */
-               NULL,                     /* security attribute */
-               &currentKeyHandle,        /* result */
-               &dwDisp);                 /* disposition, REG_CREATED_NEW_KEY or
-                                                        REG_OPENED_EXISTING_KEY */
+    if (lpClass == NULL)
+        return (HKEY)ERROR_INVALID_PARAMETER;
 
-    if (hRes == ERROR_SUCCESS)
-        bTheKeyIsOpen = TRUE;
+    lstrcpynA(lpClassCopy, lpClass, KEY_MAX_LEN);
 
-    return hRes;
+    classNameEnd  = strchr(lpClassCopy, '\\');    /* The class name ends by '\' */
+    if (!classNameEnd)                            /* or the whole string */
+    {
+        classNameEnd = lpClassCopy + strlen(lpClassCopy);
+        if (classNameEnd[-1] == ']')
+        {
+            classNameEnd--;
+        }
+    }
+    *classNameEnd = '\0';                       /* Isolate the class name */
+    if (lpClassCopy[0] == '[') {
+        classNameBeg = lpClassCopy + 1;
+    } else {
+        classNameBeg = lpClassCopy;
+    }
 
+    for (i = 0; i < REG_CLASS_NUMBER; i++) {
+        if (!strcmp(classNameBeg, reg_class_names[i])) {
+            return reg_class_keys[i];
+        }
+    }
+    return (HKEY)ERROR_INVALID_PARAMETER;
 }
 
 /******************************************************************************
  * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
  * the key name (what starts after the first '\')
  */
-LPSTR getRegKeyName(LPSTR lpLine)
+static LPSTR getRegKeyName(LPSTR lpLine)
 {
     LPSTR keyNameBeg;
     char  lpLineCopy[KEY_MAX_LEN];
@@ -409,50 +353,50 @@ LPSTR getRegKeyName(LPSTR lpLine)
 }
 
 /******************************************************************************
- * Extracts from [HKEY\some\key\path] or HKEY\some\key\path types of line
- * the key class (what ends before the first '\')
+ * Open the key
  */
-HKEY getRegClass(LPSTR lpClass)
+static HRESULT openKey( LPSTR stdInput)
 {
-    LPSTR classNameEnd;
-    LPSTR classNameBeg;
-    unsigned int i;
+    DWORD   dwDisp;
+    HRESULT hRes;
 
-    char  lpClassCopy[KEY_MAX_LEN];
+    /* Sanity checks */
+    if (stdInput == NULL)
+        return ERROR_INVALID_PARAMETER;
 
-    if (lpClass == NULL)
-        return (HKEY)ERROR_INVALID_PARAMETER;
+    /* Get the registry class */
+    currentKeyClass = getRegClass(stdInput); /* Sets global variable */
+    if (currentKeyClass == (HKEY)ERROR_INVALID_PARAMETER)
+        return (HRESULT)ERROR_INVALID_PARAMETER;
 
-    lstrcpynA(lpClassCopy, lpClass, KEY_MAX_LEN);
+    /* Get the key name */
+    currentKeyName = getRegKeyName(stdInput); /* Sets global variable */
+    if (currentKeyName == NULL)
+        return ERROR_INVALID_PARAMETER;
 
-    classNameEnd  = strchr(lpClassCopy, '\\');    /* The class name ends by '\' */
-    if (!classNameEnd)                            /* or the whole string */
-    {
-        classNameEnd = lpClassCopy + strlen(lpClassCopy);
-        if (classNameEnd[-1] == ']')
-        {
-            classNameEnd--;
-        }
-    }
-    *classNameEnd = '\0';                       /* Isolate the class name */
-    if (lpClassCopy[0] == '[') {
-        classNameBeg = lpClassCopy + 1;
-    } else {
-        classNameBeg = lpClassCopy;
-    }
+    hRes = RegCreateKeyEx(
+               currentKeyClass,          /* Class     */
+               currentKeyName,           /* Sub Key   */
+               0,                        /* MUST BE 0 */
+               NULL,                     /* object type */
+               REG_OPTION_NON_VOLATILE,  /* option, REG_OPTION_NON_VOLATILE ... */
+               KEY_ALL_ACCESS,           /* access mask, KEY_ALL_ACCESS */
+               NULL,                     /* security attribute */
+               &currentKeyHandle,        /* result */
+               &dwDisp);                 /* disposition, REG_CREATED_NEW_KEY or
+                                                        REG_OPENED_EXISTING_KEY */
+
+    if (hRes == ERROR_SUCCESS)
+        bTheKeyIsOpen = TRUE;
+
+    return hRes;
 
-    for (i = 0; i < REG_CLASS_NUMBER; i++) {
-        if (!strcmp(classNameBeg, reg_class_names[i])) {
-            return reg_class_keys[i];
-        }
-    }
-    return (HKEY)ERROR_INVALID_PARAMETER;
 }
 
 /******************************************************************************
  * Close the currently opened key.
  */
-void closeKey(void)
+static void closeKey(void)
 {
     RegCloseKey(currentKeyHandle);
 
@@ -466,86 +410,6 @@ void closeKey(void)
 }
 
 /******************************************************************************
- * This function is the main entry point to the setValue type of action.  It
- * receives the currently read line and dispatch the work depending on the
- * context.
- */
-void doSetValue(LPSTR stdInput)
-{
-    /*
-     * We encountered the end of the file, make sure we
-     * close the opened key and exit
-     */
-    if (stdInput == NULL) {
-        if (bTheKeyIsOpen != FALSE)
-            closeKey();
-
-        return;
-    }
-
-    if      ( stdInput[0] == '[')      /* We are reading a new key */
-    {
-        if ( bTheKeyIsOpen != FALSE )
-            closeKey();                    /* Close the previous key before */
-
-        /* delete the key if we encounter '-' at the start of reg key */
-        if ( stdInput[1] == '-')
-        {
-            int last_chr = strlen(stdInput) - 1;
-
-            /* skip leading "[-" and get rid of trailing "]" */
-            if (stdInput[last_chr] == ']')
-                stdInput[last_chr] = '\0';
-            delete_registry_key(stdInput+2);
-            return;
-        }
-
-        if ( openKey(stdInput) != ERROR_SUCCESS )
-            fprintf(stderr,"%s: setValue failed to open key %s\n",
-                    getAppName(), stdInput);
-    } else if( ( bTheKeyIsOpen ) &&
-               (( stdInput[0] == '@') || /* reading a default @=data pair */
-                ( stdInput[0] == '\"'))) /* reading a new value=data pair */
-    {
-        processSetValue(stdInput);
-    } else                               /* since we are assuming that the */
-    {                                  /* file format is valid we must   */
-        if ( bTheKeyIsOpen )             /* be reading a blank line which  */
-            closeKey();                    /* indicate end of this key processing */
-    }
-}
-
-/******************************************************************************
- * This function is the main entry point to the deleteValue type of action.  It
- * receives the currently read line and dispatch the work depending on the
- * context.
- */
-void doDeleteValue(LPSTR line)
-{
-    fprintf(stderr,"%s: deleteValue not yet implemented\n", getAppName());
-}
-
-/******************************************************************************
- * This function is the main entry point to the deleteKey type of action.  It
- * receives the currently read line and dispatch the work depending on the
- * context.
- */
-void doDeleteKey(LPSTR line)
-{
-    fprintf(stderr,"%s: deleteKey not yet implemented\n", getAppName());
-}
-
-/******************************************************************************
- * This function is the main entry point to the createKey type of action.  It
- * receives the currently read line and dispatch the work depending on the
- * context.
- */
-void doCreateKey(LPSTR line)
-{
-    fprintf(stderr,"%s: createKey not yet implemented\n", getAppName());
-}
-
-/******************************************************************************
  * This function is a wrapper for the setValue function.  It prepares the
  * land and clean the area once completed.
  * Note: this function modifies the line parameter.
@@ -553,7 +417,7 @@ void doCreateKey(LPSTR line)
  * line - registry file unwrapped line. Should have the registry value name and
  *      complete registry value data.
  */
-void processSetValue(LPSTR line)
+static void processSetValue(LPSTR line)
 {
     LPSTR val_name;                   /* registry value name   */
     LPSTR val_data;                   /* registry value data   */
@@ -607,14 +471,62 @@ void processSetValue(LPSTR line)
 }
 
 /******************************************************************************
- * Calls command for each line of a registry file.
+ * This function receives the currently read entry and performs the
+ * corresponding action.
+ */
+static void processRegEntry(LPSTR stdInput)
+{
+    /*
+     * We encountered the end of the file, make sure we
+     * close the opened key and exit
+     */
+    if (stdInput == NULL) {
+        if (bTheKeyIsOpen != FALSE)
+            closeKey();
+
+        return;
+    }
+
+    if      ( stdInput[0] == '[')      /* We are reading a new key */
+    {
+        if ( bTheKeyIsOpen != FALSE )
+            closeKey();                    /* Close the previous key before */
+
+        /* delete the key if we encounter '-' at the start of reg key */
+        if ( stdInput[1] == '-')
+        {
+            int last_chr = strlen(stdInput) - 1;
+
+            /* skip leading "[-" and get rid of trailing "]" */
+            if (stdInput[last_chr] == ']')
+                stdInput[last_chr] = '\0';
+            delete_registry_key(stdInput+2);
+            return;
+        }
+
+        if ( openKey(stdInput) != ERROR_SUCCESS )
+            fprintf(stderr,"%s: setValue failed to open key %s\n",
+                    getAppName(), stdInput);
+    } else if( ( bTheKeyIsOpen ) &&
+               (( stdInput[0] == '@') || /* reading a default @=data pair */
+                ( stdInput[0] == '\"'))) /* reading a new value=data pair */
+    {
+        processSetValue(stdInput);
+    } else                               /* since we are assuming that the */
+    {                                  /* file format is valid we must   */
+        if ( bTheKeyIsOpen )             /* be reading a blank line which  */
+            closeKey();                    /* indicate end of this key processing */
+    }
+}
+
+/******************************************************************************
+ * Processes a registry file.
  * Correctly processes comments (in # form), line continuation.
  *
  * Parameters:
  *   in - input stream to read from
- *   command - command to be called for each line
  */
-void processRegLines(FILE *in, CommandAPI command)
+void processRegLines(FILE *in)
 {
     LPSTR line           = NULL;  /* line read from input stream */
     ULONG lineSize       = REG_VAL_BUF_SIZE;
@@ -705,79 +617,13 @@ void processRegLines(FILE *in, CommandAPI command)
             break; /* That is the full virtual line */
         }
 
-        command(line);
+        processRegEntry(line);
     }
-    command(NULL);
+    processRegEntry(NULL);
 
     HeapFree(GetProcessHeap(), 0, line);
 }
 
-/******************************************************************************
- * This function is the main entry point to the registerDLL action.  It
- * receives the currently read line, then loads and registers the requested DLLs
- */
-void doRegisterDLL(LPSTR stdInput)
-{
-    HMODULE theLib = 0;
-    UINT retVal = 0;
-
-    /* Check for valid input */
-    if (stdInput == NULL)
-        return;
-
-    /* Load and register the library, then free it */
-    theLib = LoadLibrary(stdInput);
-    if (theLib) {
-        FARPROC lpfnDLLRegProc = GetProcAddress(theLib, "DllRegisterServer");
-        if (lpfnDLLRegProc)
-            retVal = (*lpfnDLLRegProc)();
-        else
-            fprintf(stderr,"%s: Couldn't find DllRegisterServer proc in '%s'.\n",
-                    getAppName(), stdInput);
-
-        if (retVal != S_OK)
-            fprintf(stderr,"%s: DLLRegisterServer error 0x%x in '%s'.\n",
-                    getAppName(), retVal, stdInput);
-
-        FreeLibrary(theLib);
-    } else {
-        fprintf(stderr,"%s: Could not load DLL '%s'.\n", getAppName(), stdInput);
-    }
-}
-
-/******************************************************************************
- * This function is the main entry point to the unregisterDLL action.  It
- * receives the currently read line, then loads and unregisters the requested DLLs
- */
-void doUnregisterDLL(LPSTR stdInput)
-{
-    HMODULE theLib = 0;
-    UINT retVal = 0;
-
-    /* Check for valid input */
-    if (stdInput == NULL)
-        return;
-
-    /* Load and unregister the library, then free it */
-    theLib = LoadLibrary(stdInput);
-    if (theLib) {
-        FARPROC lpfnDLLRegProc = GetProcAddress(theLib, "DllUnregisterServer");
-        if (lpfnDLLRegProc)
-            retVal = (*lpfnDLLRegProc)();
-        else
-            fprintf(stderr,"%s: Couldn't find DllUnregisterServer proc in '%s'.\n",
-                    getAppName(), stdInput);
-
-        if (retVal != S_OK)
-            fprintf(stderr,"%s: DLLUnregisterServer error 0x%x in '%s'.\n",
-                    getAppName(), retVal, stdInput);
-
-        FreeLibrary(theLib);
-    } else {
-        fprintf(stderr,"%s: Could not load DLL '%s'.\n", getAppName(), stdInput);
-    }
-}
-
 /****************************************************************************
  * REGPROC_print_error
  *
@@ -1136,7 +982,7 @@ BOOL import_registry_file(LPTSTR filename)
     FILE* reg_file = fopen(filename, "r");
 
     if (reg_file) {
-        processRegLines(reg_file, doSetValue);
+        processRegLines(reg_file);
         return TRUE;
     }
     return FALSE;
diff --git a/programs/regedit/regproc.h b/programs/regedit/regproc.h
index b462546..d0209e8 100644
--- a/programs/regedit/regproc.h
+++ b/programs/regedit/regproc.h
@@ -17,49 +17,11 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-/******************************************************************************
- * Defines and consts
- */
 #define KEY_MAX_LEN             1024
 
-/* Return values */
-#define SUCCESS               0
-#define KEY_VALUE_ALREADY_SET 2
-
-typedef void (*CommandAPI)(LPSTR lpsLine);
-
-void doSetValue(LPSTR lpsLine);
-void doDeleteValue(LPSTR lpsLine);
-void doCreateKey(LPSTR lpsLine);
-void doDeleteKey(LPSTR lpsLine);
-void doRegisterDLL(LPSTR lpsLine);
-void doUnregisterDLL(LPSTR lpsLine);
+const CHAR *getAppName(void);
 
 BOOL export_registry_key(CHAR *file_name, CHAR *reg_key_name);
 BOOL import_registry_file(LPTSTR filename);
 void delete_registry_key(CHAR *reg_key_name);
-
-const CHAR *getAppName(void);
-
-void processRegLines(FILE *in, CommandAPI command);
-
-/*
- * Generic prototypes
- */
-char*   getToken(char** str, const char* delims);
-DWORD   convertHexToDWord(char *str, BYTE *buf);
-DWORD   convertHexCSVToHex(char *str, BYTE *buf, ULONG bufLen);
-LPSTR   convertHexToHexCSV( BYTE *buf, ULONG len);
-LPSTR   convertHexToDWORDStr( BYTE *buf, ULONG len);
-LPSTR   getRegKeyName(LPSTR lpLine);
-HKEY    getRegClass(LPSTR lpLine);
-DWORD   getDataType(LPSTR *lpValue, DWORD* parse_type);
-LPSTR   getArg(LPSTR arg);
-HRESULT openKey(LPSTR stdInput);
-void    closeKey(void);
-
-/*
- * api setValue prototypes
- */
-void    processSetValue(LPSTR cmdline);
-HRESULT setValue(LPSTR val_name, LPSTR val_data);
+void processRegLines(FILE *in);
-- 
1.4.4.4




More information about the wine-patches mailing list