Thu, 26 May 2011 14:39:52 +0200
Moved some functions to functions.c
Replaced static variables by settings_t type
Makefile | file | annotate | diff | comparison | revisions | |
cline.c | file | annotate | diff | comparison | revisions | |
cline.h | file | annotate | diff | comparison | revisions | |
functions.c | file | annotate | diff | comparison | revisions | |
functions.h | file | annotate | diff | comparison | revisions | |
include.h | file | annotate | diff | comparison | revisions | |
v2.c | file | annotate | diff | comparison | revisions | |
v2.h | file | annotate | diff | comparison | revisions |
1.1 --- a/Makefile Mon May 23 16:54:56 2011 +0200 1.2 +++ b/Makefile Thu May 26 14:39:52 2011 +0200 1.3 @@ -1,8 +1,11 @@ 1.4 CC = gcc 1.5 -OBJ = cline.o v2.o 1.6 +OBJ = cline.o functions.o 1.7 1.8 cline: ${OBJ} 1.9 ${CC} -o cline ${OBJ} 1.10 1.11 %.o: %.c 1.12 ${CC} -c -std=c99 $< 1.13 + 1.14 +clean: 1.15 + rm *.o
2.1 --- a/cline.c Mon May 23 16:54:56 2011 +0200 2.2 +++ b/cline.c Thu May 26 14:39:52 2011 +0200 2.3 @@ -1,109 +1,17 @@ 2.4 -#include "include.h" 2.5 -#include "v2.h" 2.6 +#include "cline.h" 2.7 +#include "functions.h" 2.8 2.9 - 2.10 +settings_t* new_settings_t() { 2.11 + settings_t *settings = malloc(sizeof(settings_t*)); 2.12 #ifdef _WIN32 2.13 -static char fileSeparator = '\\'; 2.14 + settings->fileSeparator = '\\'; 2.15 #else 2.16 -static char fileSeparator = '/'; 2.17 + settings->fileSeparator = '/'; 2.18 #endif /* _WIN32 */ 2.19 - 2.20 -static int suffixc; 2.21 -static char** suffixv; 2.22 -static bool recursive; 2.23 -static bool includeSuffixes; 2.24 -static bool matchesOnly; 2.25 - 2.26 -bool testSuffix(char* filename) { 2.27 - bool ret = false; 2.28 - int tokenlen, fnamelen = strlen(filename); 2.29 - for (int t = 0 ; t < suffixc ; t++) { 2.30 - tokenlen = strlen(suffixv[t]); 2.31 - if (fnamelen >= tokenlen && tokenlen > 0) { 2.32 - if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) { 2.33 - ret = true; 2.34 - break; 2.35 - } 2.36 - } 2.37 - } 2.38 - return ret ^ !includeSuffixes; 2.39 -} 2.40 - 2.41 -int scanDirectory(DIR *dir, const int spaces, char* currdir) { 2.42 - DIR *subdir; 2.43 - char* subdirname; 2.44 - struct dirent *entry; 2.45 - int lines, digits, a; 2.46 - int lineSum = 0; 2.47 - 2.48 - while ((entry = readdir(dir)) != NULL) { 2.49 - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 2.50 - // Print occurence 2.51 - char entryname[strlen(entry->d_name)+spaces]; 2.52 - for (int t = 0 ; t < spaces ; t++) { 2.53 - entryname[t]=' '; 2.54 - } 2.55 - entryname[spaces] = 0; 2.56 - strcat(entryname, entry->d_name); 2.57 - 2.58 - // Check for subdirectory 2.59 - char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; 2.60 - strcpy(subdirname, currdir); 2.61 - strncat(subdirname, &fileSeparator, 1); 2.62 - strcat(subdirname, entry->d_name); 2.63 - if ((subdir = opendir(subdirname)) != NULL) { 2.64 - printf("%-60s\n", entryname); 2.65 - if (recursive) { 2.66 - lineSum += scanDirectory(subdir, spaces+1, subdirname); 2.67 - } 2.68 - closedir(subdir); 2.69 - continue; 2.70 - } 2.71 - 2.72 - // Count lines 2.73 - lines = 0; 2.74 - char filename[(1+strlen(currdir)+strlen(entry->d_name))]; 2.75 - strcpy(filename, currdir); 2.76 - strncat(filename, &fileSeparator, 1); 2.77 - strcat(filename, entry->d_name); 2.78 - if (testSuffix(filename)) { 2.79 - FILE *file = fopen(filename, "r"); 2.80 - if (file == NULL) { 2.81 - perror(" File acces failed"); 2.82 - continue; 2.83 - } 2.84 - 2.85 - do { 2.86 - a = fgetc(file); 2.87 - 2.88 - if (a == 10) { 2.89 - lines++; 2.90 - } 2.91 - } while (a != EOF); 2.92 - fclose(file); 2.93 - 2.94 - // Print line count 2.95 - #ifdef _WIN32 2.96 - printf("%-60s%13d lines\n", entryname, lines); 2.97 - #else 2.98 - printf("%-60s%14d lines\n", entryname, lines); 2.99 - #endif /* _WIN32 */ 2.100 - 2.101 - lineSum += lines; 2.102 - } 2.103 - else { 2.104 - if (!matchesOnly) { 2.105 - // Print hint 2.106 - #ifdef _WIN32 2.107 - printf("%-60s%19s\n", entryname, "no match"); 2.108 - #else 2.109 - printf("%-60s%20s\n", entryname, "no match"); 2.110 - #endif /* _WIN32 */ 2.111 - } 2.112 - } 2.113 - } 2.114 - } 2.115 - return lineSum; 2.116 + settings->suffixc = 1; 2.117 + settings->recursive = false; 2.118 + settings->includeSuffixes = false; 2.119 + settings->matchesOnly = false; 2.120 } 2.121 2.122 void printHelpText(const char* prgName) { 2.123 @@ -129,18 +37,20 @@ 2.124 "\nC source code in your working directory and its subdirectories, type:" 2.125 "\n %s -rs .c\n"; 2.126 2.127 - printf(helpText, prgName, prgName, prgName, prgName); 2.128 + printf(helpText, prgName, prgName, prgName, prgName); 2.129 } 2.130 2.131 int main(int argc, char** argv) { 2.132 2.133 + // Settings 2.134 + settings_t *settings = new_settings_t(); 2.135 + 2.136 // Program name 2.137 - char* prgName = strrchr(argv[0], fileSeparator); 2.138 + char* prgName = strrchr(argv[0], settings->fileSeparator); 2.139 2.140 if (prgName == NULL) { 2.141 prgName = argv[0]; 2.142 - } 2.143 - else { 2.144 + } else { 2.145 prgName++; 2.146 } 2.147 2.148 @@ -148,15 +58,13 @@ 2.149 char* _suffix = " "; 2.150 char _directory[3]; 2.151 _directory[0] = '.'; 2.152 - _directory[1] = fileSeparator; 2.153 + _directory[1] = settings->fileSeparator; 2.154 _directory[2] = 0; 2.155 2.156 // Get arguments 2.157 char* directory; 2.158 char* suffix; 2.159 bool showHelp = false; 2.160 - recursive = false; 2.161 - includeSuffixes = false; 2.162 char checked = 0; 2.163 2.164 for (int t = 1 ; t < argc ; t++) { 2.165 @@ -169,7 +77,7 @@ 2.166 printHelpText(prgName); 2.167 return -1; 2.168 } 2.169 - includeSuffixes = true; 2.170 + settings->includeSuffixes = true; 2.171 t++; 2.172 if (t >= argc) { 2.173 printHelpText(prgName); 2.174 @@ -184,7 +92,7 @@ 2.175 printHelpText(prgName); 2.176 return -1; 2.177 } 2.178 - includeSuffixes = false; 2.179 + settings->includeSuffixes = false; 2.180 t++; 2.181 if (t >= argc) { 2.182 printHelpText(prgName); 2.183 @@ -209,7 +117,7 @@ 2.184 return -1; 2.185 } 2.186 checked |= 4; 2.187 - recursive = true; 2.188 + settings->recursive = true; 2.189 } 2.190 if ((argflags & 32) > 0) { 2.191 if ((checked & 32) > 0) { 2.192 @@ -217,7 +125,7 @@ 2.193 return -1; 2.194 } 2.195 checked |= 32; 2.196 - matchesOnly = true; 2.197 + settings->matchesOnly = true; 2.198 } 2.199 // other 2.200 if (argflags == 0) { 2.201 @@ -247,21 +155,20 @@ 2.202 2.203 // Find tokens 2.204 char* finder; 2.205 - suffixc = 1; 2.206 finder = strchr(suffix, ','); 2.207 while (finder != NULL) { 2.208 - suffixc++; 2.209 + settings->suffixc++; 2.210 finder = strchr(finder+1, ','); 2.211 } 2.212 - suffixv = (char**) malloc(sizeof(suffixv)*suffixc); 2.213 - if (suffixv == NULL) { 2.214 + settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); 2.215 + if (settings->suffixv == NULL) { 2.216 fprintf(stderr, "Memory allocation failed.\n"); 2.217 return 1; 2.218 } 2.219 finder = strtok(suffix, ","); 2.220 int c = 0; 2.221 while (finder != NULL) { 2.222 - suffixv[c] = finder; 2.223 + settings->suffixv[c] = finder; 2.224 c++; 2.225 finder = strtok(NULL, ","); 2.226 } 2.227 @@ -270,30 +177,32 @@ 2.228 DIR *dir = opendir(directory); 2.229 if (dir == NULL) { 2.230 perror("Operation failed"); 2.231 - free(suffixv); 2.232 + free(settings->suffixv); 2.233 + free(settings); 2.234 return 1; 2.235 } 2.236 2.237 // Scan directory 2.238 - int lines = scanDirectory(dir, 0, directory); 2.239 + int lines = scanDirectory(dir, 0, directory, settings); 2.240 2.241 // Print double line and line count 2.242 #ifdef _WIN32 2.243 - const int columns = 79; 2.244 + const int columns = 79; 2.245 #else 2.246 - const int columns = 80; 2.247 + const int columns = 80; 2.248 #endif /* _WIN32 */ 2.249 2.250 for (int t = 0 ; t < columns ; t++) { 2.251 printf("="); 2.252 } 2.253 #ifdef _WIN32 2.254 - printf("\n%73d lines\n", lines); 2.255 + printf("\n%73d lines\n", lines); 2.256 #else 2.257 - printf("\n%74d lines\n", lines); 2.258 + printf("\n%74d lines\n", lines); 2.259 #endif /* _WIN32 */ 2.260 2.261 closedir(dir); 2.262 - free(suffixv); 2.263 + free(settings->suffixv); 2.264 + free(settings); 2.265 return 0; 2.266 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/cline.h Thu May 26 14:39:52 2011 +0200 3.3 @@ -0,0 +1,23 @@ 3.4 +#ifndef _CLINE_H 3.5 +#define _CLINE_H 3.6 + 3.7 +#include <stdio.h> 3.8 +#include <string.h> 3.9 +#include <stdbool.h> 3.10 +#include <stdlib.h> 3.11 +#include <dirent.h> 3.12 + 3.13 +typedef struct _settings { 3.14 + char fileSeparator; 3.15 + int suffixc; 3.16 + char** suffixv; 3.17 + bool recursive; 3.18 + bool includeSuffixes; 3.19 + bool matchesOnly; 3.20 +} settings_t; 3.21 + 3.22 +settings_t* new_settings_t(); 3.23 + 3.24 +void printHelpText(const char* prgName); 3.25 + 3.26 +#endif /* _CLINE_H */
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/functions.c Thu May 26 14:39:52 2011 +0200 4.3 @@ -0,0 +1,111 @@ 4.4 +#include "cline.h" 4.5 +#include "functions.h" 4.6 + 4.7 +int checkArgument(const char* arg, const char* expected) { 4.8 + int len = strlen(expected); 4.9 + int ret = 0; 4.10 + 4.11 + if (arg[0] == '-') { 4.12 + if (arg[1] != '-') { 4.13 + for (int t = 0 ; t < len ; t++) { 4.14 + ret |= (strchr(arg, expected[t]) > 0) << t; 4.15 + } 4.16 + } 4.17 + } 4.18 + 4.19 + return ret; 4.20 +} 4.21 + 4.22 +bool testSuffix(char* filename, settings_t* settings) { 4.23 + bool ret = false; 4.24 + int tokenlen, fnamelen = strlen(filename); 4.25 + for (int t = 0 ; t < settings->suffixc ; t++) { 4.26 + tokenlen = strlen(settings->suffixv[t]); 4.27 + if (fnamelen >= tokenlen && tokenlen > 0) { 4.28 + if (strncmp(filename+fnamelen-tokenlen, 4.29 + settings->suffixv[t], tokenlen) == 0) { 4.30 + ret = true; 4.31 + break; 4.32 + } 4.33 + } 4.34 + } 4.35 + return ret ^ !settings->includeSuffixes; 4.36 +} 4.37 + 4.38 +int scanDirectory(DIR *dir, const int spaces, 4.39 + char* currdir, settings_t* settings) { 4.40 + DIR *subdir; 4.41 + char* subdirname; 4.42 + struct dirent *entry; 4.43 + int lines, digits, a; 4.44 + int lineSum = 0; 4.45 + 4.46 + while ((entry = readdir(dir)) != NULL) { 4.47 + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { 4.48 + // Print occurence 4.49 + char entryname[strlen(entry->d_name)+spaces]; 4.50 + for (int t = 0 ; t < spaces ; t++) { 4.51 + entryname[t]=' '; 4.52 + } 4.53 + entryname[spaces] = 0; 4.54 + strcat(entryname, entry->d_name); 4.55 + 4.56 + // Check for subdirectory 4.57 + char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; 4.58 + strcpy(subdirname, currdir); 4.59 + strncat(subdirname, &settings->fileSeparator, 1); 4.60 + strcat(subdirname, entry->d_name); 4.61 + if ((subdir = opendir(subdirname)) != NULL) { 4.62 + printf("%-60s\n", entryname); 4.63 + if (settings->recursive) { 4.64 + lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); 4.65 + } 4.66 + closedir(subdir); 4.67 + continue; 4.68 + } 4.69 + 4.70 + // Count lines 4.71 + lines = 0; 4.72 + char filename[(1+strlen(currdir)+strlen(entry->d_name))]; 4.73 + strcpy(filename, currdir); 4.74 + strncat(filename, &settings->fileSeparator, 1); 4.75 + strcat(filename, entry->d_name); 4.76 + if (testSuffix(filename, settings)) { 4.77 + FILE *file = fopen(filename, "r"); 4.78 + if (file == NULL) { 4.79 + perror(" File acces failed"); 4.80 + continue; 4.81 + } 4.82 + 4.83 + do { 4.84 + a = fgetc(file); 4.85 + 4.86 + if (a == 10) { 4.87 + lines++; 4.88 + } 4.89 + } while (a != EOF); 4.90 + fclose(file); 4.91 + 4.92 + // Print line count 4.93 + #ifdef _WIN32 4.94 + printf("%-60s%13d lines\n", entryname, lines); 4.95 + #else 4.96 + printf("%-60s%14d lines\n", entryname, lines); 4.97 + #endif /* _WIN32 */ 4.98 + 4.99 + lineSum += lines; 4.100 + } 4.101 + else { 4.102 + if (!settings->matchesOnly) { 4.103 + // Print hint 4.104 + #ifdef _WIN32 4.105 + printf("%-60s%19s\n", entryname, "no match"); 4.106 + #else 4.107 + printf("%-60s%20s\n", entryname, "no match"); 4.108 + #endif /* _WIN32 */ 4.109 + } 4.110 + } 4.111 + } 4.112 + } 4.113 + return lineSum; 4.114 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/functions.h Thu May 26 14:39:52 2011 +0200 5.3 @@ -0,0 +1,9 @@ 5.4 +#ifndef _CLINE_FUNCTIONS_H 5.5 +#define _CLINE_FUNCTIONS_H 5.6 + 5.7 +int checkArgument(const char*, const char*); 5.8 +bool testSuffix(char* filename, settings_t* settings); 5.9 +int scanDirectory(DIR *dir, const int spaces, 5.10 + char* currdir, settings_t* settings); 5.11 + 5.12 +#endif /* V2_H */
6.1 --- a/include.h Mon May 23 16:54:56 2011 +0200 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,10 +0,0 @@ 6.4 -#ifndef INCLUDE_H 6.5 -#define INCLUDE_H 6.6 - 6.7 -#include <stdio.h> 6.8 -#include <string.h> 6.9 -#include <stdbool.h> 6.10 -#include <stdlib.h> 6.11 -#include <dirent.h> 6.12 - 6.13 -#endif /* INCLUDE_H */
7.1 --- a/v2.c Mon May 23 16:54:56 2011 +0200 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,17 +0,0 @@ 7.4 -#include "v2.h" 7.5 -#include "include.h" 7.6 - 7.7 -int checkArgument(const char* arg, const char* expected) { 7.8 - int len = strlen(expected); 7.9 - int ret = 0; 7.10 - 7.11 - if (arg[0] == '-') { 7.12 - if (arg[1] != '-') { 7.13 - for (int t = 0 ; t < len ; t++) { 7.14 - ret |= (strchr(arg, expected[t]) > 0) << t; 7.15 - } 7.16 - } 7.17 - } 7.18 - 7.19 - return ret; 7.20 -}
8.1 --- a/v2.h Mon May 23 16:54:56 2011 +0200 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,6 +0,0 @@ 8.4 -#ifndef V2_H 8.5 -#define V2_H 8.6 - 8.7 -int checkArgument(const char*, const char*); 8.8 - 8.9 -#endif /* V2_H */