--- a/cline.c Mon May 23 16:54:56 2011 +0200 +++ b/cline.c Thu May 26 14:39:52 2011 +0200 @@ -1,109 +1,17 @@ -#include "include.h" -#include "v2.h" - - -#ifdef _WIN32 -static char fileSeparator = '\\'; -#else -static char fileSeparator = '/'; -#endif /* _WIN32 */ - -static int suffixc; -static char** suffixv; -static bool recursive; -static bool includeSuffixes; -static bool matchesOnly; - -bool testSuffix(char* filename) { - bool ret = false; - int tokenlen, fnamelen = strlen(filename); - for (int t = 0 ; t < suffixc ; t++) { - tokenlen = strlen(suffixv[t]); - if (fnamelen >= tokenlen && tokenlen > 0) { - if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) { - ret = true; - break; - } - } - } - return ret ^ !includeSuffixes; -} - -int scanDirectory(DIR *dir, const int spaces, char* currdir) { - DIR *subdir; - char* subdirname; - struct dirent *entry; - int lines, digits, a; - int lineSum = 0; +#include "cline.h" +#include "functions.h" - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - // Print occurence - char entryname[strlen(entry->d_name)+spaces]; - for (int t = 0 ; t < spaces ; t++) { - entryname[t]=' '; - } - entryname[spaces] = 0; - strcat(entryname, entry->d_name); - - // Check for subdirectory - char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; - strcpy(subdirname, currdir); - strncat(subdirname, &fileSeparator, 1); - strcat(subdirname, entry->d_name); - if ((subdir = opendir(subdirname)) != NULL) { - printf("%-60s\n", entryname); - if (recursive) { - lineSum += scanDirectory(subdir, spaces+1, subdirname); - } - closedir(subdir); - continue; - } - - // Count lines - lines = 0; - char filename[(1+strlen(currdir)+strlen(entry->d_name))]; - strcpy(filename, currdir); - strncat(filename, &fileSeparator, 1); - strcat(filename, entry->d_name); - if (testSuffix(filename)) { - FILE *file = fopen(filename, "r"); - if (file == NULL) { - perror(" File acces failed"); - continue; - } - - do { - a = fgetc(file); - - if (a == 10) { - lines++; - } - } while (a != EOF); - fclose(file); - - // Print line count - #ifdef _WIN32 - printf("%-60s%13d lines\n", entryname, lines); - #else - printf("%-60s%14d lines\n", entryname, lines); - #endif /* _WIN32 */ - - lineSum += lines; - } - else { - if (!matchesOnly) { - // Print hint - #ifdef _WIN32 - printf("%-60s%19s\n", entryname, "no match"); - #else - printf("%-60s%20s\n", entryname, "no match"); - #endif /* _WIN32 */ - } - } - } - } - return lineSum; +settings_t* new_settings_t() { + settings_t *settings = malloc(sizeof(settings_t*)); +#ifdef _WIN32 + settings->fileSeparator = '\\'; +#else + settings->fileSeparator = '/'; +#endif /* _WIN32 */ + settings->suffixc = 1; + settings->recursive = false; + settings->includeSuffixes = false; + settings->matchesOnly = false; } void printHelpText(const char* prgName) { @@ -129,18 +37,20 @@ "\nC source code in your working directory and its subdirectories, type:" "\n %s -rs .c\n"; - printf(helpText, prgName, prgName, prgName, prgName); + printf(helpText, prgName, prgName, prgName, prgName); } int main(int argc, char** argv) { + // Settings + settings_t *settings = new_settings_t(); + // Program name - char* prgName = strrchr(argv[0], fileSeparator); + char* prgName = strrchr(argv[0], settings->fileSeparator); if (prgName == NULL) { prgName = argv[0]; - } - else { + } else { prgName++; } @@ -148,15 +58,13 @@ char* _suffix = " "; char _directory[3]; _directory[0] = '.'; - _directory[1] = fileSeparator; + _directory[1] = settings->fileSeparator; _directory[2] = 0; // Get arguments char* directory; char* suffix; bool showHelp = false; - recursive = false; - includeSuffixes = false; char checked = 0; for (int t = 1 ; t < argc ; t++) { @@ -169,7 +77,7 @@ printHelpText(prgName); return -1; } - includeSuffixes = true; + settings->includeSuffixes = true; t++; if (t >= argc) { printHelpText(prgName); @@ -184,7 +92,7 @@ printHelpText(prgName); return -1; } - includeSuffixes = false; + settings->includeSuffixes = false; t++; if (t >= argc) { printHelpText(prgName); @@ -209,7 +117,7 @@ return -1; } checked |= 4; - recursive = true; + settings->recursive = true; } if ((argflags & 32) > 0) { if ((checked & 32) > 0) { @@ -217,7 +125,7 @@ return -1; } checked |= 32; - matchesOnly = true; + settings->matchesOnly = true; } // other if (argflags == 0) { @@ -247,21 +155,20 @@ // Find tokens char* finder; - suffixc = 1; finder = strchr(suffix, ','); while (finder != NULL) { - suffixc++; + settings->suffixc++; finder = strchr(finder+1, ','); } - suffixv = (char**) malloc(sizeof(suffixv)*suffixc); - if (suffixv == NULL) { + settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); + if (settings->suffixv == NULL) { fprintf(stderr, "Memory allocation failed.\n"); return 1; } finder = strtok(suffix, ","); int c = 0; while (finder != NULL) { - suffixv[c] = finder; + settings->suffixv[c] = finder; c++; finder = strtok(NULL, ","); } @@ -270,30 +177,32 @@ DIR *dir = opendir(directory); if (dir == NULL) { perror("Operation failed"); - free(suffixv); + free(settings->suffixv); + free(settings); return 1; } // Scan directory - int lines = scanDirectory(dir, 0, directory); + int lines = scanDirectory(dir, 0, directory, settings); // Print double line and line count #ifdef _WIN32 - const int columns = 79; + const int columns = 79; #else - const int columns = 80; + const int columns = 80; #endif /* _WIN32 */ for (int t = 0 ; t < columns ; t++) { printf("="); } #ifdef _WIN32 - printf("\n%73d lines\n", lines); + printf("\n%73d lines\n", lines); #else - printf("\n%74d lines\n", lines); + printf("\n%74d lines\n", lines); #endif /* _WIN32 */ closedir(dir); - free(suffixv); + free(settings->suffixv); + free(settings); return 0; }