Avoid code duplication

svn path=/trunk/; revision=75765
This commit is contained in:
Pierre Schweitzer 2017-09-05 13:10:16 +00:00
parent 7f463d43db
commit e03a7274dc
5 changed files with 60 additions and 60 deletions

View file

@ -1,4 +1,5 @@
list(APPEND SOURCE
common.c
dirty.c
fsutil.c
fsutil.h)

View file

@ -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;
}

View 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);
}

View 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);
}

View 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