functions.c

Fri, 27 May 2011 14:45:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 27 May 2011 14:45:16 +0200
changeset 6
be923400164c
parent 3
510d6b198dde
child 8
28319b20968c
permissions
-rw-r--r--

encapsulated suffix list in type suffix_list_t

     1 #include "cline.h"
     2 #include "functions.h"
     4 int checkArgument(const char* arg, const char* expected) {
     5   int len = strlen(expected);
     6   int ret = 0;
     8   if (arg[0] == '-') {
     9     if (arg[1] != '-') {
    10       for (int t = 0 ; t < len ; t++) {
    11         ret |= (strchr(arg, expected[t]) > 0) << t;
    12       }
    13     }
    14   }  
    16   return ret;
    17 }
    19 bool testSuffix(char* filename, settings_t* settings) {
    20   bool ret = false;
    21   int tokenlen, fnamelen = strlen(filename);
    22   for (int t = 0 ; t < settings->suffixList->count ; t++) {
    23     tokenlen = strlen(settings->suffixList->items[t]);
    24     if (fnamelen >= tokenlen && tokenlen > 0) {
    25       if (strncmp(filename+fnamelen-tokenlen,
    26                   settings->suffixList->items[t], tokenlen) == 0) {
    27         ret = true;
    28         break;
    29       }
    30     }
    31   }
    32   return ret ^ !settings->includeSuffixes;
    33 }
    35 int scanDirectory(DIR *dir, const int spaces,
    36                   char* currdir, settings_t* settings) {
    37   DIR *subdir;
    38   char* subdirname;
    39   struct dirent *entry;
    40   int lines, digits, a;
    41   int lineSum = 0;
    43   while ((entry = readdir(dir)) != NULL) {
    44     if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
    45       // Print occurence
    46       char entryname[strlen(entry->d_name)+spaces];
    47       for (int t = 0 ; t < spaces ; t++) {
    48         entryname[t]=' ';
    49       }
    50       entryname[spaces] = 0;
    51       strcat(entryname, entry->d_name);
    53       // Check for subdirectory
    54       char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
    55       strcpy(subdirname, currdir);
    56       strncat(subdirname, &settings->fileSeparator, 1);
    57       strcat(subdirname, entry->d_name);
    58       if ((subdir = opendir(subdirname)) != NULL) {
    59         printf("%-60s\n", entryname);
    60         if (settings->recursive) {
    61           lineSum += scanDirectory(subdir, spaces+1, subdirname, settings);
    62         }
    63         closedir(subdir);
    64         continue;
    65       }
    67       // Count lines
    68       lines = 0;
    69       char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    70       strcpy(filename, currdir);
    71       strncat(filename, &settings->fileSeparator, 1);
    72       strcat(filename, entry->d_name);
    73       if (testSuffix(filename, settings)) {
    74         FILE *file = fopen(filename, "r");
    75         if (file == NULL) {
    76           perror("  File acces failed");
    77           continue;
    78         }
    80         do {
    81           a = fgetc(file);
    83           if (a == 10) {
    84             lines++;
    85           }
    86         } while (a != EOF);
    87         fclose(file);
    89         // Print line count
    90         #ifdef _WIN32
    91           printf("%-60s%13d lines\n", entryname, lines);
    92         #else
    93           printf("%-60s%14d lines\n", entryname, lines);
    94         #endif /* _WIN32 */
    96         lineSum += lines;
    97       }
    98       else {
    99         if (!settings->matchesOnly) {
   100           // Print hint
   101           #ifdef _WIN32
   102             printf("%-60s%19s\n", entryname, "no match");
   103           #else
   104             printf("%-60s%20s\n", entryname, "no match");
   105           #endif /* _WIN32 */
   106         }
   107       }
   108     }
   109   }
   110   return lineSum;
   111 }

mercurial