Thu, 26 May 2011 14:39:52 +0200
Moved some functions to functions.c
Replaced static variables by settings_t type
#include "cline.h" #include "functions.h" 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) { // 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) { // Settings settings_t *settings = new_settings_t(); // Program name char* prgName = strrchr(argv[0], settings->fileSeparator); if (prgName == NULL) { prgName = argv[0]; } else { prgName++; } // Defaults char* _suffix = " "; char _directory[3]; _directory[0] = '.'; _directory[1] = settings->fileSeparator; _directory[2] = 0; // Get arguments char* directory; char* suffix; bool showHelp = 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; } settings->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; } settings->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; settings->recursive = true; } if ((argflags & 32) > 0) { if ((checked & 32) > 0) { printHelpText(prgName); return -1; } checked |= 32; settings->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; finder = strchr(suffix, ','); while (finder != NULL) { settings->suffixc++; finder = strchr(finder+1, ','); } 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) { settings->suffixv[c] = finder; c++; finder = strtok(NULL, ","); } // Open directory DIR *dir = opendir(directory); if (dir == NULL) { perror("Operation failed"); free(settings->suffixv); free(settings); return 1; } // Scan directory int lines = scanDirectory(dir, 0, directory, settings); // 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(settings->suffixv); free(settings); return 0; }