HuntnGather – Diff between revs 22 and 23

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 22 Rev 23
Line 109... Line 109...
109 line = 0; 109 line = 0;
110 side = 0; 110 side = 0;
111 i = 0; 111 i = 0;
Line 112... Line 112...
112   112  
113 if(verbose) { 113 if(verbose) {
114 fprintf(stdout, "Sorting database: '%s'\n", dbFile); 114 fprintf(stdout, "Sorting temporary database file: '%s'\n", dbFile);
115 } 115 }
116 #if defined ___AsyncIO___ 116 #if defined ___AsyncIO___
117 while(run && (c = ReadCharAsync(fp)) != -1) { 117 while(run && (c = ReadCharAsync(fp)) != -1) {
118 #else 118 #else
Line 185... Line 185...
185 // Sort the database. 185 // Sort the database.
186 qsort(database, line, sizeof(char *), compare); 186 qsort(database, line, sizeof(char *), compare);
Line 187... Line 187...
187   187  
188 // Write the database lines back to the database. 188 // Write the database lines back to the database.
189 #if defined ___AsyncIO___ 189 #if defined ___AsyncIO___
190 if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { 190 if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) {
191 #else 191 #else
192 if((fp = fopen(dbFile, "w+")) == NULL) { 192 if((fp = fopen(dbFile, "w")) == NULL) {
193 #endif 193 #endif
194 fprintf(stderr, "Unable to open gather database for writing.\n"); 194 fprintf(stderr, "Unable to open gather database for writing.\n");
195 return; 195 return;
Line 196... Line 196...
196 } 196 }
-   197  
-   198 for(i = 0; i < line; ++i) {
-   199 #if defined ___AmigaOS___
-   200 // Check if CTRL+C was pressed and abort the program.
-   201 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
-   202 run = FALSE;
-   203 continue;
197   204 }
198 for(i = 0; i < line; ++i) { 205 #endif
199 #if defined ___AsyncIO___ 206 #if defined ___AsyncIO___
200 WriteAsync(fp, database[i], (LONG)(strlen(database[i]) * sizeof(char))); 207 WriteAsync(fp, database[i], (LONG)(strlen(database[i]) * sizeof(char)));
201 WriteAsync(fp, "\n", 1 * sizeof(char)); 208 WriteAsync(fp, "\n", 1 * sizeof(char));
Line 229... Line 236...
229 unsigned int size; 236 unsigned int size;
230 char *path; 237 char *path;
231 char *subPath; 238 char *subPath;
Line 232... Line 239...
232   239  
233 #if defined ___AsyncIO___ 240 #if defined ___AsyncIO___
234 if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { 241 if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) {
235 #else 242 #else
236 if((fp = fopen(dbFile, "w+")) == NULL) { 243 if((fp = fopen(dbFile, "w")) == NULL) {
237 #endif 244 #endif
238 fprintf(stderr, "Unable to open gather database for writing.\n"); 245 fprintf(stderr, "Unable to open gather database for writing.\n");
239 return; 246 return;
Line 240... Line 247...
240 } 247 }
241   248  
242 while(run && !stringStackIsEmpty(dirStack)) { 249 while(run && !stringStackIsEmpty(dirStack)) {
243 #if defined ___AmigaOS___ 250 #if defined ___AmigaOS___
244 // Check if CTRL+C was pressed and abort the program. 251 // Check if CTRL+C was pressed and abort the program.
-   252 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
245 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 253 run = FALSE;
246 run = FALSE; 254 continue;
247 } 255 }
248 #endif 256 #endif
249 if((path = stringStackPop(dirStack)) == NULL) { 257 if((path = stringStackPop(dirStack)) == NULL) {
Line 257... Line 265...
257 while(run && (dirEntry = readdir(dir)) != NULL) { 265 while(run && (dirEntry = readdir(dir)) != NULL) {
258 #if defined ___AmigaOS___ 266 #if defined ___AmigaOS___
259 // Check if CTRL+C was pressed and abort the program. 267 // Check if CTRL+C was pressed and abort the program.
260 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 268 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
261 run = FALSE; 269 run = FALSE;
-   270 continue;
262 } 271 }
263 #endif 272 #endif
264 size = sizeof(path) + sizeof(dirEntry->d_name) + 1; 273 size = sizeof(path) + sizeof(dirEntry->d_name) + 1;
265 switch(path[strlen(path) - 1]) { 274 switch(path[strlen(path) - 1]) {
266 case '/': 275 case '/':
Line 332... Line 341...
332   341  
Line 333... Line 342...
333 } 342 }
334   343  
335 /* 344 /*
336 * 345 *
337 * Gets the size of a database "dbFle". 346 * Gets the size of a file "dbFle".
-   347 */
-   348 int GetFileSize(char *dbFile) {
-   349 #if defined ___AsyncIO___
-   350 struct AsyncFile *fp;
338 */ 351 LONG size;
339 int GetDatabaseSize(char *dbFile) { 352 #else
-   353 FILE *fp;
Line -... Line 354...
-   354 int size;
-   355 #endif
-   356  
340 FILE *fp; 357 #if defined ___AsyncIO___
-   358 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
341 int size; 359 #else
342   -  
343 if((fp = fopen(dbFile, "r")) == NULL) { 360 if((fp = fopen(dbFile, "r")) == NULL) {
344 fprintf(stderr, "Unable to open gather database for reading.\n"); 361 #endif
Line -... Line 362...
-   362 fprintf(stderr, "Unable to open gather database for reading.\n");
-   363 return 0;
-   364 }
-   365  
345 fclose(fp); 366 #if defined ___AsyncIO___
346 return 0; 367 SeekAsync(fp, 0, MODE_END);
-   368 size = SeekAsync(fp, 0, MODE_CURRENT);
Line -... Line 369...
-   369 #else
-   370 fseek(fp, 0L, SEEK_END);
-   371 size = ftell(fp);
347 } 372 #endif
-   373  
-   374 #if defined ___AsyncIO___
348   375 CloseAsync(fp);
349 fseek(fp, 0L, SEEK_END); 376 #else
Line 350... Line 377...
350 size = ftell(fp); 377 fclose(fp);
351   378 #endif
352 fclose(fp); 379  
353 return size; 380 return size;
354 } 381 }
355   382  
356 /* 383 /*
357 * 384 *
358 * Counts the lines in a database file "dbFile". 385 * Counts the lines of a file.
359 */ 386 */
Line 376... Line 403...
376 return 0; 403 return 0;
377 } 404 }
Line 378... Line 405...
378   405  
379 lines = 0; 406 lines = 0;
380 #if defined ___AsyncIO___ 407 #if defined ___AsyncIO___
381 while((c = ReadCharAsync(fp)) != -1) { 408 while(run && (c = ReadCharAsync(fp)) != -1) {
382 #else 409 #else
-   410 while(run && fscanf(fp, "%c", &c) == 1) {
-   411 #endif
-   412 #if defined ___AmigaOS___
-   413 // Check if CTRL+C was pressed and abort the program.
-   414 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
-   415 run = FALSE;
-   416 continue;
383 while(fscanf(fp, "%c", &c) == 1) { 417 }
384 #endif 418 #endif
385 switch(c) { 419 switch(c) {
386 case '\n': 420 case '\n':
387 ++lines; 421 ++lines;
Line 400... Line 434...
400   434  
401 /* 435 /*
402 * 436 *
403 * Creates "files" temporary filenames. 437 * Creates "files" temporary filenames.
404 */ 438 */
405 char **CreateTempFiles(int files) { 439 char **CreateTemporaryFiles(int files) {
406 char **tmpNames; 440 char **tmpNames;
Line 407... Line 441...
407 int count; 441 int count;
Line 408... Line 442...
408   442  
409 tmpNames = malloc(files * sizeof(char *)); 443 tmpNames = malloc(files * sizeof(*tmpNames));
410   444  
Line 411... Line 445...
411 if(verbose) { 445 if(verbose) {
412 fprintf(stdout, "Creating temporary files.\r"); 446 fprintf(stdout, "Creating temporary files.\r");
413 } 447 }
414   448  
415 count = files; 449 count = files;
416 while(--count > -1) { 450 while(run && --count > -1) {
-   451 #if defined ___AmigaOS___
417 #if defined ___AmigaOS___ 452 // Check if CTRL+C was pressed and abort the program.
418 // Check if CTRL+C was pressed and abort the program. 453 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
419 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 454 run = FALSE;
Line 420... Line 455...
420 run = FALSE; 455 continue;
Line 436... Line 471...
436   471  
437 /* 472 /*
438 * 473 *
439 * Writes lines from the database "dbFile" to temporary filenames "tmpNames". 474 * Writes lines from the database "dbFile" to temporary filenames "tmpNames".
440 */ 475 */
441 void WriteTempFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) { 476 void WriteTemporaryFiles(char *dbFile, char **tmpNames, int tmpFiles, int tmpLines, int total) {
442 #if defined ___AsyncIO___ 477 #if defined ___AsyncIO___
443 struct AsyncFile *fp, *tp; 478 struct AsyncFile *fp, *tp;
444 LONG c; 479 LONG c;
445 #else 480 #else
Line 452... Line 487...
452 #if defined ___AsyncIO___ 487 #if defined ___AsyncIO___
453 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) { 488 if((fp = OpenAsync(dbFile, MODE_READ, ASYNC_BUF)) == NULL) {
454 #else 489 #else
455 if((fp = fopen(dbFile, "r")) == NULL) { 490 if((fp = fopen(dbFile, "r")) == NULL) {
456 #endif 491 #endif
457 fprintf(stderr, "Unable to open gather database for reading.\n"); 492 fprintf(stderr, "Unable to open gather database '%s' for reading.\n", dbFile);
458 return; 493 return;
459 } 494 }
Line 460... Line 495...
460   495  
461 #if defined ___AsyncIO___ 496 #if defined ___AsyncIO___
462 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { 497 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) {
463 #else 498 #else
464 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { 499 if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) {
465 #endif 500 #endif
466 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); 501 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
467 return; 502 return;
Line 480... Line 515...
480 #endif 515 #endif
481 #if defined ___AmigaOS___ 516 #if defined ___AmigaOS___
482 // Check if CTRL+C was pressed and abort the program. 517 // Check if CTRL+C was pressed and abort the program.
483 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 518 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
484 run = FALSE; 519 run = FALSE;
-   520 continue;
485 } 521 }
486 #endif 522 #endif
487 switch(c) { 523 switch(c) {
488 case '\n': 524 case '\n':
489 // Increment the total written lines. 525 // Increment the total written lines.
Line 517... Line 553...
517 } 553 }
Line 518... Line 554...
518   554  
519 // Close the previous temporary file and write to the next temporary file. 555 // Close the previous temporary file and write to the next temporary file.
520 #if defined ___AsyncIO___ 556 #if defined ___AsyncIO___
521 CloseAsync(tp); 557 CloseAsync(tp);
522 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { 558 if((tp = OpenAsync(tmpNames[--tmpFiles], MODE_WRITE, ASYNC_BUF)) == NULL) {
523 #else 559 #else
524 fclose(tp); 560 fclose(tp);
525 if((tp = fopen(tmpNames[--tmpFiles], "w+")) == NULL) { 561 if((tp = fopen(tmpNames[--tmpFiles], "w")) == NULL) {
526 #endif 562 #endif
527 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]); 563 fprintf(stderr, "Unable to open temporary file '%s' for writing.\n", tmpNames[tmpFiles]);
528 #if defined ___AsyncIO___ 564 #if defined ___AsyncIO___
529 CloseAsync(tp); 565 CloseAsync(tp);
Line 572... Line 608...
572 * 608 *
573 * Skips a line in a database file "fp". 609 * Skips a line in a database file "fp".
574 */ 610 */
Line 575... Line 611...
575   611  
576 #if defined ___AsyncIO___ 612 #if defined ___AsyncIO___
577 void SkipDatabaseLine(struct AsyncFile *fp) { 613 void SkipLine(struct AsyncFile *fp) {
578 LONG c; 614 LONG c;
579 while((c = ReadCharAsync(fp)) != -1) { 615 while(run && (c = ReadCharAsync(fp)) != -1) {
580 #else 616 #else
581 void SkipDatabaseLine(FILE *fp) { 617 void SkipDatabaseLine(FILE *fp) {
582 char c; 618 char c;
-   619 while(run && fscanf(fp, "%c", &c) == 1) {
-   620 #endif
-   621 #if defined ___AmigaOS___
-   622 // Check if CTRL+C was pressed and abort the program.
-   623 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
-   624 run = FALSE;
-   625 continue;
583 while(fscanf(fp, "%c", &c) == 1) { 626 }
584 #endif 627 #endif
585 switch(c) { 628 switch(c) {
586 case '\n': 629 case '\n':
587 return; 630 return;
Line 592... Line 635...
592 /* 635 /*
593 * 636 *
594 * Reads a line from the database file "fp". 637 * Reads a line from the database file "fp".
595 */ 638 */
596 #if defined ___AsyncIO___ 639 #if defined ___AsyncIO___
597 char *ReadDatabaseLine(struct AsyncFile *fp) { 640 char *ReadLine(struct AsyncFile *fp) {
598 LONG c; 641 LONG c;
599 #else 642 #else
600 char *ReadDatabaseLine(FILE *fp) { 643 char *ReadLine(FILE *fp) {
601 char c; 644 char c;
602 #endif 645 #endif
603 char *line; 646 char *line;
604 int line_size; 647 int line_size;
605 int i; 648 int i;
Line 615... Line 658...
615 #endif 658 #endif
616 #if defined ___AmigaOS___ 659 #if defined ___AmigaOS___
617 // Check if CTRL+C was pressed and abort the program. 660 // Check if CTRL+C was pressed and abort the program.
618 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 661 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
619 run = FALSE; 662 run = FALSE;
-   663 continue;
620 } 664 }
621 #endif 665 #endif
622 switch(c) { 666 switch(c) {
623 case '\n': 667 case '\n':
624 // Rewind the file by the number of read characters. 668 // Rewind the file by the number of read characters.
Line 664... Line 708...
664 char *tmp; 708 char *tmp;
665 char *min; 709 char *min;
666 int count; 710 int count;
Line 667... Line 711...
667   711  
668 #if defined ___AsyncIO___ 712 #if defined ___AsyncIO___
669 if((fp = OpenAsync(dbFile, MODE_READ|MODE_WRITE, ASYNC_BUF)) == NULL) { 713 if((fp = OpenAsync(dbFile, MODE_WRITE, ASYNC_BUF)) == NULL) {
670 #else 714 #else
671 if((fp = fopen(dbFile, "w+")) == NULL) { 715 if((fp = fopen(dbFile, "w")) == NULL) {
672 #endif 716 #endif
673 fprintf(stderr, "Unable to open gather database for writing.\n"); 717 fprintf(stderr, "Unable to open gather database for writing.\n");
674 return; 718 return;
Line 707... Line 751...
707 while(run && --count > -1) { 751 while(run && --count > -1) {
708 #if defined ___AmigaOS___ 752 #if defined ___AmigaOS___
709 // Check if CTRL+C was pressed and abort the program. 753 // Check if CTRL+C was pressed and abort the program.
710 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) { 754 if(SetSignal(0L, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
711 run = FALSE; 755 run = FALSE;
-   756 continue;
712 } 757 }
713 #endif 758 #endif
714 // Find the smallest line in all temporary files. 759 // Find the smallest line in all temporary files.
715 if(verbose) { 760 if(verbose) {
716 fprintf(stdout, "Merging all database lines in temporary files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0)); 761 fprintf(stdout, "Merging all database lines in temporary files: %d%%.\r", 100 - (int)(((float)count / lines) * 100.0));
717 } 762 }
Line 718... Line 763...
718   763  
719 min = NULL; 764 min = NULL;
720 for(i = 0; i < files; ++i) { 765 for(i = 0; i < files; ++i) {
721 tmp = ReadDatabaseLine(tp[i]); 766 tmp = ReadLine(tp[i]);
722 if(tmp == NULL) { 767 if(tmp == NULL) {
723 continue; 768 continue;
724 } 769 }
725 if(min == NULL || strncmp(tmp, min, strlen(tmp)) < 0) { 770 if(min == NULL || strncmp(tmp, min, strlen(tmp)) < 0) {
Line 736... Line 781...
736 } 781 }
737 free(tmp); 782 free(tmp);
738 } 783 }
Line 739... Line 784...
739   784  
740 // Forward the file where the smallest line was found. 785 // Forward the file where the smallest line was found.
Line 741... Line 786...
741 SkipDatabaseLine(tp[j]); 786 SkipLine(tp[j]);
742   787  
743 // Write the smallest line. 788 // Write the smallest line.
744 if(min != NULL) { 789 if(min != NULL) {
Line 752... Line 797...
752 } 797 }
753 } 798 }
Line 754... Line 799...
754   799  
755 // Write out any remaining contents from the temporary files. 800 // Write out any remaining contents from the temporary files.
756 for(i = 0; i < files; ++i) { 801 for(i = 0; i < files; ++i) {
757 tmp = ReadDatabaseLine(tp[i]); 802 tmp = ReadLine(tp[i]);
758 if(tmp == NULL) { 803 if(tmp == NULL) {
759 continue; 804 continue;
760 } 805 }
761 #if defined ___AsyncIO___ 806 #if defined ___AsyncIO___
Line 809... Line 854...
809   854  
810 // Generate the database file. 855 // Generate the database file.
Line 811... Line 856...
811 UpdateDatabase(dbFile, stack, stats); 856 UpdateDatabase(dbFile, stack, stats);
812   857  
813 // Get the database metrics. 858 // Get the database metrics.
Line 814... Line 859...
814 dbSize = GetDatabaseSize(dbFile); 859 dbSize = GetFileSize(dbFile);
815 dbLines = CountDatabaseLines(dbFile); 860 dbLines = GetFileLines(dbFile);
Line 816... Line 861...
816   861  
Line 825... Line 870...
825 } 870 }
Line 826... Line 871...
826   871  
Line 827... Line 872...
827 tmpLines = dbLines / tmpFiles; 872 tmpLines = dbLines / tmpFiles;
828   873  
829 // Create temporary files. 874 // Create temporary files.
830 if((tmpNames = CreateTempFiles(tmpFiles)) == NULL) { 875 if((tmpNames = CreateTemporaryFiles(tmpFiles)) == NULL) {
831 fprintf(stderr, "Unable to create temporary files.\n"); 876 fprintf(stderr, "Unable to create temporary files.\n");
Line 832... Line 877...
832 return; 877 return;
833 } 878 }
Line 834... Line 879...
834   879  
835 // Write "tmpLines" to temporary files in "tmpFiles" from "dbFile". 880 // Write "tmpLines" to temporary files in "tmpFiles" from "dbFile".
836 WriteTempFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines); 881 WriteTemporaryFiles(dbFile, tmpNames, tmpFiles, tmpLines, dbLines);
837   882