universe@10: /* universe@10: * cline.c universe@10: * universe@10: * Created on: 23.05.2011 universe@20: * Author: Mike universe@10: */ universe@10: universe@3: #include "cline.h" universe@10: #include "scanner.h" universe@10: #include "settings.h" universe@12: #include "arguments.h" universe@16: #include "stream.h" universe@0: universe@12: void printHelpText() { universe@0: const char* helpText = universe@1: "\nUsage:" universe@21: "\n cline [-hrmvV][-s suffix][-b level][]" universe@21: "\n cline [-hrmvV][-S suffix][-b level][]" universe@0: "\n\nCounts the line terminator characters (\\n) within all" universe@0: " files in the specified\ndirectory." universe@0: "\n\nOptions:" universe@21: "\n -b - binary file heuristics level (default medium)" universe@21: "\n One of: ignore low medium high" universe@0: "\n -h, --help - this help text" universe@1: "\n -m - print information about matching files only" universe@1: "\n -s - only count files with these suffixes (separated" universe@0: "\n by commas)" universe@1: "\n -S - count any file except those with these suffixes" universe@0: "\n (separated by commas)" universe@1: "\n -r, -R - includes subdirectories" universe@14: "\n -v, --version - print out version information" universe@16: "\n -V - turn verbose output off, print the result only" universe@0: "\n\n" universe@1: "The default call without any options is:" universe@12: "\n cline ./\n" universe@7: "So each file in the working directory is counted. If you want to count C" universe@7: "\nsource code in your working directory and its subdirectories, type:" universe@12: "\n cline -rs .c\n"; universe@1: universe@12: printf(helpText); universe@1: } universe@1: universe@14: int exit_with_version(settings_t* settings) { universe@16: printf("cline - Revision: %s", VERSION); universe@14: destroy_settings_t(settings); universe@14: return 0; universe@14: } universe@14: universe@12: int exit_with_help(settings_t* settings, int code) { universe@12: printHelpText(); universe@8: destroy_settings_t(settings); universe@8: return code; universe@8: } universe@8: universe@1: int main(int argc, char** argv) { universe@0: universe@3: // Settings universe@3: settings_t *settings = new_settings_t(); universe@5: if (settings == NULL) { universe@5: fprintf(stderr, "Memory allocation failed.\n"); universe@5: return 1; universe@5: } universe@3: universe@0: // Get arguments universe@7: char* directory = "./"; universe@7: char* suffix = " "; universe@8: int checked = 0; universe@0: universe@1: for (int t = 1 ; t < argc ; t++) { universe@1: universe@21: int argflags = checkArgument(argv[t], "hsSrRmvVb"); universe@1: universe@14: // s, S universe@14: if ((argflags & 6) > 0) { universe@8: if (registerArgument(&checked, 6)) { universe@12: return exit_with_help(settings, 1); universe@0: } universe@14: settings->includeSuffixes = (argflags & 2) > 0; universe@1: t++; universe@1: if (t >= argc) { universe@12: return exit_with_help(settings, 1); universe@1: } universe@1: suffix = argv[t]; universe@0: } universe@1: // h universe@1: if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { universe@14: return exit_with_help(settings, 0); universe@0: } universe@1: // r, R universe@1: if ((argflags & 24) > 0) { universe@8: if (registerArgument(&checked, 24)) { universe@12: return exit_with_help(settings, 1); universe@0: } universe@3: settings->recursive = true; universe@0: } universe@8: // m universe@1: if ((argflags & 32) > 0) { universe@8: if (registerArgument(&checked, 32)) { universe@12: return exit_with_help(settings, 1); universe@0: } universe@3: settings->matchesOnly = true; universe@0: } universe@14: // v universe@14: if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { universe@14: return exit_with_version(settings); universe@14: } universe@16: // V universe@16: if ((argflags & 128) > 0) { universe@16: if (registerArgument(&checked, 128)) { universe@16: return exit_with_help(settings, 1); universe@16: } universe@16: settings->verbose = false; universe@16: } universe@21: // b universe@21: if ((argflags & 256) > 0) { universe@21: if (registerArgument(&checked, 256)) { universe@21: return exit_with_help(settings, 1); universe@21: } universe@21: t++; universe@21: if (t >= argc) { universe@21: return exit_with_help(settings, 1); universe@21: } universe@21: if (stricmp(argv[t], "ignore") == 0) { universe@21: settings->bfileHeuristics->level = BFILE_IGNORE; universe@21: } else if (stricmp(argv[t], "low") == 0) { universe@21: settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; universe@21: } else if (stricmp(argv[t], "medium") == 0) { universe@21: settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; universe@21: } else if (stricmp(argv[t], "high") == 0) { universe@21: settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; universe@21: } else { universe@21: return exit_with_help(settings, 1); universe@21: } universe@21: } universe@6: // Path universe@1: if (argflags == 0) { universe@8: if (registerArgument(&checked, 1024)) { universe@12: return exit_with_help(settings, 1); universe@0: } universe@0: directory = argv[t]; universe@0: } universe@0: } universe@0: universe@16: // Configure output universe@16: if (!settings->verbose) { universe@16: close_stdout(); universe@16: } universe@16: universe@0: // Find tokens universe@6: char* finder = strtok(suffix, ","); universe@1: while (finder != NULL) { universe@19: add_string(settings->suffixList, finder); universe@0: finder = strtok(NULL, ","); universe@0: } universe@0: universe@0: // Open directory universe@0: DIR *dir = opendir(directory); universe@1: if (dir == NULL) { universe@0: perror("Operation failed"); universe@4: destroy_settings_t(settings); universe@0: return 1; universe@0: } universe@21: universe@0: // Scan directory universe@3: int lines = scanDirectory(dir, 0, directory, settings); universe@4: closedir(dir); universe@7: destroy_settings_t(settings); universe@0: universe@0: // Print double line and line count universe@21: for (int t = 0 ; t < 79 ; t++) { universe@0: printf("="); universe@0: } universe@21: printf("\n%73d lines\n", lines); universe@0: universe@16: if (!settings->verbose) { universe@16: reopen_stdout(); universe@16: printf("%d", lines); universe@16: } universe@16: universe@16: fflush(stdout); universe@0: return 0; universe@0: }