Mon, 30 May 2011 08:45:08 +0200
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 |
1.1 --- a/cline.c Fri May 27 15:10:23 2011 +0200 1.2 +++ b/cline.c Mon May 30 08:45:08 2011 +0200 1.3 @@ -72,6 +72,12 @@ 1.4 printf(helpText, prgName, prgName, prgName, prgName); 1.5 } 1.6 1.7 +int exit_with_help(char* prgName, settings_t* settings, int code) { 1.8 + printHelpText(prgName); 1.9 + destroy_settings_t(settings); 1.10 + return code; 1.11 +} 1.12 + 1.13 int main(int argc, char** argv) { 1.14 1.15 // Settings 1.16 @@ -94,7 +100,7 @@ 1.17 char* directory = "./"; 1.18 char* suffix = " "; 1.19 bool showHelp = false; 1.20 - char checked = 0; 1.21 + int checked = 0; 1.22 1.23 for (int t = 1 ; t < argc ; t++) { 1.24 1.25 @@ -102,84 +108,61 @@ 1.26 1.27 // s 1.28 if ((argflags & 2) > 0) { 1.29 - if ((checked & 1) > 0) { 1.30 - printHelpText(prgName); 1.31 - destroy_settings_t(settings); 1.32 - return 1; 1.33 + if (registerArgument(&checked, 6)) { 1.34 + return exit_with_help(prgName, settings, 1); 1.35 } 1.36 settings->includeSuffixes = true; 1.37 t++; 1.38 if (t >= argc) { 1.39 - printHelpText(prgName); 1.40 - destroy_settings_t(settings); 1.41 - return 1; 1.42 + return exit_with_help(prgName, settings, 1); 1.43 } 1.44 suffix = argv[t]; 1.45 - checked |= 1; 1.46 } 1.47 // S 1.48 if ((argflags & 4) > 0) { 1.49 - if ((checked & 1) > 0) { 1.50 - printHelpText(prgName); 1.51 - destroy_settings_t(settings); 1.52 - return 1; 1.53 + if (registerArgument(&checked, 6)) { 1.54 + return exit_with_help(prgName, settings, 1); 1.55 } 1.56 settings->includeSuffixes = false; 1.57 t++; 1.58 if (t >= argc) { 1.59 - printHelpText(prgName); 1.60 - destroy_settings_t(settings); 1.61 - return 1; 1.62 + return exit_with_help(prgName, settings, 1); 1.63 } 1.64 suffix = argv[t]; 1.65 - checked |= 1; 1.66 } 1.67 // h 1.68 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { 1.69 - if ((checked & 2) > 0) { 1.70 - printHelpText(prgName); 1.71 - destroy_settings_t(settings); 1.72 - return 1; 1.73 + if (registerArgument(&checked, 1)) { 1.74 + return exit_with_help(prgName, settings, 1); 1.75 } 1.76 - checked |= 2; 1.77 showHelp = true; 1.78 } 1.79 // r, R 1.80 if ((argflags & 24) > 0) { 1.81 - if ((checked & 4) > 0) { 1.82 - printHelpText(prgName); 1.83 - destroy_settings_t(settings); 1.84 - return 1; 1.85 + if (registerArgument(&checked, 24)) { 1.86 + return exit_with_help(prgName, settings, 1); 1.87 } 1.88 - checked |= 4; 1.89 settings->recursive = true; 1.90 } 1.91 + // m 1.92 if ((argflags & 32) > 0) { 1.93 - if ((checked & 32) > 0) { 1.94 - printHelpText(prgName); 1.95 - destroy_settings_t(settings); 1.96 - return 1; 1.97 + if (registerArgument(&checked, 32)) { 1.98 + return exit_with_help(prgName, settings, 1); 1.99 } 1.100 - checked |= 32; 1.101 settings->matchesOnly = true; 1.102 } 1.103 // Path 1.104 if (argflags == 0) { 1.105 - if ((checked & 8) > 0) { 1.106 - printHelpText(prgName); 1.107 - destroy_settings_t(settings); 1.108 - return 1; 1.109 + if (registerArgument(&checked, 1024)) { 1.110 + return exit_with_help(prgName, settings, 1); 1.111 } 1.112 - checked |= 8; 1.113 directory = argv[t]; 1.114 } 1.115 } 1.116 1.117 // Show help and quit 1.118 if (showHelp) { 1.119 - printHelpText(prgName); 1.120 - destroy_settings_t(settings); 1.121 - return 0; 1.122 + return exit_with_help(prgName, settings, 0); 1.123 } 1.124 1.125 // Find tokens
2.1 --- a/cline.h Fri May 27 15:10:23 2011 +0200 2.2 +++ b/cline.h Mon May 30 08:45:08 2011 +0200 2.3 @@ -31,6 +31,7 @@ 2.4 void add_suffix(suffix_list_t*, char*); 2.5 2.6 void printHelpText(const char*); 2.7 +int exit_with_help(char*, settings_t*, int); 2.8 #ifdef _cplusplus 2.9 } 2.10 #endif
3.1 --- a/functions.c Fri May 27 15:10:23 2011 +0200 3.2 +++ b/functions.c Mon May 30 08:45:08 2011 +0200 3.3 @@ -16,6 +16,12 @@ 3.4 return ret; 3.5 } 3.6 3.7 +bool registerArgument(int* reg, int mask) { 3.8 + bool ret = (*reg & mask) > 0; 3.9 + *reg |= mask; 3.10 + return ret; 3.11 +} 3.12 + 3.13 bool testSuffix(char* filename, settings_t* settings) { 3.14 bool ret = false; 3.15 int tokenlen, fnamelen = strlen(filename);
4.1 --- a/functions.h Fri May 27 15:10:23 2011 +0200 4.2 +++ b/functions.h Mon May 30 08:45:08 2011 +0200 4.3 @@ -6,6 +6,7 @@ 4.4 #endif 4.5 4.6 int checkArgument(const char*, const char*); 4.7 +bool registerArgument(int*, int); 4.8 bool testSuffix(char* filename, settings_t* settings); 4.9 int scanDirectory(DIR *dir, const int spaces, 4.10 char* currdir, settings_t* settings);