joachim99@52
|
1 /* File I/O for GNU DIFF.
|
joachim99@52
|
2
|
joachim99@68
|
3 Modified for KDiff3 by Joachim Eibl 2003, 2004, 2005.
|
joachim99@53
|
4 The original file was part of GNU DIFF.
|
joachim99@52
|
5
|
joachim99@52
|
6 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002
|
joachim99@52
|
7 Free Software Foundation, Inc.
|
joachim99@52
|
8
|
joachim99@52
|
9 GNU DIFF is free software; you can redistribute it and/or modify
|
joachim99@52
|
10 it under the terms of the GNU General Public License as published by
|
joachim99@52
|
11 the Free Software Foundation; either version 2, or (at your option)
|
joachim99@52
|
12 any later version.
|
joachim99@52
|
13
|
joachim99@52
|
14 GNU DIFF is distributed in the hope that it will be useful,
|
joachim99@52
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
joachim99@52
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
joachim99@52
|
17 GNU General Public License for more details.
|
joachim99@52
|
18
|
joachim99@52
|
19 You should have received a copy of the GNU General Public License
|
joachim99@52
|
20 along with this program; see the file COPYING.
|
joachim99@52
|
21 If not, write to the Free Software Foundation,
|
joachim99@69
|
22 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. */
|
joachim99@52
|
23
|
joachim99@52
|
24 #include "gnudiff_diff.h"
|
joachim99@52
|
25 #include <stdlib.h>
|
joachim99@52
|
26
|
joachim99@52
|
27 /* Rotate an unsigned value to the left. */
|
joachim99@52
|
28 #define ROL(v, n) ((v) << (n) | (v) >> (sizeof (v) * CHAR_BIT - (n)))
|
joachim99@52
|
29
|
joachim99@52
|
30 /* Given a hash value and a new character, return a new hash value. */
|
joachim99@52
|
31 #define HASH(h, c) ((c) + ROL (h, 7))
|
joachim99@52
|
32
|
joachim99@52
|
33 /* The type of a hash value. */
|
joachim99@52
|
34 typedef size_t hash_value;
|
joachim99@52
|
35 verify (hash_value_is_unsigned, ! TYPE_SIGNED (hash_value));
|
joachim99@52
|
36
|
joachim99@52
|
37 /* Lines are put into equivalence classes of lines that match in lines_differ.
|
joachim99@52
|
38 Each equivalence class is represented by one of these structures,
|
joachim99@52
|
39 but only while the classes are being computed.
|
joachim99@52
|
40 Afterward, each class is represented by a number. */
|
joachim99@52
|
41 struct equivclass
|
joachim99@52
|
42 {
|
joachim99@52
|
43 lin next; /* Next item in this bucket. */
|
joachim99@52
|
44 hash_value hash; /* Hash of lines in this class. */
|
joachim99@68
|
45 const QChar *line; /* A line that fits this class. */
|
joachim99@52
|
46 size_t length; /* That line's length, not counting its newline. */
|
joachim99@52
|
47 };
|
joachim99@52
|
48
|
joachim99@52
|
49 /* Hash-table: array of buckets, each being a chain of equivalence classes.
|
joachim99@52
|
50 buckets[-1] is reserved for incomplete lines. */
|
joachim99@52
|
51 static lin *buckets;
|
joachim99@52
|
52
|
joachim99@52
|
53 /* Number of buckets in the hash table array, not counting buckets[-1]. */
|
joachim99@52
|
54 static size_t nbuckets;
|
joachim99@52
|
55
|
joachim99@52
|
56 /* Array in which the equivalence classes are allocated.
|
joachim99@52
|
57 The bucket-chains go through the elements in this array.
|
joachim99@52
|
58 The number of an equivalence class is its index in this array. */
|
joachim99@52
|
59 static struct equivclass *equivs;
|
joachim99@52
|
60
|
joachim99@52
|
61 /* Index of first free element in the array `equivs'. */
|
joachim99@52
|
62 static lin equivs_index;
|
joachim99@52
|
63
|
joachim99@52
|
64 /* Number of elements allocated in the array `equivs'. */
|
joachim99@52
|
65 static lin equivs_alloc;
|
joachim99@52
|
66
|
joachim99@52
|
67
|
joachim99@52
|
68 /* Check for binary files and compare them for exact identity. */
|
joachim99@52
|
69
|
joachim99@52
|
70 /* Return 1 if BUF contains a non text character.
|
joachim99@52
|
71 SIZE is the number of characters in BUF. */
|
joachim99@52
|
72
|
joachim99@52
|
73 #define binary_file_p(buf, size) (memchr (buf, 0, size) != 0)
|
joachim99@52
|
74
|
joachim99@69
|
75 /* Compare two lines (typically one from each input file)
|
joachim99@69
|
76 according to the command line options.
|
joachim99@69
|
77 For efficiency, this is invoked only when the lines do not match exactly
|
joachim99@69
|
78 but an option like -i might cause us to ignore the difference.
|
joachim99@69
|
79 Return nonzero if the lines differ. */
|
joachim99@69
|
80
|
joachim99@69
|
81 bool GnuDiff::lines_differ (const QChar *s1, size_t len1, const QChar *s2, size_t len2 )
|
joachim99@69
|
82 {
|
joachim99@69
|
83 const QChar *t1 = s1;
|
joachim99@69
|
84 const QChar *t2 = s2;
|
joachim99@69
|
85 const QChar *s1end = s1+len1;
|
joachim99@69
|
86 const QChar *s2end = s2+len2;
|
joachim99@69
|
87
|
joachim99@69
|
88 for ( ; ; ++t1, ++t2 )
|
joachim99@69
|
89 {
|
joachim99@69
|
90 /* Test for exact char equality first, since it's a common case. */
|
joachim99@69
|
91 if ( t1!=s1end && t2!=s2end && *t1==*t2 )
|
joachim99@69
|
92 continue;
|
joachim99@69
|
93 else
|
joachim99@69
|
94 {
|
joachim99@69
|
95 while ( t1!=s1end &&
|
joachim99@69
|
96 ( bIgnoreWhiteSpace && isWhite( *t1 ) ||
|
joachim99@69
|
97 bIgnoreNumbers && (t1->isDigit() || *t1=='-' || *t1=='.' )))
|
joachim99@69
|
98 {
|
joachim99@69
|
99 ++t1;
|
joachim99@69
|
100 }
|
joachim99@69
|
101
|
joachim99@69
|
102 while ( t2 != s2end &&
|
joachim99@69
|
103 ( bIgnoreWhiteSpace && isWhite( *t2 ) ||
|
joachim99@69
|
104 bIgnoreNumbers && (t2->isDigit() || *t2=='-' || *t2=='.' )))
|
joachim99@69
|
105 {
|
joachim99@69
|
106 ++t2;
|
joachim99@69
|
107 }
|
joachim99@69
|
108
|
joachim99@69
|
109 if ( t1!=s1end && t2!=s2end )
|
joachim99@69
|
110 {
|
joachim99@69
|
111 if (ignore_case)
|
joachim99@69
|
112 { /* Lowercase comparison. */
|
joachim99@75
|
113 if ( t1->toLower() == t2->toLower() )
|
joachim99@69
|
114 continue;
|
joachim99@69
|
115 }
|
joachim99@69
|
116 else if ( *t1 == *t2 )
|
joachim99@69
|
117 continue;
|
joachim99@69
|
118 else
|
joachim99@69
|
119 return true;
|
joachim99@69
|
120 }
|
joachim99@69
|
121 else if ( t1==s1end && t2==s2end )
|
joachim99@69
|
122 return false;
|
joachim99@69
|
123 else
|
joachim99@69
|
124 return true;
|
joachim99@69
|
125 }
|
joachim99@69
|
126 }
|
joachim99@69
|
127 return false;
|
joachim99@69
|
128 }
|
joachim99@69
|
129
|
joachim99@52
|
130
|
joachim99@52
|
131 /* Split the file into lines, simultaneously computing the equivalence
|
joachim99@52
|
132 class for each line. */
|
joachim99@52
|
133
|
joachim99@53
|
134 void GnuDiff::find_and_hash_each_line (struct file_data *current)
|
joachim99@52
|
135 {
|
joachim99@52
|
136 hash_value h;
|
joachim99@69
|
137 const QChar *p = current->prefix_end;
|
joachim99@68
|
138 QChar c;
|
joachim99@52
|
139 lin i, *bucket;
|
joachim99@52
|
140 size_t length;
|
joachim99@52
|
141
|
joachim99@52
|
142 /* Cache often-used quantities in local variables to help the compiler. */
|
joachim99@68
|
143 const QChar **linbuf = current->linbuf;
|
joachim99@52
|
144 lin alloc_lines = current->alloc_lines;
|
joachim99@52
|
145 lin line = 0;
|
joachim99@52
|
146 lin linbuf_base = current->linbuf_base;
|
joachim99@52
|
147 lin *cureqs = (lin*)xmalloc (alloc_lines * sizeof *cureqs);
|
joachim99@52
|
148 struct equivclass *eqs = equivs;
|
joachim99@52
|
149 lin eqs_index = equivs_index;
|
joachim99@52
|
150 lin eqs_alloc = equivs_alloc;
|
joachim99@68
|
151 const QChar *suffix_begin = current->suffix_begin;
|
joachim99@69
|
152 const QChar *bufend = current->buffer + current->buffered;
|
joachim99@52
|
153 bool diff_length_compare_anyway =
|
joachim99@52
|
154 ignore_white_space != IGNORE_NO_WHITE_SPACE || bIgnoreNumbers;
|
joachim99@52
|
155 bool same_length_diff_contents_compare_anyway =
|
joachim99@52
|
156 diff_length_compare_anyway | ignore_case;
|
joachim99@52
|
157
|
joachim99@69
|
158 while ( p < suffix_begin)
|
joachim99@52
|
159 {
|
joachim99@69
|
160 const QChar *ip = p;
|
joachim99@52
|
161
|
joachim99@52
|
162 h = 0;
|
joachim99@52
|
163
|
joachim99@69
|
164 /* Hash this line until we find a newline or bufend is reached. */
|
joachim99@52
|
165 if (ignore_case)
|
joachim99@52
|
166 switch (ignore_white_space)
|
joachim99@52
|
167 {
|
joachim99@52
|
168 case IGNORE_ALL_SPACE:
|
joachim99@69
|
169 while ( p<bufend && (c = *p) != '\n' )
|
joachim99@69
|
170 {
|
joachim99@68
|
171 if (! (isWhite(c) || bIgnoreNumbers && (c.isDigit() || c=='-' || c=='.' ) ))
|
joachim99@75
|
172 h = HASH (h, c.toLower().unicode());
|
joachim99@69
|
173 ++p;
|
joachim99@69
|
174 }
|
joachim99@52
|
175 break;
|
joachim99@52
|
176
|
joachim99@52
|
177 default:
|
joachim99@69
|
178 while ( p<bufend && (c = *p) != '\n' )
|
joachim99@69
|
179 {
|
joachim99@75
|
180 h = HASH (h, c.toLower().unicode());
|
joachim99@69
|
181 ++p;
|
joachim99@69
|
182 }
|
joachim99@52
|
183 break;
|
joachim99@52
|
184 }
|
joachim99@52
|
185 else
|
joachim99@52
|
186 switch (ignore_white_space)
|
joachim99@52
|
187 {
|
joachim99@52
|
188 case IGNORE_ALL_SPACE:
|
joachim99@69
|
189 while ( p<bufend && (c = *p) != '\n')
|
joachim99@69
|
190 {
|
joachim99@68
|
191 if (! (isWhite(c)|| bIgnoreNumbers && (c.isDigit() || c=='-' || c=='.' ) ))
|
joachim99@69
|
192 h = HASH (h, c.unicode());
|
joachim99@69
|
193 ++p;
|
joachim99@69
|
194 }
|
joachim99@52
|
195 break;
|
joachim99@52
|
196
|
joachim99@52
|
197 default:
|
joachim99@69
|
198 while ( p<bufend && (c = *p) != '\n')
|
joachim99@69
|
199 {
|
joachim99@68
|
200 h = HASH (h, c.unicode());
|
joachim99@69
|
201 ++p;
|
joachim99@69
|
202 }
|
joachim99@52
|
203 break;
|
joachim99@52
|
204 }
|
joachim99@52
|
205
|
joachim99@68
|
206 bucket = &buckets[h % nbuckets];
|
joachim99@69
|
207 length = p - ip;
|
joachim99@69
|
208 ++p;
|
joachim99@52
|
209
|
joachim99@52
|
210 for (i = *bucket; ; i = eqs[i].next)
|
joachim99@52
|
211 if (!i)
|
joachim99@52
|
212 {
|
joachim99@52
|
213 /* Create a new equivalence class in this bucket. */
|
joachim99@52
|
214 i = eqs_index++;
|
joachim99@52
|
215 if (i == eqs_alloc)
|
joachim99@52
|
216 {
|
joachim99@66
|
217 if ((lin)(PTRDIFF_MAX / (2 * sizeof *eqs)) <= eqs_alloc)
|
joachim99@52
|
218 xalloc_die ();
|
joachim99@52
|
219 eqs_alloc *= 2;
|
joachim99@52
|
220 eqs = (equivclass*)xrealloc (eqs, eqs_alloc * sizeof *eqs);
|
joachim99@52
|
221 }
|
joachim99@52
|
222 eqs[i].next = *bucket;
|
joachim99@52
|
223 eqs[i].hash = h;
|
joachim99@52
|
224 eqs[i].line = ip;
|
joachim99@52
|
225 eqs[i].length = length;
|
joachim99@52
|
226 *bucket = i;
|
joachim99@52
|
227 break;
|
joachim99@52
|
228 }
|
joachim99@52
|
229 else if (eqs[i].hash == h)
|
joachim99@52
|
230 {
|
joachim99@68
|
231 const QChar *eqline = eqs[i].line;
|
joachim99@52
|
232
|
joachim99@52
|
233 /* Reuse existing class if lines_differ reports the lines
|
joachim99@52
|
234 equal. */
|
joachim99@52
|
235 if (eqs[i].length == length)
|
joachim99@52
|
236 {
|
joachim99@52
|
237 /* Reuse existing equivalence class if the lines are identical.
|
joachim99@52
|
238 This detects the common case of exact identity
|
joachim99@52
|
239 faster than lines_differ would. */
|
joachim99@69
|
240 if (memcmp (eqline, ip, length*sizeof(QChar)) == 0)
|
joachim99@52
|
241 break;
|
joachim99@52
|
242 if (!same_length_diff_contents_compare_anyway)
|
joachim99@52
|
243 continue;
|
joachim99@52
|
244 }
|
joachim99@52
|
245 else if (!diff_length_compare_anyway)
|
joachim99@52
|
246 continue;
|
joachim99@52
|
247
|
joachim99@69
|
248 if (! lines_differ (eqline, eqs[i].length, ip, length))
|
joachim99@52
|
249 break;
|
joachim99@52
|
250 }
|
joachim99@52
|
251
|
joachim99@52
|
252 /* Maybe increase the size of the line table. */
|
joachim99@52
|
253 if (line == alloc_lines)
|
joachim99@52
|
254 {
|
joachim99@52
|
255 /* Double (alloc_lines - linbuf_base) by adding to alloc_lines. */
|
joachim99@66
|
256 if ((lin)(PTRDIFF_MAX / 3) <= alloc_lines
|
joachim99@66
|
257 || (lin)(PTRDIFF_MAX / sizeof *cureqs) <= 2 * alloc_lines - linbuf_base
|
joachim99@66
|
258 || (lin)(PTRDIFF_MAX / sizeof *linbuf) <= alloc_lines - linbuf_base)
|
joachim99@52
|
259 xalloc_die ();
|
joachim99@52
|
260 alloc_lines = 2 * alloc_lines - linbuf_base;
|
joachim99@52
|
261 cureqs =(lin*) xrealloc (cureqs, alloc_lines * sizeof *cureqs);
|
joachim99@52
|
262 linbuf += linbuf_base;
|
joachim99@68
|
263 linbuf = (const QChar**) xrealloc (linbuf,
|
joachim99@52
|
264 (alloc_lines - linbuf_base) * sizeof *linbuf);
|
joachim99@52
|
265 linbuf -= linbuf_base;
|
joachim99@52
|
266 }
|
joachim99@52
|
267 linbuf[line] = ip;
|
joachim99@52
|
268 cureqs[line] = i;
|
joachim99@52
|
269 ++line;
|
joachim99@52
|
270 }
|
joachim99@52
|
271
|
joachim99@52
|
272 current->buffered_lines = line;
|
joachim99@52
|
273
|
joachim99@52
|
274 for (i = 0; ; i++)
|
joachim99@52
|
275 {
|
joachim99@52
|
276 /* Record the line start for lines in the suffix that we care about.
|
joachim99@52
|
277 Record one more line start than lines,
|
joachim99@52
|
278 so that we can compute the length of any buffered line. */
|
joachim99@52
|
279 if (line == alloc_lines)
|
joachim99@52
|
280 {
|
joachim99@52
|
281 /* Double (alloc_lines - linbuf_base) by adding to alloc_lines. */
|
joachim99@66
|
282 if ((lin)(PTRDIFF_MAX / 3) <= alloc_lines
|
joachim99@66
|
283 || (lin)(PTRDIFF_MAX / sizeof *cureqs) <= 2 * alloc_lines - linbuf_base
|
joachim99@66
|
284 || (lin)(PTRDIFF_MAX / sizeof *linbuf) <= alloc_lines - linbuf_base)
|
joachim99@52
|
285 xalloc_die ();
|
joachim99@52
|
286 alloc_lines = 2 * alloc_lines - linbuf_base;
|
joachim99@52
|
287 linbuf += linbuf_base;
|
joachim99@68
|
288 linbuf = (const QChar**)xrealloc (linbuf,
|
joachim99@52
|
289 (alloc_lines - linbuf_base) * sizeof *linbuf);
|
joachim99@52
|
290 linbuf -= linbuf_base;
|
joachim99@52
|
291 }
|
joachim99@69
|
292 linbuf[line] = p;
|
joachim99@52
|
293
|
joachim99@69
|
294 if ( p >= bufend)
|
joachim99@52
|
295 break;
|
joachim99@52
|
296
|
joachim99@52
|
297 if (context <= i && no_diff_means_no_output)
|
joachim99@52
|
298 break;
|
joachim99@52
|
299
|
joachim99@52
|
300 line++;
|
joachim99@52
|
301
|
joachim99@69
|
302 while (p<bufend && *p++ != '\n')
|
joachim99@69
|
303 continue;
|
joachim99@52
|
304 }
|
joachim99@52
|
305
|
joachim99@52
|
306 /* Done with cache in local variables. */
|
joachim99@52
|
307 current->linbuf = linbuf;
|
joachim99@52
|
308 current->valid_lines = line;
|
joachim99@52
|
309 current->alloc_lines = alloc_lines;
|
joachim99@52
|
310 current->equivs = cureqs;
|
joachim99@52
|
311 equivs = eqs;
|
joachim99@52
|
312 equivs_alloc = eqs_alloc;
|
joachim99@52
|
313 equivs_index = eqs_index;
|
joachim99@52
|
314 }
|
joachim99@52
|
315
|
joachim99@52
|
316 /* We have found N lines in a buffer of size S; guess the
|
joachim99@52
|
317 proportionate number of lines that will be found in a buffer of
|
joachim99@52
|
318 size T. However, do not guess a number of lines so large that the
|
joachim99@52
|
319 resulting line table might cause overflow in size calculations. */
|
joachim99@52
|
320 static lin
|
joachim99@52
|
321 guess_lines (lin n, size_t s, size_t t)
|
joachim99@52
|
322 {
|
joachim99@52
|
323 size_t guessed_bytes_per_line = n < 10 ? 32 : s / (n - 1);
|
joachim99@52
|
324 lin guessed_lines = MAX (1, t / guessed_bytes_per_line);
|
joachim99@68
|
325 return MIN (guessed_lines, (lin)(PTRDIFF_MAX / (2 * sizeof (QChar *) + 1) - 5)) + 5;
|
joachim99@52
|
326 }
|
joachim99@52
|
327
|
joachim99@52
|
328 /* Given a vector of two file_data objects, find the identical
|
joachim99@52
|
329 prefixes and suffixes of each object. */
|
joachim99@52
|
330
|
joachim99@53
|
331 void GnuDiff::find_identical_ends (struct file_data filevec[])
|
joachim99@52
|
332 {
|
joachim99@69
|
333 /* Find identical prefix. */
|
joachim99@69
|
334 const QChar *p0, *p1, *buffer0, *buffer1;
|
joachim99@69
|
335 p0 = buffer0 = filevec[0].buffer;
|
joachim99@69
|
336 p1 = buffer1 = filevec[1].buffer;
|
joachim99@52
|
337 size_t n0, n1;
|
joachim99@52
|
338 n0 = filevec[0].buffered;
|
joachim99@52
|
339 n1 = filevec[1].buffered;
|
joachim99@69
|
340 const QChar* const pEnd0 = p0 + n0;
|
joachim99@69
|
341 const QChar* const pEnd1 = p1 + n1;
|
joachim99@52
|
342
|
joachim99@52
|
343 if (p0 == p1)
|
joachim99@52
|
344 /* The buffers are the same; sentinels won't work. */
|
joachim99@52
|
345 p0 = p1 += n1;
|
joachim99@52
|
346 else
|
joachim99@52
|
347 {
|
joachim99@69
|
348 /* Loop until first mismatch, or end. */
|
joachim99@69
|
349 while ( p0!=pEnd0 && p1!=pEnd1 && *p0 == *p1 )
|
joachim99@69
|
350 {
|
joachim99@69
|
351 p0++;
|
joachim99@69
|
352 p1++;
|
joachim99@69
|
353 }
|
joachim99@52
|
354 }
|
joachim99@52
|
355
|
joachim99@52
|
356 /* Now P0 and P1 point at the first nonmatching characters. */
|
joachim99@52
|
357
|
joachim99@69
|
358 /* Skip back to last line-beginning in the prefix. */
|
joachim99@69
|
359 while (p0 != buffer0 && (p0[-1] != '\n' ))
|
joachim99@52
|
360 p0--, p1--;
|
joachim99@52
|
361
|
joachim99@52
|
362 /* Record the prefix. */
|
joachim99@52
|
363 filevec[0].prefix_end = p0;
|
joachim99@52
|
364 filevec[1].prefix_end = p1;
|
joachim99@52
|
365
|
joachim99@52
|
366 /* Find identical suffix. */
|
joachim99@52
|
367
|
joachim99@52
|
368 /* P0 and P1 point beyond the last chars not yet compared. */
|
joachim99@52
|
369 p0 = buffer0 + n0;
|
joachim99@52
|
370 p1 = buffer1 + n1;
|
joachim99@52
|
371
|
joachim99@69
|
372 const QChar *end0, *beg0;
|
joachim99@69
|
373 end0 = p0; /* Addr of last char in file 0. */
|
joachim99@52
|
374
|
joachim99@69
|
375 /* Get value of P0 at which we should stop scanning backward:
|
joachim99@69
|
376 this is when either P0 or P1 points just past the last char
|
joachim99@69
|
377 of the identical prefix. */
|
joachim99@69
|
378 beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1);
|
joachim99@52
|
379
|
joachim99@69
|
380 /* Scan back until chars don't match or we reach that point. */
|
joachim99@69
|
381 for (; p0 != beg0; p0--, p1--)
|
joachim99@69
|
382 {
|
joachim99@69
|
383 if (*p0 != *p1)
|
joachim99@69
|
384 {
|
joachim99@69
|
385 /* Point at the first char of the matching suffix. */
|
joachim99@69
|
386 beg0 = p0;
|
joachim99@69
|
387 break;
|
joachim99@69
|
388 }
|
joachim99@69
|
389 }
|
joachim99@52
|
390
|
joachim99@69
|
391 // Go to the next line (skip last line with a difference)
|
joachim99@69
|
392 if ( p0 != end0 )
|
joachim99@69
|
393 {
|
joachim99@69
|
394 if (*p0 != *p1)
|
joachim99@69
|
395 ++p0;
|
joachim99@69
|
396 while ( p0<pEnd0 && *p0++ != '\n')
|
joachim99@69
|
397 continue;
|
joachim99@69
|
398 }
|
joachim99@52
|
399
|
joachim99@69
|
400 p1 += p0 - beg0;
|
joachim99@52
|
401
|
joachim99@52
|
402 /* Record the suffix. */
|
joachim99@52
|
403 filevec[0].suffix_begin = p0;
|
joachim99@52
|
404 filevec[1].suffix_begin = p1;
|
joachim99@52
|
405
|
joachim99@52
|
406 /* Calculate number of lines of prefix to save.
|
joachim99@52
|
407
|
joachim99@52
|
408 prefix_count == 0 means save the whole prefix;
|
joachim99@52
|
409 we need this for options like -D that output the whole file,
|
joachim99@52
|
410 or for enormous contexts (to avoid worrying about arithmetic overflow).
|
joachim99@52
|
411 We also need it for options like -F that output some preceding line;
|
joachim99@52
|
412 at least we will need to find the last few lines,
|
joachim99@52
|
413 but since we don't know how many, it's easiest to find them all.
|
joachim99@52
|
414
|
joachim99@52
|
415 Otherwise, prefix_count != 0. Save just prefix_count lines at start
|
joachim99@52
|
416 of the line buffer; they'll be moved to the proper location later.
|
joachim99@52
|
417 Handle 1 more line than the context says (because we count 1 too many),
|
joachim99@52
|
418 rounded up to the next power of 2 to speed index computation. */
|
joachim99@52
|
419
|
joachim99@69
|
420 const QChar **linbuf0, **linbuf1;
|
joachim99@69
|
421 lin alloc_lines0, alloc_lines1;
|
joachim99@69
|
422 lin buffered_prefix, prefix_count, prefix_mask;
|
joachim99@69
|
423 lin middle_guess, suffix_guess;
|
joachim99@52
|
424 if (no_diff_means_no_output
|
joachim99@66
|
425 && context < (lin)(LIN_MAX / 4) && context < (lin)(n0))
|
joachim99@52
|
426 {
|
joachim99@52
|
427 middle_guess = guess_lines (0, 0, p0 - filevec[0].prefix_end);
|
joachim99@52
|
428 suffix_guess = guess_lines (0, 0, buffer0 + n0 - p0);
|
joachim99@52
|
429 for (prefix_count = 1; prefix_count <= context; prefix_count *= 2)
|
joachim99@52
|
430 continue;
|
joachim99@52
|
431 alloc_lines0 = (prefix_count + middle_guess
|
joachim99@52
|
432 + MIN (context, suffix_guess));
|
joachim99@52
|
433 }
|
joachim99@52
|
434 else
|
joachim99@52
|
435 {
|
joachim99@52
|
436 prefix_count = 0;
|
joachim99@52
|
437 alloc_lines0 = guess_lines (0, 0, n0);
|
joachim99@52
|
438 }
|
joachim99@52
|
439
|
joachim99@52
|
440 prefix_mask = prefix_count - 1;
|
joachim99@69
|
441 lin lines = 0;
|
joachim99@68
|
442 linbuf0 = (const QChar**) xmalloc (alloc_lines0 * sizeof(*linbuf0));
|
joachim99@52
|
443 p0 = buffer0;
|
joachim99@52
|
444
|
joachim99@52
|
445 /* If the prefix is needed, find the prefix lines. */
|
joachim99@52
|
446 if (! (no_diff_means_no_output
|
joachim99@52
|
447 && filevec[0].prefix_end == p0
|
joachim99@52
|
448 && filevec[1].prefix_end == p1))
|
joachim99@52
|
449 {
|
joachim99@52
|
450 end0 = filevec[0].prefix_end;
|
joachim99@52
|
451 while (p0 != end0)
|
joachim99@52
|
452 {
|
joachim99@52
|
453 lin l = lines++ & prefix_mask;
|
joachim99@52
|
454 if (l == alloc_lines0)
|
joachim99@52
|
455 {
|
joachim99@66
|
456 if ((lin)(PTRDIFF_MAX / (2 * sizeof *linbuf0)) <= alloc_lines0)
|
joachim99@52
|
457 xalloc_die ();
|
joachim99@52
|
458 alloc_lines0 *= 2;
|
joachim99@68
|
459 linbuf0 = (const QChar**) xrealloc (linbuf0, alloc_lines0 * sizeof(*linbuf0));
|
joachim99@52
|
460 }
|
joachim99@52
|
461 linbuf0[l] = p0;
|
joachim99@69
|
462 while ( p0<pEnd0 && *p0++ != '\n' )
|
joachim99@52
|
463 continue;
|
joachim99@52
|
464 }
|
joachim99@52
|
465 }
|
joachim99@52
|
466 buffered_prefix = prefix_count && context < lines ? context : lines;
|
joachim99@52
|
467
|
joachim99@52
|
468 /* Allocate line buffer 1. */
|
joachim99@52
|
469
|
joachim99@52
|
470 middle_guess = guess_lines (lines, p0 - buffer0, p1 - filevec[1].prefix_end);
|
joachim99@52
|
471 suffix_guess = guess_lines (lines, p0 - buffer0, buffer1 + n1 - p1);
|
joachim99@52
|
472 alloc_lines1 = buffered_prefix + middle_guess + MIN (context, suffix_guess);
|
joachim99@52
|
473 if (alloc_lines1 < buffered_prefix
|
joachim99@66
|
474 || (lin)(PTRDIFF_MAX / sizeof *linbuf1) <= alloc_lines1)
|
joachim99@52
|
475 xalloc_die ();
|
joachim99@68
|
476 linbuf1 = (const QChar**)xmalloc (alloc_lines1 * sizeof(*linbuf1));
|
joachim99@52
|
477
|
joachim99@69
|
478 lin i;
|
joachim99@52
|
479 if (buffered_prefix != lines)
|
joachim99@69
|
480 {
|
joachim99@52
|
481 /* Rotate prefix lines to proper location. */
|
joachim99@52
|
482 for (i = 0; i < buffered_prefix; i++)
|
joachim99@52
|
483 linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask];
|
joachim99@52
|
484 for (i = 0; i < buffered_prefix; i++)
|
joachim99@52
|
485 linbuf0[i] = linbuf1[i];
|
joachim99@69
|
486 }
|
joachim99@52
|
487
|
joachim99@52
|
488 /* Initialize line buffer 1 from line buffer 0. */
|
joachim99@52
|
489 for (i = 0; i < buffered_prefix; i++)
|
joachim99@52
|
490 linbuf1[i] = linbuf0[i] - buffer0 + buffer1;
|
joachim99@52
|
491
|
joachim99@52
|
492 /* Record the line buffer, adjusted so that
|
joachim99@52
|
493 linbuf[0] points at the first differing line. */
|
joachim99@52
|
494 filevec[0].linbuf = linbuf0 + buffered_prefix;
|
joachim99@52
|
495 filevec[1].linbuf = linbuf1 + buffered_prefix;
|
joachim99@52
|
496 filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
|
joachim99@52
|
497 filevec[0].alloc_lines = alloc_lines0 - buffered_prefix;
|
joachim99@52
|
498 filevec[1].alloc_lines = alloc_lines1 - buffered_prefix;
|
joachim99@52
|
499 filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
|
joachim99@52
|
500 }
|
joachim99@52
|
501
|
joachim99@52
|
502 /* If 1 < k, then (2**k - prime_offset[k]) is the largest prime less
|
joachim99@52
|
503 than 2**k. This table is derived from Chris K. Caldwell's list
|
joachim99@52
|
504 <http://www.utm.edu/research/primes/lists/2small/>. */
|
joachim99@52
|
505
|
joachim99@52
|
506 static unsigned char const prime_offset[] =
|
joachim99@52
|
507 {
|
joachim99@52
|
508 0, 0, 1, 1, 3, 1, 3, 1, 5, 3, 3, 9, 3, 1, 3, 19, 15, 1, 5, 1, 3, 9, 3,
|
joachim99@52
|
509 15, 3, 39, 5, 39, 57, 3, 35, 1, 5, 9, 41, 31, 5, 25, 45, 7, 87, 21,
|
joachim99@52
|
510 11, 57, 17, 55, 21, 115, 59, 81, 27, 129, 47, 111, 33, 55, 5, 13, 27,
|
joachim99@52
|
511 55, 93, 1, 57, 25
|
joachim99@52
|
512 };
|
joachim99@52
|
513
|
joachim99@52
|
514 /* Verify that this host's size_t is not too wide for the above table. */
|
joachim99@52
|
515
|
joachim99@52
|
516 verify (enough_prime_offsets,
|
joachim99@52
|
517 sizeof (size_t) * CHAR_BIT <= sizeof prime_offset);
|
joachim99@52
|
518
|
joachim99@52
|
519 /* Given a vector of two file_data objects, read the file associated
|
joachim99@52
|
520 with each one, and build the table of equivalence classes.
|
joachim99@52
|
521 Return nonzero if either file appears to be a binary file.
|
joachim99@52
|
522 If PRETEND_BINARY is nonzero, pretend they are binary regardless. */
|
joachim99@52
|
523
|
joachim99@52
|
524 bool
|
joachim99@53
|
525 GnuDiff::read_files (struct file_data filevec[], bool /*pretend_binary*/)
|
joachim99@52
|
526 {
|
joachim99@52
|
527 int i;
|
joachim99@52
|
528
|
joachim99@52
|
529 find_identical_ends (filevec);
|
joachim99@52
|
530
|
joachim99@52
|
531 equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1;
|
joachim99@66
|
532 if ((lin)(PTRDIFF_MAX / sizeof *equivs) <= equivs_alloc)
|
joachim99@52
|
533 xalloc_die ();
|
joachim99@52
|
534 equivs = (equivclass*)xmalloc (equivs_alloc * sizeof *equivs);
|
joachim99@52
|
535 /* Equivalence class 0 is permanently safe for lines that were not
|
joachim99@52
|
536 hashed. Real equivalence classes start at 1. */
|
joachim99@52
|
537 equivs_index = 1;
|
joachim99@52
|
538
|
joachim99@52
|
539 /* Allocate (one plus) a prime number of hash buckets. Use a prime
|
joachim99@52
|
540 number between 1/3 and 2/3 of the value of equiv_allocs,
|
joachim99@52
|
541 approximately. */
|
joachim99@52
|
542 for (i = 9; 1 << i < equivs_alloc / 3; i++)
|
joachim99@52
|
543 continue;
|
joachim99@52
|
544 nbuckets = ((size_t) 1 << i) - prime_offset[i];
|
joachim99@52
|
545 if (PTRDIFF_MAX / sizeof *buckets <= nbuckets)
|
joachim99@52
|
546 xalloc_die ();
|
joachim99@52
|
547 buckets = (lin*)zalloc ((nbuckets + 1) * sizeof *buckets);
|
joachim99@52
|
548 buckets++;
|
joachim99@52
|
549
|
joachim99@52
|
550 for (i = 0; i < 2; i++)
|
joachim99@52
|
551 find_and_hash_each_line (&filevec[i]);
|
joachim99@52
|
552
|
joachim99@52
|
553 filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
|
joachim99@52
|
554
|
joachim99@52
|
555 free (equivs);
|
joachim99@52
|
556 free (buckets - 1);
|
joachim99@52
|
557
|
joachim99@52
|
558 return 0;
|
joachim99@52
|
559 }
|