1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/functions.c Thu May 26 14:39:52 2011 +0200 1.3 @@ -0,0 +1,111 @@ 1.4 +#include "cline.h" 1.5 +#include "functions.h" 1.6 + 1.7 +int checkArgument(const char* arg, const char* expected) { 1.8 + int len = strlen(expected); 1.9 + int ret = 0; 1.10 + 1.11 + if (arg[0] == '-') { 1.12 + if (arg[1] != '-') { 1.13 + for (int t = 0 ; t < len ; t++) { 1.14 + ret |= (strchr(arg, expected[t]) > 0) << t; 1.15 + } 1.16 + } 1.17 + } 1.18 + 1.19 + return ret; 1.20 +} 1.21 + 1.22 +bool testSuffix(char* filename, settings_t* settings) { 1.23 + bool ret = false; 1.24 + int tokenlen, fnamelen = strlen(filename); 1.25 + for (int t = 0 ; t < settings->suffixc ; t++) { 1.26 + tokenlen = strlen(settings->suffixv[t]); 1.27 + if (fnamelen >= tokenlen && tokenlen > 0) { 1.28 + if (strncmp(filename+fnamelen-tokenlen, 1.29 + settings->suffixv[t], tokenlen) == 0) { 1.30 + ret = true; 1.31 + break; 1.32 + } 1.33 + } 1.34 + } 1.35 + return ret ^ !settings->includeSuffixes; 1.36 +} 1.37 + 1.38 +int scanDirectory(DIR *dir, const int spaces, 1.39 + char* currdir, settings_t* settings) { 1.40 + DIR *subdir; 1.41 + char* subdirname; 1.42 + struct dirent *entry; 1.43 + int lines, digits, a; 1.44 + int lineSum = 0; 1.45 + 1.46 + while ((entry = readdir(dir)) != NULL) { 1.47 + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 1.48 + // Print occurence 1.49 + char entryname[strlen(entry->d_name)+spaces]; 1.50 + for (int t = 0 ; t < spaces ; t++) { 1.51 + entryname[t]=' '; 1.52 + } 1.53 + entryname[spaces] = 0; 1.54 + strcat(entryname, entry->d_name); 1.55 + 1.56 + // Check for subdirectory 1.57 + char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; 1.58 + strcpy(subdirname, currdir); 1.59 + strncat(subdirname, &settings->fileSeparator, 1); 1.60 + strcat(subdirname, entry->d_name); 1.61 + if ((subdir = opendir(subdirname)) != NULL) { 1.62 + printf("%-60s\n", entryname); 1.63 + if (settings->recursive) { 1.64 + lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); 1.65 + } 1.66 + closedir(subdir); 1.67 + continue; 1.68 + } 1.69 + 1.70 + // Count lines 1.71 + lines = 0; 1.72 + char filename[(1+strlen(currdir)+strlen(entry->d_name))]; 1.73 + strcpy(filename, currdir); 1.74 + strncat(filename, &settings->fileSeparator, 1); 1.75 + strcat(filename, entry->d_name); 1.76 + if (testSuffix(filename, settings)) { 1.77 + FILE *file = fopen(filename, "r"); 1.78 + if (file == NULL) { 1.79 + perror(" File acces failed"); 1.80 + continue; 1.81 + } 1.82 + 1.83 + do { 1.84 + a = fgetc(file); 1.85 + 1.86 + if (a == 10) { 1.87 + lines++; 1.88 + } 1.89 + } while (a != EOF); 1.90 + fclose(file); 1.91 + 1.92 + // Print line count 1.93 + #ifdef _WIN32 1.94 + printf("%-60s%13d lines\n", entryname, lines); 1.95 + #else 1.96 + printf("%-60s%14d lines\n", entryname, lines); 1.97 + #endif /* _WIN32 */ 1.98 + 1.99 + lineSum += lines; 1.100 + } 1.101 + else { 1.102 + if (!settings->matchesOnly) { 1.103 + // Print hint 1.104 + #ifdef _WIN32 1.105 + printf("%-60s%19s\n", entryname, "no match"); 1.106 + #else 1.107 + printf("%-60s%20s\n", entryname, "no match"); 1.108 + #endif /* _WIN32 */ 1.109 + } 1.110 + } 1.111 + } 1.112 + } 1.113 + return lineSum; 1.114 +}