cline.c

Thu, 26 Jan 2012 15:55:52 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 Jan 2012 15:55:52 +0100
changeset 27
95a958e3de88
parent 26
853a1181884b
child 28
72a98cbcb9f1
permissions
-rw-r--r--

added regexp_parser struct and compile function

universe@10 1 /*
universe@10 2 * cline.c
universe@10 3 *
universe@10 4 * Created on: 23.05.2011
universe@20 5 * Author: Mike
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@12 11 #include "arguments.h"
universe@16 12 #include "stream.h"
universe@27 13 #include "regex_parser.h"
universe@0 14
universe@12 15 void printHelpText() {
universe@0 16 const char* helpText =
universe@1 17 "\nUsage:"
universe@27 18 "\n cline [Options] [Directory]"
universe@27 19 "\n cline [Options] [Directory]"
universe@0 20 "\n\nCounts the line terminator characters (\\n) within all"
universe@0 21 " files in the specified\ndirectory."
universe@0 22 "\n\nOptions:"
universe@21 23 "\n -b <level> - binary file heuristics level (default medium)"
universe@21 24 "\n One of: ignore low medium high"
universe@27 25 "\n -e <start> <end> - Excludes lines between <start> and <end>"
universe@27 26 "\n You may use this option multiple times"
universe@0 27 "\n -h, --help - this help text"
universe@1 28 "\n -m - print information about matching files only"
universe@1 29 "\n -s <suffixes> - only count files with these suffixes (separated"
universe@0 30 "\n by commas)"
universe@1 31 "\n -S <suffixes> - count any file except those with these suffixes"
universe@0 32 "\n (separated by commas)"
universe@1 33 "\n -r, -R - includes subdirectories"
universe@14 34 "\n -v, --version - print out version information"
universe@16 35 "\n -V - turn verbose output off, print the result only"
universe@0 36 "\n\n"
universe@1 37 "The default call without any options is:"
universe@12 38 "\n cline ./\n"
universe@7 39 "So each file in the working directory is counted. If you want to count C"
universe@7 40 "\nsource code in your working directory and its subdirectories, type:"
universe@27 41 "\n cline -rs .c\n"
universe@27 42 "\nIf you want to exclude comment lines, you may use the -e option."
universe@27 43 "\nAfter a line matches the regex pattern <start> any following line is"
universe@27 44 "\nnot counted unless a line matches the <end> pattern.";
universe@1 45
universe@12 46 printf(helpText);
universe@1 47 }
universe@1 48
universe@14 49 int exit_with_version(settings_t* settings) {
universe@16 50 printf("cline - Revision: %s", VERSION);
universe@14 51 destroy_settings_t(settings);
universe@14 52 return 0;
universe@14 53 }
universe@14 54
universe@12 55 int exit_with_help(settings_t* settings, int code) {
universe@12 56 printHelpText();
universe@8 57 destroy_settings_t(settings);
universe@8 58 return code;
universe@8 59 }
universe@8 60
universe@1 61 int main(int argc, char** argv) {
universe@0 62
universe@22 63 /* Settings */
universe@3 64 settings_t *settings = new_settings_t();
universe@5 65 if (settings == NULL) {
universe@5 66 fprintf(stderr, "Memory allocation failed.\n");
universe@5 67 return 1;
universe@5 68 }
universe@3 69
universe@22 70 /* Get arguments */
universe@7 71 char* directory = "./";
universe@7 72 char* suffix = " ";
universe@8 73 int checked = 0;
universe@0 74
universe@1 75 for (int t = 1 ; t < argc ; t++) {
universe@1 76
universe@27 77 int argflags = checkArgument(argv[t], "hsSrRmvVbe");
universe@1 78
universe@22 79 /* s, S */
universe@14 80 if ((argflags & 6) > 0) {
universe@8 81 if (registerArgument(&checked, 6)) {
universe@12 82 return exit_with_help(settings, 1);
universe@0 83 }
universe@14 84 settings->includeSuffixes = (argflags & 2) > 0;
universe@1 85 t++;
universe@1 86 if (t >= argc) {
universe@12 87 return exit_with_help(settings, 1);
universe@1 88 }
universe@1 89 suffix = argv[t];
universe@0 90 }
universe@22 91 /* h */
universe@1 92 if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
universe@14 93 return exit_with_help(settings, 0);
universe@0 94 }
universe@22 95 /* r, R */
universe@1 96 if ((argflags & 24) > 0) {
universe@8 97 if (registerArgument(&checked, 24)) {
universe@12 98 return exit_with_help(settings, 1);
universe@0 99 }
universe@3 100 settings->recursive = true;
universe@0 101 }
universe@22 102 /* m */
universe@1 103 if ((argflags & 32) > 0) {
universe@8 104 if (registerArgument(&checked, 32)) {
universe@12 105 return exit_with_help(settings, 1);
universe@0 106 }
universe@3 107 settings->matchesOnly = true;
universe@0 108 }
universe@22 109 /* v */
universe@14 110 if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
universe@14 111 return exit_with_version(settings);
universe@14 112 }
universe@22 113 /* V */
universe@16 114 if ((argflags & 128) > 0) {
universe@16 115 if (registerArgument(&checked, 128)) {
universe@16 116 return exit_with_help(settings, 1);
universe@16 117 }
universe@16 118 settings->verbose = false;
universe@16 119 }
universe@22 120 /* b */
universe@21 121 if ((argflags & 256) > 0) {
universe@21 122 if (registerArgument(&checked, 256)) {
universe@21 123 return exit_with_help(settings, 1);
universe@21 124 }
universe@21 125 t++;
universe@21 126 if (t >= argc) {
universe@21 127 return exit_with_help(settings, 1);
universe@21 128 }
universe@24 129 if (strcasecmp(argv[t], "ignore") == 0) {
universe@21 130 settings->bfileHeuristics->level = BFILE_IGNORE;
universe@24 131 } else if (strcasecmp(argv[t], "low") == 0) {
universe@21 132 settings->bfileHeuristics->level = BFILE_LOW_ACCURACY;
universe@24 133 } else if (strcasecmp(argv[t], "medium") == 0) {
universe@21 134 settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY;
universe@24 135 } else if (strcasecmp(argv[t], "high") == 0) {
universe@21 136 settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY;
universe@21 137 } else {
universe@21 138 return exit_with_help(settings, 1);
universe@21 139 }
universe@21 140 }
universe@27 141 if ((argflags & 512) > 0) {
universe@27 142 if (t + 2 >= argc) {
universe@27 143 return exit_with_help(settings, 1);
universe@27 144 }
universe@27 145 t++; add_string(settings->regex->pattern_list, argv[t]);
universe@27 146 t++; add_string(settings->regex->pattern_list, argv[t]);
universe@27 147 }
universe@22 148 /* Path */
universe@1 149 if (argflags == 0) {
universe@8 150 if (registerArgument(&checked, 1024)) {
universe@12 151 return exit_with_help(settings, 1);
universe@0 152 }
universe@0 153 directory = argv[t];
universe@0 154 }
universe@0 155 }
universe@0 156
universe@22 157 /* Configure output */
universe@16 158 if (!settings->verbose) {
universe@16 159 close_stdout();
universe@16 160 }
universe@16 161
universe@22 162 /* Find tokens */
universe@6 163 char* finder = strtok(suffix, ",");
universe@1 164 while (finder != NULL) {
universe@19 165 add_string(settings->suffixList, finder);
universe@0 166 finder = strtok(NULL, ",");
universe@0 167 }
universe@0 168
universe@22 169 /* Scan directory */
universe@27 170 regex_compile_all(settings->regex);
universe@23 171 int lines = scanDirectory((scanner_t){directory, 0}, settings);
universe@7 172 destroy_settings_t(settings);
universe@0 173
universe@22 174 /* Print double line and line count */
universe@21 175 for (int t = 0 ; t < 79 ; t++) {
universe@0 176 printf("=");
universe@0 177 }
universe@21 178 printf("\n%73d lines\n", lines);
universe@0 179
universe@27 180 if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) {
universe@25 181 printf("\nSome files contain too long lines.\n"
universe@27 182 "The regex parser currently supports a maximum line length of %d."
universe@27 183 "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
universe@25 184 }
universe@25 185
universe@16 186 if (!settings->verbose) {
universe@16 187 reopen_stdout();
universe@16 188 printf("%d", lines);
universe@16 189 }
universe@16 190
universe@16 191 fflush(stdout);
universe@0 192 return 0;
universe@0 193 }

mercurial