HuntnGather – Diff between revs 19 and 22
?pathlinks?
Rev 19 | Rev 22 | |||
---|---|---|---|---|
Line 13... | Line 13... | |||
13 | #include <sys/syslimits.h> |
13 | #include <sys/syslimits.h> |
|
Line 14... | Line 14... | |||
14 | |
14 | |
|
15 | #include <proto/dos.h> |
15 | #include <proto/dos.h> |
|
Line -... | Line 16... | |||
- | 16 | #include <proto/exec.h> |
||
- | 17 | |
||
- | 18 | #if defined ___AsyncIO___ |
||
- | 19 | #include <asyncio.h> |
||
16 | #include <proto/exec.h> |
20 | #endif |
|
Line 17... | Line 21... | |||
17 | |
21 | |
|
18 | #include "StringStack.h" |
22 | #include "StringStack.h" |
|
19 | |
23 | |
|
Line 20... | Line 24... | |||
20 | #if !defined ___HAVE_GETOPT___ |
24 | #if !defined ___HAVE_GETOPT___ |
|
Line 21... | Line 25... | |||
21 | #include "getopt.h" |
25 | #include "getopt.h" |
|
22 | #endif |
26 | #endif |
|
23 | |
27 | |
|
24 | #define PROGRAM_VERSION "1.7.2" |
28 | #define PROGRAM_VERSION "1.7.3" |
|
Line 37... | Line 41... | |||
37 | |
41 | |
|
38 | #if !defined FALSE |
42 | #if !defined FALSE |
|
39 | #define FALSE 0; |
43 | #define FALSE 0; |
|
Line -... | Line 44... | |||
- | 44 | #endif |
||
40 | #endif |
45 | |
|
41 | |
46 | #define ASYNC_BUF 8192 |
|
42 | #define MAX_MEM 262144 |
47 | #define MAX_MEM 262144 |
|
43 | #define NAME_BUF 32 |
48 | #define NAME_BUF 32 |
|
44 | #define PATH_BUF 128 |
49 | #define PATH_BUF 128 |
|
Line 67... | Line 72... | |||
67 | /* |
72 | /* |
|
68 | * |
73 | * |
|
69 | * Sorts a database file lexicographically. |
74 | * Sorts a database file lexicographically. |
|
70 | */ |
75 | */ |
|
71 | void SortDatabase(char *dbFile) { |
76 | void SortDatabase(char *dbFile) { |
|
- | 77 | #if defined ___AsyncIO___ |
||
- | 78 | struct AsyncFile *fp; |
||
- | 79 | LONG c; |
||
- | 80 | #else |
||
72 | FILE *fp; |
81 | FILE *fp; |
|
- | 82 | char c; |
||
- | 83 | #endif |
||
73 | char *name = NULL; |
84 | char *name = NULL; |
|
74 | char *path = NULL; |
85 | char *path = NULL; |
|
75 | char **database; |
86 | char **database; |
|
76 | char c; |
- | ||
77 | int i; |
87 | int i; |
|
78 | int side; |
88 | int side; |
|
79 | unsigned int line; |
89 | unsigned int line; |
|
80 | int name_size; |
90 | int name_size; |
|
81 | int path_size; |
91 | int path_size; |
|
Line 82... | Line 92... | |||
82 | |
92 | |
|
- | 93 | // Open database file for reading. |
||
- | 94 | #if defined ___AsyncIO___ |
||
- | 95 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
83 | // Open database file for reading. |
96 | #else |
|
- | 97 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
84 | if((fp = fopen(dbFile, "r")) == NULL) { |
98 | #endif |
|
85 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
99 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
86 | return; |
100 | return; |
|
Line 87... | Line 101... | |||
87 | } |
101 | } |
|
88 | |
102 | |
|
89 | database = malloc(sizeof(*database)); |
103 | database = malloc(sizeof(*database)); |
|
90 | name_size = NAME_BUF; |
104 | name_size = NAME_BUF; |
|
91 | name = malloc(name_size * sizeof(*name)); |
105 | name = malloc(name_size * sizeof(*name)); |
|
- | 106 | path_size = PATH_BUF; |
||
92 | path_size = PATH_BUF; |
107 | path = malloc(path_size * sizeof(*path)); |
|
93 | path = malloc(path_size * sizeof(*path)); |
108 | |
|
94 | line = 0; |
109 | line = 0; |
|
Line 95... | Line 110... | |||
95 | side = 0; |
110 | side = 0; |
|
96 | i = 0; |
111 | i = 0; |
|
97 | |
112 | |
|
- | 113 | if(verbose) { |
||
- | 114 | fprintf(stdout, "Sorting database: '%s'\n", dbFile); |
||
98 | if(verbose) { |
115 | } |
|
99 | fprintf(stdout, "Sorting database: '%s'\n", dbFile); |
116 | #if defined ___AsyncIO___ |
|
- | 117 | while(run && (c = ReadCharAsync(fp)) != -1) { |
||
100 | } |
118 | #else |
|
101 | |
119 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
102 | while(run && fscanf(fp, "%c", &c) == 1) { |
120 | #endif |
|
103 | #if defined ___AmigaOS___ |
121 | #if defined ___AmigaOS___ |
|
104 | // Check if CTRL+C was pressed and abort the program. |
122 | // Check if CTRL+C was pressed and abort the program. |
|
Line 148... | Line 166... | |||
148 | //path = realloc(path, (i + 1 + 1) * sizeof(char)); |
166 | //path = realloc(path, (i + 1 + 1) * sizeof(char)); |
|
149 | path[i] = c; |
167 | path[i] = c; |
|
150 | path[i + 1] = '\0'; |
168 | path[i + 1] = '\0'; |
|
151 | break; |
169 | break; |
|
152 | default: |
170 | default: |
|
153 | fprintf(stderr, "Database corrupted.\n"); |
171 | fprintf(stderr, "Database corrupted: %d\n", side); |
|
154 | break; |
172 | break; |
|
155 | } |
173 | } |
|
156 | ++i; |
174 | ++i; |
|
157 | break; |
175 | break; |
|
158 | } |
176 | } |
|
159 | } |
177 | } |
|
Line -... | Line 178... | |||
- | 178 | |
||
- | 179 | #if defined ___AsyncIO___ |
||
- | 180 | CloseAsync(fp); |
||
160 | |
181 | #else |
|
- | 182 | fclose(fp); |
||
Line 161... | Line 183... | |||
161 | fclose(fp); |
183 | #endif |
|
162 | |
184 | |
|
Line 163... | Line 185... | |||
163 | // Sort the database. |
185 | // Sort the database. |
|
- | 186 | qsort(database, line, sizeof(char *), compare); |
||
- | 187 | |
||
- | 188 | // Write the database lines back to the database. |
||
164 | qsort(database, line, sizeof(char *), compare); |
189 | #if defined ___AsyncIO___ |
|
- | 190 | if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { |
||
165 | |
191 | #else |
|
166 | // Write the database lines back to the database. |
192 | if((fp = fopen(dbFile, "w+")) == NULL) { |
|
167 | if((fp = fopen(dbFile, "w+")) == NULL) { |
193 | #endif |
|
Line 168... | Line 194... | |||
168 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
194 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
|
- | 195 | return; |
||
- | 196 | } |
||
- | 197 | |
||
- | 198 | for(i = 0; i < line; ++i) { |
||
169 | return; |
199 | #if defined ___AsyncIO___ |
|
- | 200 | WriteAsync(fp, database[i], (LONG)(strlen(database[i]) * sizeof(char))); |
||
170 | } |
201 | WriteAsync(fp, "\n", 1 * sizeof(char)); |
|
Line -... | Line 202... | |||
- | 202 | #else |
||
171 | |
203 | fprintf(fp, "%s\n", database[i]); |
|
- | 204 | #endif |
||
172 | for(i = 0; i < line; ++i) { |
205 | } |
|
- | 206 | |
||
- | 207 | #if defined ___AsyncIO___ |
||
- | 208 | CloseAsync(fp); |
||
173 | fprintf(fp, "%s\n", database[i]); |
209 | #else |
|
Line 174... | Line 210... | |||
174 | } |
210 | fclose(fp); |
|
175 | |
211 | #endif |
|
176 | free(database); |
212 | |
|
177 | fclose(fp); |
213 | free(database); |
|
178 | } |
214 | } |
|
- | 215 | |
||
- | 216 | /* |
||
- | 217 | * |
||
179 | |
218 | * Updates a database file "dbFile". |
|
- | 219 | */ |
||
180 | /* |
220 | void UpdateDatabase(char *dbFile, stringStack *dirStack, stats *stats) { |
|
181 | * |
221 | #if defined ___AsyncIO___ |
|
182 | * Updates a database file "dbFile". |
222 | struct AsyncFile *fp; |
|
183 | */ |
223 | #else |
|
184 | void UpdateDatabase(char *dbFile, stringStack *dirStack, stats *stats) { |
224 | FILE *fp; |
|
185 | FILE *fp; |
225 | #endif |
|
Line -... | Line 226... | |||
- | 226 | DIR *dir; |
||
- | 227 | struct dirent *dirEntry; |
||
- | 228 | struct stat dirStat; |
||
186 | DIR *dir; |
229 | unsigned int size; |
|
- | 230 | char *path; |
||
187 | struct dirent *dirEntry; |
231 | char *subPath; |
|
188 | struct stat dirStat; |
232 | |
|
189 | unsigned int size; |
233 | #if defined ___AsyncIO___ |
|
Line 190... | Line 234... | |||
190 | char *path; |
234 | if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
Line 250... | Line 294... | |||
250 | |
294 | |
|
251 | #if defined ___NOCASE_FS___ |
295 | #if defined ___NOCASE_FS___ |
|
252 | strupr(dirEntry->d_name); |
296 | strupr(dirEntry->d_name); |
|
Line -... | Line 297... | |||
- | 297 | #endif |
||
- | 298 | |
||
- | 299 | #if defined ___AsyncIO___ |
||
- | 300 | WriteAsync(fp, dirEntry->d_name, (LONG)(strlen(dirEntry->d_name) * sizeof(char))); |
||
- | 301 | WriteAsync(fp, "\t", 1 * sizeof(char)); |
||
- | 302 | WriteAsync(fp, subPath, (LONG)(strlen(subPath) * sizeof(char))); |
||
253 | #endif |
303 | WriteAsync(fp, "\n", 1 * sizeof(char)); |
|
254 | |
304 | #else |
|
255 | fprintf(fp, "%s\t%s\n", dirEntry->d_name, subPath); |
305 | fprintf(fp, "%s\t%s\n", dirEntry->d_name, subPath); |
|
Line 256... | Line 306... | |||
256 | |
306 | #endif |
|
257 | ++stats->files; |
307 | ++stats->files; |
|
258 | |
308 | |
|
Line 272... | Line 322... | |||
272 | |
322 | |
|
273 | if(verbose) { |
323 | if(verbose) { |
|
274 | fprintf(stdout, "\n"); |
324 | fprintf(stdout, "\n"); |
|
Line -... | Line 325... | |||
- | 325 | } |
||
- | 326 | |
||
- | 327 | #if defined ___AsyncIO___ |
||
275 | } |
328 | CloseAsync(fp); |
|
- | 329 | #else |
||
Line 276... | Line 330... | |||
276 | |
330 | fclose(fp); |
|
Line 277... | Line 331... | |||
277 | fclose(fp); |
331 | #endif |
|
278 | |
332 | |
|
Line 302... | Line 356... | |||
302 | /* |
356 | /* |
|
303 | * |
357 | * |
|
304 | * Counts the lines in a database file "dbFile". |
358 | * Counts the lines in a database file "dbFile". |
|
305 | */ |
359 | */ |
|
306 | int CountDatabaseLines(char *dbFile) { |
360 | int CountDatabaseLines(char *dbFile) { |
|
- | 361 | #if defined ___AsyncIO___ |
||
- | 362 | struct AsyncFile *fp; |
||
- | 363 | LONG c; |
||
- | 364 | #else |
||
307 | FILE *fp; |
365 | FILE *fp; |
|
308 | int lines; |
- | ||
309 | char c; |
366 | char c; |
|
- | 367 | #endif |
||
- | 368 | int lines; |
||
Line -... | Line 369... | |||
- | 369 | |
||
- | 370 | #if defined ___AsyncIO___ |
||
- | 371 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
310 | |
372 | #else |
|
- | 373 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
311 | if((fp = fopen(dbFile, "r")) == NULL) { |
374 | #endif |
|
312 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
- | ||
313 | fclose(fp); |
375 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
314 | return 0; |
376 | return 0; |
|
Line 315... | Line 377... | |||
315 | } |
377 | } |
|
- | 378 | |
||
- | 379 | lines = 0; |
||
- | 380 | #if defined ___AsyncIO___ |
||
316 | |
381 | while((c = ReadCharAsync(fp)) != -1) { |
|
- | 382 | #else |
||
317 | lines = 0; |
383 | while(fscanf(fp, "%c", &c) == 1) { |
|
318 | while(fscanf(fp, "%c", &c) == 1) { |
384 | #endif |
|
319 | switch(c) { |
385 | switch(c) { |
|
320 | case '\n': |
386 | case '\n': |
|
321 | ++lines; |
387 | ++lines; |
|
322 | break; |
388 | break; |
|
Line -... | Line 389... | |||
- | 389 | } |
||
- | 390 | } |
||
- | 391 | |
||
323 | } |
392 | #if defined ___AsyncIO___ |
|
- | 393 | CloseAsync(fp); |
||
Line 324... | Line 394... | |||
324 | } |
394 | #else |
|
325 | |
395 | fclose(fp); |
|
Line 326... | Line 396... | |||
326 | fclose(fp); |
396 | #endif |
|
Line 367... | Line 437... | |||
367 | /* |
437 | /* |
|
368 | * |
438 | * |
|
369 | * Writes lines from the database "dbFile" to temporary filenames "tmpNames". |
439 | * Writes lines from the database "dbFile" to temporary filenames "tmpNames". |
|
370 | */ |
440 | */ |
|
371 | void WriteTempFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { |
441 | void WriteTempFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { |
|
- | 442 | #if defined ___AsyncIO___ |
||
- | 443 | struct AsyncFile *fp, *tp; |
||
- | 444 | LONG c; |
||
- | 445 | #else |
||
372 | FILE *fp, *tp; |
446 | FILE *fp, *tp; |
|
373 | char c; |
447 | char c; |
|
- | 448 | #endif |
||
374 | int lines; |
449 | int lines; |
|
375 | int linesWritten; |
450 | int linesWritten; |
|
Line -... | Line 451... | |||
- | 451 | |
||
- | 452 | #if defined ___AsyncIO___ |
||
- | 453 | if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { |
||
376 | |
454 | #else |
|
- | 455 | if((fp = fopen(dbFile, "r")) == NULL) { |
||
377 | if((fp = fopen(dbFile, "r")) == NULL) { |
456 | #endif |
|
378 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
457 | fprintf(stderr, "Unable to open gather database for reading.\n"); |
|
379 | return; |
458 | return; |
|
Line -... | Line 459... | |||
- | 459 | } |
||
- | 460 | |
||
- | 461 | #if defined ___AsyncIO___ |
||
380 | } |
462 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
- | 463 | #else |
||
381 | |
464 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
|
382 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
- | ||
383 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
465 | #endif |
|
384 | fclose(fp); |
466 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
|
Line 385... | Line 467... | |||
385 | return; |
467 | return; |
|
386 | } |
468 | } |
|
387 | |
469 | |
|
Line 388... | Line 470... | |||
388 | if(verbose) { |
470 | if(verbose) { |
|
389 | fprintf(stdout, "Writing to temporary files.\r"); |
471 | fprintf(stdout, "Writing to temporary files.\r"); |
|
- | 472 | } |
||
- | 473 | |
||
- | 474 | linesWritten = 0; |
||
390 | } |
475 | lines = 0; |
|
- | 476 | #if defined ___AsyncIO___ |
||
391 | |
477 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
392 | linesWritten = 0; |
478 | #else |
|
393 | lines = 0; |
479 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
394 | while(run && fscanf(fp, "%c", &c) == 1) { |
480 | #endif |
|
395 | #if defined ___AmigaOS___ |
481 | #if defined ___AmigaOS___ |
|
Line 406... | Line 492... | |||
406 | if(verbose) { |
492 | if(verbose) { |
|
407 | fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)linesWritten / total) * 100.0)); |
493 | fprintf(stdout, "Writing to temporary files: %d%%.\r", (int)(((float)linesWritten / total) * 100.0)); |
|
408 | } |
494 | } |
|
Line 409... | Line 495... | |||
409 | |
495 | |
|
- | 496 | // Write the newline character back. |
||
- | 497 | #if defined ___AsyncIO___ |
||
- | 498 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
||
410 | // Write the newline character back. |
499 | #else |
|
- | 500 | if(fprintf(tp, "%c", c) != 1) { |
||
411 | if(fprintf(tp, "%c", c) != 1) { |
501 | #endif |
|
- | 502 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
||
- | 503 | #if defined ___AsyncIO___ |
||
- | 504 | CloseAsync(tp); |
||
- | 505 | CloseAsync(fp); |
||
412 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
506 | #else |
|
413 | fclose(tp); |
507 | fclose(tp); |
|
- | 508 | fclose(fp); |
||
414 | fclose(fp); |
509 | #endif |
|
415 | return; |
510 | return; |
|
416 | } |
511 | } |
|
417 | // Switch to the next temporary file. |
512 | // Switch to the next temporary file. |
|
418 | if(++lines >= tmpLines) { |
513 | if(++lines >= tmpLines) { |
|
419 | // If there are no temporary files left then run till the end. |
514 | // If there are no temporary files left then run till the end. |
|
420 | if(tmpFiles - 1 < 0) { |
515 | if(tmpFiles - 1 < 0) { |
|
421 | break; |
516 | break; |
|
Line 422... | Line 517... | |||
422 | } |
517 | } |
|
- | 518 | |
||
- | 519 | // Close the previous temporary file and write to the next temporary file. |
||
- | 520 | #if defined ___AsyncIO___ |
||
- | 521 | CloseAsync(tp); |
||
423 | |
522 | if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { |
|
424 | // Close the previous temporary file and write to the next temporary file. |
523 | #else |
|
- | 524 | fclose(tp); |
||
425 | fclose(tp); |
525 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
|
- | 526 | #endif |
||
- | 527 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
||
- | 528 | #if defined ___AsyncIO___ |
||
- | 529 | CloseAsync(tp); |
||
426 | if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { |
530 | CloseAsync(fp); |
|
427 | fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); |
531 | #else |
|
- | 532 | fclose(tp); |
||
428 | fclose(tp); |
533 | fclose(fp); |
|
429 | fclose(fp); |
534 | #endif |
|
430 | } |
535 | } |
|
431 | lines = 0; |
536 | lines = 0; |
|
432 | break; |
537 | break; |
|
433 | } |
538 | } |
|
- | 539 | break; |
||
- | 540 | default: |
||
- | 541 | #if defined ___AsyncIO___ |
||
434 | break; |
542 | if(WriteCharAsync(tp, (UBYTE)c) != 1) { |
|
- | 543 | #else |
||
435 | default: |
544 | if(fprintf(tp, "%c", c) != 1) { |
|
- | 545 | #endif |
||
- | 546 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
||
- | 547 | #if defined ___AsyncIO___ |
||
- | 548 | CloseAsync(tp); |
||
436 | if(fprintf(tp, "%c", c) != 1) { |
549 | CloseAsync(fp); |
|
437 | fprintf(stderr, "Unable to write to temporary file '%s'.\n", tmpNames[tmpFiles]); |
550 | #else |
|
- | 551 | fclose(tp); |
||
438 | fclose(tp); |
552 | fclose(fp); |
|
439 | fclose(fp); |
553 | #endif |
|
440 | return; |
554 | return; |
|
441 | } |
555 | } |
|
442 | break; |
556 | break; |
|
Line 443... | Line 557... | |||
443 | } |
557 | } |
|
Line -... | Line 558... | |||
- | 558 | } |
||
- | 559 | |
||
- | 560 | fprintf(stdout, "\n"); |
||
- | 561 | |
||
444 | } |
562 | #if defined ___AsyncIO___ |
|
445 | |
563 | CloseAsync(tp); |
|
- | 564 | CloseAsync(fp); |
||
446 | fprintf(stdout, "\n"); |
565 | #else |
|
Line 447... | Line 566... | |||
447 | |
566 | fclose(tp); |
|
448 | fclose(tp); |
567 | fclose(fp); |
|
449 | fclose(fp); |
568 | #endif |
|
450 | } |
569 | } |
|
- | 570 | |
||
- | 571 | /* |
||
- | 572 | * |
||
- | 573 | * Skips a line in a database file "fp". |
||
- | 574 | */ |
||
- | 575 | |
||
451 | |
576 | #if defined ___AsyncIO___ |
|
452 | /* |
577 | void SkipDatabaseLine(struct AsyncFile *fp) { |
|
453 | * |
- | ||
454 | * Skips a line in a database file "fp". |
578 | LONG c; |
|
- | 579 | while((c = ReadCharAsync(fp)) != -1) { |
||
- | 580 | #else |
||
455 | */ |
581 | void SkipDatabaseLine(FILE *fp) { |
|
456 | void SkipDatabaseLine(FILE *fp) { |
582 | char c; |
|
457 | char c; |
583 | while(fscanf(fp, "%c", &c) == 1) { |
|
458 | |
584 | #endif |
|
459 | while(fscanf(fp, "%c", &c) == 1) { |
- | ||
460 | if(c == '\n') { |
- | ||
461 | break; |
585 | switch(c) { |
|
Line 462... | Line 586... | |||
462 | } |
586 | case '\n': |
|
463 | } |
587 | return; |
|
464 | |
588 | } |
|
465 | return; |
589 | } |
|
- | 590 | } |
||
- | 591 | |
||
- | 592 | /* |
||
- | 593 | * |
||
466 | } |
594 | * Reads a line from the database file "fp". |
|
467 | |
595 | */ |
|
- | 596 | #if defined ___AsyncIO___ |
||
468 | /* |
597 | char *ReadDatabaseLine(struct AsyncFile *fp) { |
|
469 | * |
598 | LONG c; |
|
470 | * Reads a line from the database file "fp". |
599 | #else |
|
Line 471... | Line 600... | |||
471 | */ |
600 | char *ReadDatabaseLine(FILE *fp) { |
|
472 | char *ReadDatabaseLine(FILE *fp) { |
601 | char c; |
|
Line 473... | Line 602... | |||
473 | char c; |
602 | #endif |
|
- | 603 | char *line; |
||
- | 604 | int line_size; |
||
- | 605 | int i; |
||
474 | char *line; |
606 | |
|
- | 607 | line_size = LINE_BUF; |
||
475 | int line_size; |
608 | line = malloc(line_size * sizeof(*line)); |
|
476 | int i; |
609 | |
|
477 | |
610 | i = 0; |
|
478 | line_size = LINE_BUF; |
611 | #if defined ___AsyncIO___ |
|
479 | line = malloc(line_size * sizeof(*line)); |
612 | while(run && (c = ReadCharAsync(fp)) != -1) { |
|
480 | |
613 | #else |
|
481 | i = 0; |
614 | while(run && fscanf(fp, "%c", &c) == 1) { |
|
482 | while(run && fscanf(fp, "%c", &c) == 1) { |
615 | #endif |
|
483 | #if defined ___AmigaOS___ |
616 | #if defined ___AmigaOS___ |
|
- | 617 | // Check if CTRL+C was pressed and abort the program. |
||
- | 618 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
||
- | 619 | run = FALSE; |
||
- | 620 | } |
||
- | 621 | #endif |
||
- | 622 | switch(c) { |
||
484 | // Check if CTRL+C was pressed and abort the program. |
623 | case '\n': |
|
- | 624 | // Rewind the file by the number of read characters. |
||
485 | if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { |
625 | #if defined ___AsyncIO___ |
|
486 | run = FALSE; |
626 | if(SeekAsync(fp, -(i + 1), MODE_CURRENT) == -1) { |
|
487 | } |
627 | fprintf(stderr, "Could not seek in file.\n"); |
|
488 | #endif |
628 | return NULL; |
|
489 | switch(c) { |
629 | } |
|
Line 510... | Line 650... | |||
510 | /* |
650 | /* |
|
511 | * |
651 | * |
|
512 | * Merges temporary files "tmpNames" into a database "dbFile". |
652 | * Merges temporary files "tmpNames" into a database "dbFile". |
|
513 | */ |
653 | */ |
|
514 | void MergeDatabase(char *dbFile, char **tmpNames, int files, int lines) { |
654 | void MergeDatabase(char *dbFile, char **tmpNames, int files, int lines) { |
|
- | 655 | #if defined ___AsyncIO___ |
||
- | 656 | struct AsyncFile *fp; |
||
- | 657 | struct AsyncFile **tp; |
||
- | 658 | #else |
||
515 | FILE *fp; |
659 | FILE *fp; |
|
516 | FILE **tp; |
660 | FILE **tp; |
|
- | 661 | #endif |
||
517 | int i; |
662 | int i; |
|
518 | int j; |
663 | int j; |
|
519 | char *tmp; |
664 | char *tmp; |
|
520 | char *min; |
665 | char *min; |
|
521 | int count; |
666 | int count; |
|
Line -... | Line 667... | |||
- | 667 | |
||
- | 668 | #if defined ___AsyncIO___ |
||
- | 669 | if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { |
||
522 | |
670 | #else |
|
- | 671 | if((fp = fopen(dbFile, "w+")) == NULL) { |
||
523 | if((fp = fopen(dbFile, "w+")) == NULL) { |
672 | #endif |
|
524 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
673 | fprintf(stderr, "Unable to open gather database for writing.\n"); |
|
525 | return; |
674 | return; |
|
Line 526... | Line 675... | |||
526 | } |
675 | } |
|
527 | |
676 | |
|
Line 528... | Line 677... | |||
528 | // Allocate as many file pointers as temporary files. |
677 | // Allocate as many file pointers as temporary files. |
|
529 | tp = malloc(files * sizeof(*tp)); |
678 | tp = malloc(files * sizeof(*tp)); |
|
- | 679 | |
||
- | 680 | // Open all temporary files for reading. |
||
- | 681 | for(i = 0; i < files; ++i) { |
||
530 | |
682 | #if defined ___AsyncIO___ |
|
- | 683 | if((tp[i] = OpenAsync(tmpNames[i], MODE_READ, ASYNC_BUF)) == NULL) { |
||
531 | // Open all temporary files for reading. |
684 | #else |
|
532 | for(i = 0; i < files; ++i) { |
685 | if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { |
|
533 | if((tp[i] = fopen(tmpNames[i], "r")) == NULL) { |
686 | #endif |
|
534 | fprintf(stderr, "Unable to open temporary file '%s' for reading.\n", tmpNames[i]); |
687 | fprintf(stderr, "Unable to open temporary file '%s' for reading.\n", tmpNames[i]); |
|
- | 688 | // Close all temporary files. |
||
- | 689 | --i; |
||
- | 690 | while(i >= 0) { |
||
535 | // Close all temporary files. |
691 | #if defined ___AsyncIO___ |
|
- | 692 | CloseAsync(tp[i]); |
||
536 | --i; |
693 | #else |
|
537 | while(i >= 0) { |
694 | fclose(tp[i]); |
|
538 | fclose(tp[i]); |
695 | #endif |
|
539 | } |
696 | } |
|
Line 583... | Line 740... | |||
583 | // Forward the file where the smallest line was found. |
740 | // Forward the file where the smallest line was found. |
|
584 | SkipDatabaseLine(tp[j]); |
741 | SkipDatabaseLine(tp[j]); |
|
Line 585... | Line 742... | |||
585 | |
742 | |
|
586 | // Write the smallest line. |
743 | // Write the smallest line. |
|
- | 744 | if(min != NULL) { |
||
- | 745 | #if defined ___AsyncIO___ |
||
- | 746 | WriteAsync(fp, min, (LONG)(strlen(min) * sizeof(char))); |
||
- | 747 | WriteAsync(fp, "\n", 1 * sizeof(char)); |
||
587 | if(min != NULL) { |
748 | #else |
|
- | 749 | fprintf(fp, "%s\n", min); |
||
588 | fprintf(fp, "%s\n", min); |
750 | #endif |
|
589 | free(min); |
751 | free(min); |
|
590 | } |
752 | } |
|
Line 591... | Line 753... | |||
591 | } |
753 | } |
|
592 | |
754 | |
|
593 | // Write out any remaining contents from the temporary files. |
755 | // Write out any remaining contents from the temporary files. |
|
594 | for(i = 0; i < files; ++i) { |
756 | for(i = 0; i < files; ++i) { |
|
595 | tmp = ReadDatabaseLine(tp[i]); |
757 | tmp = ReadDatabaseLine(tp[i]); |
|
596 | if(tmp == NULL) { |
758 | if(tmp == NULL) { |
|
- | 759 | continue; |
||
- | 760 | } |
||
- | 761 | #if defined ___AsyncIO___ |
||
- | 762 | WriteAsync(fp, tmp, (LONG)(strlen(tmp) * sizeof(char))); |
||
597 | continue; |
763 | WriteAsync(fp, "\n", 1 * sizeof(char)); |
|
- | 764 | #else |
||
598 | } |
765 | fprintf(fp, "%s\n", tmp); |
|
599 | fprintf(fp, "%s\n", tmp); |
766 | #endif |
|
Line 600... | Line 767... | |||
600 | free(tmp); |
767 | free(tmp); |
|
601 | } |
768 | } |
|
- | 769 | |
||
- | 770 | // Close and delete all temporary files. |
||
- | 771 | for(i = 0; i < files; ++i) { |
||
602 | |
772 | #if defined ___AsyncIO___ |
|
- | 773 | CloseAsync(tp[i]); |
||
603 | // Close and delete all temporary files. |
774 | #else |
|
604 | for(i = 0; i < files; ++i) { |
775 | fclose(tp[i]); |
|
605 | fclose(tp[i]); |
776 | #endif |
|
Line 606... | Line 777... | |||
606 | // Delete temporary file. |
777 | // Delete temporary file. |
|
607 | remove(tmpNames[i]); |
778 | remove(tmpNames[i]); |
|
608 | } |
779 | } |
|
Line -... | Line 780... | |||
- | 780 | |
||
- | 781 | if(verbose) { |
||
- | 782 | fprintf(stdout, "\n"); |
||
609 | |
783 | } |
|
- | 784 | |
||
610 | if(verbose) { |
785 | #if defined ___AsyncIO___ |
|
Line 611... | Line 786... | |||
611 | fprintf(stdout, "\n"); |
786 | CloseAsync(fp); |
|
612 | } |
787 | #else |
|
613 | |
788 | fclose(fp); |