# HG changeset patch # User Mike Becker # Date 1306737908 -7200 # Node ID 28319b20968cb9e7beec94558955bdef14564405 # Parent 1b55f3fa52c9c1f1c96b48532fd92d5c3bfb0876 encapsulated common operations diff -r 1b55f3fa52c9 -r 28319b20968c cline.c --- a/cline.c Fri May 27 15:10:23 2011 +0200 +++ b/cline.c Mon May 30 08:45:08 2011 +0200 @@ -72,6 +72,12 @@ printf(helpText, prgName, prgName, prgName, prgName); } +int exit_with_help(char* prgName, settings_t* settings, int code) { + printHelpText(prgName); + destroy_settings_t(settings); + return code; +} + int main(int argc, char** argv) { // Settings @@ -94,7 +100,7 @@ char* directory = "./"; char* suffix = " "; bool showHelp = false; - char checked = 0; + int checked = 0; for (int t = 1 ; t < argc ; t++) { @@ -102,84 +108,61 @@ // s if ((argflags & 2) > 0) { - if ((checked & 1) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 6)) { + return exit_with_help(prgName, settings, 1); } settings->includeSuffixes = true; t++; if (t >= argc) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + return exit_with_help(prgName, settings, 1); } suffix = argv[t]; - checked |= 1; } // S if ((argflags & 4) > 0) { - if ((checked & 1) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 6)) { + return exit_with_help(prgName, settings, 1); } settings->includeSuffixes = false; t++; if (t >= argc) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + return exit_with_help(prgName, settings, 1); } suffix = argv[t]; - checked |= 1; } // h if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { - if ((checked & 2) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 1)) { + return exit_with_help(prgName, settings, 1); } - checked |= 2; showHelp = true; } // r, R if ((argflags & 24) > 0) { - if ((checked & 4) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 24)) { + return exit_with_help(prgName, settings, 1); } - checked |= 4; settings->recursive = true; } + // m if ((argflags & 32) > 0) { - if ((checked & 32) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 32)) { + return exit_with_help(prgName, settings, 1); } - checked |= 32; settings->matchesOnly = true; } // Path if (argflags == 0) { - if ((checked & 8) > 0) { - printHelpText(prgName); - destroy_settings_t(settings); - return 1; + if (registerArgument(&checked, 1024)) { + return exit_with_help(prgName, settings, 1); } - checked |= 8; directory = argv[t]; } } // Show help and quit if (showHelp) { - printHelpText(prgName); - destroy_settings_t(settings); - return 0; + return exit_with_help(prgName, settings, 0); } // Find tokens diff -r 1b55f3fa52c9 -r 28319b20968c cline.h --- a/cline.h Fri May 27 15:10:23 2011 +0200 +++ b/cline.h Mon May 30 08:45:08 2011 +0200 @@ -31,6 +31,7 @@ void add_suffix(suffix_list_t*, char*); void printHelpText(const char*); +int exit_with_help(char*, settings_t*, int); #ifdef _cplusplus } #endif diff -r 1b55f3fa52c9 -r 28319b20968c functions.c --- a/functions.c Fri May 27 15:10:23 2011 +0200 +++ b/functions.c Mon May 30 08:45:08 2011 +0200 @@ -16,6 +16,12 @@ return ret; } +bool registerArgument(int* reg, int mask) { + bool ret = (*reg & mask) > 0; + *reg |= mask; + return ret; +} + bool testSuffix(char* filename, settings_t* settings) { bool ret = false; int tokenlen, fnamelen = strlen(filename); diff -r 1b55f3fa52c9 -r 28319b20968c functions.h --- a/functions.h Fri May 27 15:10:23 2011 +0200 +++ b/functions.h Mon May 30 08:45:08 2011 +0200 @@ -6,6 +6,7 @@ #endif int checkArgument(const char*, const char*); +bool registerArgument(int*, int); bool testSuffix(char* filename, settings_t* settings); int scanDirectory(DIR *dir, const int spaces, char* currdir, settings_t* settings);