cline.c

changeset 1
34a5e235d16e
parent 0
518bfd1cc1e8
child 3
510d6b198dde
     1.1 --- a/cline.c	Mon May 23 16:42:44 2011 +0200
     1.2 +++ b/cline.c	Mon May 23 16:43:13 2011 +0200
     1.3 @@ -1,25 +1,26 @@
     1.4 -#include <stdio.h>
     1.5 -#include <string.h>
     1.6 -#include <stdbool.h>
     1.7 -#include <stdlib.h>
     1.8 -#include <dirent.h>
     1.9 +#include "include.h"
    1.10 +#include "v2.h"
    1.11 +
    1.12 +
    1.13 +#ifdef _WIN32
    1.14 +static char fileSeparator = '\\';
    1.15 +#else
    1.16 +static char fileSeparator = '/';
    1.17 +#endif /* _WIN32 */
    1.18  
    1.19  static int suffixc;
    1.20  static char** suffixv;
    1.21  static bool recursive;
    1.22  static bool includeSuffixes;
    1.23 +static bool matchesOnly;
    1.24  
    1.25 -bool testSuffix(char* filename)
    1.26 -{
    1.27 +bool testSuffix(char* filename) {
    1.28    bool ret = false;
    1.29    int tokenlen, fnamelen = strlen(filename);
    1.30 -  for (int t = 0 ; t < suffixc ; t++)
    1.31 -  {
    1.32 +  for (int t = 0 ; t < suffixc ; t++) {
    1.33      tokenlen = strlen(suffixv[t]);
    1.34 -    if (fnamelen >= tokenlen)
    1.35 -    {
    1.36 -      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0)
    1.37 -      {
    1.38 +    if (fnamelen >= tokenlen && tokenlen > 0) {
    1.39 +      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) {
    1.40          ret = true;
    1.41          break;
    1.42        }
    1.43 @@ -28,38 +29,31 @@
    1.44    return ret ^ !includeSuffixes;
    1.45  }
    1.46  
    1.47 -int scanDirectory(DIR *dir, const int spaces, char* currdir)
    1.48 -{
    1.49 +int scanDirectory(DIR *dir, const int spaces, char* currdir) {
    1.50    DIR *subdir;
    1.51    char* subdirname;
    1.52    struct dirent *entry;
    1.53    int lines, digits, a;
    1.54    int lineSum = 0;
    1.55  
    1.56 -  while ((entry = readdir(dir)) != NULL)
    1.57 -  {
    1.58 -    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
    1.59 -    {
    1.60 +  while ((entry = readdir(dir)) != NULL) {
    1.61 +    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    1.62        // Print occurence
    1.63        char entryname[strlen(entry->d_name)+spaces];
    1.64 -      for (int t = 0 ; t < spaces ; t++)
    1.65 -      {
    1.66 +      for (int t = 0 ; t < spaces ; t++) {
    1.67          entryname[t]=' ';
    1.68        }
    1.69        entryname[spaces] = 0;
    1.70        strcat(entryname, entry->d_name);
    1.71 -      printf("%-60s", entryname);
    1.72    
    1.73        // Check for subdirectory
    1.74        char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
    1.75        strcpy(subdirname, currdir);
    1.76 -      strcat(subdirname, "/");
    1.77 +      strncat(subdirname, &fileSeparator, 1);
    1.78        strcat(subdirname, entry->d_name);
    1.79 -      if ((subdir = opendir(subdirname)) != NULL)
    1.80 -      {
    1.81 -        printf("\n");
    1.82 -        if (recursive)
    1.83 -        {
    1.84 +      if ((subdir = opendir(subdirname)) != NULL) {
    1.85 +        printf("%-60s\n", entryname);
    1.86 +        if (recursive) {
    1.87            lineSum += scanDirectory(subdir, spaces+1, subdirname);
    1.88          }
    1.89          closedir(subdir);
    1.90 @@ -70,169 +64,184 @@
    1.91        lines = 0;
    1.92        char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    1.93        strcpy(filename, currdir);
    1.94 -      strcat(filename, "/");
    1.95 +      strncat(filename, &fileSeparator, 1);
    1.96        strcat(filename, entry->d_name);
    1.97 -      if (testSuffix(filename))
    1.98 -      {
    1.99 +      if (testSuffix(filename)) {
   1.100          FILE *file = fopen(filename, "r");
   1.101 -        if (file == NULL)
   1.102 -        {
   1.103 +        if (file == NULL) {
   1.104            perror("  File acces failed");
   1.105            continue;
   1.106          }
   1.107  
   1.108 -        do
   1.109 -        {
   1.110 +        do {
   1.111            a = fgetc(file);
   1.112  
   1.113 -          if (a == 10)
   1.114 -          {
   1.115 +          if (a == 10) {
   1.116              lines++;
   1.117            }
   1.118          } while (a != EOF);
   1.119          fclose(file);
   1.120  
   1.121          // Print line count
   1.122 -        printf("%14d lines\n", lines);
   1.123 +        #ifdef _WIN32
   1.124 +          printf("%-60s%13d lines\n", entryname, lines);
   1.125 +        #else
   1.126 +          printf("%-60s%14d lines\n", entryname, lines);
   1.127 +        #endif /* _WIN32 */
   1.128  
   1.129          lineSum += lines;
   1.130        }
   1.131 -      else
   1.132 -      {
   1.133 -        // Print hint
   1.134 -        printf("%20s\n", "no match");
   1.135 +      else {
   1.136 +        if (!matchesOnly) {
   1.137 +          // Print hint
   1.138 +          #ifdef _WIN32
   1.139 +            printf("%-60s%19s\n", entryname, "no match");
   1.140 +          #else
   1.141 +            printf("%-60s%20s\n", entryname, "no match");
   1.142 +          #endif /* _WIN32 */
   1.143 +        }
   1.144        }
   1.145      }
   1.146    }
   1.147    return lineSum;
   1.148  }
   1.149  
   1.150 -int main(int argc, char** argv)
   1.151 -{
   1.152 +void printHelpText(const char* prgName) {
   1.153    // Help text
   1.154    const char* helpText = 
   1.155 -    "\nUsage:\n%s [-h|--help|(-s=<suffix>|-S=<suffix>)|(-r|-R)|<directory>]"
   1.156 +    "\nUsage:"
   1.157 +    "\n      %s [-hrm][-s suffix][<directory>]"
   1.158 +    "\n      %s [-hrm][-S suffix][<directory>]"
   1.159      "\n\nCounts the line terminator characters (\\n) within all"
   1.160      " files in the specified\ndirectory."
   1.161      "\n\nOptions:"
   1.162      "\n  -h, --help          - this help text"
   1.163 -    "\n  -s=<suffixes>       - only count files with these suffixes (separated"
   1.164 +    "\n  -m                  - print information about matching files only"
   1.165 +    "\n  -s <suffixes>       - only count files with these suffixes (separated"
   1.166      "\n                        by commas)"
   1.167 -    "\n  -S=<suffixes>       - count any file except those with these suffixes"
   1.168 +    "\n  -S <suffixes>       - count any file except those with these suffixes"
   1.169      "\n                        (separated by commas)"
   1.170 -    "\n  -R                  - excludes subdirectories"
   1.171 -    "\n  -r                  - includes subdirecotires"
   1.172 +    "\n  -r, -R              - includes subdirectories"
   1.173      "\n\n"
   1.174 -    "The default call without any options is:"
   1.175 -    "\n  %s -r ./ -S=\n"
   1.176 +    "The default call without any options is:"    
   1.177 +    "\n  %s ./\n"
   1.178      "That means each file in each subdirectory is counted. If you want to count"
   1.179      "\nC source code in your working directory and its subdirectories, type:"
   1.180 -    "\n  %s -s=.c\n";
   1.181 +    "\n  %s -rs .c\n";
   1.182 +    
   1.183 +    printf(helpText, prgName, prgName, prgName, prgName);
   1.184 +}
   1.185 +
   1.186 +int main(int argc, char** argv) {
   1.187  
   1.188    // Program name
   1.189 -  char* prgName = strrchr(argv[0], '/');
   1.190 -  if (prgName == NULL)
   1.191 -  {
   1.192 +  char* prgName = strrchr(argv[0], fileSeparator);
   1.193 +  
   1.194 +  if (prgName == NULL) {
   1.195      prgName = argv[0];
   1.196    }
   1.197 -  else
   1.198 -  {
   1.199 +  else {
   1.200      prgName++;
   1.201    }
   1.202  
   1.203    // Defaults
   1.204 -  char* _suffix = "";
   1.205 -  char* _directory = "./";
   1.206 +  char* _suffix = " ";
   1.207 +  char _directory[3];
   1.208 +  _directory[0] = '.';
   1.209 +  _directory[1] = fileSeparator;
   1.210 +  _directory[2] = 0;
   1.211  
   1.212    // Get arguments
   1.213    char* directory;
   1.214    char* suffix;
   1.215    bool showHelp = false;
   1.216 -  recursive = true;
   1.217 +  recursive = false;
   1.218    includeSuffixes = false;
   1.219    char checked = 0;
   1.220  
   1.221 -  for (int t = 1 ; t < argc ; t++)
   1.222 -  {
   1.223 -    if (strncmp(argv[t], "-s=", 3) == 0)
   1.224 -    {
   1.225 -      if ((checked & 1) > 0)
   1.226 -      {
   1.227 -        printf(helpText, prgName, prgName, prgName);
   1.228 +  for (int t = 1 ; t < argc ; t++) {
   1.229 +
   1.230 +    int argflags = checkArgument(argv[t], "hsSrRm");
   1.231 +
   1.232 +    // s
   1.233 +    if ((argflags & 2) > 0) {
   1.234 +      if ((checked & 1) > 0) {
   1.235 +        printHelpText(prgName);
   1.236          return -1;
   1.237        }
   1.238        includeSuffixes = true;
   1.239 -      suffix = argv[t]+3; 
   1.240 +      t++;
   1.241 +      if (t >= argc) {
   1.242 +        printHelpText(prgName);
   1.243 +        return -1;
   1.244 +      }
   1.245 +      suffix = argv[t]; 
   1.246        checked |= 1;
   1.247      }
   1.248 -    else if (strncmp(argv[t], "-S=", 3) == 0)
   1.249 -    {
   1.250 -      if ((checked & 1) > 0)
   1.251 -      {
   1.252 -        printf(helpText, prgName, prgName, prgName);
   1.253 +    // S
   1.254 +    if ((argflags & 4) > 0) {
   1.255 +      if ((checked & 1) > 0) {
   1.256 +        printHelpText(prgName);
   1.257          return -1;
   1.258        }
   1.259        includeSuffixes = false;
   1.260 -      suffix = argv[t]+3;
   1.261 +      t++;
   1.262 +      if (t >= argc) {
   1.263 +        printHelpText(prgName);
   1.264 +        return -1;
   1.265 +      }
   1.266 +      suffix = argv[t];
   1.267        checked |= 1;
   1.268      }
   1.269 -    else if (strcmp(argv[t], "-h") == 0 || strcmp(argv[t], "--help") == 0)
   1.270 -    {
   1.271 -      if ((checked & 2) > 0)
   1.272 -      {
   1.273 -        printf(helpText, prgName, prgName, prgName);
   1.274 +    // h
   1.275 +    if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
   1.276 +      if ((checked & 2) > 0) {
   1.277 +        printHelpText(prgName);
   1.278          return -1;
   1.279        }
   1.280        checked |= 2;
   1.281        showHelp = true;
   1.282      }
   1.283 -    else if (strcmp(argv[t], "-r") == 0)
   1.284 -    {
   1.285 -      if ((checked & 4) > 0)
   1.286 -      {
   1.287 -        printf(helpText, prgName, prgName, prgName);
   1.288 +    // r, R
   1.289 +    if ((argflags & 24) > 0) {
   1.290 +      if ((checked & 4) > 0) {
   1.291 +        printHelpText(prgName);
   1.292          return -1;
   1.293        }
   1.294        checked |= 4;
   1.295        recursive = true;
   1.296      }
   1.297 -    else if (strcmp(argv[t], "-R") == 0)
   1.298 -    {
   1.299 -      if ((checked & 4) > 0)
   1.300 -      {
   1.301 -        printf(helpText, prgName, prgName, prgName);
   1.302 +    if ((argflags & 32) > 0) {
   1.303 +      if ((checked & 32) > 0) {
   1.304 +        printHelpText(prgName);
   1.305          return -1;
   1.306        }
   1.307 -      checked |= 4;
   1.308 -      recursive = false;
   1.309 +      checked |= 32;
   1.310 +      matchesOnly = true;
   1.311      }
   1.312 -    else
   1.313 -    {
   1.314 -      if ((checked & 8) > 0)
   1.315 -      {
   1.316 -        printf(helpText, prgName, prgName, prgName);
   1.317 +    // other
   1.318 +    if (argflags == 0) {
   1.319 +      if ((checked & 8) > 0) {
   1.320 +        printHelpText(prgName);
   1.321          return -1;
   1.322        }
   1.323 -      checked |= 8;      
   1.324 +      checked |= 8;
   1.325        directory = argv[t];
   1.326      }
   1.327    }
   1.328  
   1.329    // Show help and quit
   1.330 -  if (showHelp)
   1.331 -  {
   1.332 -    printf(helpText, prgName, prgName, prgName);
   1.333 +  if (showHelp) {
   1.334 +    printHelpText(prgName);
   1.335      return 0;
   1.336    }
   1.337  
   1.338    // Default values
   1.339 -  if ((checked & 1) == 0)
   1.340 -  {
   1.341 +  if ((checked & 1) == 0) {
   1.342      suffix = _suffix;
   1.343    }
   1.344  
   1.345 -  if ((checked & 8) == 0)
   1.346 -  {
   1.347 +  if ((checked & 8) == 0) {
   1.348      directory = _directory;
   1.349    }
   1.350  
   1.351 @@ -240,21 +249,18 @@
   1.352    char* finder;
   1.353    suffixc = 1;
   1.354    finder = strchr(suffix, ',');
   1.355 -  while (finder != NULL)
   1.356 -  {
   1.357 +  while (finder != NULL) {
   1.358      suffixc++;
   1.359      finder = strchr(finder+1, ',');
   1.360    }
   1.361    suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
   1.362 -  if (suffixv == NULL)
   1.363 -  {
   1.364 +  if (suffixv == NULL) {
   1.365      fprintf(stderr, "Memory allocation failed.\n");
   1.366      return 1;
   1.367    }
   1.368    finder = strtok(suffix, ",");
   1.369    int c = 0;
   1.370 -  while (finder != NULL)
   1.371 -  {
   1.372 +  while (finder != NULL) {
   1.373      suffixv[c] = finder;
   1.374      c++;
   1.375      finder = strtok(NULL, ",");
   1.376 @@ -262,8 +268,7 @@
   1.377  
   1.378    // Open directory
   1.379    DIR *dir = opendir(directory);
   1.380 -  if (dir == NULL)
   1.381 -  {
   1.382 +  if (dir == NULL) {
   1.383      perror("Operation failed");
   1.384      free(suffixv);
   1.385      return 1;
   1.386 @@ -273,11 +278,20 @@
   1.387    int lines = scanDirectory(dir, 0, directory);
   1.388  
   1.389    // Print double line and line count
   1.390 -  for (int t = 0 ; t < 80 ; t++)
   1.391 -  {
   1.392 +  #ifdef _WIN32
   1.393 +     const int columns = 79;
   1.394 +  #else
   1.395 +     const int columns = 80;
   1.396 +  #endif /* _WIN32 */
   1.397 +
   1.398 +  for (int t = 0 ; t < columns ; t++) {
   1.399      printf("=");
   1.400    }
   1.401 -  printf("\n%74d lines\n", lines);
   1.402 +  #ifdef _WIN32
   1.403 +     printf("\n%73d lines\n", lines);
   1.404 +  #else
   1.405 +     printf("\n%74d lines\n", lines);
   1.406 +  #endif /* _WIN32 */
   1.407  
   1.408    closedir(dir);
   1.409    free(suffixv);

mercurial