HuntnGather – Diff between revs 1 and 2

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 1 Rev 2
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // 2 // Copyright (C) 2021 Wizardry and Steamworks - License: MIT //
3 /////////////////////////////////////////////////////////////////////////// 3 ///////////////////////////////////////////////////////////////////////////
4   4  
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
-   8 #include <dirent.h>
8 #include <signal.h> 9 #include <signal.h>
9 #include <ctype.h> 10 #include <ctype.h>
-   11  
-   12 #include <sys/types.h>
-   13 #include <sys/stat.h>
10   14  
11 #include <proto/dos.h> 15 #include <proto/dos.h>
12 #include <proto/exec.h> 16 #include <proto/exec.h>
13   17  
14 #if defined ___AmigaOS___ 18 #if defined ___AmigaOS___
15 #include "getopt.h" 19 #include "getopt.h"
16 #endif 20 #endif
17   21  
18 #if !defined TRUE 22 #if !defined TRUE
19 #define TRUE 1; 23 #define TRUE 1;
20 #endif 24 #endif
21   25  
22 #if !defined FALSE 26 #if !defined FALSE
23 #define FALSE 0; 27 #define FALSE 0;
24 #endif 28 #endif
-   29  
-   30 #define DEFAULT_DATABASE_FILE "S:gather.db"
25   31  
26 /*************************************************************************/ 32 /*************************************************************************/
27 /* Version string used for querrying the program version. */ 33 /* Version string used for querrying the program version. */
28 /*************************************************************************/ 34 /*************************************************************************/
29 TEXT version_string[] = 35 TEXT version_string[] =
30 "\0$VER: Hunt 1.2 "__DATE__" by Wizardry and Steamworks"; 36 "\0$VER: Hunt 1.3 "__DATE__" by Wizardry and Steamworks";
31   37  
32 int run = TRUE; 38 int run = TRUE;
33   39  
34 void SignalHandler(int sig) { 40 void SignalHandler(int sig) {
35 // Toggle the run flag to stop execution. 41 // Toggle the run flag to stop execution.
36 run = FALSE; 42 run = FALSE;
37 } 43 }
38   44  
39 /* 45 /*
40 * -  
41 * Counts the lines in a database file "dbFile". -  
42 */ -  
43 int CountDatabaseLines(char *dbFile) { -  
44 FILE *fp; -  
45 int lines; -  
46 char c; -  
47   -  
48 if((fp = fopen(dbFile, "r")) == NULL) { -  
49 fprintf(stderr, "Unable to open gather database for reading.\n"); -  
50 fclose(fp); -  
51 return 0; -  
52 } -  
53   -  
54 lines = 0; -  
55 while(fscanf(fp, "%c", &c) == 1) { -  
56 switch(c) { -  
57 case '\n': -  
58 ++lines; -  
59 break; -  
60 } -  
61 } -  
62   -  
63 fclose(fp); -  
64   -  
65 return lines; -  
66 } -  
67   -  
68   -  
69 /* -  
70 * Convert string to uppercase. 46 * Convert string to uppercase.
71 */ 47 */
72 char *strupr(char *str) { 48 char *strupr(char *str) {
73 char *up; 49 char *up;
74 int i; 50 int i;
75   51  
76 up = (char *) malloc((strlen(str) + 1) * sizeof(char)); 52 up = (char *) malloc((strlen(str) + 1) * sizeof(char));
77 sprintf(up, "%s", str); 53 sprintf(up, "%s", str);
78   54  
79 i = strlen(up); 55 i = strlen(up);
80 while(--i > -1) { 56 while(--i > -1) {
81 up[i] = toupper(up[i]); 57 up[i] = toupper(up[i]);
82 } 58 }
83   59  
84 return up; 60 return up;
85 } 61 }
86   62  
87 /* 63 /*
88 * 64 *
89 * Search the database for a matching string. 65 * Search the database for a matching string.
90 */ 66 */
91 void SearchDatabase(char *dbFile, char* string, int lines) { 67 void SearchDatabase(char *dbFile, char* string) {
92 FILE *fp; 68 FILE *fp;
93 char *name; 69 char *name;
94 char *path; 70 char *path;
95 char c; 71 char c;
96 int i; 72 int i;
97 int side; 73 int side;
98 int match; 74 int match;
99 int total; 75 int total;
100   76  
101 if((fp = fopen(dbFile, "r")) == NULL) { 77 if((fp = fopen(dbFile, "r")) == NULL) {
102 fprintf(stderr, "Unable to open gather database for reading.\n"); 78 fprintf(stderr, "Unable to open gather database for reading.\n");
103 return; 79 return;
104 } 80 }
105   81  
106 name = (char *) malloc(sizeof(char)); 82 name = (char *) malloc(sizeof(char));
107 path = (char *) malloc(sizeof(char)); 83 path = (char *) malloc(sizeof(char));
108 i = 0; 84 i = 0;
109 side = 0; 85 side = 0;
110 match = FALSE; 86 match = FALSE;
111 total = 0; 87 total = 0;
112 while(run && fscanf(fp, "%c", &c) == 1) { 88 while(run && fscanf(fp, "%c", &c) == 1) {
113 #if defined ___AmigaOS___ 89 #if defined ___AmigaOS___
114 // Check if CTRL+C was pressed and abort the program. 90 // Check if CTRL+C was pressed and abort the program.
115 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 91 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
-   92 run = FALSE;
116 break; 93 break;
117 } 94 }
118 #endif 95 #endif
119   96  
120 switch(c) { 97 switch(c) {
121 case '\n': 98 case '\n':
122 ++total; 99 ++total;
123 if(match) { 100 if(match) {
124 fprintf(stdout, "%s\n", path); 101 fprintf(stdout, "%s\n", path);
125 match = FALSE; 102 match = FALSE;
126 } 103 }
127 if(name != NULL) { 104 if(name != NULL) {
128 free(name); 105 free(name);
129 name = (char *) malloc(sizeof(char)); 106 name = (char *) malloc(sizeof(char));
130 } 107 }
131 --side; 108 --side;
132 i = 0; 109 i = 0;
133 break; 110 break;
134 case '\t': 111 case '\t':
135 // Case insensitive match. 112 // Case insensitive match.
136 if(strstr(strupr(name), strupr(string)) != NULL) { 113 if(strstr(strupr(name), strupr(string)) != NULL) {
137 match = TRUE; 114 match = TRUE;
138 } 115 }
139 if(path != NULL) { 116 if(path != NULL) {
140 free(path); 117 free(path);
141 path = (char *) malloc(sizeof(char)); 118 path = (char *) malloc(sizeof(char));
142 } 119 }
143 ++side; 120 ++side;
144 i = 0; 121 i = 0;
145 break; 122 break;
146 default: 123 default:
147 switch(side) { 124 switch(side) {
148 case 0: 125 case 0:
149 name = (char *) realloc(name, (i + 1 + 1) * sizeof(char)); 126 name = (char *) realloc(name, (i + 1 + 1) * sizeof(char));
150 name[i] = c; 127 name[i] = c;
151 name[i + 1] = '\0'; 128 name[i + 1] = '\0';
152 break; 129 break;
153 case 1: 130 case 1:
154 path = (char *) realloc(path, (i + 1 + 1) * sizeof(char)); 131 path = (char *) realloc(path, (i + 1 + 1) * sizeof(char));
155 path[i] = c; 132 path[i] = c;
156 path[i + 1] = '\0'; 133 path[i + 1] = '\0';
157 break; 134 break;
158 default: 135 default:
159 fprintf(stderr, "Database corrupted.\n"); 136 fprintf(stderr, "Database corrupted.\n");
160 break; 137 break;
161 } 138 }
162 ++i; 139 ++i;
163 break; 140 break;
164 } 141 }
165 } 142 }
166   143  
167 fclose(fp); 144 fclose(fp);
168 } 145 }
-   146  
-   147 /*
-   148 *
-   149 * Search the database for the matching string.
169   150 */
170 void Hunt(char *dbFile, char *needle) { -  
171 int lines; -  
172   -  
173 lines = CountDatabaseLines(dbFile); -  
174   151 void Hunt(char *dbFile, char *needle) {
175 // Search the database for the matching string. 152 // Search the database for the matching string.
176 SearchDatabase(dbFile, needle, lines); 153 SearchDatabase(dbFile, needle);
177 } 154 }
178   155  
179 int main(int argc, char **argv) { 156 int main(int argc, char **argv) {
180 int option; 157 int option;
-   158 char *dbFile;
-   159 struct stat path;
181   160  
182 // Bind handler to SIGINT. 161 // Bind handler to SIGINT.
183 signal(SIGINT, SignalHandler); 162 signal(SIGINT, SignalHandler);
-   163  
184   164 dbFile = DEFAULT_DATABASE_FILE;
185 while((option = getopt(argc, argv, "h")) != -1) { 165 while((option = getopt(argc, argv, "hd:")) != -1) {
-   166 switch(option) {
-   167 case 'd':
-   168 dbFile = optarg;
186 switch(option) { 169 break;
187 case 'h': 170 case 'h':
188 fprintf(stdout, "SYNTAX: %s STRING", argv[0]); 171 fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]);
189 break; 172 break;
190 case '?': 173 case '?':
191 fprintf(stderr, "Invalid option %c.\n", optopt); 174 fprintf(stderr, "Invalid option %c.\n", optopt);
192 fprintf(stdout, "SYNTAX: %s STRING\n", argv[0]); 175 fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]);
193 return 1; 176 return 1;
194 } 177 }
195 } 178 }
196   179  
197 if(optind > argc) { 180 if(optind > argc) {
198 fprintf(stdout, "SYNTAX: %s [-q] STRING\n", argv[0]); 181 fprintf(stdout, "SYNTAX: %s [-d DATABASE] STRING\n", argv[0]);
-   182 return 1;
-   183 }
-   184  
-   185 stat(dbFile, &path);
-   186 if(!S_ISREG(path.st_mode)) {
-   187 fprintf(stderr, "%s is not a file.\n", dbFile);
199 return 1; 188 return 1;
200 } 189 }
201   190  
202 Hunt("S:gather.db", argv[1]); 191 Hunt("S:gather.db", argv[1]);
203   192  
204 return 0; 193 return 0;
205 } 194 }
206   195