Mon, 23 May 2011 16:54:56 +0200
removed binary files from repository and added hgignore
#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; 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; } void printHelpText(const char* prgName) { // Help text const char* helpText = "\nUsage:" "\n %s [-hrm][-s suffix][<directory>]" "\n %s [-hrm][-S suffix][<directory>]" "\n\nCounts the line terminator characters (\\n) within all" " files in the specified\ndirectory." "\n\nOptions:" "\n -h, --help - this help text" "\n -m - print information about matching files only" "\n -s <suffixes> - only count files with these suffixes (separated" "\n by commas)" "\n -S <suffixes> - count any file except those with these suffixes" "\n (separated by commas)" "\n -r, -R - includes subdirectories" "\n\n" "The default call without any options is:" "\n %s ./\n" "That means each file in each subdirectory is counted. If you want to count" "\nC source code in your working directory and its subdirectories, type:" "\n %s -rs .c\n"; printf(helpText, prgName, prgName, prgName, prgName); } int main(int argc, char** argv) { // Program name char* prgName = strrchr(argv[0], fileSeparator); if (prgName == NULL) { prgName = argv[0]; } else { prgName++; } // Defaults char* _suffix = " "; char _directory[3]; _directory[0] = '.'; _directory[1] = 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++) { int argflags = checkArgument(argv[t], "hsSrRm"); // s if ((argflags & 2) > 0) { if ((checked & 1) > 0) { printHelpText(prgName); return -1; } includeSuffixes = true; t++; if (t >= argc) { printHelpText(prgName); return -1; } suffix = argv[t]; checked |= 1; } // S if ((argflags & 4) > 0) { if ((checked & 1) > 0) { printHelpText(prgName); return -1; } includeSuffixes = false; t++; if (t >= argc) { printHelpText(prgName); return -1; } suffix = argv[t]; checked |= 1; } // h if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { if ((checked & 2) > 0) { printHelpText(prgName); return -1; } checked |= 2; showHelp = true; } // r, R if ((argflags & 24) > 0) { if ((checked & 4) > 0) { printHelpText(prgName); return -1; } checked |= 4; recursive = true; } if ((argflags & 32) > 0) { if ((checked & 32) > 0) { printHelpText(prgName); return -1; } checked |= 32; matchesOnly = true; } // other if (argflags == 0) { if ((checked & 8) > 0) { printHelpText(prgName); return -1; } checked |= 8; directory = argv[t]; } } // Show help and quit if (showHelp) { printHelpText(prgName); return 0; } // Default values if ((checked & 1) == 0) { suffix = _suffix; } if ((checked & 8) == 0) { directory = _directory; } // Find tokens char* finder; suffixc = 1; finder = strchr(suffix, ','); while (finder != NULL) { suffixc++; finder = strchr(finder+1, ','); } suffixv = (char**) malloc(sizeof(suffixv)*suffixc); if (suffixv == NULL) { fprintf(stderr, "Memory allocation failed.\n"); return 1; } finder = strtok(suffix, ","); int c = 0; while (finder != NULL) { suffixv[c] = finder; c++; finder = strtok(NULL, ","); } // Open directory DIR *dir = opendir(directory); if (dir == NULL) { perror("Operation failed"); free(suffixv); return 1; } // Scan directory int lines = scanDirectory(dir, 0, directory); // Print double line and line count #ifdef _WIN32 const int columns = 79; #else const int columns = 80; #endif /* _WIN32 */ for (int t = 0 ; t < columns ; t++) { printf("="); } #ifdef _WIN32 printf("\n%73d lines\n", lines); #else printf("\n%74d lines\n", lines); #endif /* _WIN32 */ closedir(dir); free(suffixv); return 0; }