[FSUTIL]
authorPierre Schweitzer <pierre@reactos.org>
Tue, 5 Sep 2017 13:10:16 +0000 (13:10 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Tue, 5 Sep 2017 13:10:16 +0000 (13:10 +0000)
Avoid code duplication

svn path=/trunk/; revision=75765

reactos/base/applications/cmdutils/fsutil/CMakeLists.txt
reactos/base/applications/cmdutils/fsutil/common.c [new file with mode: 0644]
reactos/base/applications/cmdutils/fsutil/dirty.c
reactos/base/applications/cmdutils/fsutil/fsutil.c
reactos/base/applications/cmdutils/fsutil/fsutil.h

diff --git a/reactos/base/applications/cmdutils/fsutil/common.c b/reactos/base/applications/cmdutils/fsutil/common.c
new file mode 100644 (file)
index 0000000..563b52c
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS FS utility tool
+ * FILE:            base/applications/cmdutils/common.c
+ * PURPOSE:         FSutil common functions
+ * PROGRAMMERS:     Pierre Schweitzer <pierre@reactos.org>
+ */
+
+#include "fsutil.h"
+
+int FindHandler(int argc,
+                const TCHAR *argv[],
+                HandlerItem * HandlersList,
+                int HandlerListCount,
+                void (*UsageHelper)(const TCHAR *))
+{
+    int i;
+    int ret;
+    const TCHAR * Command;
+
+    ret = 1;
+    Command = NULL;
+    i = HandlerListCount;
+
+    /* If we have a command, does it match a known one? */
+    if (argc > 1)
+    {
+        /* Browse all the known commands finding the right one */
+        Command = argv[1];
+        for (i = 0; i < HandlerListCount; ++i)
+        {
+            if (_tcsicmp(Command, HandlersList[i].Command) == 0)
+            {
+                ret = HandlersList[i].Handler(argc - 1, &argv[1]);
+                break;
+            }
+        }
+    }
+
+    /* We failed finding someone to handle the caller's needs, print out */
+    if (i == HandlerListCount)
+    {
+        UsageHelper(Command);
+    }
+
+    return ret;
+}
index ead5a36..cec651e 100644 (file)
@@ -93,34 +93,7 @@ PrintUsage(const TCHAR * Command)
 int
 DirtyMain(int argc, const TCHAR *argv[])
 {
-    int i;
-    int ret;
-    const TCHAR * Command;
-
-    ret = 1;
-    Command = NULL;
-    i = (sizeof(HandlersList) / sizeof(HandlersList[0]));
-
-    /* If we have a command, does it match a known one? */
-    if (argc > 1)
-    {
-        /* Browse all the known commands finding the right one */
-        Command = argv[1];
-        for (i = 0; i < (sizeof(HandlersList) / sizeof(HandlersList[0])); ++i)
-        {
-            if (_tcsicmp(Command, HandlersList[i].Command) == 0)
-            {
-                ret = HandlersList[i].Handler(argc - 1, &argv[1]);
-                break;
-            }
-        }
-    }
-
-    /* We failed finding someone to handle the caller's needs, print out */
-    if (i == (sizeof(HandlersList) / sizeof(HandlersList[0])))
-    {
-        PrintUsage(Command);
-    }
-
-    return ret;
+    return FindHandler(argc, argv, (HandlerItem *)&HandlersList,
+                       (sizeof(HandlersList) / sizeof(HandlersList[0])),
+                       PrintUsage);
 }
index 9269f0f..293b721 100644 (file)
@@ -39,34 +39,7 @@ int
 __cdecl
 _tmain(int argc, const TCHAR *argv[])
 {
-    int i;
-    int ret;
-    const TCHAR * Command;
-
-    ret = 1;
-    Command = NULL;
-    i = (sizeof(HandlersList) / sizeof(HandlersList[0]));
-
-    /* If we have a command, does it match a known one? */
-    if (argc > 1)
-    {
-        /* Browse all the known commands finding the right one */
-        Command = argv[1];
-        for (i = 0; i < (sizeof(HandlersList) / sizeof(HandlersList[0])); ++i)
-        {
-            if (_tcsicmp(Command, HandlersList[i].Command) == 0)
-            {
-                ret = HandlersList[i].Handler(argc - 1, &argv[1]);
-                break;
-            }
-        }
-    }
-
-    /* We failed finding someone to handle the caller's needs, print out */
-    if (i == (sizeof(HandlersList) / sizeof(HandlersList[0])))
-    {
-        PrintUsage(Command);
-    }
-
-    return ret;
+    return FindHandler(argc, argv, (HandlerItem *)&HandlersList,
+                       (sizeof(HandlersList) / sizeof(HandlersList[0])),
+                       PrintUsage);
 }
index 1eaaba0..bd4a306 100644 (file)
@@ -12,4 +12,10 @@ typedef struct
     const TCHAR * Desc;
 } HandlerItem;
 
+int FindHandler(int argc,
+                const TCHAR *argv[],
+                HandlerItem * HandlersList,
+                int HandlerListCount,
+                void (*UsageHelper)(const TCHAR *));
+
 #endif