home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / io.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  16KB  |  566 lines

  1. /* File I/O for GNU DIFF.
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. /* Rotate a value n bits to the left. */
  23. #define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
  24. #define ROL(v, n) ((v) << (n) | (v) >> UINT_BIT - (n))
  25.  
  26. /* Given a hash value and a new character, return a new hash value. */
  27. #define HASH(h, c) ((c) + ROL (h, 7))
  28.  
  29. /* Current file under consideration. */
  30. struct file_data *current;
  31.  
  32. /* Check for binary files and compare them for exact identity.  */
  33.  
  34. /* Return 1 if BUF contains a character with the 0200 bit set.
  35.    SIZE is the number of characters in BUF.  */
  36.  
  37. static int
  38. binary_file_p (buf, size)
  39.      char *buf;
  40.      int size;
  41. {
  42.   while (--size >= 0)
  43.     if (*buf++ & 0200)
  44.       return 1;
  45.   return 0;
  46. }
  47.  
  48. int binary_file_threshold = 512;
  49.  
  50. /* Slurp the current file completely into core.
  51.    Return nonzero if it appears to be a binary file.  */
  52.  
  53. static int
  54. slurp ()
  55. {
  56.   /* If we have a nonexistent file at this stage, treat it as empty.  */
  57.   if (current->desc < 0)
  58.     {
  59.       current->bufsize = 0;
  60.       current->buffered_chars = 0;
  61.       current->buffer = 0;
  62.     }
  63.   /* If it's a regular file, we can just get the size out of the stat
  64.      block and slurp it in all at once. */
  65.   /* In all cases, we leave room in the buffer for 2 extra chars
  66.      beyond those that current->bufsize describes:
  67.      one for a newline (in case the text does not end with one)
  68.      and one for a sentinel in find_identical_ends.  */
  69. #ifdef AMIGA
  70.   else if (current->stat.st_type < 0)
  71. #else
  72.   else if ((current->stat.st_mode & S_IFMT) == S_IFREG)
  73. #endif
  74.     {
  75.       current->bufsize = current->stat.st_size;
  76.       current->buffer = (char *) xmalloc (current->bufsize + 2);
  77.       current->buffered_chars
  78.     = read (current->desc, current->buffer, current->bufsize);
  79.       if (current->buffered_chars < 0)
  80.     pfatal_with_name (current->name);
  81.     }
  82.   else
  83.     {
  84.       int cc;
  85.  
  86.       current->bufsize = 4096;
  87.       current->buffer = (char *) xmalloc (current->bufsize + 2);
  88.       current->buffered_chars = 0;
  89.       
  90.       /* Not a regular file; read it in a little at a time, growing the
  91.      buffer as necessary. */
  92.       while ((cc = read (current->desc,
  93.              current->buffer + current->buffered_chars,
  94.              current->bufsize - current->buffered_chars))
  95.          > 0)
  96.     {
  97.       current->buffered_chars += cc;
  98.       if (current->buffered_chars == current->bufsize)
  99.         {
  100.           current->bufsize = current->bufsize * 2;
  101.           current->buffer = (char *) xrealloc (current->buffer,
  102.                            current->bufsize + 2);
  103.         }
  104.     }
  105.       if (cc < 0)
  106.     pfatal_with_name (current->name);
  107.     }
  108.   
  109.   /* Check first part of file to see if it's a binary file.  */
  110.   if (! always_text_flag
  111.       && binary_file_p (current->buffer,
  112.             min (current->buffered_chars, binary_file_threshold)))
  113.     return 1;
  114.  
  115.   /* If not binary, make sure text ends in a newline,
  116.      but remember that we had to add one.  */
  117.   if (current->buffered_chars > 0
  118.       && current->buffer[current->buffered_chars - 1] != '\n')
  119.     {
  120.       current->missing_newline = 1;
  121.       current->buffer[current->buffered_chars++] = '\n';
  122.     }
  123.   else
  124.     current->missing_newline = 0;
  125.  
  126.   return 0;
  127. }
  128.  
  129. /* Split the file into lines, simultaneously computing the hash codes for
  130.    each line. */
  131.  
  132. void
  133. find_and_hash_each_line ()
  134. {
  135.   unsigned h;
  136.   int i;
  137.   unsigned char *p = (unsigned char *) current->prefix_end, *ip, c;
  138.  
  139.   /* Attempt to get a good initial guess as to the number of lines. */
  140.   current->linbufsize = current->buffered_chars / 50 + 5;
  141.   current->linbuf
  142.     = (struct line_def *) xmalloc (current->linbufsize * sizeof (struct line_def));
  143.  
  144.   if (function_regexp)
  145.     {
  146.       /* If the -C or -F option is used, we need to find the lines
  147.      of the matching prefix.  At least we will need to find the last few,
  148.      but since we don't know how many, it's easiest to find them all.  */
  149.       current->buffered_lines = 0;
  150.       p = (unsigned char *) current->buffer;
  151.     }
  152.   else
  153.     {
  154.       /* Skip the identical prefixes, except be prepared to handle context.
  155.      In fact, handle 1 more preceding line than the context says,
  156.      in case shift_boundaries moves things backwards in this file.  */
  157.       current->buffered_lines = current->prefix_lines - context - 1;
  158.       if (current->buffered_lines < 0)
  159.     current->buffered_lines = 0;
  160.       for (i = 0; i < context + 1; ++i)
  161.     /* Unless we are at the beginning, */
  162.     if ((char *) p != current->buffer)
  163.       /* Back up at least 1 char until at the start of a line.  */
  164.       while ((char *) --p != current->buffer && p[-1] != '\n')
  165.         ;
  166.     }
  167.  
  168.   while ((char *) p < current->suffix_begin)
  169.     {
  170.       h = 0;
  171.       ip = p;
  172.  
  173.       if (current->prefix_end <= (char *) p)
  174.     {
  175.       /* Hash this line until we find a newline. */
  176.       if (ignore_case_flag)
  177.         {
  178.           if (ignore_all_space_flag)
  179.         while ((c = *p) != '\n')
  180.           {
  181.             if (! isspace (c))
  182.               if (isupper (c))
  183.             h = HASH (h, tolower (c));
  184.               else
  185.             h = HASH (h, c);
  186.             ++p;
  187.           }
  188.           else if (ignore_space_change_flag)
  189.  
  190.         while ((c = *p) != '\n')
  191.           {
  192.             if (c == ' ' || c == '\t')
  193.               {
  194.             while ((c = *p) == ' ' || c == '\t')
  195.               ++p;
  196.             if (c == '\n')
  197.               break;
  198.             h = HASH (h, ' ');
  199.               }
  200.             else if (isupper (c))
  201.               h = HASH (h, tolower (c));
  202.             else
  203.               h = HASH (h, c);
  204.             ++p;
  205.           }
  206.           else
  207.         while ((c = *p) != '\n')
  208.           {
  209.             if (isupper (c))
  210.               h = HASH (h, tolower (c));
  211.             else
  212.               h = HASH (h, c);
  213.             ++p;
  214.           }
  215.         }
  216.       else
  217.         {
  218.           if (ignore_all_space_flag)
  219.         while ((c = *p) != '\n')
  220.           {
  221.             if (! isspace (c))
  222.               h = HASH (h, c);
  223.             ++p;
  224.           }
  225.           else if (ignore_space_change_flag)
  226.         while ((c = *p) != '\n')
  227.           {
  228.             if (c == ' ' || c == '\t')
  229.               {
  230.             while ((c = *p) == ' ' || c == '\t')
  231.               ++p;
  232.             if (c == '\n')
  233.               break;
  234.             h = HASH (h, ' ');
  235.               }
  236.             else
  237.               h = HASH (h, c);
  238.             ++p;
  239.           }
  240.           else
  241.         while ((c = *p) != '\n')
  242.           {
  243.             h = HASH (h, c);
  244.             ++p;
  245.           }
  246.         }
  247.     }
  248.       else
  249.     /* This line is part of the matching prefix,
  250.        so we don't need to hash it.  */
  251.     while (*p != '\n')
  252.       ++p;
  253.       
  254.       /* Maybe increase the size of the line table. */
  255.       if (current->buffered_lines >= current->linbufsize)
  256.     {
  257.       while (current->buffered_lines >= current->linbufsize)
  258.         current->linbufsize *= 2;
  259.       current->linbuf
  260.         = (struct line_def *) xrealloc (current->linbuf,
  261.                         current->linbufsize
  262.                         * sizeof (struct line_def));
  263.     }
  264.       current->linbuf[current->buffered_lines].text = (char *) ip;
  265.       current->linbuf[current->buffered_lines].length = p - ip;
  266.       current->linbuf[current->buffered_lines].hash = h;
  267.       ++current->buffered_lines;
  268.       ++p;
  269.     }
  270.  
  271.   i = 0;
  272.   while (i < context && (char *) p < current->buffer + current->buffered_chars)
  273.     {
  274.       ip = p;
  275.       while (*p++ != '\n')
  276.     ;
  277.       /* Maybe increase the size of the line table. */
  278.       if (current->buffered_lines >= current->linbufsize)
  279.     {
  280.       while (current->buffered_lines >= current->linbufsize)
  281.         current->linbufsize *= 2;
  282.       current->linbuf
  283.         = (struct line_def *) xrealloc (current->linbuf,
  284.                         current->linbufsize
  285.                         * sizeof (struct line_def));
  286.     }
  287.       current->linbuf[current->buffered_lines].text = (char *) ip;
  288.       current->linbuf[current->buffered_lines].length = p - ip - 1;
  289.       current->linbuf[current->buffered_lines].hash = 0;
  290.       ++current->buffered_lines;
  291.       ++i;
  292.     }
  293. }
  294.  
  295. /* Given a vector of two file_data objects, find the identical
  296.    prefixes and suffixes of each object. */
  297.  
  298. static void
  299. find_identical_ends (filevec)
  300.      struct file_data filevec[];
  301. {
  302.   char *p0, *p1, *end0, *beg0;
  303.   int lines;
  304.  
  305.   if (filevec[0].buffered_chars == 0 || filevec[1].buffered_chars == 0)
  306.     {
  307.       filevec[0].prefix_end = filevec[0].buffer;
  308.       filevec[1].prefix_end = filevec[1].buffer;
  309.       filevec[0].prefix_lines = filevec[1].prefix_lines = 0;
  310.       filevec[0].suffix_begin = filevec[0].buffer + filevec[0].buffered_chars;
  311.       filevec[1].suffix_begin = filevec[1].buffer + filevec[1].buffered_chars;
  312.       filevec[0].suffix_lines = filevec[1].suffix_lines = 0;
  313.       return;
  314.     }
  315.  
  316.   /* Find identical prefix.  */
  317.  
  318.   p0 = filevec[0].buffer;
  319.   p1 = filevec[1].buffer;
  320.   lines = 0;
  321.  
  322.   /* Insert end "sentinels", in this case characters that are guarranteed
  323.      to make the equality test false, and thus terminate the loop.  */
  324.  
  325.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  326.     p0[filevec[0].buffered_chars] = ~p1[filevec[0].buffered_chars];
  327.   else
  328.     p1[filevec[1].buffered_chars] = ~p0[filevec[1].buffered_chars];
  329.  
  330.   /* Loop until first mismatch, or to the sentinel characters.  */
  331.   while (1)
  332.     {
  333.       char c = *p0++;
  334.       if (c != *p1++)
  335.     break;
  336.       if (c == '\n')
  337.     ++lines;
  338.     }
  339.  
  340.   /* If the sentinel was passed, and lengths are equal, the
  341.      files are identical.  */
  342.   if (p0 - filevec[0].buffer > filevec[0].buffered_chars
  343.       && filevec[0].buffered_chars == filevec[1].buffered_chars)
  344.     {
  345.       filevec[0].prefix_end = p0 - 1;
  346.       filevec[1].prefix_end = p1 - 1;
  347.       filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  348.       filevec[0].suffix_begin = filevec[0].buffer;
  349.       filevec[1].suffix_begin = filevec[1].buffer;
  350.       filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  351.       return;
  352.     }
  353.  
  354.   /* Point at first nonmatching characters.  */
  355.   --p0, --p1;
  356.  
  357.   /* Skip back to last line-beginning in the prefix.  */
  358.   while (p0 != filevec[0].buffer && p0[-1] != '\n')
  359.     --p0, --p1;
  360.  
  361.   /* Record the prefix.  */
  362.   filevec[0].prefix_end = p0;
  363.   filevec[1].prefix_end = p1;
  364.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  365.   
  366.   /* Find identical suffix.  */
  367.  
  368.   /* P0 and P1 point beyond the last chars not yet compared.  */
  369.   p0 = filevec[0].buffer + filevec[0].buffered_chars;
  370.   p1 = filevec[1].buffer + filevec[1].buffered_chars;
  371.   lines = 0;
  372.   end0 = p0;        /* Addr of last char in file 0.  */
  373.  
  374.   /* Get value of P0 at which we should stop scanning backward:
  375.      this is when either P0 or P1 points at the last char
  376.      of the identical prefix.  */
  377.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  378.     beg0 = filevec[0].prefix_end - 1;
  379.   else
  380.     beg0 = (filevec[0].prefix_end - 1
  381.         + filevec[1].buffered_chars - filevec[0].buffered_chars);
  382.  
  383.   /* Scan back until chars don't match or we reach that point.  */
  384.   while (p0 != beg0)
  385.     {
  386.       char c = *--p0;
  387.       if (c != *--p1)
  388.     break;
  389.       if (c == '\n')
  390.     ++lines;
  391.     }
  392.  
  393.   /* Make P0 and P1 point at the first char of the matching suffix.  */
  394.   ++p0, ++p1;
  395.  
  396.   /* Advance to next place that is a line-beginning in both files.  */
  397.   while (p0 != end0
  398.      && !((p0 == filevec[0].buffer || p0[-1] == '\n')
  399.           &&
  400.           (p1 == filevec[1].buffer || p1[-1] == '\n')))
  401.     ++p0, ++p1;
  402.  
  403.   /* Subtract one, since the last newline isn't followed by a line.  */
  404.   --lines;
  405.   
  406.   /* Record the suffix.  */
  407.   filevec[0].suffix_begin = p0;
  408.   filevec[1].suffix_begin = p1;
  409.   filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  410. }
  411.  
  412. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  413.    Each equivalence class is represented by one of these structures,
  414.    but only while the classes are being computed.
  415.    Afterward, each class is represented by a number.  */
  416. struct equivclass
  417. {
  418.   struct equivclass *next;    /* Next item in this bucket. */
  419.   struct line_def line;    /* A line that fits this class. */
  420. };
  421.  
  422. /* Hash-table: array of buckets, each being a chain of equivalence classes.  */
  423. static struct equivclass **buckets;
  424.   
  425. /* Size of the bucket array. */
  426. static int nbuckets;
  427.  
  428. /* Array in which the equivalence classes are allocated.
  429.    The bucket-chains go through the elements in this array.
  430.    The number of an equivalence class is its index in this array.  */
  431. static struct equivclass *equivs;
  432.  
  433. /* Index of first free element in the array `equivs'.  */
  434. static int equivs_index;
  435.  
  436. /* Size allocated to the array `equivs'.  */
  437. static int equivs_alloc;
  438.  
  439. /* Largest primes less than some power of two, for nbuckets.  Values range
  440.    from useful to preposterous.  If one of these numbers isn't prime
  441.    after all, don't blame it on me, blame it on primes (6) . . . */
  442. static int primes[] =
  443. {
  444.   509,
  445.   1021,
  446.   2039,
  447.   4093,
  448.   8191,
  449.   16381,
  450.   32749,
  451.   65521,
  452.   131071,
  453.   262139,
  454.   524287,
  455.   1048573,
  456.   2097143,
  457.   4194301,
  458.   8388593,
  459.   16777213,
  460.   33554393,
  461.   67108859,            /* Preposterously large . . . */
  462.   -1
  463. };
  464.  
  465. /* Index of current nbuckets in primes. */
  466. static int primes_index;
  467.  
  468. /* Find the equiv class associated with line N of the current file.  */
  469.  
  470. static int
  471. find_equiv_class (n)
  472.      int n;
  473. {
  474.   int bucket = current->linbuf[n].hash % nbuckets;
  475.   struct equivclass *b = buckets[bucket], *p = NULL;
  476.  
  477.   /* Equivalence class 0 is permanently allocated to lines that were
  478.      not hashed because they were parts of identical prefixes or
  479.      suffixes. */
  480.   if (n < current->prefix_lines
  481.       || current->linbuf[n].text >= current->suffix_begin)
  482.     return 0;
  483.  
  484.   /* Check through the appropriate bucket to see if there isn't already
  485.      an equivalence class for this line. */
  486.   while (b)
  487.     {
  488.       if (b->line.hash == current->linbuf[n].hash
  489.       && (b->line.length == current->linbuf[n].length
  490.           /* Lines of different lengths can match with certain options.  */
  491.           || length_varies)
  492.       && !line_cmp (&b->line, ¤t->linbuf[n]))
  493.     return b - equivs;
  494.       p = b, b = b->next;
  495.     }
  496.  
  497.   /* Create a new equivalence class in this bucket. */
  498.  
  499.   p = &equivs[equivs_index++];
  500.   p->next = buckets[bucket];
  501.   buckets[bucket] = p;
  502.   p->line = current->linbuf[n];
  503.  
  504.   return equivs_index - 1;
  505. }
  506.  
  507. /* Given a vector of two file_data objects, read the file associated
  508.    with each one, and build the table of equivalence classes.
  509.    Return nonzero if either file appears to be a binary file.  */
  510.  
  511. int
  512. read_files (filevec)
  513.      struct file_data filevec[];
  514. {
  515.   int i, j;
  516.   int binary = 0;
  517.   int this_binary;
  518.  
  519.   current = &filevec[0];
  520.   binary = this_binary = slurp ();
  521.  
  522.   current = &filevec[1];
  523.   this_binary = slurp ();
  524.   if (binary || this_binary)
  525.     return 1;
  526.  
  527.   find_identical_ends (filevec);
  528.  
  529.   for (i = 0; i < 2; ++i)
  530.     {
  531.       current = &filevec[i];
  532.       find_and_hash_each_line ();
  533.     }
  534.  
  535.   /* This is guaranteed to be enough space.  */
  536.   equivs_alloc = filevec[0].buffered_lines + filevec[1].buffered_lines + 1;
  537.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
  538.   /* Equivalence class 0 is permanently safe for lines that were not
  539.      hashed.  Real equivalence classes start at 1. */
  540.   equivs_index = 1;
  541.   
  542.   primes_index = 0;
  543.   while (primes[primes_index] < equivs_alloc / 3)
  544.     primes_index++;
  545.  
  546.   buckets = (struct equivclass **) xmalloc (primes[primes_index] * sizeof (struct equivclass *));
  547.   bzero (buckets, primes[primes_index] * sizeof (struct equivclass *));
  548.   nbuckets = primes[primes_index];
  549.  
  550.   for (i = 0; i < 2; ++i)
  551.     {
  552.       current = &filevec[i];
  553.       current->equivs
  554.     = (int *) xmalloc (current->buffered_lines * sizeof (int));
  555.       for (j = 0; j < current->buffered_lines; ++j)
  556.     current->equivs[j] = find_equiv_class (j);
  557.     }
  558.  
  559.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  560.  
  561.   free (equivs);
  562.   free (buckets);
  563.  
  564.   return 0;
  565. }
  566.