Thu, 15 Sep 2011 13:29:06 +0200
refactored sources
universe@10 | 1 | /* |
universe@10 | 2 | * cline.c |
universe@10 | 3 | * |
universe@10 | 4 | * Created on: 23.05.2011 |
universe@10 | 5 | * Author: beckermi |
universe@10 | 6 | */ |
universe@10 | 7 | |
universe@3 | 8 | #include "cline.h" |
universe@10 | 9 | #include "scanner.h" |
universe@10 | 10 | #include "settings.h" |
universe@0 | 11 | |
universe@1 | 12 | void printHelpText(const char* prgName) { |
universe@0 | 13 | // Help text |
universe@0 | 14 | const char* helpText = |
universe@1 | 15 | "\nUsage:" |
universe@1 | 16 | "\n %s [-hrm][-s suffix][<directory>]" |
universe@1 | 17 | "\n %s [-hrm][-S suffix][<directory>]" |
universe@0 | 18 | "\n\nCounts the line terminator characters (\\n) within all" |
universe@0 | 19 | " files in the specified\ndirectory." |
universe@0 | 20 | "\n\nOptions:" |
universe@0 | 21 | "\n -h, --help - this help text" |
universe@1 | 22 | "\n -m - print information about matching files only" |
universe@1 | 23 | "\n -s <suffixes> - only count files with these suffixes (separated" |
universe@0 | 24 | "\n by commas)" |
universe@1 | 25 | "\n -S <suffixes> - count any file except those with these suffixes" |
universe@0 | 26 | "\n (separated by commas)" |
universe@1 | 27 | "\n -r, -R - includes subdirectories" |
universe@0 | 28 | "\n\n" |
universe@1 | 29 | "The default call without any options is:" |
universe@1 | 30 | "\n %s ./\n" |
universe@7 | 31 | "So each file in the working directory is counted. If you want to count C" |
universe@7 | 32 | "\nsource code in your working directory and its subdirectories, type:" |
universe@1 | 33 | "\n %s -rs .c\n"; |
universe@1 | 34 | |
universe@3 | 35 | printf(helpText, prgName, prgName, prgName, prgName); |
universe@1 | 36 | } |
universe@1 | 37 | |
universe@8 | 38 | int exit_with_help(char* prgName, settings_t* settings, int code) { |
universe@8 | 39 | printHelpText(prgName); |
universe@8 | 40 | destroy_settings_t(settings); |
universe@8 | 41 | return code; |
universe@8 | 42 | } |
universe@8 | 43 | |
universe@1 | 44 | int main(int argc, char** argv) { |
universe@0 | 45 | |
universe@3 | 46 | // Settings |
universe@3 | 47 | settings_t *settings = new_settings_t(); |
universe@5 | 48 | if (settings == NULL) { |
universe@5 | 49 | fprintf(stderr, "Memory allocation failed.\n"); |
universe@5 | 50 | return 1; |
universe@5 | 51 | } |
universe@3 | 52 | |
universe@0 | 53 | // Program name |
universe@3 | 54 | char* prgName = strrchr(argv[0], settings->fileSeparator); |
universe@1 | 55 | |
universe@1 | 56 | if (prgName == NULL) { |
universe@0 | 57 | prgName = argv[0]; |
universe@3 | 58 | } else { |
universe@0 | 59 | prgName++; |
universe@0 | 60 | } |
universe@0 | 61 | |
universe@0 | 62 | // Get arguments |
universe@7 | 63 | char* directory = "./"; |
universe@7 | 64 | char* suffix = " "; |
universe@0 | 65 | bool showHelp = false; |
universe@8 | 66 | int checked = 0; |
universe@0 | 67 | |
universe@1 | 68 | for (int t = 1 ; t < argc ; t++) { |
universe@1 | 69 | |
universe@1 | 70 | int argflags = checkArgument(argv[t], "hsSrRm"); |
universe@1 | 71 | |
universe@1 | 72 | // s |
universe@1 | 73 | if ((argflags & 2) > 0) { |
universe@8 | 74 | if (registerArgument(&checked, 6)) { |
universe@8 | 75 | return exit_with_help(prgName, settings, 1); |
universe@0 | 76 | } |
universe@3 | 77 | settings->includeSuffixes = true; |
universe@1 | 78 | t++; |
universe@1 | 79 | if (t >= argc) { |
universe@8 | 80 | return exit_with_help(prgName, settings, 1); |
universe@1 | 81 | } |
universe@1 | 82 | suffix = argv[t]; |
universe@0 | 83 | } |
universe@1 | 84 | // S |
universe@1 | 85 | if ((argflags & 4) > 0) { |
universe@8 | 86 | if (registerArgument(&checked, 6)) { |
universe@8 | 87 | return exit_with_help(prgName, settings, 1); |
universe@0 | 88 | } |
universe@3 | 89 | settings->includeSuffixes = false; |
universe@1 | 90 | t++; |
universe@1 | 91 | if (t >= argc) { |
universe@8 | 92 | return exit_with_help(prgName, settings, 1); |
universe@1 | 93 | } |
universe@1 | 94 | suffix = argv[t]; |
universe@0 | 95 | } |
universe@1 | 96 | // h |
universe@1 | 97 | if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { |
universe@10 | 98 | return exit_with_help(prgName, settings, 1); |
universe@0 | 99 | } |
universe@1 | 100 | // r, R |
universe@1 | 101 | if ((argflags & 24) > 0) { |
universe@8 | 102 | if (registerArgument(&checked, 24)) { |
universe@8 | 103 | return exit_with_help(prgName, settings, 1); |
universe@0 | 104 | } |
universe@3 | 105 | settings->recursive = true; |
universe@0 | 106 | } |
universe@8 | 107 | // m |
universe@1 | 108 | if ((argflags & 32) > 0) { |
universe@8 | 109 | if (registerArgument(&checked, 32)) { |
universe@8 | 110 | return exit_with_help(prgName, settings, 1); |
universe@0 | 111 | } |
universe@3 | 112 | settings->matchesOnly = true; |
universe@0 | 113 | } |
universe@6 | 114 | // Path |
universe@1 | 115 | if (argflags == 0) { |
universe@8 | 116 | if (registerArgument(&checked, 1024)) { |
universe@8 | 117 | return exit_with_help(prgName, settings, 1); |
universe@0 | 118 | } |
universe@0 | 119 | directory = argv[t]; |
universe@0 | 120 | } |
universe@0 | 121 | } |
universe@0 | 122 | |
universe@0 | 123 | // Find tokens |
universe@6 | 124 | char* finder = strtok(suffix, ","); |
universe@1 | 125 | while (finder != NULL) { |
universe@6 | 126 | add_suffix(settings->suffixList, finder); |
universe@0 | 127 | finder = strtok(NULL, ","); |
universe@0 | 128 | } |
universe@0 | 129 | |
universe@0 | 130 | // Open directory |
universe@0 | 131 | DIR *dir = opendir(directory); |
universe@1 | 132 | if (dir == NULL) { |
universe@0 | 133 | perror("Operation failed"); |
universe@4 | 134 | destroy_settings_t(settings); |
universe@0 | 135 | return 1; |
universe@0 | 136 | } |
universe@0 | 137 | |
universe@0 | 138 | // Scan directory |
universe@3 | 139 | int lines = scanDirectory(dir, 0, directory, settings); |
universe@4 | 140 | closedir(dir); |
universe@7 | 141 | destroy_settings_t(settings); |
universe@0 | 142 | |
universe@0 | 143 | // Print double line and line count |
universe@1 | 144 | #ifdef _WIN32 |
universe@3 | 145 | const int columns = 79; |
universe@1 | 146 | #else |
universe@3 | 147 | const int columns = 80; |
universe@1 | 148 | #endif /* _WIN32 */ |
universe@1 | 149 | |
universe@1 | 150 | for (int t = 0 ; t < columns ; t++) { |
universe@0 | 151 | printf("="); |
universe@0 | 152 | } |
universe@1 | 153 | #ifdef _WIN32 |
universe@3 | 154 | printf("\n%73d lines\n", lines); |
universe@1 | 155 | #else |
universe@3 | 156 | printf("\n%74d lines\n", lines); |
universe@1 | 157 | #endif /* _WIN32 */ |
universe@0 | 158 | |
universe@0 | 159 | return 0; |
universe@0 | 160 | } |