src/cline.c

changeset 66
be2084398c37
parent 62
7f5f9f43d0c0
     1.1 --- a/src/cline.c	Fri Jun 03 18:13:46 2022 +0200
     1.2 +++ b/src/cline.c	Fri Jun 03 20:05:15 2022 +0200
     1.3 @@ -39,6 +39,7 @@
     1.4      "\n\nOptions:"
     1.5      "\n  -b <level>          - binary file heuristics level (default medium)"
     1.6      "\n                        One of: ignore low medium high"
     1.7 +    "\n  -c                  - Count non-whitespace characters instead of lines"
     1.8      "\n  -E <pattern>        - Excludes any line matching the <pattern>"
     1.9      "\n  -e <start> <end>    - Excludes lines between <start> and <end>"
    1.10      "\n                        You may use these options multiple times"
    1.11 @@ -63,11 +64,11 @@
    1.12      "\nsource code in your working directory and its subdirectories, type:"
    1.13      "\n  cline -rs .c\n"
    1.14      "\nIf you want to exclude comment lines, you may use the -e/-E option."
    1.15 -    "\nAfter a line matches the regex pattern <start> any following line is"
    1.16 -    "\nnot counted unless a line matches the <end> pattern. A line is still "
    1.17 -    "\ncounted when it does not start or end with the respective patterns."
    1.18 -    "\nPlease note, that cline does not remove whitespace characters as this"
    1.19 -    "\nmight not be reasonable in some cases."
    1.20 +    "\nAfter a line matches the regex pattern <start>, this and any following"
    1.21 +    "\nline is not counted unless a line matches the <end> pattern. A line is"
    1.22 +    "\nstill counted when it does not start or end with the respective pattern."
    1.23 +    "\nPlease note, that cline does not trim the lines before matching against"
    1.24 +    "\nthe pattern."
    1.25      "\n\nExample (C without comments):"
    1.26      "\n  cline -s .c,.h --exclude-cstyle-comments"
    1.27      "\n");
    1.28 @@ -107,7 +108,7 @@
    1.29  
    1.30    for (int t = 1 ; t < argc ; t++) {
    1.31  
    1.32 -    int argflags = checkArgument(argv[t], "hsSrRmvVbeEi");
    1.33 +    int argflags = checkArgument(argv[t], "hsSrRmvVbeEic");
    1.34      int paropt = 0;
    1.35  
    1.36      /* h */
    1.37 @@ -201,12 +202,19 @@
    1.38      }
    1.39      /* i */
    1.40      if ((argflags & 2048) > 0) {
    1.41 -      // cannot be used together with -V
    1.42 +      /* cannot be used together with -V */
    1.43        if (registerArgument(&checked, 128)) {
    1.44          return exit_with_help(settings, 1);
    1.45        }
    1.46        settings->individual_sums = true;
    1.47      }
    1.48 +    if ((argflags & 4096) > 0) {
    1.49 +        if (registerArgument(&checked, 4096)) {
    1.50 +            return exit_with_help(settings, 1);
    1.51 +        }
    1.52 +        settings->count_chars = true;
    1.53 +        settings->regex->count_chars = true;
    1.54 +    }
    1.55      if (argflags == 0) {
    1.56        /* SHORTCUTS */
    1.57        if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) {
    1.58 @@ -235,15 +243,16 @@
    1.59      /* Don't waste memory when only the total sum is needed */
    1.60      string_list_t *output = settings->verbose ? new_string_list_t() : NULL;
    1.61      char *outbuf;
    1.62 +    const char* result_type = settings->count_chars ? "chars" : "lines";
    1.63      
    1.64 -    int total = 0;
    1.65 +    unsigned total = 0;
    1.66      if (directories->count == 0) {
    1.67          add_string(directories, ".");
    1.68      }
    1.69 -    for (int t = 0 ; t < directories->count ; t++) {
    1.70 +    for (unsigned t = 0 ; t < directories->count ; t++) {
    1.71        scanDirectory((scanner_t){directories->items[t], 0}, settings,
    1.72            output, result);
    1.73 -      total += result->lines;
    1.74 +      total += result->result;
    1.75        if (directories->count > 1 ) {
    1.76          outbuf = (char*) malloc(81);
    1.77          memset(outbuf, '-', 79);
    1.78 @@ -251,8 +260,8 @@
    1.79          outbuf[80] = 0;
    1.80          add_string(output, outbuf);
    1.81          outbuf = (char*) malloc(81);
    1.82 -        snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t],
    1.83 -                result->lines);
    1.84 +        snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t],
    1.85 +                result->result, result_type);
    1.86          add_string(output, outbuf);
    1.87          outbuf = (char*) malloc(81);
    1.88          memset(outbuf, '-', 79);
    1.89 @@ -272,32 +281,33 @@
    1.90        
    1.91        if (result->ext) {
    1.92          if (result->ext->count > 0) {
    1.93 -          for (int t = 0 ; t < 79 ; t++) {
    1.94 +          for (unsigned t = 0 ; t < 79 ; t++) {
    1.95              printf("=");
    1.96            }
    1.97            printf("\nIndividual sums:\n");
    1.98 -          for (int t = 0 ; t < result->ext->count ; t++) {
    1.99 -            printf(" %-62s%10d lines\n",
   1.100 -                    result->ext->extensions[t],
   1.101 -                    result->ext->lines[t]);
   1.102 +          for (unsigned t = 0 ; t < result->ext->count ; t++) {
   1.103 +            printf(" %-62s%10u %s\n",
   1.104 +                   result->ext->extensions[t],
   1.105 +                   result->ext->result[t],
   1.106 +                   result_type);
   1.107            }
   1.108          }
   1.109        }
   1.110        
   1.111 -      for (int t = 0 ; t < 79 ; t++) {
   1.112 +      for (unsigned t = 0 ; t < 79 ; t++) {
   1.113          printf("=");
   1.114        }
   1.115 -      printf("\n%73d lines\n", total);
   1.116 +      printf("\n%73d %s\n", total, result_type);
   1.117  
   1.118        if (settings->confusing_lnlen &&
   1.119            settings->regex->pattern_list->count > 0) {
   1.120  
   1.121          printf("\nSome files contain too long lines.\n"
   1.122 -          "The regex parser currently supports a maximum line length of %d."
   1.123 -          "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH);
   1.124 +          "The parser currently supports a maximum line length of %u."
   1.125 +          "\nThe result might be wrong.\n", MAX_LINELENGTH);
   1.126        }
   1.127      } else {
   1.128 -      printf("%d", total);
   1.129 +      printf("%u", total);
   1.130      }
   1.131      destroy_scanresult_t(result);
   1.132      destroy_string_list_t(output);

mercurial