scanner.c

Fri, 16 Sep 2011 09:14:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 16 Sep 2011 09:14:59 +0200
changeset 12
902cb8d2053c
parent 10
ecf787666f44
child 14
ee9333c91dda
permissions
-rw-r--r--

removed dynamic programm name

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

mercurial