⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 io.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
/* File I/O for GNU DIFF.   Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.This file is part of GNU DIFF.GNU DIFF is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU DIFF; see the file COPYING.  If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */#include "diff.h"/* 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.  */#ifndef word#define word int#endif/* 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 *line;	/* A line that fits this class. */  size_t length;	/* That line's length, not counting its newline.  */};/* Hash-table: array of buckets, each being a chain of equivalence classes.   buckets[-1] is reserved for incomplete lines.  */static int *buckets;/* Number of buckets in the hash table array, not counting buckets[-1]. */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 *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) (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.  */intsip (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->bufsize = sizeof (word);      current->buffer = xmalloc (current->bufsize);    }  else    {      current->bufsize = STAT_BLOCKSIZE (current->stat);      current->buffer = xmalloc (current->bufsize);      if (! skip_test)	{	  /* Check first part of file to see if it's a binary file.  */#if HAVE_SETMODE	  int oldmode = setmode (current->desc, O_BINARY);#endif	  size_t n = read (current->desc, current->buffer, current->bufsize);	  if (n == -1)	    pfatal_with_name (current->name);	  current->buffered_chars = n;#if HAVE_SETMODE	  if (oldmode != O_BINARY)	    {	      if (lseek (current->desc, - (off_t) n, SEEK_CUR) == -1)		pfatal_with_name (current->name);	      setmode (current->desc, oldmode);	      current->buffered_chars = 0;	    }#endif	  return binary_file_p (current->buffer, n);	}    }  current->buffered_chars = 0;  return 0;}/* Slurp the rest of the current file completely into memory.  */voidslurp (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.  */      /* Get the size out of the stat block.	 Allocate enough room for appended newline and sentinel.  */      cc = current->stat.st_size + 1 + sizeof (word);      if (current->bufsize < cc)	{	  current->bufsize = cc;	  current->buffer = xrealloc (current->buffer, cc);	}      if (current->buffered_chars < current->stat.st_size)	{	  cc = read (current->desc,		     current->buffer + current->buffered_chars,		     current->stat.st_size - current->buffered_chars);	  if (cc == -1)	    pfatal_with_name (current->name);	  current->buffered_chars += cc;	}    }  /* 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)	    {	      current->bufsize = current->bufsize * 2;	      current->buffer = xrealloc (current->buffer, current->bufsize);	    }	  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;	}      /* Allocate just enough room for appended newline and sentinel.  */      current->bufsize = current->buffered_chars + 1 + sizeof (word);      current->buffer = xrealloc (current->buffer, current->bufsize);    }}/* Split the file into lines, simultaneously computing the equivalence class for   each line. */static voidfind_and_hash_each_line (current)     struct file_data *current;{  unsigned h;  unsigned char const *p = (unsigned char const *) 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 **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 *eqs = equivs;  int eqs_index = equivs_index;  int eqs_alloc = equivs_alloc;  char const *suffix_begin = current->suffix_begin;  char const *bufend = current->buffer + current->buffered_chars;  int use_line_cmp = ignore_some_line_changes;  while ((char const *) p < suffix_begin)    {      char const *ip = (char const *) p;      /* Compute the equivalence class for this line.  */      h = 0;      /* Hash this line until we find a newline. */      if (ignore_case_flag)	{	  if (ignore_all_space_flag)	    while ((c = *p++) != '\n')	      {		if (! ISSPACE (c))		  h = HASH (h, ISUPPER (c) ? tolower (c) : c);	      }	  else if (ignore_space_change_flag)	    while ((c = *p++) != '\n')	      {		if (ISSPACE (c))		  {		    for (;;)		      {			c = *p++;			if (!ISSPACE (c))			  break;			if (c == '\n')			  goto hashing_done;		      }		    h = HASH (h, ' ');		  }		/* C is now the first non-space.  */		h = HASH (h, ISUPPER (c) ? tolower (c) : c);	      }	  else	    while ((c = *p++) != '\n')	      h = HASH (h, ISUPPER (c) ? tolower (c) : c);	}      else	{	  if (ignore_all_space_flag)	    while ((c = *p++) != '\n')	      {		if (! ISSPACE (c))		  h = HASH (h, c);	      }	  else if (ignore_space_change_flag)	    while ((c = *p++) != '\n')	      {		if (ISSPACE (c))		  {		    for (;;)		      {			c = *p++;			if (!ISSPACE (c))			  break;			if (c == '\n')			  goto hashing_done;		      }		    h = HASH (h, ' ');		  }		/* C is now the first non-space.  */		h = HASH (h, c);	      }	  else	    while ((c = *p++) != '\n')	      h = HASH (h, c);	}   hashing_done:;      bucket = &buckets[h % nbuckets];      length = (char const *) p - ip - 1;      if ((char const *) p == bufend	  && current->missing_newline	  && ROBUST_OUTPUT_STYLE (output_style))	{	  /* This line is incomplete.  If this is significant,	     put the line into bucket[-1].  */	  if (! (ignore_space_change_flag | ignore_all_space_flag))	    bucket = &buckets[-1];	  /* Omit the inserted newline when computing linbuf later.  */	  p--;	  bufend = suffix_begin = (char const *) p;	}      for (i = *bucket;  ;  i = eqs[i].next)	if (!i)	  {	    /* Create a new equivalence class in this bucket. */	    i = eqs_index++;	    if (i == eqs_alloc)	      eqs = (struct equivclass *)		      xrealloc (eqs, (eqs_alloc*=2) * sizeof(*eqs));	    eqs[i].next = *bucket;	    eqs[i].hash = h;	    eqs[i].line = ip;	    eqs[i].length = length;	    *bucket = i;	    break;	  }	else if (eqs[i].hash == h)	  {	    char const *eqline = eqs[i].line;	    /* Reuse existing equivalence class if the lines are identical.	       This detects the common case of exact identity	       faster than complete comparison would.  */	    if (eqs[i].length == length && memcmp (eqline, ip, length) == 0)	      break;	    /* Reuse existing class if line_cmp reports the lines equal.  */	    if (use_line_cmp && line_cmp (eqline, ip) == 0)	      break;	  }      /* Maybe increase the size of the line table. */      if (line == alloc_lines)	{	  /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */	  alloc_lines = 2 * alloc_lines - linbuf_base;	  cureqs = (int *) xrealloc (cureqs, alloc_lines * sizeof (*cureqs));	  linbuf = (char const **) xrealloc (linbuf + linbuf_base,					     (alloc_lines - linbuf_base)					     * sizeof (*linbuf))		   - linbuf_base;	}      linbuf[line] = ip;      cureqs[line] = i;      ++line;    }  current->buffered_lines = line;  for (i = 0;  ;  i++)    {      /* Record the line start for lines in the suffix that we care about.	 Record one more line start than lines,	 so that we can compute the length of any buffered line.  */      if (line == alloc_lines)	{	  /* Double (alloc_lines - linbuf_base) by adding to alloc_lines.  */	  alloc_lines = 2 * alloc_lines - linbuf_base;	  linbuf = (char const **) xrealloc (linbuf + linbuf_base,					     (alloc_lines - linbuf_base)

⌨️ 快捷键说明

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