io.c
来自「WinMerge可以显示两个文件的不同之处」· C语言 代码 · 共 848 行 · 第 1/2 页
C
848 行
eqs[i].next = *bucket;
eqs[i].hash = h;
eqs[i].line = ip;
eqs[i].length = length;
*bucket = i;
break;
}
/* "line_cmp" changed to "lines_differ" by diffutils 2.8.1 */
else if (eqs[i].hash == h
&& (eqs[i].length == length || varies)
&& ! line_cmp (eqs[i].line, eqs[i].length, ip, length))
/* Reuse existing equivalence class. */
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 HUGE **) xrealloc ((void *)(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 HUGE **) xrealloc ((void *)(linbuf + linbuf_base),
(alloc_lines - linbuf_base)
* sizeof (*linbuf))
- linbuf_base;
}
linbuf[line] = (char const HUGE *) p;
if ((char const HUGE *) p == bufend)
{
linbuf[line] -= (char const HUGE *) p == incomplete_tail;
break;
}
if (context <= i && no_diff_means_no_output)
break;
line++;
while (p[0] != '\n' && (p[0] != '\r' || p[1] == '\n'))
p++;
p++;
}
/* Done with cache in local variables. */
current->linbuf = linbuf;
current->valid_lines = line;
current->alloc_lines = alloc_lines;
current->equivs = cureqs;
equivs = eqs;
equivs_alloc = eqs_alloc;
equivs_index = eqs_index;
}
/* Prepare the end of the text. Make sure it's initialized.
Make sure text ends in a newline,
but remember that we had to add one unless -B is in effect. */
static void
prepare_text_end (current)
struct file_data *current;
{
FSIZE buffered_chars = current->buffered_chars;
char HUGE *p = current->buffer;
if (buffered_chars == 0 || p[buffered_chars - 1] == '\n' || p[buffered_chars - 1] == '\r')
current->missing_newline = 0;
else
{
p[buffered_chars++] = '\n';
current->buffered_chars = buffered_chars;
current->missing_newline = p[buffered_chars-2] != '\r' && ! ignore_blank_lines_flag;
}
/* Don't use uninitialized storage when planting or using sentinels. */
if (p)
bzero (p + buffered_chars, sizeof (word));
}
/* Given a vector of two file_data objects, find the identical
prefixes and suffixes of each object. */
static void
find_identical_ends (filevec)
struct file_data filevec[];
{
word HUGE *w0, HUGE *w1;
char HUGE *p0, HUGE *p1, HUGE *buffer0, HUGE *buffer1;
char const HUGE *end0, HUGE *beg0;
char const HUGE **linbuf0, HUGE **linbuf1;
int i, lines;
FSIZE n0, n1;
FSIZE tem;
FSIZE alloc_lines0, alloc_lines1;
int buffered_prefix, prefix_count, prefix_mask;
int ttt;
slurp (&filevec[0]);
if (filevec[0].desc != filevec[1].desc)
slurp (&filevec[1]);
else
{
filevec[1].buffer = filevec[0].buffer;
filevec[1].bufsize = filevec[0].bufsize;
filevec[1].buffered_chars = filevec[0].buffered_chars;
}
for (i = 0; i < 2; i++)
prepare_text_end (&filevec[i]);
/* Find identical prefix. */
p0 = buffer0 = filevec[0].buffer;
p1 = buffer1 = filevec[1].buffer;
n0 = filevec[0].buffered_chars;
n1 = filevec[1].buffered_chars;
if (p0 == p1)
/* The buffers are the same; sentinels won't work. */
p0 = p1 += n1;
else
{
/* Insert end sentinels, in this case characters that are guaranteed
to make the equality test false, and thus terminate the loop. */
if (n0 < n1)
p0[n0] = (char)(~p1[n0]);
else
p1[n1] = (char)(~p0[n1]);
/* Loop until first mismatch, or to the sentinel characters. */
/* Compare a word at a time for speed. */
w0 = (word *) p0;
w1 = (word *) p1;
while (*w0++ == *w1++)
;
--w0, --w1;
/* Do the last few bytes of comparison a byte at a time. */
p0 = (char *) w0;
p1 = (char *) w1;
while (*p0++ == *p1++)
;
--p0, --p1;
/* Don't mistakenly count missing newline as part of prefix. */
if (ROBUST_OUTPUT_STYLE (output_style)
&& (buffer0 + n0 - filevec[0].missing_newline < p0)
!=
(buffer1 + n1 - filevec[1].missing_newline < p1))
--p0, --p1;
}
/* Now P0 and P1 point at the first nonmatching characters. */
/* Skip back to last line-beginning in the prefix,
and then discard up to HORIZON_LINES lines from the prefix. */
i = horizon_lines;
/* This loop can be done in one line, but isn't not easy to read, so unrolled into simple statements */
while (p0 != buffer0)
{
/* we know p0[-1] == p1[-1], but maybe p0[0] != p1[0] */
int linestart=0;
if (p0[-1] == '\n')
linestart=1;
/* only count \r if not followed by a \n on either side */
if (p0[-1] == '\r' && p0[0] != '\n' && p1[0] != '\n')
linestart=1;
if (linestart && !(i--))
break;
--p0, --p1;
}
/* Record the prefix. */
filevec[0].prefix_end = p0;
filevec[1].prefix_end = p1;
/* Find identical suffix. */
/* P0 and P1 point beyond the last chars not yet compared. */
p0 = buffer0 + n0;
p1 = buffer1 + n1;
if (! ROBUST_OUTPUT_STYLE (output_style)
|| filevec[0].missing_newline == filevec[1].missing_newline)
{
end0 = p0; /* Addr of last char in file 0. */
/* Get value of P0 at which we should stop scanning backward:
this is when either P0 or P1 points just past the last char
of the identical prefix. */
beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1);
/* Scan back until chars don't match or we reach that point. */
while (p0 != beg0)
if (*--p0 != *--p1)
{
/* Point at the first char of the matching suffix. */
++p0, ++p1;
beg0 = p0;
break;
}
/* Are we at a line-beginning in both files? If not, add the rest of
this line to the main body. Discard up to HORIZON_LINES lines from
the identical suffix. Also, discard one extra line,
because shift_boundaries may need it. */
i = horizon_lines + !((buffer0 == p0 || p0[-1] == '\n' || (p0[-1] == '\r' && p0[0] != '\n'))
&&
(buffer1 == p1 || p1[-1] == '\n' || (p1[-1] == '\r' && p1[0] != '\n')));
while (i-- && p0 != end0)
while (*p0++ != '\n' && (p0[-1] != '\r' || p0[0] == '\n'))
;
p1 += p0 - (char HUGE *)beg0;
}
/* Record the suffix. */
filevec[0].suffix_begin = p0;
filevec[1].suffix_begin = p1;
/* Calculate number of lines of prefix to save.
prefix_count == 0 means save the whole prefix;
we need this with for options like -D that output the whole file.
We also need it for options like -F that output some preceding line;
at least we will need to find the last few lines,
but since we don't know how many, it's easiest to find them all.
Otherwise, prefix_count != 0. Save just prefix_count lines at start
of the line buffer; they'll be moved to the proper location later.
Handle 1 more line than the context says (because we count 1 too many),
rounded up to the next power of 2 to speed index computation. */
if (no_diff_means_no_output && ! function_regexp_list)
{
for (prefix_count = 1; prefix_count < context + 1; prefix_count *= 2)
;
prefix_mask = prefix_count - 1;
ttt = p0 - (char HUGE *)filevec[0].prefix_end;
alloc_lines0
= prefix_count
+ GUESS_LINES (0, 0, ttt)
+ context;
}
else
{
prefix_count = 0;
prefix_mask = ~0;
alloc_lines0 = GUESS_LINES (0, 0, n0);
}
lines = 0;
linbuf0 = (char const HUGE **) xmalloc (alloc_lines0 * sizeof (*linbuf0));
/* If the prefix is needed, find the prefix lines. */
if (! (no_diff_means_no_output
&& filevec[0].prefix_end == p0
&& filevec[1].prefix_end == p1))
{
p0 = buffer0;
end0 = filevec[0].prefix_end;
while (p0 != end0)
{
int l = lines++ & prefix_mask;
if ((FSIZE)l == alloc_lines0)
linbuf0 = (char const HUGE **) xrealloc ((void *)linbuf0, (alloc_lines0 *= 2)
* sizeof(*linbuf0));
linbuf0[l] = p0;
/* Perry/WinMerge (2004-01-05) altered original diffutils loop "while (*p0++ != '\n') ;" for other EOLs */
while (1)
{
char ch = *p0++;
/* stop at any eol, \n or \r or \r\n */
if (ch == '\n') break;
if (ch == '\r' && (p0==end0 || *p0!='\n')) break;
}
}
}
buffered_prefix = prefix_count && context < lines ? context : lines;
/* Allocate line buffer 1. */
tem = prefix_count ? filevec[1].suffix_begin - buffer1 : n1;
ttt = filevec[1].prefix_end - buffer1;
alloc_lines1
= (buffered_prefix
+ GUESS_LINES (lines, ttt, tem)
+ context);
linbuf1 = (char const HUGE **) xmalloc (alloc_lines1 * sizeof (*linbuf1));
if (buffered_prefix != lines)
{
/* Rotate prefix lines to proper location. */
for (i = 0; i < buffered_prefix; i++)
linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask];
for (i = 0; i < buffered_prefix; i++)
linbuf0[i] = linbuf1[i];
}
/* Initialize line buffer 1 from line buffer 0. */
for (i = 0; i < buffered_prefix; i++)
linbuf1[i] = linbuf0[i] - buffer0 + buffer1;
/* Record the line buffer, adjusted so that
linbuf*[0] points at the first differing line. */
filevec[0].linbuf = linbuf0 + buffered_prefix;
filevec[1].linbuf = linbuf1 + buffered_prefix;
filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
filevec[0].alloc_lines = alloc_lines0 - buffered_prefix;
filevec[1].alloc_lines = alloc_lines1 - buffered_prefix;
filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
}
/* Largest primes less than some power of two, for nbuckets. Values range
from useful to preposterous. If one of these numbers isn't prime
after all, don't blame it on me, blame it on primes (6) . . . */
static int const primes[] =
{
509,
1021,
2039,
4093,
8191,
16381,
32749,
#if 32767 < INT_MAX
65521,
131071,
262139,
524287,
1048573,
2097143,
4194301,
8388593,
16777213,
33554393,
67108859, /* Preposterously large . . . */
134217689,
268435399,
536870909,
1073741789,
2147483647,
#endif
0
};
/* Given a vector of two file_data objects, read the file associated
with each one, and build the table of equivalence classes.
Return 1 if either file appears to be a binary file.
If PRETEND_BINARY is nonzero, pretend they are binary regardless. */
int
read_files (filevec, pretend_binary)
struct file_data filevec[];
int pretend_binary;
{
int i;
int skip_test = always_text_flag | pretend_binary;
int appears_binary = pretend_binary | sip (&filevec[0], skip_test);
if (filevec[0].desc != filevec[1].desc)
appears_binary |= sip (&filevec[1], skip_test | appears_binary);
else
{
filevec[1].buffer = filevec[0].buffer;
filevec[1].bufsize = filevec[0].bufsize;
filevec[1].buffered_chars = filevec[0].buffered_chars;
}
if (appears_binary)
return 1;
find_identical_ends (filevec);
equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1;
#ifdef __MSDOS__
if ((equivs = (struct equivclass HUGE *) farmalloc ((long) equivs_alloc * sizeof(struct equivclass))) == NULL)
fatal ("far memory exhausted");
#else
equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
#endif /*__MSDOS__*/
/* Equivalence class 0 is permanently safe for lines that were not
hashed. Real equivalence classes start at 1. */
equivs_index = 1;
for (i = 0; primes[i] < equivs_alloc / 3; i++)
if (! primes[i])
abort ();
nbuckets = primes[i];
buckets = (int *) xmalloc (nbuckets * sizeof (*buckets));
bzero (buckets, nbuckets * sizeof (*buckets));
for (i = 0; i < 2; ++i)
find_and_hash_each_line (&filevec[i]);
filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
free (equivs);
free (buckets);
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?