cline.c

changeset 3
510d6b198dde
parent 1
34a5e235d16e
child 4
c3acfb3b4957
     1.1 --- a/cline.c	Mon May 23 16:54:56 2011 +0200
     1.2 +++ b/cline.c	Thu May 26 14:39:52 2011 +0200
     1.3 @@ -1,109 +1,17 @@
     1.4 -#include "include.h"
     1.5 -#include "v2.h"
     1.6 +#include "cline.h"
     1.7 +#include "functions.h"
     1.8  
     1.9 -
    1.10 +settings_t* new_settings_t() {
    1.11 +  settings_t *settings = malloc(sizeof(settings_t*));
    1.12  #ifdef _WIN32
    1.13 -static char fileSeparator = '\\';
    1.14 +  settings->fileSeparator      = '\\';
    1.15  #else
    1.16 -static char fileSeparator = '/';
    1.17 +  settings->fileSeparator      = '/';
    1.18  #endif /* _WIN32 */
    1.19 -
    1.20 -static int suffixc;
    1.21 -static char** suffixv;
    1.22 -static bool recursive;
    1.23 -static bool includeSuffixes;
    1.24 -static bool matchesOnly;
    1.25 -
    1.26 -bool testSuffix(char* filename) {
    1.27 -  bool ret = false;
    1.28 -  int tokenlen, fnamelen = strlen(filename);
    1.29 -  for (int t = 0 ; t < suffixc ; t++) {
    1.30 -    tokenlen = strlen(suffixv[t]);
    1.31 -    if (fnamelen >= tokenlen && tokenlen > 0) {
    1.32 -      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) {
    1.33 -        ret = true;
    1.34 -        break;
    1.35 -      }
    1.36 -    }
    1.37 -  }
    1.38 -  return ret ^ !includeSuffixes;
    1.39 -}
    1.40 -
    1.41 -int scanDirectory(DIR *dir, const int spaces, char* currdir) {
    1.42 -  DIR *subdir;
    1.43 -  char* subdirname;
    1.44 -  struct dirent *entry;
    1.45 -  int lines, digits, a;
    1.46 -  int lineSum = 0;
    1.47 -
    1.48 -  while ((entry = readdir(dir)) != NULL) {
    1.49 -    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    1.50 -      // Print occurence
    1.51 -      char entryname[strlen(entry->d_name)+spaces];
    1.52 -      for (int t = 0 ; t < spaces ; t++) {
    1.53 -        entryname[t]=' ';
    1.54 -      }
    1.55 -      entryname[spaces] = 0;
    1.56 -      strcat(entryname, entry->d_name);
    1.57 -  
    1.58 -      // Check for subdirectory
    1.59 -      char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
    1.60 -      strcpy(subdirname, currdir);
    1.61 -      strncat(subdirname, &fileSeparator, 1);
    1.62 -      strcat(subdirname, entry->d_name);
    1.63 -      if ((subdir = opendir(subdirname)) != NULL) {
    1.64 -        printf("%-60s\n", entryname);
    1.65 -        if (recursive) {
    1.66 -          lineSum += scanDirectory(subdir, spaces+1, subdirname);
    1.67 -        }
    1.68 -        closedir(subdir);
    1.69 -        continue;
    1.70 -      }
    1.71 -
    1.72 -      // Count lines
    1.73 -      lines = 0;
    1.74 -      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    1.75 -      strcpy(filename, currdir);
    1.76 -      strncat(filename, &fileSeparator, 1);
    1.77 -      strcat(filename, entry->d_name);
    1.78 -      if (testSuffix(filename)) {
    1.79 -        FILE *file = fopen(filename, "r");
    1.80 -        if (file == NULL) {
    1.81 -          perror("  File acces failed");
    1.82 -          continue;
    1.83 -        }
    1.84 -
    1.85 -        do {
    1.86 -          a = fgetc(file);
    1.87 -
    1.88 -          if (a == 10) {
    1.89 -            lines++;
    1.90 -          }
    1.91 -        } while (a != EOF);
    1.92 -        fclose(file);
    1.93 -
    1.94 -        // Print line count
    1.95 -        #ifdef _WIN32
    1.96 -          printf("%-60s%13d lines\n", entryname, lines);
    1.97 -        #else
    1.98 -          printf("%-60s%14d lines\n", entryname, lines);
    1.99 -        #endif /* _WIN32 */
   1.100 -
   1.101 -        lineSum += lines;
   1.102 -      }
   1.103 -      else {
   1.104 -        if (!matchesOnly) {
   1.105 -          // Print hint
   1.106 -          #ifdef _WIN32
   1.107 -            printf("%-60s%19s\n", entryname, "no match");
   1.108 -          #else
   1.109 -            printf("%-60s%20s\n", entryname, "no match");
   1.110 -          #endif /* _WIN32 */
   1.111 -        }
   1.112 -      }
   1.113 -    }
   1.114 -  }
   1.115 -  return lineSum;
   1.116 +  settings->suffixc            = 1;
   1.117 +  settings->recursive          = false;
   1.118 +  settings->includeSuffixes    = false;
   1.119 +  settings->matchesOnly        = false;
   1.120  }
   1.121  
   1.122  void printHelpText(const char* prgName) {
   1.123 @@ -129,18 +37,20 @@
   1.124      "\nC source code in your working directory and its subdirectories, type:"
   1.125      "\n  %s -rs .c\n";
   1.126      
   1.127 -    printf(helpText, prgName, prgName, prgName, prgName);
   1.128 +  printf(helpText, prgName, prgName, prgName, prgName);
   1.129  }
   1.130  
   1.131  int main(int argc, char** argv) {
   1.132  
   1.133 +  // Settings
   1.134 +  settings_t *settings = new_settings_t();
   1.135 +
   1.136    // Program name
   1.137 -  char* prgName = strrchr(argv[0], fileSeparator);
   1.138 +  char* prgName = strrchr(argv[0], settings->fileSeparator);
   1.139    
   1.140    if (prgName == NULL) {
   1.141      prgName = argv[0];
   1.142 -  }
   1.143 -  else {
   1.144 +  } else {
   1.145      prgName++;
   1.146    }
   1.147  
   1.148 @@ -148,15 +58,13 @@
   1.149    char* _suffix = " ";
   1.150    char _directory[3];
   1.151    _directory[0] = '.';
   1.152 -  _directory[1] = fileSeparator;
   1.153 +  _directory[1] = settings->fileSeparator;
   1.154    _directory[2] = 0;
   1.155  
   1.156    // Get arguments
   1.157    char* directory;
   1.158    char* suffix;
   1.159    bool showHelp = false;
   1.160 -  recursive = false;
   1.161 -  includeSuffixes = false;
   1.162    char checked = 0;
   1.163  
   1.164    for (int t = 1 ; t < argc ; t++) {
   1.165 @@ -169,7 +77,7 @@
   1.166          printHelpText(prgName);
   1.167          return -1;
   1.168        }
   1.169 -      includeSuffixes = true;
   1.170 +      settings->includeSuffixes = true;
   1.171        t++;
   1.172        if (t >= argc) {
   1.173          printHelpText(prgName);
   1.174 @@ -184,7 +92,7 @@
   1.175          printHelpText(prgName);
   1.176          return -1;
   1.177        }
   1.178 -      includeSuffixes = false;
   1.179 +      settings->includeSuffixes = false;
   1.180        t++;
   1.181        if (t >= argc) {
   1.182          printHelpText(prgName);
   1.183 @@ -209,7 +117,7 @@
   1.184          return -1;
   1.185        }
   1.186        checked |= 4;
   1.187 -      recursive = true;
   1.188 +      settings->recursive = true;
   1.189      }
   1.190      if ((argflags & 32) > 0) {
   1.191        if ((checked & 32) > 0) {
   1.192 @@ -217,7 +125,7 @@
   1.193          return -1;
   1.194        }
   1.195        checked |= 32;
   1.196 -      matchesOnly = true;
   1.197 +      settings->matchesOnly = true;
   1.198      }
   1.199      // other
   1.200      if (argflags == 0) {
   1.201 @@ -247,21 +155,20 @@
   1.202  
   1.203    // Find tokens
   1.204    char* finder;
   1.205 -  suffixc = 1;
   1.206    finder = strchr(suffix, ',');
   1.207    while (finder != NULL) {
   1.208 -    suffixc++;
   1.209 +    settings->suffixc++;
   1.210      finder = strchr(finder+1, ',');
   1.211    }
   1.212 -  suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
   1.213 -  if (suffixv == NULL) {
   1.214 +  settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
   1.215 +  if (settings->suffixv == NULL) {
   1.216      fprintf(stderr, "Memory allocation failed.\n");
   1.217      return 1;
   1.218    }
   1.219    finder = strtok(suffix, ",");
   1.220    int c = 0;
   1.221    while (finder != NULL) {
   1.222 -    suffixv[c] = finder;
   1.223 +    settings->suffixv[c] = finder;
   1.224      c++;
   1.225      finder = strtok(NULL, ",");
   1.226    }
   1.227 @@ -270,30 +177,32 @@
   1.228    DIR *dir = opendir(directory);
   1.229    if (dir == NULL) {
   1.230      perror("Operation failed");
   1.231 -    free(suffixv);
   1.232 +    free(settings->suffixv);
   1.233 +    free(settings);
   1.234      return 1;
   1.235    }
   1.236    
   1.237    // Scan directory
   1.238 -  int lines = scanDirectory(dir, 0, directory);
   1.239 +  int lines = scanDirectory(dir, 0, directory, settings);
   1.240  
   1.241    // Print double line and line count
   1.242    #ifdef _WIN32
   1.243 -     const int columns = 79;
   1.244 +    const int columns = 79;
   1.245    #else
   1.246 -     const int columns = 80;
   1.247 +    const int columns = 80;
   1.248    #endif /* _WIN32 */
   1.249  
   1.250    for (int t = 0 ; t < columns ; t++) {
   1.251      printf("=");
   1.252    }
   1.253    #ifdef _WIN32
   1.254 -     printf("\n%73d lines\n", lines);
   1.255 +    printf("\n%73d lines\n", lines);
   1.256    #else
   1.257 -     printf("\n%74d lines\n", lines);
   1.258 +    printf("\n%74d lines\n", lines);
   1.259    #endif /* _WIN32 */
   1.260  
   1.261    closedir(dir);
   1.262 -  free(suffixv);
   1.263 +  free(settings->suffixv);
   1.264 +  free(settings);
   1.265    return 0;
   1.266  }

mercurial