📄 util.c
字号:
else if (c2 == '\r')
c2 = 0;
}
if (c1 != c2)
break;
}
/* If we got here, c1 == c2 */
if (!c1)
return 0;
}
}
return (1);
}
/* Find the consecutive changes at the start of the script START.
Return the last link before the first gap. */
struct change *
find_change (start)
struct change *start;
{
return start;
}
struct change *
find_reverse_change (start)
struct change *start;
{
return start;
}
/* Divide SCRIPT into pieces by calling HUNKFUN and
print each piece with PRINTFUN.
Both functions take one arg, an edit script.
HUNKFUN is called with the tail of the script
and returns the last link that belongs together with the start
of the tail.
PRINTFUN takes a subscript which belongs together (with a null
link at the end) and prints it. */
void
print_script (script, hunkfun, printfun)
struct change *script;
struct change * (*hunkfun) PARAMS((struct change *));
void (*printfun) PARAMS((struct change *));
{
struct change *next = script;
while (next)
{
struct change *this, *end;
/* Find a set of changes that belong together. */
this = next;
end = (*hunkfun) (next);
/* Disconnect them from the rest of the changes,
making them a hunk, and remember the rest for next iteration. */
next = end->link;
end->link = 0;
#ifdef DEBUG
debug_script (this);
#endif
/* Print this hunk. */
(*printfun) (this);
/* Reconnect the script so it will all be freed properly. */
end->link = next;
}
}
/* Print the text of a single line LINE,
flagging it with the characters in LINE_FLAG (which say whether
the line is inserted, deleted, changed, etc.). */
void
print_1_line (line_flag, line)
char const *line_flag;
char const HUGE * const *line;
{
char const HUGE *text = line[0], HUGE *limit = line[1]; /* Help the compiler. */
FILE *out = outfile; /* Help the compiler some more. */
char const *flag_format = 0;
/* If -T was specified, use a Tab between the line-flag and the text.
Otherwise use a Space (as Unix diff does).
Print neither space nor tab if line-flags are empty. */
if (line_flag && *line_flag)
{
flag_format = tab_align_flag ? "%s\t" : "%s ";
fprintf (out, flag_format, line_flag);
}
output_1_line (text, limit, flag_format, line_flag);
if ((!line_flag || line_flag[0]) && limit[-1] != '\n' && limit[-1] != '\r'
&& line_end_char == '\n')
fprintf (out, "\n\\ No newline at end of file\n");
}
/*
A version of fwrite which converts any embedded \r or \n or \r\n to \n
before passing it to fwrite. This is meant to be used with mixed eol mode input
being written to a text mode stream.
*/
static size_t
fwrite_textify( const void *buffer, size_t size, size_t count, FILE *stream )
{
/*
\r = carriage return
\n = line feed
We have to handle the carriage returns (\r) specially
because some of them may be the first half of a \r\n pair
We output \r\n for any solo \r or solo \n, but also for any \r\n pair
so the tricky part is just avoiding outputing \r\n\r\n for the \r\n pair.
*/
int bytes=0;
unsigned int i;
const char * text = buffer;
int cr = 0;
char ch;
i = 0;
while (1)
{
// first handle any pending carriage returns
// before even checking if we've finished file
if (cr)
{
// currently handling a carriage return
// we always finish the line for carriage returns
bytes += fwrite("\n", 1, 1, stream);
if (i==size*count)
{
// we're done
return bytes;
}
// now check to see if we need to swallow the trailing line feed
// of a carriage return/line feed pair (\r\n)
if (text[i] == '\n')
++i;
// finished the pending carriage return
cr = 0;
}
// check if we finished
if (i==size*count)
return bytes;
ch = text[i];
// first check if new character is a carriage return
if (ch == '\r')
{
// activate our special mode flag, and go to next character
cr = 1;
++i;
continue;
}
// (any bare \n characters are ok, stream will convert them)
bytes += fwrite(&text[i], 1, 1, stream);
++i;
}
}
/* Output a line from TEXT up to LIMIT. Without -t, output verbatim.
With -t, expand white space characters to spaces, and if FLAG_FORMAT
is nonzero, output it with argument LINE_FLAG after every
internal carriage return, so that tab stops continue to line up. */
void
output_1_line (text, limit, flag_format, line_flag)
char const HUGE *text, HUGE *limit, *flag_format, *line_flag;
{
char * pos = NULL;
if (!tab_expand_flag)
fwrite_textify (text, sizeof (char), limit - text, outfile);
else
{
register FILE *out = outfile;
register unsigned char c;
register char const HUGE *t = text;
register unsigned column = 0;
while (t < limit)
switch ((c = *t++))
{
case '\t':
{
unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
column += spaces;
do
putc (' ', out);
while (--spaces);
}
break;
case '\r':
putc (c, out);
if (flag_format && t < limit && *t != '\n')
fprintf (out, flag_format, line_flag);
column = 0;
break;
case '\b':
if (column == 0)
continue;
column--;
putc (c, out);
break;
default:
if (isprint (c))
column++;
putc (c, out);
break;
}
}
}
int
change_letter (inserts, deletes)
int inserts, deletes;
{
if (!inserts)
return 'd';
else if (!deletes)
return 'a';
else
return 'c';
}
/* Translate an internal line number (an index into diff's table of lines)
into an actual line number in the input file.
The internal line number is LNUM. FILE points to the data on the file.
Internal line numbers count from 0 starting after the prefix.
Actual line numbers count from 1 within the entire file. */
int
translate_line_number (file, lnum)
struct file_data const *file;
int lnum;
{
return lnum + file->prefix_lines + 1;
}
void
translate_range (file, a, b, aptr, bptr)
struct file_data const *file;
int a, b;
int *aptr, *bptr;
{
*aptr = translate_line_number (file, a - 1) + 1;
*bptr = translate_line_number (file, b + 1) - 1;
}
/* Print a pair of line numbers with SEPCHAR, translated for file FILE.
If the two numbers are identical, print just one number.
Args A and B are internal line numbers.
We print the translated (real) line numbers. */
void
print_number_range (sepchar, file, a, b)
int sepchar;
struct file_data *file;
int a, b;
{
int trans_a, trans_b;
translate_range (file, a, b, &trans_a, &trans_b);
/* Note: we can have B < A in the case of a range of no lines.
In this case, we should print the line number before the range,
which is B. */
if (trans_b > trans_a)
fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
else
fprintf (outfile, "%d", trans_b);
}
int iseolch (char ch)
{
return ch=='\n' || ch=='\r';
}
/* Look at a hunk of edit script and report the range of lines in each file
that it applies to. HUNK is the start of the hunk, which is a chain
of `struct change'. The first and last line numbers of file 0 are stored in
*FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
Note that these are internal line numbers that count from 0.
If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
Also set *DELETES nonzero if any lines of file 0 are deleted
and set *INSERTS nonzero if any lines of file 1 are inserted.
If only ignorable lines are inserted or deleted, both are
set to 0. */
void
analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
struct change *hunk;
int *first0, *last0, *first1, *last1;
int *deletes, *inserts;
{
int l0, l1, show_from, show_to;
int i;
int trivial = ignore_blank_lines_flag || ignore_regexp_list;
struct change *next;
show_from = show_to = 0;
*first0 = hunk->line0;
*first1 = hunk->line1;
next = hunk;
do
{
l0 = next->line0 + next->deleted - 1;
l1 = next->line1 + next->inserted - 1;
show_from += next->deleted;
show_to += next->inserted;
for (i = next->line0; i <= l0 && trivial; i++)
if (!ignore_blank_lines_flag || (!iseolch(files[0].linbuf[i][0]) && files[0].linbuf[i][0] != 0))
{
struct regexp_list *r;
char const HUGE *line = files[0].linbuf[i];
int len = files[0].linbuf[i + 1] - line;
for (r = ignore_regexp_list; r; r = r->next)
if (0 <= re_search (&r->buf, line, len, 0, len, 0))
break; /* Found a match. Ignore this line. */
/* If we got all the way through the regexp list without
finding a match, then it's nontrivial. */
if (!r)
trivial = 0;
}
for (i = next->line1; i <= l1 && trivial; i++)
if (!ignore_blank_lines_flag || (!iseolch(files[1].linbuf[i][0]) && files[1].linbuf[i][0] != 0))
{
struct regexp_list *r;
char const HUGE *line = files[1].linbuf[i];
int len = files[1].linbuf[i + 1] - line;
for (r = ignore_regexp_list; r; r = r->next)
if (0 <= re_search (&r->buf, line, len, 0, len, 0))
break; /* Found a match. Ignore this line. */
/* If we got all the way through the regexp list without
finding a match, then it's nontrivial. */
if (!r)
trivial = 0;
}
}
while ((next = next->link) != 0);
*last0 = l0;
*last1 = l1;
/* If all inserted or deleted lines are ignorable,
tell the caller to ignore this hunk. */
if (trivial)
show_from = show_to = 0;
/* WinMerge editor needs to know if there were trivial changes though,
so stash that off in the trivial field */
hunk->trivial = trivial;
*deletes = show_from;
*inserts = show_to;
}
/* malloc a block of memory, with fatal error message if we can't do it. */
VOID *
xmalloc (size)
size_t size;
{
register VOID *value;
if (size == 0)
size = 1;
value = (VOID *) malloc (size);
if (!value)
#ifdef __MSDOS__
fatal ("real memory exhausted");
#else
fatal ("virtual memory exhausted");
#endif
return value;
}
/* realloc a block of memory, with fatal error message if we can't do it. */
VOID *
xrealloc (old, size)
VOID *old;
size_t size;
{
register VOID *value;
if (size == 0)
size = 1;
value = (VOID *) realloc (old, size);
if (!value)
#ifdef __MSDOS__
fatal ("real memory exhausted");
#else
fatal ("virtual memory exhausted");
#endif
return value;
}
/* Concatenate three strings, returning a newly malloc'd string. */
char *
concat (s1, s2, s3)
char const *s1, *s2, *s3;
{
size_t len = strlen (s1) + strlen (s2) + strlen (s3);
char *new = xmalloc (len + 1);
sprintf (new, "%s%s%s", s1, s2, s3);
return new;
}
/* Yield the newly malloc'd pathname
of the file in DIR whose filename is FILE. */
char *
dir_file_pathname (dir, file)
char const *dir, *file;
{
#if defined(__MSDOS__) || defined(__NT__) || defined(WIN32)
char sep = dir[strlen(dir) - 1];
return concat (dir, "\\" + (*dir && ((sep == '/') || (sep == '\\'))), file);
#else
return concat (dir, "/" + (*dir && dir[strlen (dir) - 1] == '/'), file);
#endif /*__MSDOS__||__NT__*/
}
void
debug_script (sp)
struct change *sp;
{
fflush (stdout);
for (; sp; sp = sp->link)
fprintf (stderr, "%3d %3d delete %d insert %d\n",
sp->line0, sp->line1, sp->deleted, sp->inserted);
fflush (stderr);
}
#if !HAVE_MEMCHR
char *
memchr (s, c, n)
char const *s;
int c;
size_t n;
{
unsigned char const *p = (unsigned char const *) s, *lim = p + n;
for (; p < lim; p++)
if (*p == c)
return (char *) p;
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -