Commit e93f7253 authored by Bruce Momjian's avatar Bruce Momjian

Add C functions to centralize entab processing

parent db90bcf8
...@@ -28,6 +28,24 @@ extern char *optarg; ...@@ -28,6 +28,24 @@ extern char *optarg;
extern int optind; extern int optind;
static void
output_accumulated_spaces(int *prv_spaces, char **dst)
{
for (; *prv_spaces > 0; *prv_spaces--)
*((*dst)++) = ' ';
}
static void
trim_trailing_whitespace(int *prv_spaces, char **dst, char *out_line)
{
while (*dst > out_line &&
(*((*dst) - 1) == ' ' || *((*dst) - 1) == '\t'))
(*dst)--;
*prv_spaces = 0;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
...@@ -168,19 +186,14 @@ main(int argc, char **argv) ...@@ -168,19 +186,14 @@ main(int argc, char **argv)
prv_spaces = 0; prv_spaces = 0;
} }
else else
/* output accumulated spaces */ output_accumulated_spaces(&prv_spaces, &dst);
{
for (; prv_spaces > 0; prv_spaces--)
*(dst++) = ' ';
}
} }
} }
/* Not a potential space/tab replacement */ /* Not a potential space/tab replacement */
else else
{ {
/* output accumulated spaces */ /* output accumulated spaces */
for (; prv_spaces > 0; prv_spaces--) output_accumulated_spaces(&prv_spaces, &dst);
*(dst++) = ' ';
/* This can only happen in a quote. */ /* This can only happen in a quote. */
if (*src == '\t') if (*src == '\t')
col_in_tab = 0; col_in_tab = 0;
...@@ -211,13 +224,7 @@ main(int argc, char **argv) ...@@ -211,13 +224,7 @@ main(int argc, char **argv)
clip_lines == TRUE && clip_lines == TRUE &&
quote_char == ' ' && quote_char == ' ' &&
escaped == FALSE) escaped == FALSE)
{ trim_trailing_whitespace(&prv_spaces, &dst, out_line);
/* trim spaces starting from the end */
while (dst > out_line &&
(*(dst - 1) == ' ' || *(dst - 1) == '\t'))
dst--;
prv_spaces = 0;
}
*(dst++) = *src; *(dst++) = *src;
} }
col_in_tab %= tab_size; col_in_tab %= tab_size;
...@@ -225,15 +232,8 @@ main(int argc, char **argv) ...@@ -225,15 +232,8 @@ main(int argc, char **argv)
} }
/* for cases where the last line of file has no newline */ /* for cases where the last line of file has no newline */
if (clip_lines == TRUE && escaped == FALSE) if (clip_lines == TRUE && escaped == FALSE)
{ trim_trailing_whitespace(&prv_spaces, &dst, out_line);
while (dst > out_line && output_accumulated_spaces(&prv_spaces, &dst);
(*(dst - 1) == ' ' || *(dst - 1) == '\t'))
dst--;
prv_spaces = 0;
}
/* output accumulated spaces */
for (; prv_spaces > 0; prv_spaces--)
*(dst++) = ' ';
*dst = NUL; *dst = NUL;
if (fputs(out_line, stdout) == EOF) if (fputs(out_line, stdout) == EOF)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment