Thu, 15 Sep 2011 13:29:06 +0200
refactored sources
universe@10 | 1 | /* |
universe@10 | 2 | * functions.c |
universe@10 | 3 | * |
universe@10 | 4 | * Created on: 23.05.2011 |
universe@10 | 5 | * Author: beckermi |
universe@10 | 6 | */ |
universe@1 | 7 | |
universe@1 | 8 | |
universe@10 | 9 | #include "scanner.h" |
universe@10 | 10 | #include "suffix_fnc.h" |
universe@3 | 11 | |
universe@3 | 12 | int scanDirectory(DIR *dir, const int spaces, |
universe@3 | 13 | char* currdir, settings_t* settings) { |
universe@3 | 14 | DIR *subdir; |
universe@3 | 15 | char* subdirname; |
universe@3 | 16 | struct dirent *entry; |
universe@3 | 17 | int lines, digits, a; |
universe@3 | 18 | int lineSum = 0; |
universe@3 | 19 | |
universe@3 | 20 | while ((entry = readdir(dir)) != NULL) { |
universe@3 | 21 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@3 | 22 | // Print occurence |
universe@3 | 23 | char entryname[strlen(entry->d_name)+spaces]; |
universe@3 | 24 | for (int t = 0 ; t < spaces ; t++) { |
universe@3 | 25 | entryname[t]=' '; |
universe@3 | 26 | } |
universe@3 | 27 | entryname[spaces] = 0; |
universe@3 | 28 | strcat(entryname, entry->d_name); |
universe@3 | 29 | |
universe@3 | 30 | // Check for subdirectory |
universe@3 | 31 | char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; |
universe@3 | 32 | strcpy(subdirname, currdir); |
universe@3 | 33 | strncat(subdirname, &settings->fileSeparator, 1); |
universe@3 | 34 | strcat(subdirname, entry->d_name); |
universe@3 | 35 | if ((subdir = opendir(subdirname)) != NULL) { |
universe@3 | 36 | printf("%-60s\n", entryname); |
universe@3 | 37 | if (settings->recursive) { |
universe@3 | 38 | lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); |
universe@3 | 39 | } |
universe@3 | 40 | closedir(subdir); |
universe@3 | 41 | continue; |
universe@3 | 42 | } |
universe@3 | 43 | |
universe@3 | 44 | // Count lines |
universe@3 | 45 | lines = 0; |
universe@3 | 46 | char filename[(1+strlen(currdir)+strlen(entry->d_name))]; |
universe@3 | 47 | strcpy(filename, currdir); |
universe@3 | 48 | strncat(filename, &settings->fileSeparator, 1); |
universe@3 | 49 | strcat(filename, entry->d_name); |
universe@3 | 50 | if (testSuffix(filename, settings)) { |
universe@3 | 51 | FILE *file = fopen(filename, "r"); |
universe@3 | 52 | if (file == NULL) { |
universe@3 | 53 | perror(" File acces failed"); |
universe@3 | 54 | continue; |
universe@3 | 55 | } |
universe@3 | 56 | |
universe@3 | 57 | do { |
universe@3 | 58 | a = fgetc(file); |
universe@3 | 59 | |
universe@3 | 60 | if (a == 10) { |
universe@3 | 61 | lines++; |
universe@3 | 62 | } |
universe@3 | 63 | } while (a != EOF); |
universe@3 | 64 | fclose(file); |
universe@3 | 65 | |
universe@3 | 66 | // Print line count |
universe@3 | 67 | #ifdef _WIN32 |
universe@3 | 68 | printf("%-60s%13d lines\n", entryname, lines); |
universe@3 | 69 | #else |
universe@3 | 70 | printf("%-60s%14d lines\n", entryname, lines); |
universe@3 | 71 | #endif /* _WIN32 */ |
universe@3 | 72 | |
universe@3 | 73 | lineSum += lines; |
universe@3 | 74 | } |
universe@3 | 75 | else { |
universe@3 | 76 | if (!settings->matchesOnly) { |
universe@3 | 77 | // Print hint |
universe@3 | 78 | #ifdef _WIN32 |
universe@3 | 79 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 80 | #else |
universe@3 | 81 | printf("%-60s%20s\n", entryname, "no match"); |
universe@3 | 82 | #endif /* _WIN32 */ |
universe@3 | 83 | } |
universe@3 | 84 | } |
universe@3 | 85 | } |
universe@3 | 86 | } |
universe@3 | 87 | return lineSum; |
universe@3 | 88 | } |