Mon, 30 May 2011 08:45:08 +0200
encapsulated common operations
universe@3 | 1 | #include "cline.h" |
universe@3 | 2 | #include "functions.h" |
universe@1 | 3 | |
universe@1 | 4 | int checkArgument(const char* arg, const char* expected) { |
universe@1 | 5 | int len = strlen(expected); |
universe@1 | 6 | int ret = 0; |
universe@1 | 7 | |
universe@1 | 8 | if (arg[0] == '-') { |
universe@1 | 9 | if (arg[1] != '-') { |
universe@1 | 10 | for (int t = 0 ; t < len ; t++) { |
universe@1 | 11 | ret |= (strchr(arg, expected[t]) > 0) << t; |
universe@1 | 12 | } |
universe@1 | 13 | } |
universe@1 | 14 | } |
universe@1 | 15 | |
universe@1 | 16 | return ret; |
universe@1 | 17 | } |
universe@3 | 18 | |
universe@8 | 19 | bool registerArgument(int* reg, int mask) { |
universe@8 | 20 | bool ret = (*reg & mask) > 0; |
universe@8 | 21 | *reg |= mask; |
universe@8 | 22 | return ret; |
universe@8 | 23 | } |
universe@8 | 24 | |
universe@3 | 25 | bool testSuffix(char* filename, settings_t* settings) { |
universe@3 | 26 | bool ret = false; |
universe@3 | 27 | int tokenlen, fnamelen = strlen(filename); |
universe@6 | 28 | for (int t = 0 ; t < settings->suffixList->count ; t++) { |
universe@6 | 29 | tokenlen = strlen(settings->suffixList->items[t]); |
universe@3 | 30 | if (fnamelen >= tokenlen && tokenlen > 0) { |
universe@3 | 31 | if (strncmp(filename+fnamelen-tokenlen, |
universe@6 | 32 | settings->suffixList->items[t], tokenlen) == 0) { |
universe@3 | 33 | ret = true; |
universe@3 | 34 | break; |
universe@3 | 35 | } |
universe@3 | 36 | } |
universe@3 | 37 | } |
universe@3 | 38 | return ret ^ !settings->includeSuffixes; |
universe@3 | 39 | } |
universe@3 | 40 | |
universe@3 | 41 | int scanDirectory(DIR *dir, const int spaces, |
universe@3 | 42 | char* currdir, settings_t* settings) { |
universe@3 | 43 | DIR *subdir; |
universe@3 | 44 | char* subdirname; |
universe@3 | 45 | struct dirent *entry; |
universe@3 | 46 | int lines, digits, a; |
universe@3 | 47 | int lineSum = 0; |
universe@3 | 48 | |
universe@3 | 49 | while ((entry = readdir(dir)) != NULL) { |
universe@3 | 50 | if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { |
universe@3 | 51 | // Print occurence |
universe@3 | 52 | char entryname[strlen(entry->d_name)+spaces]; |
universe@3 | 53 | for (int t = 0 ; t < spaces ; t++) { |
universe@3 | 54 | entryname[t]=' '; |
universe@3 | 55 | } |
universe@3 | 56 | entryname[spaces] = 0; |
universe@3 | 57 | strcat(entryname, entry->d_name); |
universe@3 | 58 | |
universe@3 | 59 | // Check for subdirectory |
universe@3 | 60 | char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; |
universe@3 | 61 | strcpy(subdirname, currdir); |
universe@3 | 62 | strncat(subdirname, &settings->fileSeparator, 1); |
universe@3 | 63 | strcat(subdirname, entry->d_name); |
universe@3 | 64 | if ((subdir = opendir(subdirname)) != NULL) { |
universe@3 | 65 | printf("%-60s\n", entryname); |
universe@3 | 66 | if (settings->recursive) { |
universe@3 | 67 | lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); |
universe@3 | 68 | } |
universe@3 | 69 | closedir(subdir); |
universe@3 | 70 | continue; |
universe@3 | 71 | } |
universe@3 | 72 | |
universe@3 | 73 | // Count lines |
universe@3 | 74 | lines = 0; |
universe@3 | 75 | char filename[(1+strlen(currdir)+strlen(entry->d_name))]; |
universe@3 | 76 | strcpy(filename, currdir); |
universe@3 | 77 | strncat(filename, &settings->fileSeparator, 1); |
universe@3 | 78 | strcat(filename, entry->d_name); |
universe@3 | 79 | if (testSuffix(filename, settings)) { |
universe@3 | 80 | FILE *file = fopen(filename, "r"); |
universe@3 | 81 | if (file == NULL) { |
universe@3 | 82 | perror(" File acces failed"); |
universe@3 | 83 | continue; |
universe@3 | 84 | } |
universe@3 | 85 | |
universe@3 | 86 | do { |
universe@3 | 87 | a = fgetc(file); |
universe@3 | 88 | |
universe@3 | 89 | if (a == 10) { |
universe@3 | 90 | lines++; |
universe@3 | 91 | } |
universe@3 | 92 | } while (a != EOF); |
universe@3 | 93 | fclose(file); |
universe@3 | 94 | |
universe@3 | 95 | // Print line count |
universe@3 | 96 | #ifdef _WIN32 |
universe@3 | 97 | printf("%-60s%13d lines\n", entryname, lines); |
universe@3 | 98 | #else |
universe@3 | 99 | printf("%-60s%14d lines\n", entryname, lines); |
universe@3 | 100 | #endif /* _WIN32 */ |
universe@3 | 101 | |
universe@3 | 102 | lineSum += lines; |
universe@3 | 103 | } |
universe@3 | 104 | else { |
universe@3 | 105 | if (!settings->matchesOnly) { |
universe@3 | 106 | // Print hint |
universe@3 | 107 | #ifdef _WIN32 |
universe@3 | 108 | printf("%-60s%19s\n", entryname, "no match"); |
universe@3 | 109 | #else |
universe@3 | 110 | printf("%-60s%20s\n", entryname, "no match"); |
universe@3 | 111 | #endif /* _WIN32 */ |
universe@3 | 112 | } |
universe@3 | 113 | } |
universe@3 | 114 | } |
universe@3 | 115 | } |
universe@3 | 116 | return lineSum; |
universe@3 | 117 | } |