encapsulated common operations

Mon, 30 May 2011 08:45:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 May 2011 08:45:08 +0200
changeset 8
28319b20968c
parent 7
1b55f3fa52c9
child 9
1dd63a32ffc4
child 10
ecf787666f44

encapsulated common operations

cline.c file | annotate | diff | comparison | revisions
cline.h file | annotate | diff | comparison | revisions
functions.c file | annotate | diff | comparison | revisions
functions.h file | annotate | diff | comparison | revisions
--- 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
--- 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
--- 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);
--- 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);

mercurial