home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 317a.lha / RCS / diff / io.c < prev    next >
C/C++ Source or Header  |  1989-12-05  |  16KB  |  569 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_de
  143. ));
  144.  
  145.   if (function_regexp)
  146.     {
  147.       /* If the -C or -F option is used, we need to find the lines
  148.      of the matching prefix.  At least we will need to find the last few,
  149.      but since we don't know how many, it's easiest to find them all.  */
  150.       current->buffered_lines = 0;
  151.       p = (unsigned char *) current->buffer;
  152.     }
  153.   else
  154.     {
  155.       /* Skip the identical prefixes, except be prepared to handle context.
  156.      In fact, handle 1 more preceding line than the context says,
  157.      in case shift_boundaries moves things backwards in this file.  */
  158.       current->buffered_lines = current->prefix_lines - context - 1;
  159.       if (current->buffered_lines < 0)
  160.     current->buffered_lines = 0;
  161.       for (i = 0; i < context + 1; ++i)
  162.     /* Unless we are at the beginning, */
  163.     if ((char *) p != current->buffer)
  164.       /* Back up at least 1 char until at the start of a line.  */
  165.       while ((char *) --p != current->buffer && p[-1] != '\n')
  166.         ;
  167.     }
  168.  
  169.   while ((char *) p < current->suffix_begin)
  170.     {
  171.       h = 0;
  172.       ip = p;
  173.  
  174.       if (current->prefix_end <= (char *) p)
  175.     {
  176.       /* Hash this line until we find a newline. */
  177.       if (ignore_case_flag)
  178.         {
  179.           if (ignore_all_space_flag)
  180.         while ((c = *p) != '\n')
  181.           {
  182.             if (! isspace (c))
  183.               if (isupper (c))
  184.             h = HASH (h, tolower (c));
  185.               else
  186.             h = HASH (h, c);
  187.             ++p;
  188.           }
  189.           else if (ignore_space_change_flag)
  190.  
  191.         while ((c = *p) != '\n')
  192.           {
  193.             if (c == ' ' || c == '\t')
  194.               {
  195.             while ((c = *p) == ' ' || c == '\t')
  196.               ++p;
  197.             if (c == '\n')
  198.               break;
  199.             h = HASH (h, ' ');
  200.               }
  201.             else if (isupper (c))
  202.               h = HASH (h, tolower (c));
  203.             else
  204.               h = HASH (h, c);
  205.             ++p;
  206.           }
  207.           else
  208.         while ((c = *p) != '\n')
  209.           {
  210.             if (isupper (c))
  211.               h = HASH (h, tolower (c));
  212.             else
  213.               h = HASH (h, c);
  214.             ++p;
  215.           }
  216.         }
  217.       else
  218.         {
  219.           if (ignore_all_space_flag)
  220.         while ((c = *p) != '\n')
  221.           {
  222.             if (! isspace (c))
  223.               h = HASH (h, c);
  224.             ++p;
  225.           }
  226.           else if (ignore_space_change_flag)
  227.         while ((c = *p) != '\n')
  228.           {
  229.             if (c == ' ' || c == '\t')
  230.               {
  231.             while ((c = *p) == ' ' || c == '\t')
  232.               ++p;
  233.             if (c == '\n')
  234.               break;
  235.             h = HASH (h, ' ');
  236.               }
  237.             else
  238.               h = HASH (h, c);
  239.             ++p;
  240.           }
  241.           else
  242.         while ((c = *p) != '\n')
  243.           {
  244.             h = HASH (h, c);
  245.             ++p;
  246.           }
  247.         }
  248.     }
  249.       else
  250.     /* This line is part of the matching prefix,
  251.        so we don't need to hash it.  */
  252.     while (*p != '\n')
  253.       ++p;
  254.       
  255.       /* Maybe increase the size of the line table. */
  256.       if (current->buffered_lines >= current->linbufsize)
  257.     {
  258.       while (current->buffered_lines >= current->linbufsize)
  259.         current->linbufsize *= 2;
  260.       current->linbuf
  261.         = (struct line_def *) xrealloc (current->linbuf,
  262.                         current->linbufsize
  263.                         * sizeof (struct line_def));
  264.     }
  265.       current->linbuf[current->buffered_lines].text = (char *) ip;
  266.       current->linbuf[current->buffered_lines].length = p - ip;
  267.       current->linbuf[current->buffered_lines].hash = h;
  268.       ++current->buffered_lines;
  269.       ++p;
  270.     }
  271.  
  272.   i = 0;
  273.   while (i < context && (char *) p < current->buffer + current->buffered_chars)
  274.     {
  275.       ip = p;
  276.       while (*p++ != '\n')
  277.     ;
  278.       /* Maybe increase the size of the line table. */
  279.       if (current->buffered_lines >= current->linbufsize)
  280.     {
  281.       while (current->buffered_lines >= current->linbufsize)
  282.         current->linbufsize *= 2;
  283.       current->linbuf
  284.         = (struct line_def *) xrealloc (current->linbuf,
  285.                         current->linbufsize
  286.                         * sizeof (struct line_def));
  287.     }
  288.       current->linbuf[current->buffered_lines].text = (char *) ip;
  289.       current->linbuf[current->buffered_lines].length = p - ip - 1;
  290.       current->linbuf[current->buffered_lines].hash = 0;
  291.       ++current->buffered_lines;
  292.       ++i;
  293.     }
  294. }
  295.  
  296. /* Given a vector of two file_data objects, find the identical
  297.    prefixes and suffixes of each object. */
  298.  
  299. static void
  300. find_identical_ends (filevec)
  301.      struct file_data filevec[];
  302. {
  303.   char *p0, *p1, *end0, *beg0;
  304.   int lines;
  305.  
  306.   if (filevec[0].buffered_chars == 0 || filevec[1].buffered_chars == 0)
  307.     {
  308.       filevec[0].prefix_end = filevec[0].buffer;
  309.       filevec[1].prefix_end = filevec[1].buffer;
  310.       filevec[0].prefix_lines = filevec[1].prefix_lines = 0;
  311.       filevec[0].suffix_begin = filevec[0].buffer + filevec[0].buffered_chars;
  312.       filevec[1].suffix_begin = filevec[1].buffer + filevec[1].buffered_chars;
  313.       filevec[0].suffix_lines = filevec[1].suffix_lines = 0;
  314.       return;
  315.     }
  316.  
  317.   /* Find identical prefix.  */
  318.  
  319.   p0 = filevec[0].buffer;
  320.   p1 = filevec[1].buffer;
  321.   lines = 0;
  322.  
  323.   /* Insert end "sentinels", in this case characters that are guarranteed
  324.      to make the equality test false, and thus terminate the loop.  */
  325.  
  326.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  327.     p0[filevec[0].buffered_chars] = ~p1[filevec[0].buffered_chars];
  328.   else
  329.     p1[filevec[1].buffered_chars] = ~p0[filevec[1].buffered_chars];
  330.  
  331.   /* Loop until first mismatch, or to the sentinel characters.  */
  332.   while (1)
  333.     {
  334.       char c = *p0++;
  335.       if (c != *p1++)
  336.     break;
  337.       if (c == '\n')
  338.     ++lines;
  339.     }
  340.  
  341.   /* If the sentinel was passed, and lengths are equal, the
  342.      files are identical.  */
  343.   if (p0 - filevec[0].buffer > filevec[0].buffered_chars
  344.       && filevec[0].buffered_chars == filevec[1].buffered_chars)
  345.     {
  346.       filevec[0].prefix_end = p0 - 1;
  347.       filevec[1].prefix_end = p1 - 1;
  348.       filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  349.       filevec[0].suffix_begin = filevec[0].buffer;
  350.       filevec[1].suffix_begin = filevec[1].buffer;
  351.       filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  352.       return;
  353.     }
  354.  
  355.   /* Point at first nonmatching characters.  */
  356.   --p0, --p1;
  357.  
  358.   /* Skip back to last line-beginning in the prefix.  */
  359.   while (p0 != filevec[0].buffer && p0[-1] != '\n')
  360.     --p0, --p1;
  361.  
  362.   /* Record the prefix.  */
  363.   filevec[0].prefix_end = p0;
  364.   filevec[1].prefix_end = p1;
  365.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  366.   
  367.   /* Find identical suffix.  */
  368.  
  369.   /* P0 and P1 point beyond the last chars not yet compared.  */
  370.   p0 = filevec[0].buffer + filevec[0].buffered_chars;
  371.   p1 = filevec[1].buffer + filevec[1].buffered_chars;
  372.   lines = 0;
  373.   end0 = p0;        /* Addr of last char in file 0.  */
  374.  
  375.   /* Get value of P0 at which we should stop scanning backward:
  376.      this is when either P0 or P1 points at the last char
  377.      of the identical prefix.  */
  378.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  379.     beg0 = filevec[0].prefix_end - 1;
  380.   else
  381.     beg0 = (filevec[0].prefix_end - 1
  382.         + filevec[1].buffered_chars - filevec[0].buffered_chars);
  383.  
  384.   /* Scan back until chars don't match or we reach that point.  */
  385.   while (p0 != beg0)
  386.     {
  387.       char c = *--p0;
  388.       if (c != *--p1)
  389.     break;
  390.       if (c == '\n')
  391.     ++lines;
  392.     }
  393.  
  394.   /* Make P0 and P1 point at the first char of the matching suffix.  */
  395.   ++p0, ++p1;
  396.  
  397.   /* Advance to next place that is a line-beginning in both files.  */
  398.   while (p0 != end0
  399.      && !((p0 == filevec[0].buffer || p0[-1] == '\n')
  400.           &&
  401.           (p1 == filevec[1].buffer || p1[-1] == '\n')))
  402.     ++p0, ++p1;
  403.  
  404.   /* Subtract one, since the last newline isn't followed by a line.  */
  405.   --lines;
  406.   
  407.   /* Record the suffix.  */
  408.   filevec[0].suffix_begin = p0;
  409.   filevec[1].suffix_begin = p1;
  410.   filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  411. }
  412.  
  413. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  414.    Each equivalence class is represented by one of these structures,
  415.    but only while the classes are being computed.
  416.    Afterward, each class is represented by a number.  */
  417. struct equivclass
  418. {
  419.   struct equivclass *next;    /* Next item in this bucket. */
  420.   struct line_def line;    /* A line that fits this class. */
  421. };
  422.  
  423. /* Hash-table: array of buckets, each being a chain of equivalence classes.  */
  424. static struct equivclass **buckets;
  425.   
  426. /* Size of the bucket array. */
  427. static int nbuckets;
  428.  
  429. /* Array in which the equivalence classes are allocated.
  430.    The bucket-chains go through the elements in this array.
  431.    The number of an equivalence class is its index in this array.  */
  432. static struct equivclass *equivs;
  433.  
  434. /* Index of first free element in the array `equivs'.  */
  435. static int equivs_index;
  436.  
  437. /* Size allocated to the array `equivs'.  */
  438. static int equivs_alloc;
  439.  
  440. /* Largest primes less than some power of two, for nbuckets.  Values range
  441.    from useful to preposterous.  If one of these numbers isn't prime
  442.    after all, don't blame it on me, blame it on primes (6) . . . */
  443. static int primes[] =
  444. {
  445.   509,
  446.   1021,
  447.   2039,
  448.   4093,
  449.   8191,
  450.   16381,
  451.   32749,
  452.   65521,
  453.   131071,
  454.   262139,
  455.   524287,
  456.   1048573,
  457.   2097143,
  458.   4194301,
  459.   8388593,
  460.   16777213,
  461.   33554393,
  462.   67108859,            /* Preposterously large . . . */
  463.   -1
  464. };
  465.  
  466. /* Index of current nbuckets in primes. */
  467. static int primes_index;
  468.  
  469. /* Find the equiv class associated with line N of the current file.  */
  470.  
  471. static int
  472. find_equiv_class (n)
  473.      int n;
  474. {
  475.   int bucket = current->linbuf[n].hash % nbuckets;
  476.   struct equivclass *b = buckets[bucket], *p = NULL;
  477.  
  478.   /* Equivalence class 0 is permanently allocated to lines that were
  479.      not hashed because they were parts of identical prefixes or
  480.      suffixes. */
  481.   if (n < current->prefix_lines
  482.       || current->linbuf[n].text >= current->suffix_begin)
  483.     return 0;
  484.  
  485.   /* Check through the appropriate bucket to see if there isn't already
  486.      an equivalence class for this line. */
  487.   while (b)
  488.     {
  489.       if (b->line.hash == current->linbuf[n].hash
  490.       && (b->line.length == current->linbuf[n].length
  491.           /* Lines of different lengths can match with certain options.  */
  492.           || length_varies)
  493.       && !line_cmp (&b->line, ¤t->linbuf[n]))
  494.     return b - equivs;
  495.       p = b, b = b->next;
  496.     }
  497.  
  498.   /* Create a new equivalence class in this bucket. */
  499.  
  500.   p = &equivs[equivs_index++];
  501.   p->next = buckets[bucket];
  502.   buckets[bucket] = p;
  503.   p->line = current->linbuf[n];
  504.  
  505.   return equivs_index - 1;
  506. }
  507.  
  508. /* Given a vector of two file_data objects, read the file associated
  509.    with each one, and build the table of equivalence classes.
  510.    Return nonzero if either file appears to be a binary file.  */
  511.  
  512. int
  513. read_files (filevec)
  514.      struct file_data filevec[];
  515. {
  516.   int i, j;
  517.   int binary = 0;
  518.   int this_binary;
  519.  
  520.   current = &filevec[0];
  521.   binary = this_binary = slurp ();
  522.  
  523.   current = &filevec[1];
  524.   this_binary = slurp ();
  525.   if (binary || this_binary)
  526.     return 1;
  527.  
  528.   find_identical_ends (filevec);
  529.  
  530.   for (i = 0; i < 2; ++i)
  531.     {
  532.       current = &filevec[i];
  533.       find_and_hash_each_line ();
  534.     }
  535.  
  536.   /* This is guaranteed to be enough space.  */
  537.   equivs_alloc = filevec[0].buffered_lines + filevec[1].buffered_lines + 1;
  538.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivcl
  539. ss));
  540.   /* Equivalence class 0 is permanently safe for lines that were not
  541.      hashed.  Real equivalence classes start at 1. */
  542.   equivs_index = 1;
  543.   
  544.   primes_index = 0;
  545.   while (primes[primes_index] < equivs_alloc / 3)
  546.     primes_index++;
  547.  
  548.   buckets = (struct equivclass **) xmalloc (primes[primes_index] * sizeof (stru
  549. t equivclass *));
  550.   bzero (buckets, primes[primes_index] * sizeof (struct equivclass *));
  551.   nbuckets = primes[primes_index];
  552.  
  553.   for (i = 0; i < 2; ++i)
  554.     {
  555.       current = &filevec[i];
  556.       current->equivs
  557.     = (int *) xmalloc (current->buffered_lines * sizeof (int));
  558.       for (j = 0; j < current->buffered_lines; ++j)
  559.     current->equivs[j] = find_equiv_class (j);
  560.     }
  561.  
  562.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  563.  
  564.   free (equivs);
  565.   free (buckets);
  566.  
  567.   return 0;
  568. }
  569.