HuntnGather – Diff between revs 39 and 41

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 39 Rev 41
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 <ctype.h> 8 #include <ctype.h>
9 #if !defined ___AmigaOS___ 9 #if !defined ___AmigaOS___
10 #include <dirent.h> 10 #include <dirent.h>
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <signal.h> 12 #include <signal.h>
13 #endif 13 #endif
14   14  
15 #include <sys/types.h> 15 #include <sys/types.h>
16 #include <sys/syslimits.h> 16 #include <sys/syslimits.h>
17   17  
18 #if defined ___AmigaOS___ 18 #if defined ___AmigaOS___
19 #include <proto/dos.h> 19 #include <proto/dos.h>
20 #include <proto/exec.h> 20 #include <proto/exec.h>
21 #include <proto/locale.h> 21 #include <proto/locale.h>
22 #include <clib/utility_protos.h> 22 #include <clib/utility_protos.h>
23 #endif 23 #endif
24   24  
25 #if defined ___AsyncIO___ 25 #if defined ___AsyncIO___
26 #include <asyncio.h> 26 #include <asyncio.h>
27 #endif 27 #endif
28   28  
29 #include "/shared/utilities.h" 29 #include "/shared/utilities.h"
30   30  
31 /* 31 /*
32 * 32 *
33 * Returns largest of strings. 33 * Returns largest of strings.
34 */ 34 */
35 #if defined ___AmigaOS___ 35 #if defined ___AmigaOS___
36 LONG StringLenMax(char *a, char *b) { 36 LONG StringLenMax(char *a, char *b) {
37 LONG p = strlen(a); 37 LONG p = strlen(a);
38 LONG q = strlen(b); 38 LONG q = strlen(b);
39 #else 39 #else
40 int StrlenMax(char *a, char *b) { 40 int StrlenMax(char *a, char *b) {
41 int q = strlen(a); 41 int q = strlen(a);
42 int p = strlen(b); 42 int p = strlen(b);
43 #endif 43 #endif
44 return p > q ? p : q; 44 return p > q ? p : q;
45 } 45 }
46   46  
47 /* 47 /*
48 * 48 *
49 * Returns largest of strings. 49 * Returns largest of strings.
50 */ 50 */
51 #if defined ___AmigaOS___ 51 #if defined ___AmigaOS___
52 LONG StringLenMin(char *a, char *b) { 52 LONG StringLenMin(char *a, char *b) {
53 LONG p = strlen(a); 53 LONG p = strlen(a);
54 LONG q = strlen(b); 54 LONG q = strlen(b);
55 #else 55 #else
56 int StrlenMin(char *a, char *b) { 56 int StrlenMin(char *a, char *b) {
57 int q = strlen(a); 57 int q = strlen(a);
58 int p = strlen(b); 58 int p = strlen(b);
59 #endif 59 #endif
60 return p > q ? q : p; 60 return p > q ? q : p;
61 } 61 }
62   62  
63 /* 63 /*
64 * 64 *
65 * Converts a string to case. 65 * Converts a string to case.
66 */ 66 */
67 void StrUpr(char *s) { 67 void StrUpr(char *s) {
68 while(*s != '\0') { 68 while(*s != '\0') {
69 #if defined ___AmigaOS___ 69 #if defined ___AmigaOS___
70 *s = ToUpper(*s); 70 *s = ToUpper(*s);
71 #else 71 #else
72 *s = toupper((unsigned char)*s); 72 *s = toupper((unsigned char)*s);
73 #endif 73 #endif
74 ++s; 74 ++s;
75 } 75 }
76 } 76 }
77   77  
78 /* 78 /*
79 * 79 *
80 * Gets the filesystem type of a file or directory. 80 * Gets the filesystem type of a file or directory.
81 */ 81 */
82 FS_TYPE GetFsType(char *path) { 82 FS_TYPE GetFsType(char *path) {
83 #if defined ___AmigaOS___ 83 #if defined ___AmigaOS___
84 struct FileInfoBlock *FIB; 84 struct FileInfoBlock *FIB;
85 BPTR lock; 85 BPTR lock;
86 #else 86 #else
87 struct stat dirStat; 87 struct stat dirStat;
88 #endif 88 #endif
89   89  
90 #if defined ___AmigaOS___ 90 #if defined ___AmigaOS___
91 if((lock = Lock(path, ACCESS_READ)) == NULL) { 91 if((lock = Lock(path, ACCESS_READ)) == NULL) {
92 fprintf(stderr, "Lock failed for path '%s'.\n", path); 92 fprintf(stderr, "Lock failed for path '%s'.\n", path);
93 return UNKNOWN; 93 return UNKNOWN;
94 } 94 }
95   95  
96 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) { 96 if((FIB = AllocDosObject(DOS_FIB, NULL)) == NULL) {
97 fprintf(stderr, "Could not allocated file information block for path '%s'.\n", path); 97 fprintf(stderr, "Could not allocated file information block for path '%s'.\n", path);
98 UnLock(lock); 98 UnLock(lock);
99 return UNKNOWN; 99 return UNKNOWN;
100 } 100 }
101   101  
102 if(Examine(lock, FIB) == FALSE) { 102 if(Examine(lock, FIB) == FALSE) {
103 fprintf(stderr, "Path '%s' could not be examined.\n", path); 103 fprintf(stderr, "Path '%s' could not be examined.\n", path);
104 UnLock(lock); 104 UnLock(lock);
105 FreeDosObject(DOS_FIB, FIB); 105 FreeDosObject(DOS_FIB, FIB);
106 FIB = NULL; 106 FIB = NULL;
107 return UNKNOWN; 107 return UNKNOWN;
108 } 108 }
109   109  
110 if(FIB->fib_DirEntryType < 0) { 110 if(FIB->fib_DirEntryType < 0) {
111 UnLock(lock); 111 UnLock(lock);
112 FreeDosObject(DOS_FIB, FIB); 112 FreeDosObject(DOS_FIB, FIB);
113 FIB = NULL; 113 FIB = NULL;
114 return REGULAR; 114 return REGULAR;
115 #else 115 #else
116 stat(path, &dirStat); 116 stat(path, &dirStat);
117 if(!S_ISDIR(dirStat.st_mode)) { 117 if(!S_ISDIR(dirStat.st_mode)) {
118 return REGULAR; 118 return REGULAR;
119 #endif 119 #endif
120 } 120 }
121   121  
122 #if defined ___AmigaOS___ 122 #if defined ___AmigaOS___
123 UnLock(lock); 123 UnLock(lock);
124 FreeDosObject(DOS_FIB, FIB); 124 FreeDosObject(DOS_FIB, FIB);
125 FIB = NULL; 125 FIB = NULL;
126 #endif 126 #endif
127   127  
128 return DIRECTORY; 128 return DIRECTORY;
129 } 129 }
130   130  
131 /* 131 /*
132 * 132 *
-   133 * Get the size of a file.
-   134 */
-   135 int GetFileSize(char *dbFile) {
-   136 #if defined ___AsyncIO___
-   137 struct AsyncFile *fp;
-   138 #else
-   139 FILE *fp;
-   140 #endif
-   141 int size;
-   142  
-   143 #if defined ___AsyncIO___
-   144 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
-   145 #else
-   146 if((fp = fopen(dbFile, "r")) == NULL) {
-   147 #endif
-   148 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
-   149 return -1;
-   150 }
-   151  
-   152 #if defined ___AsyncIO___
-   153 SeekAsync(fp, 0, MODE_END);
-   154 size = SeekAsync(fp, 0, MODE_CURRENT);
-   155 #else
-   156 fseek(fp, 0L, SEEK_END);
-   157 size = ftell(fp);
-   158 #endif
-   159  
-   160 #if defined ___AsyncIO___
-   161 CloseAsync(fp);
-   162 #else
-   163 fclose(fp);
-   164 #endif
-   165  
-   166 return size;
-   167 }
-   168  
-   169 /*
-   170 *
133 * Counts the lines of a file. 171 * Counts the lines of a file.
134 */ 172 */
135 int CountFileLines(char *dbFile) { 173 int CountFileLines(char *dbFile) {
136 #if defined ___AsyncIO___ 174 #if defined ___AsyncIO___
137 struct AsyncFile *fp; 175 struct AsyncFile *fp;
138 LONG c; -  
139 #else 176 #else
140 FILE *fp; 177 FILE *fp;
141 char c; -  
142 #endif 178 #endif
143 int lines; 179 int lines;
-   180 dbLine *line = NULL;
144   181  
145 #if defined ___AsyncIO___ 182 #if defined ___AsyncIO___
146 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 183 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
147 #else 184 #else
148 if((fp = fopen(dbFile, "r")) == NULL) { 185 if((fp = fopen(dbFile, "r")) == NULL) {
149 #endif 186 #endif
150 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile); 187 fprintf(stderr, "Could not open file '%s' for reading.\n", dbFile);
151 return -1; 188 return -1;
152 } 189 }
153   -  
154 lines = 0; 190  
155 if(PROGRAM_VERBOSE) { 191 if(PROGRAM_VERBOSE) {
156 fprintf(stdout, "Counting lines in '%s'...\r", dbFile); 192 fprintf(stdout, "Counting lines in '%s'...\r", dbFile);
157 } 193 }
158   194  
159 #if defined ___AsyncIO___ 195 lines = 0;
160 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { -  
161 #else -  
162 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { -  
163 #endif 196 while(PROGRAM_RUN && (line = ReadLine(fp)) != NULL) {
164 #if defined ___AmigaOS___ 197 #if defined ___AmigaOS___
165 // Check if CTRL+C was pressed and abort the program. 198 // Check if CTRL+C was pressed and abort the program.
166 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 199 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
167 PROGRAM_RUN = FALSE; 200 PROGRAM_RUN = FALSE;
168 continue; 201 continue;
169 } 202 }
170 #endif 203 #endif
171 switch(c) { -  
172 case '\n': 204  
173 ++lines; 205 ++lines;
174   206  
175 if(PROGRAM_VERBOSE) { 207 if(PROGRAM_VERBOSE) {
176 fprintf(stdout, "Counting lines in '%s': %d.\r", dbFile, lines); 208 fprintf(stdout, "Counting lines in '%s': %d.\r", dbFile, lines);
177 } 209 }
-   210  
-   211 free(line->string);
178 break; 212 free(line);
-   213 line = NULL;
179 } 214 }
-   215  
-   216 if(line != NULL) {
-   217 free(line->string);
-   218 free(line);
-   219 line = NULL;
180 } 220 }
181   221  
182 #if defined ___AsyncIO___ 222 #if defined ___AsyncIO___
183 CloseAsync(fp); 223 CloseAsync(fp);
184 #else 224 #else
185 fclose(fp); 225 fclose(fp);
186 #endif 226 #endif
187   227  
188 if(PROGRAM_VERBOSE) { 228 if(PROGRAM_VERBOSE) {
189 fprintf(stdout, "\n"); 229 fprintf(stdout, "\n");
190 } 230 }
191   231  
192 return lines; 232 return lines;
193 } 233 }
194   234  
195 /* 235 /*
196 * 236 *
197 * Gets the absolute path to file by name. 237 * Gets the absolute path to file by name.
198 */ 238 */
199 char *PathToAbsolute(char *path) { 239 char *PathToAbsolute(char *path) {
200 char *abs; 240 char *abs;
201 #if defined ___AmigaOS___ 241 #if defined ___AmigaOS___
202 BPTR lock; 242 BPTR lock;
203 #endif 243 #endif
204   244  
205 #if defined ___AmigaOS___ 245 #if defined ___AmigaOS___
206 if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) { 246 if((abs = malloc(PATH_MAX * sizeof(*abs))) == NULL) {
207 fprintf(stderr, "Memory allocation failure.\n"); 247 fprintf(stderr, "Memory allocation failure.\n");
208 return NULL; 248 return NULL;
209 } 249 }
210 if((lock = Lock(path, SHARED_LOCK)) == 0) { 250 if((lock = Lock(path, SHARED_LOCK)) == 0) {
211 fprintf(stderr, "Lock failed for path '%s'.\n", path); 251 fprintf(stderr, "Lock failed for path '%s'.\n", path);
212 return NULL; 252 return NULL;
213 } 253 }
214 if(NameFromLock(lock, abs, PATH_MAX) == FALSE) { 254 if(NameFromLock(lock, abs, PATH_MAX) == FALSE) {
215 fprintf(stderr, "Full path for '%s' could not be retrieved.\n", path); 255 fprintf(stderr, "Full path for '%s' could not be retrieved.\n", path);
216 UnLock(lock); 256 UnLock(lock);
217 return NULL; 257 return NULL;
218 } 258 }
219 UnLock(lock); 259 UnLock(lock);
220 #else 260 #else
221 /* 261 /*
222 * On POSIX this should be: 262 * On POSIX this should be:
223 * abs = realpath(path, NULL); 263 * abs = realpath(path, NULL);
224 * 264 *
225 */ 265 */
226 if((abs = malloc((strlen(path) + 1) * sizeof(*abs))) == NULL) { 266 if((abs = malloc((strlen(path) + 1) * sizeof(*abs))) == NULL) {
227 fprintf(stderr, "Memory allocation failure.\n"); 267 fprintf(stderr, "Memory allocation failure.\n");
228 return NULL; 268 return NULL;
229 } 269 }
230 sprintf(abs, "%s", path); 270 sprintf(abs, "%s", path);
231 #endif 271 #endif
232   272  
233 return abs; 273 return abs;
234 } 274 }
235   275  
236 /* 276 /*
237 * 277 *
238 * Compares path parts for equality. 278 * Compares path parts for equality.
239 */ 279 */
240 #if defined ___AmigaOS___ 280 #if defined ___AmigaOS___
241 BOOL PathCompare(char *path, char *look) { 281 BOOL PathCompare(char *path, char *look) {
242 #else 282 #else
243 int PathCompare(char *path, char *look) { 283 int PathCompare(char *path, char *look) {
244 #endif 284 #endif
245 char *a; 285 char *a;
246 char *b; 286 char *b;
247   287  
248 for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) { 288 for(a = path, b = look; *a != '\0' && *b != '\0'; ++a, ++b) {
249 if(*b != '\0' && *a != *b) { 289 if(*b != '\0' && *a != *b) {
250 return FALSE; 290 return FALSE;
251 } 291 }
252 } 292 }
253   293  
254 return *b == '\0'; 294 return *b == '\0';
255 } 295 }
256   296  
257 /* 297 /*
258 * 298 *
259 * Creates a temporary file and returns its name. 299 * Creates a temporary file and returns its name.
260 */ 300 */
261 char *CreateTemporaryFile(void) { 301 char *CreateTemporaryFile(void) {
262 char *name; 302 char *name;
263   303  
264 name = tmpnam(NULL); 304 name = tmpnam(NULL);
265   305  
266 return name; 306 return name;
267 } 307 }
268   308  
269 /* 309 /*
270 * 310 *
271 * Create multiple temporary files and return their names. 311 * Create multiple temporary files and return their names.
272 */ 312 */
273 VECTOR *CreateTemporaryFiles(int files) { 313 VECTOR *CreateTemporaryFiles(int files) {
274 VECTOR *tmpNames; 314 VECTOR *tmpNames;
275 int total; 315 int total;
276   316  
277 if((tmpNames = malloc(1 * sizeof(*tmpNames))) == NULL) { 317 if((tmpNames = malloc(1 * sizeof(*tmpNames))) == NULL) {
278 fprintf(stderr, "Memory allocation failure.\n"); 318 fprintf(stderr, "Memory allocation failure.\n");
279 return NULL; 319 return NULL;
280 } 320 }
281   321  
282 if((tmpNames->array = malloc(files * sizeof(*tmpNames->array))) == NULL) { 322 if((tmpNames->array = malloc(files * sizeof(*tmpNames->array))) == NULL) {
283 fprintf(stderr, "Memory allocation failure.\n"); 323 fprintf(stderr, "Memory allocation failure.\n");
284 return NULL; 324 return NULL;
285 } 325 }
286   326  
287 if(PROGRAM_VERBOSE) { 327 if(PROGRAM_VERBOSE) {
288 fprintf(stdout, "Creating temporary files...\r"); 328 fprintf(stdout, "Creating temporary files...\r");
289 } 329 }
290   330  
291 tmpNames->length = 0; 331 tmpNames->length = 0;
292 total = files; 332 total = files;
293 while(PROGRAM_RUN && --files > -1) { 333 while(PROGRAM_RUN && --files > -1) {
294 #if defined ___AmigaOS___ 334 #if defined ___AmigaOS___
295 // Check if CTRL+C was pressed and abort the program. 335 // Check if CTRL+C was pressed and abort the program.
296 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 336 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
297 PROGRAM_RUN = FALSE; 337 PROGRAM_RUN = FALSE;
298 continue; 338 continue;
299 } 339 }
300 #endif 340 #endif
301 tmpNames->array[files] = CreateTemporaryFile(); 341 tmpNames->array[files] = CreateTemporaryFile();
302 ++tmpNames->length; 342 ++tmpNames->length;
303   343  
304 if(PROGRAM_VERBOSE) { 344 if(PROGRAM_VERBOSE) {
305 fprintf(stdout, "Creating temporary files: %d%%.\r", (int)(((float)tmpNames->length / total) * 100.0)); 345 fprintf(stdout, "Creating temporary files: %d%%.\r", (int)(((float)tmpNames->length / total) * 100.0));
306 } 346 }
307 } 347 }
308   348  
309 if(PROGRAM_VERBOSE) { 349 if(PROGRAM_VERBOSE) {
310 fprintf(stdout, "\n"); 350 fprintf(stdout, "\n");
311 } 351 }
312   352  
313 return tmpNames; 353 return tmpNames;
314 } 354 }
315   355  
316 /* 356 /*
317 * 357 *
318 * Skips a line in a file. 358 * Skips a line in a file.
319 */ 359 */
320 #if defined ___AsyncIO___ 360 #if defined ___AsyncIO___
321 void SkipLine(struct AsyncFile *fp) { 361 void SkipLine(struct AsyncFile *fp) {
322 LONG c; 362 LONG c;
323 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { 363 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
324 #else 364 #else
325 void SkipLine(FILE *fp) { 365 void SkipLine(FILE *fp) {
326 char c; 366 char c;
327 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 367 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
328 #endif 368 #endif
329 #if defined ___AmigaOS___ 369 #if defined ___AmigaOS___
330 // Check if CTRL+C was pressed and abort the program. 370 // Check if CTRL+C was pressed and abort the program.
331 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 371 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
332 PROGRAM_RUN = FALSE; 372 PROGRAM_RUN = FALSE;
333 continue; 373 continue;
334 } 374 }
335 #endif 375 #endif
336 switch(c) { 376 switch(c) {
337 case '\n': 377 case '\n':
338 return; 378 return;
339 } 379 }
340 } 380 }
341 } 381 }
342   382  
343 /* 383 /*
344 * 384 *
345 * Peeks for the next line in a file. 385 * Peeks for the next line in a file.
346 */ 386 */
347 #if defined ___AsyncIO___ 387 #if defined ___AsyncIO___
348 dbLine *PeekLine(struct AsyncFile *fp) { 388 dbLine *PeekLine(struct AsyncFile *fp) {
349 #else 389 #else
350 dbLine *PeekLine(FILE *fp) { 390 dbLine *PeekLine(FILE *fp) {
351 #endif 391 #endif
352 dbLine *line; 392 dbLine *line;
353   393  
354 // Read the next line. 394 // Read the next line.
355 if((line = ReadLine(fp)) == NULL) { 395 if((line = ReadLine(fp)) == NULL) {
356 return NULL; 396 return NULL;
357 } 397 }
358   398  
359 // Rewind the file by the number of read characters. 399 // Rewind the file by the number of read characters.
360 #if defined ___AsyncIO___ 400 #if defined ___AsyncIO___
361 if(SeekAsync(fp, -(line->length + 1), MODE_CURRENT) == -1) { 401 if(SeekAsync(fp, -(line->length + 1), MODE_CURRENT) == -1) {
362 fprintf(stderr, "Could not seek in file.\n"); 402 fprintf(stderr, "Could not seek in file.\n");
363 free(line->string); 403 free(line->string);
364 free(line); 404 free(line);
365 line = NULL; 405 line = NULL;
366 return NULL; 406 return NULL;
367 } 407 }
368 #else 408 #else
369 if(fseek(fp, -(line->length + 1), SEEK_CUR) != 0) { 409 if(fseek(fp, -(line->length + 1), SEEK_CUR) != 0) {
370 fprintf(stderr, "Could not seek in file.\n"); 410 fprintf(stderr, "Could not seek in file.\n");
371 free(line->string); 411 free(line->string);
372 free(line); 412 free(line);
373 line = NULL; 413 line = NULL;
374 return NULL; 414 return NULL;
375 } 415 }
376 #endif 416 #endif
377   417  
378 return line; 418 return line;
379 } 419 }
380   420  
381 /* 421 /*
382 * 422 *
383 * Reads the next line in a file. 423 * Reads the next line in a file.
384 */ 424 */
385 #if defined ___AsyncIO___ 425 #if defined ___AsyncIO___
386 dbLine *ReadLine(struct AsyncFile *fp) { 426 dbLine *ReadLine(struct AsyncFile *fp) {
387 LONG c; 427 LONG c;
388 #else 428 #else
389 dbLine *ReadLine(FILE *fp) { 429 dbLine *ReadLine(FILE *fp) {
390 char c; 430 char c;
391 #endif 431 #endif
392 int i; 432 int i;
393 dbLine *line; 433 dbLine *line;
394   434  
395 i = 0; 435 i = 0;
396 #if defined ___AsyncIO___ 436 #if defined ___AsyncIO___
397 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) { 437 while(PROGRAM_RUN && (c = ReadCharAsync(fp)) != -1) {
398 #else 438 #else
399 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) { 439 while(PROGRAM_RUN && fscanf(fp, "%c", &c) == 1) {
400 #endif 440 #endif
401 #if defined ___AmigaOS___ 441 #if defined ___AmigaOS___
402 // Check if CTRL+C was pressed and abort the program. 442 // Check if CTRL+C was pressed and abort the program.
403 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 443 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
404 PROGRAM_RUN = FALSE; 444 PROGRAM_RUN = FALSE;
405 continue; 445 continue;
406 } 446 }
407 #endif 447 #endif
408 switch(c) { 448 switch(c) {
409 case '\n': 449 case '\n':
410 // Rewind the file by the number of read characters. 450 // Rewind the file by the number of read characters.
411 #if defined ___AsyncIO___ 451 #if defined ___AsyncIO___
412 if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) { 452 if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) {
413 fprintf(stderr, "Could not seek in file.\n"); 453 fprintf(stderr, "Could not seek in file.\n");
414 fprintf(stderr, "index at: %d\n", i); 454 fprintf(stderr, "index at: %d\n", i);
415 return NULL; 455 return NULL;
416 } 456 }
417 #else 457 #else
418 if(fseek(fp, -(i + 1), SEEK_CUR) != 0) { 458 if(fseek(fp, -(i + 1), SEEK_CUR) != 0) {
419 fprintf(stderr, "Could not seek in file.\n"); 459 fprintf(stderr, "Could not seek in file.\n");
420 return NULL; 460 return NULL;
421 } 461 }
422 #endif 462 #endif
423 goto LINE; 463 goto LINE;
424 } 464 }
425   465  
426 ++i; 466 ++i;
427 } 467 }
428   468  
429 LINE: 469 LINE:
430   470  
431 if(i == 0) { 471 if(i == 0) {
432 return NULL; 472 return NULL;
433 } 473 }
434   474  
435 if((line = malloc(1 * sizeof(*line))) == NULL) { 475 if((line = malloc(1 * sizeof(*line))) == NULL) {
436 fprintf(stderr, "Memory allocation error.\n"); 476 fprintf(stderr, "Memory allocation error.\n");
437 return NULL; 477 return NULL;
438 } 478 }
439   479  
440 if((line->string = malloc((i + 1) * sizeof(*line->string))) == NULL) { 480 if((line->string = malloc((i + 1) * sizeof(*line->string))) == NULL) {
441 fprintf(stderr, "Memory allocation error.\n"); 481 fprintf(stderr, "Memory allocation error.\n");
442 free(line); 482 free(line);
443 line = NULL; 483 line = NULL;
444 return NULL; 484 return NULL;
445 } 485 }
446   486  
447 #if defined ___AsyncIO___ 487 #if defined ___AsyncIO___
448 if(ReadAsync(fp, line->string, i) != i) { 488 if(ReadAsync(fp, line->string, i) != i) {
449 #else 489 #else
450 if(fread(line->string, sizeof(char), i, fp) != i) { 490 if(fread(line->string, sizeof(char), i, fp) != i) {
451 #endif 491 #endif
452 fprintf(stderr, "Unable to read line.\n"); 492 fprintf(stderr, "Unable to read line.\n");
453 free(line->string); 493 free(line->string);
454 free(line); 494 free(line);
455 line = NULL; 495 line = NULL;
456 return NULL; 496 return NULL;
457 } 497 }
458   498  
459 // Clear newline. 499 // Clear newline.
460 #if defined ___AsyncIO___ 500 #if defined ___AsyncIO___
461 ReadCharAsync(fp); 501 ReadCharAsync(fp);
462 #else 502 #else
463 fgetc(fp); 503 fgetc(fp);
464 #endif 504 #endif
465   505  
466 line->length = i; 506 line->length = i;
467   507  
468 return line; 508 return line;
469 } 509 }
470   510  
471 /* 511 /*
472 * 512 *
473 * Delete a file. 513 * Delete a file.
474 */ 514 */
475 #if defined ___AmigaOS___ 515 #if defined ___AmigaOS___
476 BOOL RemoveFile(char *name) { 516 BOOL RemoveFile(char *name) {
477 return DeleteFile(name); 517 return DeleteFile(name);
478 #else 518 #else
479 int RemoveFile(char *name) { 519 int RemoveFile(char *name) {
480 return remove(name) == 0; 520 return remove(name) == 0;
481 #endif 521 #endif
482 } 522 }
483   523  
484 /* 524 /*
485 * 525 *
486 * Deletes files. 526 * Deletes files.
487 */ 527 */
488 void RemoveFiles(VECTOR *names) { 528 void RemoveFiles(VECTOR *names) {
489 int i; 529 int i;
490 for(i = 0; i < names->length; ++i) { 530 for(i = 0; i < names->length; ++i) {
491 if(RemoveFile(names->array[i]) == FALSE) { 531 if(RemoveFile(names->array[i]) == FALSE) {
492 fprintf(stderr, "Could not remove file '%s'.\n", (char *)names->array[i]); 532 fprintf(stderr, "Could not remove file '%s'.\n", (char *)names->array[i]);
493 continue; 533 continue;
494 } 534 }
495 fprintf(stderr, "Removing file '%s'\n", (char *)names->array[i]); 535 fprintf(stderr, "Removing file '%s'\n", (char *)names->array[i]);
496 } 536 }
497 } 537 }
498   538  
499 /* 539 /*
500 * 540 *
501 * Copies a file to another file by name. 541 * Copies a file to another file by name.
502 */ 542 */
503 void CopyFile(char *a, char *b) { 543 void CopyFile(char *a, char *b) {
504 #if defined ___AsyncIO___ 544 #if defined ___AsyncIO___
505 struct AsyncFile *ap; 545 struct AsyncFile *ap;
506 struct AsyncFile *bp; 546 struct AsyncFile *bp;
507 LONG c; 547 LONG c;
508 #else 548 #else
509 FILE *ap; 549 FILE *ap;
510 FILE *bp; 550 FILE *bp;
511 char c; 551 char c;
512 #endif 552 #endif
513   553  
514 // Open database file for writing. 554 // Open database file for writing.
515 #if defined ___AsyncIO___ 555 #if defined ___AsyncIO___
516 if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) { 556 if((ap = OpenAsync(a, MODE_READ, ASYNC_BUF)) == NULL) {
517 #else 557 #else
518 if((ap = fopen(a, "r")) == NULL) { 558 if((ap = fopen(a, "r")) == NULL) {
519 #endif 559 #endif
520 fprintf(stderr, "Could not open file '%s' for reading.\n", a); 560 fprintf(stderr, "Could not open file '%s' for reading.\n", a);
521 return; 561 return;
522 } 562 }
523   563  
524 // Open temporary file for reading. 564 // Open temporary file for reading.
525 #if defined ___AsyncIO___ 565 #if defined ___AsyncIO___
526 if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) { 566 if((bp = OpenAsync(b, MODE_WRITE, ASYNC_BUF)) == NULL) {
527 #else 567 #else
528 if((bp = fopen(b, "w")) == NULL) { 568 if((bp = fopen(b, "w")) == NULL) {
529 #endif 569 #endif
530 fprintf(stderr, "Could not open file '%s' for writing.\n", b); 570 fprintf(stderr, "Could not open file '%s' for writing.\n", b);
531   571  
532 // Close database file. 572 // Close database file.
533 #if defined ___AsyncIO___ 573 #if defined ___AsyncIO___
534 CloseAsync(ap); 574 CloseAsync(ap);
535 #else 575 #else
536 fclose(ap); 576 fclose(ap);
537 #endif 577 #endif
538   578  
539 return; 579 return;
540 } 580 }
541   581  
542 #if defined ___AsyncIO___ 582 #if defined ___AsyncIO___
543 while(PROGRAM_RUN && (c = ReadCharAsync(ap)) != -1) { 583 while(PROGRAM_RUN && (c = ReadCharAsync(ap)) != -1) {
544 #else 584 #else
545 while(PROGRAM_RUN && fscanf(ap, "%c", &c) == 1) { 585 while(PROGRAM_RUN && fscanf(ap, "%c", &c) == 1) {
546 #endif 586 #endif
547 #if defined ___AmigaOS___ 587 #if defined ___AmigaOS___
548 // Check if CTRL+C was pressed and abort the program. 588 // Check if CTRL+C was pressed and abort the program.
549 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 589 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
550 PROGRAM_RUN = FALSE; 590 PROGRAM_RUN = FALSE;
551 continue; 591 continue;
552 } 592 }
553 #endif 593 #endif
554 #if defined ___AsyncIO___ 594 #if defined ___AsyncIO___
555 if(WriteCharAsync(bp, (UBYTE)c) != 1) { 595 if(WriteCharAsync(bp, (UBYTE)c) != 1) {
556 #else 596 #else
557 if(fprintf(bp, "%c", c) != 1) { 597 if(fprintf(bp, "%c", c) != 1) {
558 #endif 598 #endif
559 fprintf(stderr, "Could not write to file '%s'.\n", b); 599 fprintf(stderr, "Could not write to file '%s'.\n", b);
560 break; 600 break;
561 } 601 }
562 } 602 }
563   603  
564 #if defined ___AsyncIO___ 604 #if defined ___AsyncIO___
565 CloseAsync(ap); 605 CloseAsync(ap);
566 CloseAsync(bp); 606 CloseAsync(bp);
567 #else 607 #else
568 fclose(ap); 608 fclose(ap);
569 fclose(bp); 609 fclose(bp);
570 #endif 610 #endif
571 } 611 }
572   612  
573 /* 613 /*
574 * 614 *
575 * Create a database entry from a line of text. 615 * Create a database entry from a line of text.
576 */ 616 */
577 dbEntry* CreateDatabaseEntry(dbLine *line) { 617 dbEntry* CreateDatabaseEntry(dbLine *line) {
578 dbEntry *entry; 618 dbEntry *entry;
579 char *ptr; 619 char *ptr;
580 int side; 620 int side;
581 int i; 621 int i;
582 int j; 622 int j;
583   623  
584 if((entry = malloc(1 * sizeof(*entry))) == NULL) { 624 if((entry = malloc(1 * sizeof(*entry))) == NULL) {
585 fprintf(stderr, "Memory allocation failure.\n"); 625 fprintf(stderr, "Memory allocation failure.\n");
586 return NULL; 626 return NULL;
587 } 627 }
588   628  
589 if((entry->name = malloc((line->length + 1) * sizeof(*entry->name))) == NULL) { 629 if((entry->name = malloc((line->length + 1) * sizeof(*entry->name))) == NULL) {
590 fprintf(stderr, "Memory allocation failure.\n"); 630 fprintf(stderr, "Memory allocation failure.\n");
591 return NULL; 631 return NULL;
592 } 632 }
593   633  
594 if((entry->path = malloc((line->length + 1) * sizeof(*entry->path))) == NULL) { 634 if((entry->path = malloc((line->length + 1) * sizeof(*entry->path))) == NULL) {
595 fprintf(stderr, "Memory allocation failure.\n"); 635 fprintf(stderr, "Memory allocation failure.\n");
596 return NULL; 636 return NULL;
597 } 637 }
598   638  
599 for(ptr = line->string, side = 0, i = 0, j = 0; PROGRAM_RUN && *ptr != '\0'; ++ptr) { 639 for(ptr = line->string, side = 0, i = 0, j = 0; PROGRAM_RUN && *ptr != '\0'; ++ptr) {
600 #if defined ___AmigaOS___ 640 #if defined ___AmigaOS___
601 // Check if CTRL+C was pressed and abort the program. 641 // Check if CTRL+C was pressed and abort the program.
602 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 642 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
603 PROGRAM_RUN = FALSE; 643 PROGRAM_RUN = FALSE;
604 continue; 644 continue;
605 } 645 }
606 #endif 646 #endif
607 switch(*ptr) { 647 switch(*ptr) {
608 case '\t': 648 case '\t':
609 entry->name[i] = '\0'; 649 entry->name[i] = '\0';
610 ++side; 650 ++side;
611 break; 651 break;
612 case '\n': 652 case '\n':
613 entry->path[j] = '\0'; 653 entry->path[j] = '\0';
614 return entry; 654 return entry;
615 default: 655 default:
616 switch(side) { 656 switch(side) {
617 case 0: 657 case 0:
618 entry->name[i++] = *ptr; 658 entry->name[i++] = *ptr;
619 break; 659 break;
620 case 1: 660 case 1:
621 entry->path[j++] = *ptr; 661 entry->path[j++] = *ptr;
622 break; 662 break;
623 } 663 }
624 break; 664 break;
625 } 665 }
626 } 666 }
627   667  
628 return entry; 668 return entry;
629 } 669 }
630   670  
631 /* 671 /*
632 * Compare two strings. 672 * Compare two strings.
633 */ 673 */
634 #if defined ___AmigaOS___ 674 #if defined ___AmigaOS___
635 BOOL StringMatch(char *a, char *b) { 675 BOOL StringMatch(char *a, char *b) {
636 #else 676 #else
637 int StringMatch(char *a, char *b) { 677 int StringMatch(char *a, char *b) {
638 #endif 678 #endif
639 #if defined ___AmigaOS___ 679 #if defined ___AmigaOS___
640 ULONG size; 680 ULONG size;
641 BOOL success; 681 BOOL success;
642 UBYTE *pattern; 682 UBYTE *pattern;
643   683  
644 // "must be at least 2 times as large plus 2 bytes" 684 // "must be at least 2 times as large plus 2 bytes"
645 size = strlen(b) * 2 + 2; 685 size = strlen(b) * 2 + 2;
646   686  
647 success = FALSE; 687 success = FALSE;
648   688  
649 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) { 689 if(pattern = AllocVec(size, MEMF_ANY|MEMF_CLEAR)) {
650 switch(ParsePatternNoCase(b, pattern, (LONG)size)) { 690 switch(ParsePatternNoCase(b, pattern, (LONG)size)) {
651 case 1: // the pattern contains wildcards 691 case 1: // the pattern contains wildcards
652 success = MatchPatternNoCase(pattern, a); 692 success = MatchPatternNoCase(pattern, a);
653   693  
654 break; 694 break;
655 case 0: // no wildcards so fall back to exact name match 695 case 0: // no wildcards so fall back to exact name match
656 #if defined ___NOCASE_FS___ 696 #if defined ___NOCASE_FS___
657 success = (Strnicmp(a, b, StringLenMax(a, b)) == 0); 697 success = (Strnicmp(a, b, StringLenMax(a, b)) == 0);
658 #else 698 #else
659 success = (StrnCmp(a, b, StringLenMax(a, b)) == 0); 699 success = (StrnCmp(a, b, StringLenMax(a, b)) == 0);
660 #endif 700 #endif
661   701  
662 break; 702 break;
663 } 703 }
664   704  
665 FreeVec(pattern); 705 FreeVec(pattern);
666 pattern = NULL; 706 pattern = NULL;
667 } 707 }
668   708  
669 return success; 709 return success;
670 #else 710 #else
671 int success; 711 int success;
672 char *e = a; 712 char *e = a;
673 char *n = b; 713 char *n = b;
674   714  
675 success = FALSE; 715 success = FALSE;
676   716  
677 #if defined ___NOCASE_FS___ 717 #if defined ___NOCASE_FS___
678 StrUpr(e); 718 StrUpr(e);
679 StrUpr(n); 719 StrUpr(n);
680 #endif 720 #endif
681   721  
682 // search for substring 722 // search for substring
683 success = strstr(e, n) != NULL; 723 success = strstr(e, n) != NULL;
684   724  
685 return success; 725 return success;
686 #endif 726 #endif
687 } 727 }
688   728  
689   729  
690   730  
691   731  
692   732  
693   733