io.c

来自「WinMerge可以显示两个文件的不同之处」· C语言 代码 · 共 848 行 · 第 1/2 页

C
848
字号
/* File I/O for GNU DIFF.
   Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.

This file is part of GNU DIFF.

GNU DIFF is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU DIFF; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "diff.h"

// reduce some noise produced with the MSVC compiler
#if defined (_AFXDLL)
#pragma warning(disable : 4131 4013 4090)
#endif


/* Rotate a value n bits to the left. */
#define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
#define ROL(v, n) ((v) << (n) | (v) >> (UINT_BIT - (n)))

/* Given a hash value and a new character, return a new hash value. */
#define HASH(h, c) ((c) + ROL (h, 7))

/* Guess remaining number of lines from number N of lines so far,
   size S so far, and total size T.  */
#define GUESS_LINES(n,s,t) (((t) - (s)) / ((n) < 10 ? 32 : (s) / ((n)-1)) + 5)

/* Type used for fast prefix comparison in find_identical_ends.  */
typedef unsigned word;

/* Lines are put into equivalence classes (of lines that match in line_cmp).
   Each equivalence class is represented by one of these structures,
   but only while the classes are being computed.
   Afterward, each class is represented by a number.  */
struct equivclass
{
  int next;	/* Next item in this bucket. */
  unsigned hash;	/* Hash of lines in this class.  */
  char const HUGE *line;	/* A line that fits this class. */
  size_t length;	/* The length of that line.  */
};

/* Hash-table: array of buckets, each being a chain of equivalence classes.  */
static int *buckets;
  
/* Number of buckets in the hash table array. */
static int nbuckets;

/* Array in which the equivalence classes are allocated.
   The bucket-chains go through the elements in this array.
   The number of an equivalence class is its index in this array.  */
static struct equivclass HUGE *equivs;

/* Index of first free element in the array `equivs'.  */
static int equivs_index;

/* Number of elements allocated in the array `equivs'.  */
static int equivs_alloc;

static void find_and_hash_each_line PARAMS((struct file_data *));
static void find_identical_ends PARAMS((struct file_data[]));
static void prepare_text_end PARAMS((struct file_data *));

/* Check for binary files and compare them for exact identity.  */

/* Return 1 if BUF contains a non text character.
   SIZE is the number of characters in BUF.  */

#define binary_file_p(buf, size) (size != 0 && memchr (buf, '\0', size) != 0)

/* Get ready to read the current file.
   Return nonzero if SKIP_TEST is zero,
   and if it appears to be a binary file.  */

int
sip (current, skip_test)
     struct file_data *current;
     int skip_test;
{
  /* If we have a nonexistent file at this stage, treat it as empty.  */
  if (current->desc < 0)
    {
      /* Leave room for a sentinel.  */
      current->buffer = xmalloc (sizeof (word));
      current->bufsize = sizeof (word);
      current->buffered_chars = 0;
    }
  else
    {
      current->bufsize = current->buffered_chars
	= STAT_BLOCKSIZE (current->stat);

      if (S_ISREG (current->stat.st_mode))
	/* Get the size out of the stat block.
	   Allocate enough room for appended newline and sentinel.
	   Allocate at least one block, to prevent overrunning the buffer
	   when comparing growing binary files.  */
	current->bufsize = max (current->bufsize,
				current->stat.st_size + sizeof (word) + 1);

#ifdef __MSDOS__
      if ((current->buffer = (char HUGE *) farmalloc (current->bufsize)) == NULL)
	fatal ("far memory exhausted");
#else
      current->buffer = xmalloc (current->bufsize);
#endif /*__MSDOS__*/

      if (skip_test)
	current->buffered_chars = 0;
      else
	{
	  /* Check first part of file to see if it's a binary file.  */
	  current->buffered_chars = read (current->desc,
					  current->buffer,
#ifdef __MSDOS__
			(unsigned int)
#endif /*__MSDOS__*/
					  current->buffered_chars);
	  if (current->buffered_chars == -1)
	    pfatal_with_name (current->name);
	  return binary_file_p (current->buffer, current->buffered_chars);
	}
    }
  
  return 0;
}

/* Slurp the rest of the current file completely into memory.  */

void
slurp (current)
     struct file_data *current;
{
  size_t cc;

  if (current->desc < 0)
    /* The file is nonexistent.  */
    ;
  else if (S_ISREG (current->stat.st_mode))
    {
      /* It's a regular file; slurp in the rest all at once.  */
#ifdef __MSDOS__
      /* read inside of segment boundaries */
      while ((current->stat.st_size - current->buffered_chars) > 0)
      {
        unsigned nr;
        char HUGE *rp;

        rp = current->buffer + current->buffered_chars;
        nr = 0 - (unsigned)((long)rp & 0x0000FFFF);
        if (nr == 0)
          nr = 0xFFF0;
        nr = read (current->desc, rp, nr);
        if (nr == 0)
          break;
        if (nr == 0xFFFF)
          pfatal_with_name (current->name);
        current->buffered_chars += nr ;
      }
#else
      cc = current->stat.st_size - current->buffered_chars;
      if (cc)
	{
	  cc = read (current->desc,
		     current->buffer + current->buffered_chars,
		     cc);
	  if (cc == -1)
	    pfatal_with_name (current->name);
	  current->buffered_chars += cc;
	}
#endif /*__MSDOS__*/
    }
  /* It's not a regular file; read it, growing the buffer as needed.  */
  else if (always_text_flag || current->buffered_chars != 0)
    {
      for (;;)
	{
	  if (current->buffered_chars == current->bufsize)
	    {
#ifdef __MSDOS__
	      current->bufsize += 4096;
	      current->buffer = (char HUGE *) farrealloc (current->buffer, current->bufsize);
#else
	      current->bufsize = current->bufsize * 2;
	      current->buffer = xrealloc (current->buffer, current->bufsize);
#endif /*__MSDOS__*/
	    }
	  cc = read (current->desc,
		     current->buffer + current->buffered_chars,
		     current->bufsize - current->buffered_chars);
	  if (cc == 0)
	    break;
	  if (cc == -1)
	    pfatal_with_name (current->name);
	  current->buffered_chars += cc;
	}
#ifndef __MSDOS__
      /* Allocate just enough room for appended newline and sentinel.  */
      current->bufsize = current->buffered_chars + sizeof (word) + 1;
      current->buffer = xrealloc (current->buffer, current->bufsize);
#endif /*!__MSDOS__*/
    }
}

static int
ISWSPACE (char ch)
{
	return ch==' ' || ch=='\t';
}

/* Split the file into lines, simultaneously computing the equivalence class for
   each line. */
static void
find_and_hash_each_line (current)
     struct file_data *current;
{
  unsigned h;
  unsigned char const HUGE *p = (unsigned char const HUGE *) current->prefix_end;
  unsigned char c;
  int i, *bucket;
  size_t length;

  /* Cache often-used quantities in local variables to help the compiler.  */
  char const HUGE **linbuf = current->linbuf;
  int alloc_lines = current->alloc_lines;
  int line = 0;
  int linbuf_base = current->linbuf_base;
  int *cureqs = (int *) xmalloc (alloc_lines * sizeof (int));
  struct equivclass HUGE *eqs = equivs;
  int eqs_index = equivs_index;
  int eqs_alloc = equivs_alloc;
  char const HUGE *suffix_begin = current->suffix_begin;
  char const HUGE *bufend = current->buffer + current->buffered_chars;
  char const HUGE *incomplete_tail
    = current->missing_newline && ROBUST_OUTPUT_STYLE (output_style)
      ? bufend : (char const HUGE *) 0;
  int varies = length_varies;

  /* prepare_text_end put a zero word at the end of the buffer, 
  so we're not in danger of overrunning the end of the file */

  while ((char const HUGE *) p < suffix_begin)
    {
      char const HUGE *ip = (char const HUGE *) p;

      /* Compute the equivalence class (hash) for this line.  */

      h = 0;


      /*
	 loops advance pointer to eol (end of line)
	 respecting UNIX (\r), MS-DOS/Windows (\r\n), and MAC (\r) eols
	 Normally eol characters are hashed
	 If ignore_eol_diff option is set, eol characters are not hashed
	 and the eol characters are removed from line as well, in code
	 further down (after hashing_done label)
      */

      /* Hash this line until we find a newline. */
      if (ignore_case_flag)
	{
	  if (ignore_all_space_flag)
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
		if (! ISWSPACE (c))
		  h = HASH (h, isupper (c) ? tolower (c) : c);
	      }
	  else if (ignore_space_change_flag)
	    /* Note that \r must be hashed (if !ignore_eol_diff) */
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
		if (ISWSPACE (c))
		  {
		    /* skip whitespace after whitespace */
		    while (ISWSPACE (c = *p++))
		      ;
		    if (c=='\n')
		      {
			goto hashing_done; /* never hash trailing \n */
		      }
		    else if (c == '\r')
		      {
			/*
			    \r must be hashed if !ignore_eol_diff
			    Also, we must always advance to end of line
			    which means we can only stop on \r if not
			    followed by \n
			*/
			if (ignore_eol_diff)
			  {
			    if (*p == '\n') /* continue to LF after CR */
			      continue;
			    else
			      goto hashing_done;
			  }
		      }
		    else
		      {
			/* runs of whitespace not ending line hashed as one space */
		        h = HASH (h, ' ');
		      }
		  }
		/* c is now the first non-space.  */
		/* c can be a \r (CR) if !ignore_eol_diff */
		h = HASH (h, isupper (c) ? tolower (c) : c);
		if (c == '\r' && *p != '\n')
		  goto hashing_done;
	      }
	  else
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
		h = HASH (h, isupper (c) ? tolower (c) : c);
	      }
	}
      else
	{
	  if (ignore_all_space_flag)
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
		if (! ISWSPACE (c))
		  h = HASH (h, c);
	      }
	  else if (ignore_space_change_flag)
	    /* Note that \r must be hashed (if !ignore_eol_diff) */
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
		if (ISWSPACE (c))
		  {
		    /* skip whitespace after whitespace */
		    while (ISWSPACE (c = *p++))
		      ;
		    if (c=='\n')
		      {
			goto hashing_done; /* never hash trailing \n */
		      }
		    else if (c == '\r')
		      {
			/*
			    \r must be hashed if !ignore_eol_diff
			    Also, we must always advance to end of line
			    which means we can only stop on \r if not
			    followed by \n
			*/
			if (ignore_eol_diff)
			  {
			    if (*p == '\n') /* continue to LF after CR */
			      continue;
			    else
			      goto hashing_done;
			  }
		      }
		    else
		      {
			/* runs of whitespace not ending line hashed as one space */
		        h = HASH (h, ' ');
		      }
		  }
		/* c is now the first non-space.  */
		/* c can be a \r (CR) if !ignore_eol_diff */
		h = HASH (h, c);
		if (c == '\r' && *p != '\n')
		  goto hashing_done;
	      }
	  else
	    while ((c = *p++) != '\n' && (c != '\r' || *p == '\n'))
	      {
		if (ignore_eol_diff && (c=='\r' || c=='\n'))
		  continue;
	      h = HASH (h, c);
	      }
	}
   hashing_done:;

      bucket = &buckets[h % nbuckets];
      length = (char const HUGE *) p - ip - ((char const HUGE *) p == incomplete_tail);
      if (ignore_eol_diff)
	{
	  /* Remove all eols characters and adjust line length */
	  if (length>1 && p[-2]=='\r' && p[-1]=='\n')
	  {
	    ((char HUGE *)p)[-2] = 0;
	    length -= 2;
	  }
	  else if (p[-1] == '\n' || p[-1] == '\r')
	  {
	    ((char HUGE *)p)[-1] = 0;
	    --length;
	  }
	}
      for (i = *bucket;  ;  i = eqs[i].next)
	if (!i)
	  {
	    /* Create a new equivalence class in this bucket. */
	    i = eqs_index++;
	    if (i == eqs_alloc)
#ifdef __MSDOS__
          if ((eqs = (struct equivclass HUGE *) farrealloc (eqs, (long) (eqs_alloc*=2) * sizeof(*eqs))) == NULL)
            fatal ("far memory exhausted");
#else
	      eqs = (struct equivclass *)
		      xrealloc (eqs, (eqs_alloc*=2) * sizeof(*eqs));
#endif /*__MSDOS__*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?