home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d111 / labels.lha / Labels / num.c < prev    next >
C/C++ Source or Header  |  1987-11-15  |  4KB  |  210 lines

  1. /*
  2.  *    This program takes two files of numerically sorted labels,
  3.  *    and merges them to the standard output.
  4.  *    The lines should be of the form <VALUE> <LABEL> <SPACE(S)>.
  5.  *    In most cases, duplicates will be filtered out.
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. #define DEBUG
  11. #define FINAL_OUTPUT
  12.  
  13. char line1[80];
  14. char line2[80];
  15.  
  16. char *name1;
  17. char *name2;
  18.  
  19. long value1, value2;
  20.  
  21. #ifdef DEBUG
  22. FILE *dbgchn;
  23. #endif
  24.  
  25. #define negabs(x)    (x>0? -x: x)
  26.  
  27. int freadln(file, line)
  28. register FILE *file;
  29. register char *line;
  30. {
  31.     register int ch = ' ';
  32.     char *linepos = line;
  33.  
  34.     while ((line == linepos) && (ch != EOF)) {    /* Skip empty lines */
  35.         while ((ch = getc(file)) != '\n' && (ch != EOF))
  36.             *linepos++ = ch;
  37.     }
  38.  
  39.     *linepos = '\0';
  40. #ifdef DEBUG
  41.     {
  42.         int filenr = (line == line1)? 1 : 2;
  43.         fprintf(dbgchn, "%d read: `%s'\n", filenr, line);
  44.     }
  45. #endif
  46.  
  47.     return ch == EOF;
  48. }
  49.  
  50. int oldscanline(file, line, name, value)    /* Name first */
  51. FILE *file;
  52. char *line;
  53. char **name;
  54. long *value;
  55. {
  56.     register char *ch = line;
  57.  
  58.     if ( freadln(file, line) == 0 ) {
  59.         while (*ch == ' ')    ch++;    /* Find beginning of label */
  60.         *name = ch;                    /* Return it */
  61.         while (*ch != ' ')    ch++;    /* Find end of label */
  62.         *ch = '\0';                    /* Null-terminate it */
  63.         if (sscanf(ch+1, "%lx", value) == 1) {    /* Skip name */
  64. #ifdef DEBUG2
  65.             fprintf(dbgchn, "value = %8lx\n", *value);
  66. #endif
  67.             return 0;                /* OK */
  68.         }
  69.     }
  70.     return 1;                        /* BAD */
  71. }
  72.  
  73. int scanline(file, line, name, value)    /* Value first */
  74. FILE *file;
  75. char *line;
  76. char **name;
  77. long *value;
  78. {
  79.     register char *ch = &line[8];    /* Skip 8 digit value */
  80.  
  81.     if ( freadln(file, line) == 0 ) {
  82.         while (*ch == ' ')    ch++;    /* Find beginning of label */
  83.         *name = ch;                    /* Return it */
  84.         while (*ch != ' ')    ch++;    /* Find end of label */
  85.         *ch = '\0';                    /* Null-terminate it */
  86.         if (sscanf(line, "%lx", value) == 1) {
  87. #ifdef DEBUG2
  88.             fprintf(dbgchn, "value = %8lx\n", *value);
  89. #endif
  90.             return 0;                /* OK */
  91.         }
  92.     }
  93.     return 1;                        /* BAD */
  94. }
  95.  
  96. #ifdef FINAL_OUTPUT
  97. int writeline(name, value)
  98. char *name;
  99. long value;
  100. {
  101.     int i;
  102.  
  103.     if (value & 0xFFFF0000)
  104.         printf("%08lx  ", value);
  105.     else if ((short) value & 0xFF00)
  106.         printf("    %04lx  ", value);
  107.     else
  108.         printf("      %02lx  ", value);
  109.  
  110.     printf(name);
  111.  
  112.     if (negabs(value) > -65536) {
  113.         printf("  ");
  114.         for (i=strlen(name); i < 30; i++)
  115.             putchar('_');
  116.         printf(" %6ld", value);
  117.     }
  118.  
  119.     putchar('\n');
  120.  
  121.     return 0;
  122. }
  123.  
  124. #else
  125.  
  126. int writeline(name, value)
  127. char *name;
  128. long value;
  129. {
  130.     return printf("%-30s  %lx\n", name, value);
  131. }
  132.  
  133. #endif
  134.  
  135. main(argc, argv)
  136. int argc;
  137. char *argv[];
  138. {
  139.     FILE *file1, *file2, *file;
  140.  
  141.     int comp;
  142.     int finished = 0;
  143.  
  144.     if (argc != 3) {
  145.         fprintf(stderr, "Usage: %s file1 file2\n", argv[0]);
  146.         exit(1);
  147.     }
  148.  
  149.     if ((file1 = fopen(argv[1], "r")) == NULL) {
  150.         fprintf(stderr, "Cannot open %s\n", argv[1]);
  151.         exit(1);
  152.     }
  153.  
  154.     if ((file2 = fopen(argv[2], "r")) == NULL) {
  155.         fprintf(stderr, "Cannot open %s\n", argv[2]);
  156.         exit(1);
  157.     }
  158.  
  159. #ifdef DEBUG
  160.     /* Note: When used with ConMan 0.98B, this opens TWO windows */
  161.     /*       but closes only one. */
  162.     dbgchn = fopen("CON:50/0/540/160/File IO Info", "w");
  163. #endif
  164.  
  165.     if (scanline(file1, line1, &name1, &value1))
  166.         finished = 1;
  167.     else if (scanline(file2, line2, &name2, &value2))
  168.         finished = 2;
  169.  
  170.     while (finished == 0) {
  171.         comp = strcmp(name1, name2);
  172.         if ((value1 < value2) || 
  173.             ((value1 == value2) && (comp < 0))) {    /* First value is lower */
  174.             writeline(name1, value1);
  175.             if (scanline(file1, line1, &name1, &value1))
  176.                 finished = 1;
  177.         } else if ((value1 > value2) ||
  178.             ((value1 == value2) && (comp > 0))) {    /* Second value is lower */
  179.             writeline(name2, value2);
  180.             if (scanline(file2, line2, &name2, &value2))
  181.                 finished = 2;
  182.         } else {
  183.             /* Exact duplicates. Print one and drop both */
  184.             writeline(name1, value1);
  185.             if (scanline(file1, line1, &name1, &value1))
  186.                 finished = 1;
  187.             else if (scanline(file2, line2, &name2, &value2))
  188.                 finished = 2;
  189.         }    /* End comparison */
  190.     }    /* End while */
  191.  
  192. #ifdef DEBUG
  193.     fprintf(dbgchn, "***** FINISHED WITH FILE %d *****\n", finished);
  194. #endif
  195.  
  196.     if (finished == 1)    file = file2;
  197.     else                file = file1;
  198.  
  199.     while (!scanline(file, line1, &name1, &value1))    /* Wind up rest of other file */
  200.         writeline(name1, value1);
  201.  
  202.     fclose(file1);
  203.     fclose(file2);
  204.  
  205. #ifdef DEBUG
  206.     fclose(dbgchn);
  207. #endif
  208.     fprintf(stderr, "Terminated successfully\n");
  209. }
  210.