cline version 2

Mon, 23 May 2011 16:43:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 May 2011 16:43:13 +0200
changeset 1
34a5e235d16e
parent 0
518bfd1cc1e8
child 2
29ac790c27ad

cline version 2

Makefile file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
cline.exe file | annotate | diff | comparison | revisions
cline.o file | annotate | diff | comparison | revisions
include.h file | annotate | diff | comparison | revisions
v2.c file | annotate | diff | comparison | revisions
v2.h file | annotate | diff | comparison | revisions
v2.o file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Mon May 23 16:42:44 2011 +0200
     1.2 +++ b/Makefile	Mon May 23 16:43:13 2011 +0200
     1.3 @@ -1,4 +1,8 @@
     1.4  CC = gcc
     1.5 +OBJ = cline.o v2.o
     1.6  
     1.7 -cline: cline.c
     1.8 -	${CC} -o cline -std=c99 cline.c
     1.9 +cline: ${OBJ}
    1.10 +	${CC} -o cline ${OBJ}
    1.11 +
    1.12 +%.o: %.c
    1.13 +	${CC} -c -std=c99 $<
     2.1 --- a/cline.c	Mon May 23 16:42:44 2011 +0200
     2.2 +++ b/cline.c	Mon May 23 16:43:13 2011 +0200
     2.3 @@ -1,25 +1,26 @@
     2.4 -#include <stdio.h>
     2.5 -#include <string.h>
     2.6 -#include <stdbool.h>
     2.7 -#include <stdlib.h>
     2.8 -#include <dirent.h>
     2.9 +#include "include.h"
    2.10 +#include "v2.h"
    2.11 +
    2.12 +
    2.13 +#ifdef _WIN32
    2.14 +static char fileSeparator = '\\';
    2.15 +#else
    2.16 +static char fileSeparator = '/';
    2.17 +#endif /* _WIN32 */
    2.18  
    2.19  static int suffixc;
    2.20  static char** suffixv;
    2.21  static bool recursive;
    2.22  static bool includeSuffixes;
    2.23 +static bool matchesOnly;
    2.24  
    2.25 -bool testSuffix(char* filename)
    2.26 -{
    2.27 +bool testSuffix(char* filename) {
    2.28    bool ret = false;
    2.29    int tokenlen, fnamelen = strlen(filename);
    2.30 -  for (int t = 0 ; t < suffixc ; t++)
    2.31 -  {
    2.32 +  for (int t = 0 ; t < suffixc ; t++) {
    2.33      tokenlen = strlen(suffixv[t]);
    2.34 -    if (fnamelen >= tokenlen)
    2.35 -    {
    2.36 -      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0)
    2.37 -      {
    2.38 +    if (fnamelen >= tokenlen && tokenlen > 0) {
    2.39 +      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) {
    2.40          ret = true;
    2.41          break;
    2.42        }
    2.43 @@ -28,38 +29,31 @@
    2.44    return ret ^ !includeSuffixes;
    2.45  }
    2.46  
    2.47 -int scanDirectory(DIR *dir, const int spaces, char* currdir)
    2.48 -{
    2.49 +int scanDirectory(DIR *dir, const int spaces, char* currdir) {
    2.50    DIR *subdir;
    2.51    char* subdirname;
    2.52    struct dirent *entry;
    2.53    int lines, digits, a;
    2.54    int lineSum = 0;
    2.55  
    2.56 -  while ((entry = readdir(dir)) != NULL)
    2.57 -  {
    2.58 -    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
    2.59 -    {
    2.60 +  while ((entry = readdir(dir)) != NULL) {
    2.61 +    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    2.62        // Print occurence
    2.63        char entryname[strlen(entry->d_name)+spaces];
    2.64 -      for (int t = 0 ; t < spaces ; t++)
    2.65 -      {
    2.66 +      for (int t = 0 ; t < spaces ; t++) {
    2.67          entryname[t]=' ';
    2.68        }
    2.69        entryname[spaces] = 0;
    2.70        strcat(entryname, entry->d_name);
    2.71 -      printf("%-60s", entryname);
    2.72    
    2.73        // Check for subdirectory
    2.74        char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
    2.75        strcpy(subdirname, currdir);
    2.76 -      strcat(subdirname, "/");
    2.77 +      strncat(subdirname, &fileSeparator, 1);
    2.78        strcat(subdirname, entry->d_name);
    2.79 -      if ((subdir = opendir(subdirname)) != NULL)
    2.80 -      {
    2.81 -        printf("\n");
    2.82 -        if (recursive)
    2.83 -        {
    2.84 +      if ((subdir = opendir(subdirname)) != NULL) {
    2.85 +        printf("%-60s\n", entryname);
    2.86 +        if (recursive) {
    2.87            lineSum += scanDirectory(subdir, spaces+1, subdirname);
    2.88          }
    2.89          closedir(subdir);
    2.90 @@ -70,169 +64,184 @@
    2.91        lines = 0;
    2.92        char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    2.93        strcpy(filename, currdir);
    2.94 -      strcat(filename, "/");
    2.95 +      strncat(filename, &fileSeparator, 1);
    2.96        strcat(filename, entry->d_name);
    2.97 -      if (testSuffix(filename))
    2.98 -      {
    2.99 +      if (testSuffix(filename)) {
   2.100          FILE *file = fopen(filename, "r");
   2.101 -        if (file == NULL)
   2.102 -        {
   2.103 +        if (file == NULL) {
   2.104            perror("  File acces failed");
   2.105            continue;
   2.106          }
   2.107  
   2.108 -        do
   2.109 -        {
   2.110 +        do {
   2.111            a = fgetc(file);
   2.112  
   2.113 -          if (a == 10)
   2.114 -          {
   2.115 +          if (a == 10) {
   2.116              lines++;
   2.117            }
   2.118          } while (a != EOF);
   2.119          fclose(file);
   2.120  
   2.121          // Print line count
   2.122 -        printf("%14d lines\n", lines);
   2.123 +        #ifdef _WIN32
   2.124 +          printf("%-60s%13d lines\n", entryname, lines);
   2.125 +        #else
   2.126 +          printf("%-60s%14d lines\n", entryname, lines);
   2.127 +        #endif /* _WIN32 */
   2.128  
   2.129          lineSum += lines;
   2.130        }
   2.131 -      else
   2.132 -      {
   2.133 -        // Print hint
   2.134 -        printf("%20s\n", "no match");
   2.135 +      else {
   2.136 +        if (!matchesOnly) {
   2.137 +          // Print hint
   2.138 +          #ifdef _WIN32
   2.139 +            printf("%-60s%19s\n", entryname, "no match");
   2.140 +          #else
   2.141 +            printf("%-60s%20s\n", entryname, "no match");
   2.142 +          #endif /* _WIN32 */
   2.143 +        }
   2.144        }
   2.145      }
   2.146    }
   2.147    return lineSum;
   2.148  }
   2.149  
   2.150 -int main(int argc, char** argv)
   2.151 -{
   2.152 +void printHelpText(const char* prgName) {
   2.153    // Help text
   2.154    const char* helpText = 
   2.155 -    "\nUsage:\n%s [-h|--help|(-s=<suffix>|-S=<suffix>)|(-r|-R)|<directory>]"
   2.156 +    "\nUsage:"
   2.157 +    "\n      %s [-hrm][-s suffix][<directory>]"
   2.158 +    "\n      %s [-hrm][-S suffix][<directory>]"
   2.159      "\n\nCounts the line terminator characters (\\n) within all"
   2.160      " files in the specified\ndirectory."
   2.161      "\n\nOptions:"
   2.162      "\n  -h, --help          - this help text"
   2.163 -    "\n  -s=<suffixes>       - only count files with these suffixes (separated"
   2.164 +    "\n  -m                  - print information about matching files only"
   2.165 +    "\n  -s <suffixes>       - only count files with these suffixes (separated"
   2.166      "\n                        by commas)"
   2.167 -    "\n  -S=<suffixes>       - count any file except those with these suffixes"
   2.168 +    "\n  -S <suffixes>       - count any file except those with these suffixes"
   2.169      "\n                        (separated by commas)"
   2.170 -    "\n  -R                  - excludes subdirectories"
   2.171 -    "\n  -r                  - includes subdirecotires"
   2.172 +    "\n  -r, -R              - includes subdirectories"
   2.173      "\n\n"
   2.174 -    "The default call without any options is:"
   2.175 -    "\n  %s -r ./ -S=\n"
   2.176 +    "The default call without any options is:"    
   2.177 +    "\n  %s ./\n"
   2.178      "That means each file in each subdirectory is counted. If you want to count"
   2.179      "\nC source code in your working directory and its subdirectories, type:"
   2.180 -    "\n  %s -s=.c\n";
   2.181 +    "\n  %s -rs .c\n";
   2.182 +    
   2.183 +    printf(helpText, prgName, prgName, prgName, prgName);
   2.184 +}
   2.185 +
   2.186 +int main(int argc, char** argv) {
   2.187  
   2.188    // Program name
   2.189 -  char* prgName = strrchr(argv[0], '/');
   2.190 -  if (prgName == NULL)
   2.191 -  {
   2.192 +  char* prgName = strrchr(argv[0], fileSeparator);
   2.193 +  
   2.194 +  if (prgName == NULL) {
   2.195      prgName = argv[0];
   2.196    }
   2.197 -  else
   2.198 -  {
   2.199 +  else {
   2.200      prgName++;
   2.201    }
   2.202  
   2.203    // Defaults
   2.204 -  char* _suffix = "";
   2.205 -  char* _directory = "./";
   2.206 +  char* _suffix = " ";
   2.207 +  char _directory[3];
   2.208 +  _directory[0] = '.';
   2.209 +  _directory[1] = fileSeparator;
   2.210 +  _directory[2] = 0;
   2.211  
   2.212    // Get arguments
   2.213    char* directory;
   2.214    char* suffix;
   2.215    bool showHelp = false;
   2.216 -  recursive = true;
   2.217 +  recursive = false;
   2.218    includeSuffixes = false;
   2.219    char checked = 0;
   2.220  
   2.221 -  for (int t = 1 ; t < argc ; t++)
   2.222 -  {
   2.223 -    if (strncmp(argv[t], "-s=", 3) == 0)
   2.224 -    {
   2.225 -      if ((checked & 1) > 0)
   2.226 -      {
   2.227 -        printf(helpText, prgName, prgName, prgName);
   2.228 +  for (int t = 1 ; t < argc ; t++) {
   2.229 +
   2.230 +    int argflags = checkArgument(argv[t], "hsSrRm");
   2.231 +
   2.232 +    // s
   2.233 +    if ((argflags & 2) > 0) {
   2.234 +      if ((checked & 1) > 0) {
   2.235 +        printHelpText(prgName);
   2.236          return -1;
   2.237        }
   2.238        includeSuffixes = true;
   2.239 -      suffix = argv[t]+3; 
   2.240 +      t++;
   2.241 +      if (t >= argc) {
   2.242 +        printHelpText(prgName);
   2.243 +        return -1;
   2.244 +      }
   2.245 +      suffix = argv[t]; 
   2.246        checked |= 1;
   2.247      }
   2.248 -    else if (strncmp(argv[t], "-S=", 3) == 0)
   2.249 -    {
   2.250 -      if ((checked & 1) > 0)
   2.251 -      {
   2.252 -        printf(helpText, prgName, prgName, prgName);
   2.253 +    // S
   2.254 +    if ((argflags & 4) > 0) {
   2.255 +      if ((checked & 1) > 0) {
   2.256 +        printHelpText(prgName);
   2.257          return -1;
   2.258        }
   2.259        includeSuffixes = false;
   2.260 -      suffix = argv[t]+3;
   2.261 +      t++;
   2.262 +      if (t >= argc) {
   2.263 +        printHelpText(prgName);
   2.264 +        return -1;
   2.265 +      }
   2.266 +      suffix = argv[t];
   2.267        checked |= 1;
   2.268      }
   2.269 -    else if (strcmp(argv[t], "-h") == 0 || strcmp(argv[t], "--help") == 0)
   2.270 -    {
   2.271 -      if ((checked & 2) > 0)
   2.272 -      {
   2.273 -        printf(helpText, prgName, prgName, prgName);
   2.274 +    // h
   2.275 +    if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
   2.276 +      if ((checked & 2) > 0) {
   2.277 +        printHelpText(prgName);
   2.278          return -1;
   2.279        }
   2.280        checked |= 2;
   2.281        showHelp = true;
   2.282      }
   2.283 -    else if (strcmp(argv[t], "-r") == 0)
   2.284 -    {
   2.285 -      if ((checked & 4) > 0)
   2.286 -      {
   2.287 -        printf(helpText, prgName, prgName, prgName);
   2.288 +    // r, R
   2.289 +    if ((argflags & 24) > 0) {
   2.290 +      if ((checked & 4) > 0) {
   2.291 +        printHelpText(prgName);
   2.292          return -1;
   2.293        }
   2.294        checked |= 4;
   2.295        recursive = true;
   2.296      }
   2.297 -    else if (strcmp(argv[t], "-R") == 0)
   2.298 -    {
   2.299 -      if ((checked & 4) > 0)
   2.300 -      {
   2.301 -        printf(helpText, prgName, prgName, prgName);
   2.302 +    if ((argflags & 32) > 0) {
   2.303 +      if ((checked & 32) > 0) {
   2.304 +        printHelpText(prgName);
   2.305          return -1;
   2.306        }
   2.307 -      checked |= 4;
   2.308 -      recursive = false;
   2.309 +      checked |= 32;
   2.310 +      matchesOnly = true;
   2.311      }
   2.312 -    else
   2.313 -    {
   2.314 -      if ((checked & 8) > 0)
   2.315 -      {
   2.316 -        printf(helpText, prgName, prgName, prgName);
   2.317 +    // other
   2.318 +    if (argflags == 0) {
   2.319 +      if ((checked & 8) > 0) {
   2.320 +        printHelpText(prgName);
   2.321          return -1;
   2.322        }
   2.323 -      checked |= 8;      
   2.324 +      checked |= 8;
   2.325        directory = argv[t];
   2.326      }
   2.327    }
   2.328  
   2.329    // Show help and quit
   2.330 -  if (showHelp)
   2.331 -  {
   2.332 -    printf(helpText, prgName, prgName, prgName);
   2.333 +  if (showHelp) {
   2.334 +    printHelpText(prgName);
   2.335      return 0;
   2.336    }
   2.337  
   2.338    // Default values
   2.339 -  if ((checked & 1) == 0)
   2.340 -  {
   2.341 +  if ((checked & 1) == 0) {
   2.342      suffix = _suffix;
   2.343    }
   2.344  
   2.345 -  if ((checked & 8) == 0)
   2.346 -  {
   2.347 +  if ((checked & 8) == 0) {
   2.348      directory = _directory;
   2.349    }
   2.350  
   2.351 @@ -240,21 +249,18 @@
   2.352    char* finder;
   2.353    suffixc = 1;
   2.354    finder = strchr(suffix, ',');
   2.355 -  while (finder != NULL)
   2.356 -  {
   2.357 +  while (finder != NULL) {
   2.358      suffixc++;
   2.359      finder = strchr(finder+1, ',');
   2.360    }
   2.361    suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
   2.362 -  if (suffixv == NULL)
   2.363 -  {
   2.364 +  if (suffixv == NULL) {
   2.365      fprintf(stderr, "Memory allocation failed.\n");
   2.366      return 1;
   2.367    }
   2.368    finder = strtok(suffix, ",");
   2.369    int c = 0;
   2.370 -  while (finder != NULL)
   2.371 -  {
   2.372 +  while (finder != NULL) {
   2.373      suffixv[c] = finder;
   2.374      c++;
   2.375      finder = strtok(NULL, ",");
   2.376 @@ -262,8 +268,7 @@
   2.377  
   2.378    // Open directory
   2.379    DIR *dir = opendir(directory);
   2.380 -  if (dir == NULL)
   2.381 -  {
   2.382 +  if (dir == NULL) {
   2.383      perror("Operation failed");
   2.384      free(suffixv);
   2.385      return 1;
   2.386 @@ -273,11 +278,20 @@
   2.387    int lines = scanDirectory(dir, 0, directory);
   2.388  
   2.389    // Print double line and line count
   2.390 -  for (int t = 0 ; t < 80 ; t++)
   2.391 -  {
   2.392 +  #ifdef _WIN32
   2.393 +     const int columns = 79;
   2.394 +  #else
   2.395 +     const int columns = 80;
   2.396 +  #endif /* _WIN32 */
   2.397 +
   2.398 +  for (int t = 0 ; t < columns ; t++) {
   2.399      printf("=");
   2.400    }
   2.401 -  printf("\n%74d lines\n", lines);
   2.402 +  #ifdef _WIN32
   2.403 +     printf("\n%73d lines\n", lines);
   2.404 +  #else
   2.405 +     printf("\n%74d lines\n", lines);
   2.406 +  #endif /* _WIN32 */
   2.407  
   2.408    closedir(dir);
   2.409    free(suffixv);
     3.1 Binary file cline.exe has changed
     4.1 Binary file cline.o has changed
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/include.h	Mon May 23 16:43:13 2011 +0200
     5.3 @@ -0,0 +1,10 @@
     5.4 +#ifndef INCLUDE_H
     5.5 +#define INCLUDE_H
     5.6 +
     5.7 +#include <stdio.h>
     5.8 +#include <string.h>
     5.9 +#include <stdbool.h>
    5.10 +#include <stdlib.h>
    5.11 +#include <dirent.h>
    5.12 +
    5.13 +#endif /* INCLUDE_H */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/v2.c	Mon May 23 16:43:13 2011 +0200
     6.3 @@ -0,0 +1,17 @@
     6.4 +#include "v2.h"
     6.5 +#include "include.h"
     6.6 +
     6.7 +int checkArgument(const char* arg, const char* expected) {
     6.8 +  int len = strlen(expected);
     6.9 +  int ret = 0;
    6.10 +
    6.11 +  if (arg[0] == '-') {
    6.12 +    if (arg[1] != '-') {
    6.13 +      for (int t = 0 ; t < len ; t++) {
    6.14 +        ret |= (strchr(arg, expected[t]) > 0) << t;
    6.15 +      }
    6.16 +    }
    6.17 +  }  
    6.18 +
    6.19 +  return ret;
    6.20 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/v2.h	Mon May 23 16:43:13 2011 +0200
     7.3 @@ -0,0 +1,6 @@
     7.4 +#ifndef V2_H
     7.5 +#define V2_H
     7.6 +
     7.7 +int checkArgument(const char*, const char*);
     7.8 +
     7.9 +#endif /* V2_H */
     8.1 Binary file v2.o has changed

mercurial