From f536d45f7a9e10ee15a19caf2d4c44c1c43d8f07 Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Wed, 27 Jan 2021 17:13:17 +0100 Subject: [PATCH] Apply new code format from CLion --- src/generate.c | 737 ++++++++++++++++++++++++----------------------- src/generate.h | 9 +- src/remove.c | 763 ++++++++++++++++++++++++++----------------------- src/shape.c | 147 +++++----- src/tools.c | 217 +++++++------- src/tools.h | 46 +-- 6 files changed, 1022 insertions(+), 897 deletions(-) diff --git a/src/generate.c b/src/generate.c index c183d62..1478351 100644 --- a/src/generate.c +++ b/src/generate.c @@ -5,12 +5,12 @@ * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2, as published * by the Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -33,8 +33,9 @@ #include "generate.h" -static int horiz_precalc (const sentry_t *sarr, - size_t *topiltf, size_t *botiltf, size_t *hspace) + +static int horiz_precalc(const sentry_t *sarr, + size_t *topiltf, size_t *botiltf, size_t *hspace) /* * Calculate data for horizontal box side generation. * @@ -43,26 +44,26 @@ static int horiz_precalc (const sentry_t *sarr, * topiltf RESULT: individual lines (columns) to fill by shapes 1, 2, and 3 * botiltf in top part of box (topiltf) and bottom part of box * hspace RESULT: number of columns excluding corners (sum over iltf) - * + * * RETURNS: == 0 on success (result values are set) * != 0 on error * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - int tnumsh; /* number of existent shapes in top part */ - int bnumsh; - size_t twidth; /* current hspace for top side */ - size_t bwidth; /* current hspace for bottom side */ - int i; - size_t target_width; /* assumed text width for minimum box size */ - int btoggle, ttoggle; /* for case 3 w/ 2 elastics */ + int tnumsh; /* number of existent shapes in top part */ + int bnumsh; + size_t twidth; /* current hspace for top side */ + size_t bwidth; /* current hspace for bottom side */ + int i; + size_t target_width; /* assumed text width for minimum box size */ + int btoggle, ttoggle; /* for case 3 w/ 2 elastics */ /* * Initialize future result values */ - memset (topiltf, 0, (SHAPES_PER_SIDE-2) * sizeof(size_t)); - memset (botiltf, 0, (SHAPES_PER_SIDE-2) * sizeof(size_t)); + memset(topiltf, 0, (SHAPES_PER_SIDE - 2) * sizeof(size_t)); + memset(botiltf, 0, (SHAPES_PER_SIDE - 2) * sizeof(size_t)); *hspace = 0; /* @@ -70,29 +71,34 @@ static int horiz_precalc (const sentry_t *sarr, * minimum box size required by current design */ if (input.maxline >= (opt.design->minwidth - sarr[north_side[0]].width - - sarr[north_side[SHAPES_PER_SIDE-1]].width)) { + sarr[north_side[SHAPES_PER_SIDE - 1]].width)) { target_width = input.maxline; } else { target_width = opt.design->minwidth - sarr[north_side[0]].width - - sarr[north_side[SHAPES_PER_SIDE-1]].width; + sarr[north_side[SHAPES_PER_SIDE - 1]].width; } /* * Compute number of existent shapes in top and in bottom part */ - tnumsh = 0; bnumsh = 0; - for (i=1; iminwidth %d, input.maxline %d, target_width" - " %d, tnumsh %d, bnumsh %d\n", opt.design->minwidth, - input.maxline, target_width, tnumsh, bnumsh); - #endif +#ifdef DEBUG + fprintf (stderr, "in horiz_precalc:\n "); + fprintf (stderr, "opt.design->minwidth %d, input.maxline %d, target_width" + " %d, tnumsh %d, bnumsh %d\n", opt.design->minwidth, + input.maxline, target_width, tnumsh, bnumsh); +#endif twidth = 0; bwidth = 0; @@ -132,13 +138,12 @@ static int horiz_precalc (const sentry_t *sarr, /* * only one shape -> it must be elastic */ - for (i=1; i one must be elastic, the other must not */ - for (i=1; i one or two of them must be elastic * If two are elastic, they are the two outer ones. */ - for (i=1; i= (opt.design->minheight - sarr[west_side[0]].height - - sarr[west_side[SHAPES_PER_SIDE-1]].height)) { + sarr[west_side[SHAPES_PER_SIDE - 1]].height)) { target_height = input.anz_lines; } else { target_height = opt.design->minheight - sarr[west_side[0]].height - - sarr[west_side[SHAPES_PER_SIDE-1]].height; + sarr[west_side[SHAPES_PER_SIDE - 1]].height; } /* * Compute number of existent shapes in left and right part (1..3) */ - lnumsh = 0; rnumsh = 0; - for (i=1; i it must be elastic */ - for (i=1; i one must be elastic, the other must not */ - for (i=1; i one or two of them must be elastic * If two are elastic, they are the two outer ones. */ - for (i=1; iheight; ++line) { - result->chars[line] = (char *) calloc (1, result->width+1); + for (line = 0; line < result->height; ++line) { + result->chars[line] = (char *) calloc(1, result->width + 1); if (result->chars[line] == NULL) { - perror (PROJECT); - if ((long)--line >= 0) do { - BFREE (result->chars[line--]); - } while ((long)line >= 0); + perror(PROJECT); + if ((long) --line >= 0) { + do { + BFREE (result->chars[line--]); + } while ((long) line >= 0); + } return 1; /* out of memory */ } } - cshape = (seite == north_side)? 0 : 2; + cshape = (seite == north_side) ? 0 : 2; - for (j=0; jwidth; j+=sarr[seite[cshape+1]].width) { - while (iltf[cshape] == 0) - cshape += (seite == north_side)? 1 : -1; - for (line=0; lineheight; ++line) - strcat (result->chars[line], sarr[seite[cshape+1]].chars[line]); - iltf[cshape] -= sarr[seite[cshape+1]].width; + for (j = 0; j < result->width; j += sarr[seite[cshape + 1]].width) { + while (iltf[cshape] == 0) { + cshape += (seite == north_side) ? 1 : -1; + } + for (line = 0; line < result->height; ++line) { + strcat(result->chars[line], sarr[seite[cshape + 1]].chars[line]); + } + iltf[cshape] -= sarr[seite[cshape + 1]].width; } return 0; /* all clear */ @@ -448,44 +453,47 @@ static int vert_assemble (const sentry_t *sarr, const shape_t *seite, -static void horiz_assemble (const sentry_t *sarr, const shape_t *seite, - size_t *iltf, sentry_t *result) +static void horiz_assemble(const sentry_t *sarr, const shape_t *seite, + size_t *iltf, sentry_t *result) { - size_t j; - size_t sc; /* index to shape chars (lines) */ - int cshape; /* current shape (idx to iltf) */ + size_t j; + size_t sc; /* index to shape chars (lines) */ + int cshape; /* current shape (idx to iltf) */ shape_t ctop, cbottom; if (seite == east_side) { ctop = seite[0]; - cbottom = seite[SHAPES_PER_SIDE-1]; + cbottom = seite[SHAPES_PER_SIDE - 1]; cshape = 0; } else { - ctop = seite[SHAPES_PER_SIDE-1]; + ctop = seite[SHAPES_PER_SIDE - 1]; cbottom = seite[0]; cshape = 2; } - for (j=0; jchars[j] = sarr[ctop].chars[j]; - for (j=0; jchars[result->height-sarr[cbottom].height+j] = - sarr[cbottom].chars[j]; + } + for (j = 0; j < sarr[cbottom].height; ++j) { + result->chars[result->height - sarr[cbottom].height + j] = + sarr[cbottom].chars[j]; + } sc = 0; - for (j=sarr[ctop].height; j < result->height-sarr[cbottom].height; ++j) - { + for (j = sarr[ctop].height; j < result->height - sarr[cbottom].height; ++j) { while (iltf[cshape] == 0) { - if (seite == east_side) + if (seite == east_side) { ++cshape; - else + } else { --cshape; + } sc = 0; } - if (sc == sarr[seite[cshape+1]].height) + if (sc == sarr[seite[cshape + 1]].height) { sc = 0; - result->chars[j] = sarr[seite[cshape+1]].chars[sc]; + } + result->chars[j] = sarr[seite[cshape + 1]].chars[sc]; ++sc; iltf[cshape] -= 1; } @@ -493,7 +501,7 @@ static void horiz_assemble (const sentry_t *sarr, const shape_t *seite, -static int horiz_generate (sentry_t *tresult, sentry_t *bresult) +static int horiz_generate(sentry_t *tresult, sentry_t *bresult) /* * Generate top and bottom parts of box (excluding corners). * @@ -503,44 +511,52 @@ static int horiz_generate (sentry_t *tresult, sentry_t *bresult) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - size_t biltf[SHAPES_PER_SIDE-2]; /* individual lines to fill (bottom) */ - size_t tiltf[SHAPES_PER_SIDE-2]; /* individual lines to fill (top) */ - int rc; /* received return code */ + size_t biltf[SHAPES_PER_SIDE - 2]; /* individual lines to fill (bottom) */ + size_t tiltf[SHAPES_PER_SIDE - 2]; /* individual lines to fill (top) */ + int rc; /* received return code */ - tresult->height = highest (opt.design->shape, - SHAPES_PER_SIDE, NW, NNW, N, NNE, NE); - bresult->height = highest (opt.design->shape, - SHAPES_PER_SIDE, SW, SSW, S, SSE, SE); + tresult->height = highest(opt.design->shape, + SHAPES_PER_SIDE, NW, NNW, N, NNE, NE); + bresult->height = highest(opt.design->shape, + SHAPES_PER_SIDE, SW, SSW, S, SSE, SE); - rc = horiz_precalc (opt.design->shape, tiltf, biltf, &(tresult->width)); - if (rc) return rc; + rc = horiz_precalc(opt.design->shape, tiltf, biltf, &(tresult->width)); + if (rc) { + return rc; + } bresult->width = tresult->width; - #ifdef DEBUG - fprintf (stderr, "Top side box rect width %d, height %d.\n", - tresult->width, tresult->height); - fprintf (stderr, "Top columns to fill: %s %d, %s %d, %s %d.\n", - shape_name[north_side[1]], tiltf[0], - shape_name[north_side[2]], tiltf[1], - shape_name[north_side[3]], tiltf[2]); - fprintf (stderr, "Bottom side box rect width %d, height %d.\n", - bresult->width, bresult->height); - fprintf (stderr, "Bottom columns to fill: %s %d, %s %d, %s %d.\n", - shape_name[south_side[1]], biltf[0], - shape_name[south_side[2]], biltf[1], - shape_name[south_side[3]], biltf[2]); - #endif +#ifdef DEBUG + fprintf (stderr, "Top side box rect width %d, height %d.\n", + tresult->width, tresult->height); + fprintf (stderr, "Top columns to fill: %s %d, %s %d, %s %d.\n", + shape_name[north_side[1]], tiltf[0], + shape_name[north_side[2]], tiltf[1], + shape_name[north_side[3]], tiltf[2]); + fprintf (stderr, "Bottom side box rect width %d, height %d.\n", + bresult->width, bresult->height); + fprintf (stderr, "Bottom columns to fill: %s %d, %s %d, %s %d.\n", + shape_name[south_side[1]], biltf[0], + shape_name[south_side[2]], biltf[1], + shape_name[south_side[3]], biltf[2]); +#endif - tresult->chars = (char **) calloc (tresult->height, sizeof(char *)); - bresult->chars = (char **) calloc (bresult->height, sizeof(char *)); - if (tresult->chars == NULL || bresult->chars == NULL) return 1; + tresult->chars = (char **) calloc(tresult->height, sizeof(char *)); + bresult->chars = (char **) calloc(bresult->height, sizeof(char *)); + if (tresult->chars == NULL || bresult->chars == NULL) { + return 1; + } - rc = vert_assemble (opt.design->shape, north_side, tiltf, tresult); - if (rc) return rc; - rc = vert_assemble (opt.design->shape, south_side, biltf, bresult); - if (rc) return rc; + rc = vert_assemble(opt.design->shape, north_side, tiltf, tresult); + if (rc) { + return rc; + } + rc = vert_assemble(opt.design->shape, south_side, biltf, bresult); + if (rc) { + return rc; + } - #if defined(DEBUG) && 1 +#if defined(DEBUG) && 1 { /* * Debugging code - Output horizontal sides of box @@ -557,14 +573,14 @@ static int horiz_generate (sentry_t *tresult, sentry_t *bresult) bresult->chars[j]? bresult->chars[j] : "(null)"); } } - #endif +#endif return 0; /* all clear */ } -static int vert_generate (sentry_t *lresult, sentry_t *rresult) +static int vert_generate(sentry_t *lresult, sentry_t *rresult) /* * Generate vertical sides of box. * @@ -575,47 +591,53 @@ static int vert_generate (sentry_t *lresult, sentry_t *rresult) */ { size_t vspace = 0; - size_t leftiltf[SHAPES_PER_SIDE-2]; /* individual lines to fill */ - size_t rightiltf[SHAPES_PER_SIDE-2]; /* individual lines to fill */ - int rc; /* received return code */ + size_t leftiltf[SHAPES_PER_SIDE - 2]; /* individual lines to fill */ + size_t rightiltf[SHAPES_PER_SIDE - 2]; /* individual lines to fill */ + int rc; /* received return code */ - lresult->width = widest (opt.design->shape, - SHAPES_PER_SIDE, SW, WSW, W, WNW, NW); - rresult->width = widest (opt.design->shape, - SHAPES_PER_SIDE, SE, ESE, E, ENE, NE); + lresult->width = widest(opt.design->shape, + SHAPES_PER_SIDE, SW, WSW, W, WNW, NW); + rresult->width = widest(opt.design->shape, + SHAPES_PER_SIDE, SE, ESE, E, ENE, NE); - rc = vert_precalc (opt.design->shape, leftiltf, rightiltf, &vspace); - if (rc) return rc; + rc = vert_precalc(opt.design->shape, leftiltf, rightiltf, &vspace); + if (rc) { + return rc; + } lresult->height = vspace + - opt.design->shape[NW].height + opt.design->shape[SW].height; + opt.design->shape[NW].height + opt.design->shape[SW].height; rresult->height = vspace + - opt.design->shape[NE].height + opt.design->shape[SE].height; + opt.design->shape[NE].height + opt.design->shape[SE].height; - #ifdef DEBUG - fprintf (stderr, "Left side box rect width %d, height %d, vspace %d.\n", - lresult->width, lresult->height, vspace); - fprintf (stderr, "Left lines to fill: %s %d, %s %d, %s %d.\n", - shape_name[west_side[1]], leftiltf[0], - shape_name[west_side[2]], leftiltf[1], - shape_name[west_side[3]], leftiltf[2]); - fprintf (stderr, "Right side box rect width %d, height %d, vspace %d.\n", - rresult->width, rresult->height, vspace); - fprintf (stderr, "Right lines to fill: %s %d, %s %d, %s %d.\n", - shape_name[east_side[1]], rightiltf[0], - shape_name[east_side[2]], rightiltf[1], - shape_name[east_side[3]], rightiltf[2]); - #endif +#ifdef DEBUG + fprintf (stderr, "Left side box rect width %d, height %d, vspace %d.\n", + lresult->width, lresult->height, vspace); + fprintf (stderr, "Left lines to fill: %s %d, %s %d, %s %d.\n", + shape_name[west_side[1]], leftiltf[0], + shape_name[west_side[2]], leftiltf[1], + shape_name[west_side[3]], leftiltf[2]); + fprintf (stderr, "Right side box rect width %d, height %d, vspace %d.\n", + rresult->width, rresult->height, vspace); + fprintf (stderr, "Right lines to fill: %s %d, %s %d, %s %d.\n", + shape_name[east_side[1]], rightiltf[0], + shape_name[east_side[2]], rightiltf[1], + shape_name[east_side[3]], rightiltf[2]); +#endif - lresult->chars = (char **) calloc (lresult->height, sizeof(char *)); - if (lresult->chars == NULL) return 1; - rresult->chars = (char **) calloc (rresult->height, sizeof(char *)); - if (rresult->chars == NULL) return 1; + lresult->chars = (char **) calloc(lresult->height, sizeof(char *)); + if (lresult->chars == NULL) { + return 1; + } + rresult->chars = (char **) calloc(rresult->height, sizeof(char *)); + if (rresult->chars == NULL) { + return 1; + } - horiz_assemble (opt.design->shape, west_side, leftiltf, lresult); - horiz_assemble (opt.design->shape, east_side, rightiltf, rresult); + horiz_assemble(opt.design->shape, west_side, leftiltf, lresult); + horiz_assemble(opt.design->shape, east_side, rightiltf, rresult); - #if defined(DEBUG) && 1 +#if defined(DEBUG) && 1 { /* * Debugging code - Output left and right side of box @@ -632,14 +654,14 @@ static int vert_generate (sentry_t *lresult, sentry_t *rresult) rresult->chars[j]? rresult->chars[j] : "(null)"); } } - #endif +#endif return 0; /* all clear */ } -int generate_box (sentry_t *thebox) +int generate_box(sentry_t *thebox) /* * * RETURNS: == 0 if successful (thebox is set) @@ -651,19 +673,23 @@ int generate_box (sentry_t *thebox) int rc; int i; - rc = horiz_generate (&(thebox[0]), &(thebox[2])); - if (rc) goto err; + rc = horiz_generate(&(thebox[0]), &(thebox[2])); + if (rc) { + goto err; + } - rc = vert_generate (&(thebox[3]), &(thebox[1])); - if (rc) goto err; + rc = vert_generate(&(thebox[3]), &(thebox[1])); + if (rc) { + goto err; + } return 0; /* all clear */ -err: - for (i=0; ilen; @@ -701,20 +727,21 @@ static int justify_line (line_t *line, int skew) return 0; } - for (p=line->text; *p==' ' || *p=='\t'; ++p); - newlen = line->len - (p-line->text); + for (p = line->text; *p == ' ' || *p == '\t'; ++p) { + } + newlen = line->len - (p - line->text); switch (opt.justify) { case 'l': if (opt.design->indentmode == 't') { - memmove (line->text+input.indent, p, newlen+1); + memmove(line->text + input.indent, p, newlen + 1); line->vischar = newlen + input.indent - line->invis; line->len = line->vischar; line->num_leading_blanks = input.indent; } else { - memmove (line->text, p, newlen+1); + memmove(line->text, p, newlen + 1); line->vischar = newlen - line->invis; line->len = line->vischar; line->num_leading_blanks = 0; @@ -725,35 +752,37 @@ static int justify_line (line_t *line, int skew) if (opt.design->indentmode == 't') { shift = (input.maxline - input.indent - newlen + line->invis) / 2 + input.indent; skew -= input.indent; - if ((input.maxline-input.indent - newlen) % 2 && skew == 1) + if ((input.maxline - input.indent - newlen) % 2 && skew == 1) { ++shift; + } } else { shift = (input.maxline - newlen + line->invis) / 2; - if ((input.maxline - newlen +line->invis) % 2 && skew == 1) + if ((input.maxline - newlen + line->invis) % 2 && skew == 1) { ++shift; + } } - newtext = (char *) calloc (shift + newlen + line->invis + 1, sizeof(char)); + newtext = (char *) calloc(shift + newlen + line->invis + 1, sizeof(char)); if (newtext == NULL) { - perror (PROJECT); + perror(PROJECT); return 2; } - spaces = (char *) malloc (shift+1); + spaces = (char *) malloc(shift + 1); if (spaces == NULL) { - perror (PROJECT); + perror(PROJECT); BFREE (newtext); return 3; } - memset (spaces, ' ', shift); + memset(spaces, ' ', shift); spaces[shift] = '\0'; - #if defined(DEBUG) && 0 - fprintf (stderr, "j(c): newlen=%d, shift=%d, spaces=\"%s\"\n", - newlen, shift, spaces); - #endif - strncpy (newtext, spaces, shift); - strncat (newtext, p, newlen+line->invis); - newtext[shift+newlen+line->invis] = '\0'; +#if defined(DEBUG) && 0 + fprintf (stderr, "j(c): newlen=%d, shift=%d, spaces=\"%s\"\n", + newlen, shift, spaces); +#endif + strncpy(newtext, spaces, shift); + strncat(newtext, p, newlen + line->invis); + newtext[shift + newlen + line->invis] = '\0'; BFREE (spaces); BFREE (line->text); line->text = newtext; @@ -764,22 +793,22 @@ static int justify_line (line_t *line, int skew) case 'r': shift = input.maxline - (newlen - line->invis); - newtext = (char *) calloc (input.maxline+1000, sizeof(char)); + newtext = (char *) calloc(input.maxline + 1000, sizeof(char)); if (newtext == NULL) { - perror (PROJECT); + perror(PROJECT); return 2; } - spaces = (char *) malloc (shift+1); + spaces = (char *) malloc(shift + 1); if (spaces == NULL) { - perror (PROJECT); + perror(PROJECT); BFREE (newtext); return 3; } - memset (spaces, ' ', shift); + memset(spaces, ' ', shift); spaces[shift] = '\0'; - strncpy (newtext, spaces, shift); - strncat (newtext, p, newlen); - newtext[input.maxline+line->invis] = '\0'; + strncpy(newtext, spaces, shift); + strncat(newtext, p, newlen); + newtext[input.maxline + line->invis] = '\0'; BFREE (spaces); BFREE (line->text); line->text = newtext; @@ -789,7 +818,7 @@ static int justify_line (line_t *line, int skew) break; default: - fprintf (stderr, "%s: internal error\n", PROJECT); + fprintf(stderr, "%s: internal error\n", PROJECT); return 1; } @@ -799,7 +828,7 @@ static int justify_line (line_t *line, int skew) if (opt.justify != 'r' && oldlen == input.maxline) { size_t k; input.maxline = 0; - for (k=0; k input.maxline) { input.maxline = input.lines[k].len; } @@ -811,7 +840,7 @@ static int justify_line (line_t *line, int skew) -int output_box (const sentry_t *thebox) +int output_box(const sentry_t *thebox) /* * Generate final output using the previously generated box parts. * @@ -824,31 +853,31 @@ int output_box (const sentry_t *thebox) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - size_t j; - size_t nol = thebox[BRIG].height; /* number of output lines */ - char trailspc[LINE_MAX_BYTES+1]; - char *indentspc; - int indentspclen; - size_t vfill, vfill1, vfill1_save, vfill2; /* empty lines/columns in box */ - size_t hfill; - char *hfill1, *hfill2; /* space before/after text */ - size_t hpl, hpr; - size_t r; - int rc; - char obuf[LINE_MAX_BYTES+1]; /* final output buffer */ - size_t obuf_len; /* length of content of obuf */ - size_t skip_start; /* lines to skip for box top */ - size_t skip_end; /* lines to skip for box bottom */ - size_t skip_left; /* true if left box part is to be skipped */ - int ntabs, nspcs; /* needed for unexpand of tabs */ - char *restored_indent; - size_t *contentPos; /* column of first char of input text in output text */ + size_t j; + size_t nol = thebox[BRIG].height; /* number of output lines */ + char trailspc[LINE_MAX_BYTES + 1]; + char *indentspc; + int indentspclen; + size_t vfill, vfill1, vfill1_save, vfill2; /* empty lines/columns in box */ + size_t hfill; + char *hfill1, *hfill2; /* space before/after text */ + size_t hpl, hpr; + size_t r; + int rc; + char obuf[LINE_MAX_BYTES + 1]; /* final output buffer */ + size_t obuf_len; /* length of content of obuf */ + size_t skip_start; /* lines to skip for box top */ + size_t skip_end; /* lines to skip for box bottom */ + size_t skip_left; /* true if left box part is to be skipped */ + int ntabs, nspcs; /* needed for unexpand of tabs */ + char *restored_indent; + size_t *contentPos; /* column of first char of input text in output text */ - #ifdef DEBUG - fprintf (stderr, "Padding used: left %d, top %d, right %d, bottom %d\n", - opt.design->padding[BLEF], opt.design->padding[BTOP], - opt.design->padding[BRIG], opt.design->padding[BBOT]); - #endif +#ifdef DEBUG + fprintf (stderr, "Padding used: left %d, top %d, right %d, bottom %d\n", + opt.design->padding[BLEF], opt.design->padding[BTOP], + opt.design->padding[BRIG], opt.design->padding[BBOT]); +#endif /* * Create string of spaces for indentation @@ -864,25 +893,25 @@ int output_box (const sentry_t *thebox) else { indentspclen = input.indent; } - indentspc = (char *) malloc (indentspclen + 1); + indentspc = (char *) malloc(indentspclen + 1); if (indentspc == NULL) { - perror (PROJECT); + perror(PROJECT); return 1; } if (opt.tabexp == 'u') { - memset (indentspc, (int)'\t', ntabs); - memset (indentspc + ntabs, (int)' ', nspcs); + memset(indentspc, (int) '\t', ntabs); + memset(indentspc + ntabs, (int) ' ', nspcs); } else { - memset (indentspc, (int)' ', indentspclen); + memset(indentspc, (int) ' ', indentspclen); } indentspc[indentspclen] = '\0'; } else { - indentspc = (char *) strdup (""); + indentspc = (char *) strdup(""); if (indentspc == NULL) { - perror (PROJECT); + perror(PROJECT); return 1; } } @@ -891,7 +920,7 @@ int output_box (const sentry_t *thebox) * Provide string of spaces for filling of space between text and * right side of box */ - memset (trailspc, (int)' ', LINE_MAX_BYTES); + memset(trailspc, (int) ' ', LINE_MAX_BYTES); trailspc[LINE_MAX_BYTES] = '\0'; /* @@ -913,21 +942,21 @@ int output_box (const sentry_t *thebox) } vfill1 += opt.design->padding[BTOP]; vfill2 += opt.design->padding[BBOT]; - vfill += opt.design->padding[BTOP] + opt.design->padding[BBOT]; + vfill += opt.design->padding[BTOP] + opt.design->padding[BBOT]; vfill1_save = vfill1; /* * Provide strings for horizontal text alignment. */ hfill = thebox[BTOP].width - input.maxline; - hfill1 = (char *) malloc (hfill+1); - hfill2 = (char *) malloc (hfill+1); + hfill1 = (char *) malloc(hfill + 1); + hfill2 = (char *) malloc(hfill + 1); if (!hfill1 || !hfill2) { - perror (PROJECT); + perror(PROJECT); return 1; } - memset (hfill1, (int)' ', hfill+1); - memset (hfill2, (int)' ', hfill+1); + memset(hfill1, (int) ' ', hfill + 1); + memset(hfill2, (int) ' ', hfill + 1); hfill1[hfill] = '\0'; hfill2[hfill] = '\0'; hpl = 0; @@ -946,8 +975,8 @@ int output_box (const sentry_t *thebox) else { hfill -= opt.design->padding[BLEF] + opt.design->padding[BRIG]; if (opt.halign == 'c') { - hpl = hfill/2 + opt.design->padding[BLEF]; - hpr = hfill/2 + opt.design->padding[BRIG] + (hfill%2); + hpl = hfill / 2 + opt.design->padding[BLEF]; + hpr = hfill / 2 + opt.design->padding[BRIG] + (hfill % 2); } else if (opt.halign == 'r') { hpl = hfill + opt.design->padding[BLEF]; @@ -962,123 +991,127 @@ int output_box (const sentry_t *thebox) hfill1[hpl] = '\0'; hfill2[hpr] = '\0'; - #if defined(DEBUG) - fprintf (stderr, "Alignment: hfill %d hpl %d hpr %d, vfill %d " - "vfill1 %d vfill2 %d.\n", hfill, hpl, hpr, vfill, vfill1, vfill2); - fprintf (stderr, " hfill1 = \"%s\"; hfill2 = \"%s\"; " - "indentspc = \"%s\";\n", hfill1, hfill2, indentspc); - #endif +#if defined(DEBUG) + fprintf (stderr, "Alignment: hfill %d hpl %d hpr %d, vfill %d " + "vfill1 %d vfill2 %d.\n", hfill, hpl, hpr, vfill, vfill1, vfill2); + fprintf (stderr, " hfill1 = \"%s\"; hfill2 = \"%s\"; " + "indentspc = \"%s\";\n", hfill1, hfill2, indentspc); +#endif /* * Find out if and how many leading or trailing blank lines must be * skipped because the corresponding box side was defined empty. */ skip_start = 0; - skip_end = 0; - skip_left = 0; - if (empty_side (opt.design->shape, BTOP)) + skip_end = 0; + skip_left = 0; + if (empty_side(opt.design->shape, BTOP)) { skip_start = opt.design->shape[NW].height; - if (empty_side (opt.design->shape, BBOT)) + } + if (empty_side(opt.design->shape, BBOT)) { skip_end = opt.design->shape[SW].height; - if (empty_side (opt.design->shape, BLEF)) - skip_left = opt.design->shape[NW].width; /* could simply be 1, though */ - #if defined(DEBUG) - fprintf (stderr, "skip_start = %d; skip_end = %d; skip_left = %d; " - "nol = %d;\n", skip_start, skip_end, skip_left, nol); - #endif + } + if (empty_side(opt.design->shape, BLEF)) { + skip_left = opt.design->shape[NW].width; + } /* could simply be 1, though */ +#if defined(DEBUG) + fprintf (stderr, "skip_start = %d; skip_end = %d; skip_left = %d; " + "nol = %d;\n", skip_start, skip_end, skip_left, nol); +#endif /* * Generate actual output */ - contentPos = calloc (input.anz_lines, sizeof(size_t)); - for (j=skip_start; j= 0? input.lines[ti].text : "", hfill2, - trailspc, thebox[BRIG].chars[j]); + concat_strings(obuf, LINE_MAX_BYTES + 1, 7, restored_indent, + skip_left ? "" : thebox[BLEF].chars[j], hfill1, + ti >= 0 ? input.lines[ti].text : "", hfill2, + trailspc, thebox[BRIG].chars[j]); } else { /* bottom vfill */ r = thebox[BTOP].width; trailspc[r] = '\0'; - restored_indent = tabbify_indent (input.anz_lines - 1, indentspc, indentspclen); - concat_strings (obuf, LINE_MAX_BYTES+1, 4, restored_indent, - skip_left?"":thebox[BLEF].chars[j], trailspc, - thebox[BRIG].chars[j]); + restored_indent = tabbify_indent(input.anz_lines - 1, indentspc, indentspclen); + concat_strings(obuf, LINE_MAX_BYTES + 1, 4, restored_indent, + skip_left ? "" : thebox[BLEF].chars[j], trailspc, + thebox[BRIG].chars[j]); } trailspc[r] = ' '; } else { /* box bottom */ - restored_indent = tabbify_indent (input.anz_lines - 1, indentspc, indentspclen); - concat_strings (obuf, LINE_MAX_BYTES+1, 4, restored_indent, - skip_left?"":thebox[BLEF].chars[j], - thebox[BBOT].chars[j-(nol-thebox[BBOT].height)], - thebox[BRIG].chars[j]); + restored_indent = tabbify_indent(input.anz_lines - 1, indentspc, indentspclen); + concat_strings(obuf, LINE_MAX_BYTES + 1, 4, restored_indent, + skip_left ? "" : thebox[BLEF].chars[j], + thebox[BBOT].chars[j - (nol - thebox[BBOT].height)], + thebox[BRIG].chars[j]); } - obuf_len = strlen (obuf); + obuf_len = strlen(obuf); if (obuf_len > LINE_MAX_BYTES) { size_t newlen = LINE_MAX_BYTES; - btrim (obuf, &newlen); + btrim(obuf, &newlen); } else { - btrim (obuf, &obuf_len); + btrim(obuf, &obuf_len); } if (opt.tabexp == 'k') { BFREE (restored_indent); } - fprintf (opt.outfile, "%s%s", obuf, (input.final_newline || j < nol-skip_end-1 ? "\n" : "")); + fprintf(opt.outfile, "%s%s", obuf, (input.final_newline || j < nol - skip_end - 1 ? "\n" : "")); } /* add info line for web ui if requested with -q */ if (opt.q) { - fprintf (opt.outfile, "%d ", (int) (thebox[BTOP].height + vfill1_save - skip_start)); + fprintf(opt.outfile, "%d ", (int) (thebox[BTOP].height + vfill1_save - skip_start)); for (j = 0; j < input.anz_lines; j++) { - fprintf (opt.outfile, "%d%s", - (contentPos[j] == SIZE_MAX ? (int) -1 : (int) contentPos[j]), - j < input.anz_lines-1 ? " ": "\n"); + fprintf(opt.outfile, "%d%s", + (contentPos[j] == SIZE_MAX ? (int) -1 : (int) contentPos[j]), + j < input.anz_lines - 1 ? " " : "\n"); } } diff --git a/src/generate.h b/src/generate.h index 563b32c..75418b4 100644 --- a/src/generate.h +++ b/src/generate.h @@ -5,12 +5,12 @@ * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2, as published * by the Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -26,8 +26,9 @@ #define GENERATE_H -int generate_box (sentry_t *thebox); -int output_box (const sentry_t *thebox); +int generate_box(sentry_t *thebox); + +int output_box(const sentry_t *thebox); #endif /*GENERATE_H*/ diff --git a/src/remove.c b/src/remove.c index 67ecec9..23c4b79 100644 --- a/src/remove.c +++ b/src/remove.c @@ -5,12 +5,12 @@ * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2, as published * by the Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -33,8 +33,8 @@ -static int best_match (const line_t *line, - char **ws, char **we, char **es, char **ee) +static int best_match(const line_t *line, + char **ws, char **we, char **es, char **ee) /* * Find positions of west and east box parts in line. * @@ -48,41 +48,41 @@ static int best_match (const line_t *line, * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - size_t numw = 0; /* number of shape lines on west side */ - size_t nume = 0; /* number of shape lines on east side */ - size_t j; /* counts number of lines of all shapes tested */ - size_t k; /* line counter within shape */ - int w; /* shape counter */ - sentry_t *cs; /* current shape */ - char *s; /* duplicate of current shape part */ - char *p; /* position found by strstr */ - size_t cq; /* current quality */ - char *q; /* space check rover */ - line_t chkline; /* for calls to empty_line() */ - size_t quality; + size_t numw = 0; /* number of shape lines on west side */ + size_t nume = 0; /* number of shape lines on east side */ + size_t j; /* counts number of lines of all shapes tested */ + size_t k; /* line counter within shape */ + int w; /* shape counter */ + sentry_t *cs; /* current shape */ + char *s; /* duplicate of current shape part */ + char *p; /* position found by strstr */ + size_t cq; /* current quality */ + char *q; /* space check rover */ + line_t chkline; /* for calls to empty_line() */ + size_t quality; *ws = *we = *es = *ee = NULL; - numw = opt.design->shape[WNW].height; - numw += opt.design->shape[ W ].height; + numw = opt.design->shape[WNW].height; + numw += opt.design->shape[W].height; numw += opt.design->shape[WSW].height; - nume = opt.design->shape[ENE].height; - nume += opt.design->shape[ E ].height; + nume = opt.design->shape[ENE].height; + nume += opt.design->shape[E].height; nume += opt.design->shape[ESE].height; #ifdef DEBUG - fprintf (stderr, "Number of WEST side shape lines: %d\n", numw); - fprintf (stderr, "Number of EAST side shape lines: %d\n", nume); + fprintf (stderr, "Number of WEST side shape lines: %d\n", numw); + fprintf (stderr, "Number of EAST side shape lines: %d\n", nume); #endif /* * Find match for WEST side */ - if (!empty_side (opt.design->shape, BLEF)) { + if (!empty_side(opt.design->shape, BLEF)) { quality = 0; cs = opt.design->shape + WNW; - for (j=0,k=0,w=3; jheight) { k = 0; cs = opt.design->shape + west_side[--w]; @@ -90,35 +90,37 @@ static int best_match (const line_t *line, chkline.text = cs->chars[k]; chkline.len = cs->width; - if (empty_line (&chkline) && !(quality==0 && j==numw-1)) + if (empty_line(&chkline) && !(quality == 0 && j == numw - 1)) { continue; + } - s = (char *) strdup (cs->chars[k]); + s = (char *) strdup(cs->chars[k]); if (s == NULL) { - perror (PROJECT); + perror(PROJECT); return -1; } cq = cs->width; do { - p = strstr (line->text, s); + p = strstr(line->text, s); if (p) { - q = p-1; + q = p - 1; while (q >= line->text) { if (*q-- != ' ') { p = NULL; break; } } - if (p) + if (p) { break; + } } if (!p && cq) { - if (*s == ' ') - memmove (s, s+1, cq--); - else if (s[cq-1] == ' ') + if (*s == ' ') { + memmove(s, s + 1, cq--); + } else if (s[cq - 1] == ' ') { s[--cq] = '\0'; - else { + } else { cq = 0; break; } @@ -146,33 +148,34 @@ static int best_match (const line_t *line, /* * Find match for EAST side */ - if (!empty_side (opt.design->shape, BRIG)) { + if (!empty_side(opt.design->shape, BRIG)) { quality = 0; cs = opt.design->shape + ENE; - for (j=0,k=0,w=1; jheight) { k = 0; cs = opt.design->shape + east_side[++w]; } - #ifdef DEBUG - fprintf (stderr, "\nj %d, k %d, w %d, cs->chars[k] = \"%s\"\n", - j, k, w, cs->chars[k]?cs->chars[k]:"(null)"); - #endif +#ifdef DEBUG + fprintf (stderr, "\nj %d, k %d, w %d, cs->chars[k] = \"%s\"\n", + j, k, w, cs->chars[k]?cs->chars[k]:"(null)"); +#endif chkline.text = cs->chars[k]; chkline.len = cs->width; - if (empty_line (&chkline)) + if (empty_line(&chkline)) { continue; + } - s = (char *) strdup (cs->chars[k]); + s = (char *) strdup(cs->chars[k]); if (s == NULL) { - perror (PROJECT); + perror(PROJECT); return -1; } cq = cs->width; do { - p = my_strnrstr (line->text, s, cq, 0); + p = my_strnrstr(line->text, s, cq, 0); if (p) { q = p + cq; while (*q) { @@ -181,15 +184,16 @@ static int best_match (const line_t *line, break; } } - if (p) + if (p) { break; + } } if (!p && cq) { - if (*s == ' ') - memmove (s, s+1, cq--); - else if (s[cq-1] == ' ') + if (*s == ' ') { + memmove(s, s + 1, cq--); + } else if (s[cq - 1] == ' ') { s[--cq] = '\0'; - else { + } else { cq = 0; break; } @@ -214,13 +218,13 @@ static int best_match (const line_t *line, } } - return *ws || *es ? 1:0; + return *ws || *es ? 1 : 0; } -static int hmm (const int aside, const size_t follow, - const char *p, const char *ecs, const int cnt) +static int hmm(const int aside, const size_t follow, + const char *p, const char *ecs, const int cnt) /* * (horizontal middle match) * @@ -238,56 +242,60 @@ static int hmm (const int aside, const size_t follow, * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - int cmp; + int cmp; sentry_t *cs; - shape_t sh; - int rc; + shape_t sh; + int rc; #ifdef DEBUG - fprintf (stderr, "hmm (%s, %d, \'%c\', \'%c\', %d)\n", - aside==BTOP?"BTOP":"BBOT", follow, p[0], *ecs, cnt); + fprintf (stderr, "hmm (%s, %d, \'%c\', \'%c\', %d)\n", + aside==BTOP?"BTOP":"BBOT", follow, p[0], *ecs, cnt); #endif - if (p > ecs) /* last shape tried was too long */ + if (p > ecs) { /* last shape tried was too long */ return 2; + } - sh = leftmost (aside, cnt); - if (sh == ANZ_SHAPES) + sh = leftmost(aside, cnt); + if (sh == ANZ_SHAPES) { return 1; + } cs = opt.design->shape + sh; - cmp = strncmp (p, cs->chars[follow], cs->width); + cmp = strncmp(p, cs->chars[follow], cs->width); if (cmp == 0) { - if (p+cs->width == ecs) { - if (leftmost (aside, cnt+1) == ANZ_SHAPES) + if (p + cs->width == ecs) { + if (leftmost(aside, cnt + 1) == ANZ_SHAPES) { return 0; /* good! all clear, it matched */ - else - return 3; /* didn't use all shapes to do it */ + } else { + return 3; + } /* didn't use all shapes to do it */ } if (cs->elastic) { - rc = hmm (aside, follow, p+cs->width, ecs, cnt); + rc = hmm(aside, follow, p + cs->width, ecs, cnt); #ifdef DEBUG - fprintf (stderr, "hmm returned %d\n", rc); + fprintf (stderr, "hmm returned %d\n", rc); #endif if (rc) { - rc = hmm (aside, follow, p+cs->width, ecs, cnt+1); + rc = hmm(aside, follow, p + cs->width, ecs, cnt + 1); #ifdef DEBUG - fprintf (stderr, "hmm returned %d\n", rc); + fprintf (stderr, "hmm returned %d\n", rc); #endif } } else { - rc = hmm (aside, follow, p+cs->width, ecs, cnt+1); + rc = hmm(aside, follow, p + cs->width, ecs, cnt + 1); #ifdef DEBUG - fprintf (stderr, "hmm returned %d\n", rc); + fprintf (stderr, "hmm returned %d\n", rc); #endif } - if (rc == 0) + if (rc == 0) { return 0; /* we're on the way back */ - else - return 4; /* can't continue on this path */ + } else { + return 4; + } /* can't continue on this path */ } else { return 5; /* no match */ @@ -296,7 +304,7 @@ static int hmm (const int aside, const size_t follow, -static int detect_horiz (const int aside, size_t *hstart, size_t *hend) +static int detect_horiz(const int aside, size_t *hstart, size_t *hend) /* * Detect which part of the input belongs to the top/bottom of the box * @@ -318,44 +326,43 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - size_t follow; /* possible box line */ - sentry_t *cs; /* current shape */ - line_t *line; /* currently processed input line */ - size_t lcnt; /* line counter */ - char *p = NULL; /* middle line part scanner */ - char *q; /* space check rover */ - char *wcs = NULL; /* west corner shape position */ - char *ecs = NULL; /* east corner shape position */ - int mmok = 0; /* true if middle match was ok */ - size_t mheight; /* regular height of box part */ - int result_init = 0; /* true if hstart etc. was init. */ - int nowside; /* true if west side is empty */ - int goeast; /* no. of finds to ignore on right */ - int gowest; /* set to request search start incr. */ + size_t follow; /* possible box line */ + sentry_t *cs; /* current shape */ + line_t *line; /* currently processed input line */ + size_t lcnt; /* line counter */ + char *p = NULL; /* middle line part scanner */ + char *q; /* space check rover */ + char *wcs = NULL; /* west corner shape position */ + char *ecs = NULL; /* east corner shape position */ + int mmok = 0; /* true if middle match was ok */ + size_t mheight; /* regular height of box part */ + int result_init = 0; /* true if hstart etc. was init. */ + int nowside; /* true if west side is empty */ + int goeast; /* no. of finds to ignore on right */ + int gowest; /* set to request search start incr. */ *hstart = *hend = 0; - nowside = empty_side (opt.design->shape, BLEF); + nowside = empty_side(opt.design->shape, BLEF); mheight = opt.design->shape[sides[aside][0]].height; if (aside == BTOP) { follow = 0; - line=input.lines; + line = input.lines; } else { follow = mheight - 1; line = input.lines + input.anz_lines - 1; } - for (lcnt=0; lcnt= input.lines; ++lcnt) - { + for (lcnt = 0; lcnt < mheight && lcnt < input.anz_lines + && line >= input.lines; ++lcnt) { goeast = gowest = 0; - #ifdef DEBUG - fprintf (stderr, "----- Processing line index %2d ----------" - "-------------------------------------\n", - aside == BTOP? lcnt: input.anz_lines - lcnt - 1); - #endif +#ifdef DEBUG + fprintf (stderr, "----- Processing line index %2d ----------" + "-------------------------------------\n", + aside == BTOP? lcnt: input.anz_lines - lcnt - 1); +#endif do { /* @@ -366,34 +373,38 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend) wcs = NULL; if (gowest) { gowest = 0; - if (*p == ' ' || *p == '\t') + if (*p == ' ' || *p == '\t') { ++p; - else + } else { break; + } } else { p = line->text; } } else { - cs = opt.design->shape + sides[aside][aside==BTOP?0:SHAPES_PER_SIDE-1]; + cs = opt.design->shape + sides[aside][aside == BTOP ? 0 : SHAPES_PER_SIDE - 1]; if (gowest) { gowest = 0; - wcs = strstr (wcs+1, cs->chars[follow]); + wcs = strstr(wcs + 1, cs->chars[follow]); } else { - wcs = strstr (line->text, cs->chars[follow]); + wcs = strstr(line->text, cs->chars[follow]); } if (wcs) { - for (q=wcs-1; q>=line->text; --q) { - if (*q != ' ' && *q != '\t') + for (q = wcs - 1; q >= line->text; --q) { + if (*q != ' ' && *q != '\t') { break; + } } - if (q >= line->text) + if (q >= line->text) { wcs = NULL; + } } - if (!wcs) + if (!wcs) { break; + } p = wcs + cs->width; } @@ -401,52 +412,52 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend) /* Now, wcs is either NULL (if west side is empty) */ /* or not NULL (if west side is not empty). In any case, p */ /* points to where we start searching for the east corner. */ - #ifdef DEBUG - if (wcs) - fprintf (stderr, "West corner shape matched at " - "position %d.\n", wcs - line->text); - else - fprintf (stderr, "West box side is empty.\n"); - #endif - +#ifdef DEBUG + if (wcs) { + fprintf (stderr, "West corner shape matched at " + "position %d.\n", wcs - line->text); + } else { + fprintf (stderr, "West box side is empty.\n"); + } +#endif /* * Look for east corner shape */ - cs = opt.design->shape + sides[aside][aside==BTOP?SHAPES_PER_SIDE-1:0]; - ecs = my_strnrstr (p, cs->chars[follow], cs->width, goeast); + cs = opt.design->shape + sides[aside][aside == BTOP ? SHAPES_PER_SIDE - 1 : 0]; + ecs = my_strnrstr(p, cs->chars[follow], cs->width, goeast); if (ecs) { - for (q=ecs+cs->width; *q; ++q) { - if (*q != ' ' && *q != '\t') + for (q = ecs + cs->width; *q; ++q) { + if (*q != ' ' && *q != '\t') { break; + } } - if (*q) + if (*q) { ecs = NULL; + } } if (!ecs) { - if (goeast == 0) + if (goeast == 0) { break; - else { + } else { goeast = 0; gowest = 1; continue; } } - #ifdef DEBUG - fprintf (stderr, "East corner shape matched at position %d.\n", - ecs-line->text); - #endif +#ifdef DEBUG + fprintf (stderr, "East corner shape matched at position %d.\n", ecs-line->text); +#endif /* * Check if text between corner shapes is valid */ - mmok = !hmm (aside, follow, p, ecs, 0); - if (!mmok) + mmok = !hmm(aside, follow, p, ecs, 0); + if (!mmok) { ++goeast; - #ifdef DEBUG - fprintf (stderr, "Text between corner shapes is %s.\n", - mmok? "VALID": "NOT valid"); - #endif - + } +#ifdef DEBUG + fprintf (stderr, "Text between corner shapes is %s.\n", mmok? "VALID": "NOT valid"); +#endif } while (!mmok); /* @@ -455,19 +466,22 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend) if (mmok) { /* match found */ if (!result_init) { result_init = 1; - if (aside == BTOP) + if (aside == BTOP) { *hstart = lcnt; - else + } else { *hend = (input.anz_lines - lcnt - 1) + 1; + } } - if (aside == BTOP) + if (aside == BTOP) { *hend = lcnt + 1; - else + } else { *hstart = input.anz_lines - lcnt - 1; + } } else { - if (result_init) + if (result_init) { break; + } } wcs = NULL; @@ -485,7 +499,7 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend) } } - return result_init? 0: 1; + return result_init ? 0 : 1; } @@ -503,110 +517,127 @@ static design_t *detect_design() * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - design_t *d = designs; /* ptr to currently tested design */ - long hits; /* hit points of the current design */ - long maxhits = 0; /* maximum no. of hits so far */ - design_t *res = NULL; /* ptr to design with the most hits */ - int dcnt; /* design loop counter */ - shape_t scnt; /* shape loop counter */ - size_t j, k; - char *p; - char *s; - line_t shpln; /* a line which is part of a shape */ - size_t a; - int empty[ANZ_SIDES]; + design_t *d = designs; /* ptr to currently tested design */ + long hits; /* hit points of the current design */ + long maxhits = 0; /* maximum no. of hits so far */ + design_t *res = NULL; /* ptr to design with the most hits */ + int dcnt; /* design loop counter */ + shape_t scnt; /* shape loop counter */ + size_t j, k; + char *p; + char *s; + line_t shpln; /* a line which is part of a shape */ + size_t a; + int empty[ANZ_SIDES]; - for (dcnt=0; dcntname); + fprintf (stderr, "CONSIDERING DESIGN ---- \"%s\" ---------------\n", + d->name); #endif hits = 0; - for (j=0; jshape, j); + for (j = 0; j < ANZ_SIDES; ++j) { + empty[j] = empty_side(d->shape, j); + } #ifdef DEBUG - fprintf (stderr, "Empty sides: TOP %d, LEFT %d, BOTTOM %d, RIGHT %d\n", - empty[BTOP], empty[BLEF], empty[BBOT], empty[BRIG]); + fprintf (stderr, "Empty sides: TOP %d, LEFT %d, BOTTOM %d, RIGHT %d\n", + empty[BTOP], empty[BLEF], empty[BBOT], empty[BRIG]); #endif - for (scnt=0; scntshape[scnt].height; ++j) { + } + for (j = 0; j < d->shape[scnt].height; ++j) { shpln.text = d->shape[scnt].chars[j]; shpln.len = d->shape[scnt].width; - if (empty_line (&shpln)) + if (empty_line(&shpln)) { continue; - for (s=shpln.text; *s==' ' || *s=='\t'; ++s); - for (k=0; kshape[scnt].height; ++k) { + } + for (s = shpln.text; *s == ' ' || *s == '\t'; ++s) { + } + for (k = 0; k < d->shape[scnt].height; ++k) { a = k; - if (scnt == SW) + if (scnt == SW) { a += input.anz_lines - d->shape[scnt].height; - if (a >= input.anz_lines) + } + if (a >= input.anz_lines) { break; - for (p=input.lines[a].text; *p==' '||*p=='\t'; ++p); - if (strncmp (p, s, shpln.len-(s-shpln.text)) == 0) + } + for (p = input.lines[a].text; *p == ' ' || *p == '\t'; ++p) { + } + if (strncmp(p, s, shpln.len - (s - shpln.text)) == 0) { ++hits; + } } } - #ifdef DEBUG - fprintf (stderr, "After %s corner check:\t%ld hits.\n", - shape_name[scnt], hits); - #endif +#ifdef DEBUG + fprintf (stderr, "After %s corner check:\t%ld hits.\n", shape_name[scnt], hits); +#endif break; - case NE: case SE: + case NE: + case SE: /* * Try and find east corner shapes. Every non-empty shape * line is searched for on every input line. A hit is * generated whenever a match is found. */ if (empty[BRIG] || (empty[BTOP] && scnt == NE) - || (empty[BBOT] && scnt == SE)) + || (empty[BBOT] && scnt == SE)) { break; - for (j=0; jshape[scnt].height; ++j) { + } + for (j = 0; j < d->shape[scnt].height; ++j) { shpln.text = d->shape[scnt].chars[j]; shpln.len = d->shape[scnt].width; - if (empty_line (&shpln)) + if (empty_line(&shpln)) { continue; - for (s = shpln.text + shpln.len -1; - (*s==' ' || *s=='\t') && shpln.len; - --s, --(shpln.len)); - for (k=0; kshape[scnt].height; ++k) { + } + for (s = shpln.text + shpln.len - 1; + (*s == ' ' || *s == '\t') && shpln.len; + --s, --(shpln.len)) { + } + for (k = 0; k < d->shape[scnt].height; ++k) { a = k; - if (scnt == SE) + if (scnt == SE) { a += input.anz_lines - d->shape[scnt].height; - if (a >= input.anz_lines) + } + if (a >= input.anz_lines) { break; - for (p=input.lines[a].text + input.lines[a].len -1; - p>=input.lines[a].text && (*p==' ' || *p=='\t'); - --p); + } + for (p = input.lines[a].text + input.lines[a].len - 1; + p >= input.lines[a].text && (*p == ' ' || *p == '\t'); + --p) { + } p = p - shpln.len + 1; - if (p < input.lines[a].text) + if (p < input.lines[a].text) { continue; - if (strncmp (p, shpln.text, shpln.len) == 0) + } + if (strncmp(p, shpln.text, shpln.len) == 0) { ++hits; + } } } - #ifdef DEBUG - fprintf (stderr, "After %s corner check:\t%ld hits.\n", - shape_name[scnt], hits); - #endif +#ifdef DEBUG + fprintf (stderr, "After %s corner check:\t%ld hits.\n", shape_name[scnt], hits); +#endif break; default: - if (isempty (d->shape+scnt)) + if (isempty(d->shape + scnt)) { continue; + } if ((scnt >= NNW && scnt <= NNE) || (scnt >= SSE && scnt <= SSW)) { @@ -617,36 +648,43 @@ static design_t *detect_design() * twice in an uninterrupted row to generate a hit. */ if ((scnt >= NNW && scnt <= NNE && empty[BTOP]) - || (scnt >= SSE && scnt <= SSW && empty[BBOT])) { + || (scnt >= SSE && scnt <= SSW && empty[BBOT])) { ++hits; break; /* horizontal box part is empty */ } - for (j=0; jshape[scnt].height; ++j) { + for (j = 0; j < d->shape[scnt].height; ++j) { shpln.text = d->shape[scnt].chars[j]; shpln.len = d->shape[scnt].width; - if (empty_line (&shpln)) + if (empty_line(&shpln)) { continue; - for (k=0; kshape[scnt].height; ++k) { + } + for (k = 0; k < d->shape[scnt].height; ++k) { a = k; - if (scnt >= SSE && scnt <= SSW) - a += input.anz_lines-d->shape[scnt].height; - if (a >= input.anz_lines) + if (scnt >= SSE && scnt <= SSW) { + a += input.anz_lines - d->shape[scnt].height; + } + if (a >= input.anz_lines) { break; - for (p=input.lines[a].text; - *p == ' ' || *p == '\t'; ++p); + } + for (p = input.lines[a].text; + *p == ' ' || *p == '\t'; ++p) { + } p += d->shape[NW].width; - if (p-input.lines[a].text - >= (long) input.lines[a].len) + if (p - input.lines[a].text + >= (long) input.lines[a].len) { continue; - p = strstr (p, shpln.text); + } + p = strstr(p, shpln.text); if (p) { if (d->shape[scnt].elastic) { p += shpln.len; - if (p-input.lines[a].text - >= (long) input.lines[a].len) + if (p - input.lines[a].text + >= (long) input.lines[a].len) { continue; - if (!strncmp (p, shpln.text, shpln.len)) + } + if (!strncmp(p, shpln.text, shpln.len)) { ++hits; + } } else { ++hits; @@ -662,13 +700,13 @@ static design_t *detect_design() break; } else { - fprintf (stderr, "%s: internal error\n", PROJECT); + fprintf(stderr, "%s: internal error\n", PROJECT); return NULL; } - #ifdef DEBUG - fprintf (stderr, "After %s shape check:\t%ld hits.\n", - shape_name[scnt], hits); - #endif + #ifdef DEBUG + fprintf (stderr, "After %s shape check:\t%ld hits.\n", + shape_name[scnt], hits); + #endif } } @@ -677,78 +715,85 @@ static design_t *detect_design() * bottom box parts. Check if east and west line ends match a * non-empty shape line. If so, generate a hit. */ - if (((empty[BTOP]? 0: d->shape[NW].height) - + (empty[BBOT]? 0: d->shape[SW].height)) < input.anz_lines) - { - for (k = empty[BTOP]? 0: d->shape[NW].height; - k < input.anz_lines -(empty[BBOT]? 0: d->shape[SW].height); - ++k) - { - for (p=input.lines[k].text; *p==' ' || *p=='\t'; ++p); - for (scnt=WSW; scnt<=WNW; ++scnt) { + if (((empty[BTOP] ? 0 : d->shape[NW].height) + + (empty[BBOT] ? 0 : d->shape[SW].height)) < input.anz_lines) { + for (k = empty[BTOP] ? 0 : d->shape[NW].height; + k < input.anz_lines - (empty[BBOT] ? 0 : d->shape[SW].height); + ++k) { + for (p = input.lines[k].text; *p == ' ' || *p == '\t'; ++p) { + } + for (scnt = WSW; scnt <= WNW; ++scnt) { a = 0; - if (isempty (d->shape + scnt)) + if (isempty(d->shape + scnt)) { continue; - for (j=0; jshape[scnt].height; ++j) { + } + for (j = 0; j < d->shape[scnt].height; ++j) { shpln.text = d->shape[scnt].chars[j]; shpln.len = d->shape[scnt].width; - if (empty_line (&shpln)) + if (empty_line(&shpln)) { continue; - for (s=shpln.text; *s==' ' || *s=='\t'; ++s); - if (strncmp (p, s, shpln.len-(s-shpln.text)) == 0) { + } + for (s = shpln.text; *s == ' ' || *s == '\t'; ++s) { + } + if (strncmp(p, s, shpln.len - (s - shpln.text)) == 0) { ++hits; a = 1; break; } } - if (a) + if (a) { break; + } } - for (scnt=ENE; scnt<=ESE; ++scnt) { + for (scnt = ENE; scnt <= ESE; ++scnt) { a = 0; - if (isempty (d->shape + scnt)) + if (isempty(d->shape + scnt)) { continue; - for (j=0; jshape[scnt].height; ++j) { + } + for (j = 0; j < d->shape[scnt].height; ++j) { shpln.text = d->shape[scnt].chars[j]; shpln.len = d->shape[scnt].width; - if (empty_line (&shpln)) + if (empty_line(&shpln)) { continue; - for (p=input.lines[k].text + input.lines[k].len -1; - p>=input.lines[a].text && (*p==' ' || *p=='\t'); - --p); - for (s = shpln.text + shpln.len -1; - (*s==' ' || *s=='\t') && shpln.len; - --s, --(shpln.len)); + } + for (p = input.lines[k].text + input.lines[k].len - 1; + p >= input.lines[a].text && (*p == ' ' || *p == '\t'); + --p) { + } + for (s = shpln.text + shpln.len - 1; + (*s == ' ' || *s == '\t') && shpln.len; + --s, --(shpln.len)) { + } p = p - shpln.len + 1; - if (strncmp (p, shpln.text, shpln.len) == 0) { + if (strncmp(p, shpln.text, shpln.len) == 0) { ++hits; a = 1; break; } } - if (a) + if (a) { break; + } } } } - #ifdef DEBUG - fprintf (stderr, "After side checks:\t%ld hits.\n", hits); - #endif - +#ifdef DEBUG + fprintf (stderr, "After side checks:\t%ld hits.\n", hits); +#endif if (hits > maxhits) { maxhits = hits; res = d; } } - #ifdef DEBUG - if (res) - fprintf (stderr, "CHOOSING \"%s\" design (%ld hits).\n", - res->name, maxhits); - else - fprintf (stderr, "NO DESIGN FOUND WITH EVEN ONE HIT!\n"); - #endif +#ifdef DEBUG + if (res) { + fprintf (stderr, "CHOOSING \"%s\" design (%ld hits).\n", res->name, maxhits); + } else { + fprintf (stderr, "NO DESIGN FOUND WITH EVEN ONE HIT!\n"); + } +#endif return res; } @@ -769,9 +814,9 @@ int remove_box() size_t textend = 0; /* index of 1st line of south side */ size_t boxstart = 0; /* index of 1st line of box */ size_t boxend = 0; /* index of 1st line trailing the box */ - int m; /* true if a match was found */ + int m; /* true if a match was found */ size_t j; /* loop counter */ - int did_something = 0; /* true if there was something to remove */ + int did_something = 0; /* true if there was something to remove */ /* * If the user didn't specify a design to remove, autodetect it. @@ -782,13 +827,13 @@ int remove_box() design_t *tmp = detect_design(); if (tmp) { opt.design = tmp; - #ifdef DEBUG - fprintf (stderr, "Design autodetection: Removing box of " - "design \"%s\".\n", opt.design->name); - #endif +#ifdef DEBUG + fprintf (stderr, "Design autodetection: Removing box of " + "design \"%s\".\n", opt.design->name); +#endif } else { - fprintf (stderr, "%s: Box design autodetection failed. Use -d " + fprintf(stderr, "%s: Box design autodetection failed. Use -d " "option.\n", PROJECT); return 1; } @@ -803,15 +848,15 @@ int remove_box() * add a number of spaces equal to the east side width. */ input.maxline += opt.design->shape[NE].width; - for (j=0; jshape, BTOP)) { + if (empty_side(opt.design->shape, BTOP)) { #ifdef DEBUG - fprintf (stderr, "----> Top box side is empty: boxstart == textstart == 0.\n"); + fprintf (stderr, "----> Top box side is empty: boxstart == textstart == 0.\n"); #endif } else { - detect_horiz (BTOP, &boxstart, &textstart); + detect_horiz(BTOP, &boxstart, &textstart); #ifdef DEBUG - fprintf (stderr, "----> First line of box is %d, ", boxstart); - fprintf (stderr, "first line of box body (text) is %d.\n", textstart); + fprintf (stderr, "----> First line of box is %d, ", boxstart); + fprintf (stderr, "first line of box body (text) is %d.\n", textstart); #endif } @@ -850,68 +895,70 @@ int remove_box() /* * Phase 2: Find out how many lines belong to the bottom of the box */ - if (empty_side (opt.design->shape, BBOT)) { - textend = input.anz_lines; + if (empty_side(opt.design->shape, BBOT)) { + textend = input.anz_lines; boxend = input.anz_lines; #ifdef DEBUG - fprintf (stderr, "----> Bottom box side is empty: boxend == textend == %d.\n", - input.anz_lines); + fprintf (stderr, "----> Bottom box side is empty: boxend == textend == %d.\n", + input.anz_lines); #endif } else { textend = 0; boxend = 0; - detect_horiz (BBOT, &textend, &boxend); + detect_horiz(BBOT, &textend, &boxend); if (textend == 0 && boxend == 0) { - textend = input.anz_lines; + textend = input.anz_lines; boxend = input.anz_lines; } #ifdef DEBUG - fprintf (stderr, "----> Last line of box body (text) is %d, ", textend-1); - fprintf (stderr, "last line of box is %d.\n", boxend-1); + fprintf (stderr, "----> Last line of box body (text) is %d, ", textend-1); + fprintf (stderr, "last line of box is %d.\n", boxend-1); #endif } /* * Phase 3: Iterate over body lines, removing box sides where applicable */ - for (j=textstart; jshape[NW].width + opt.design->padding[BLEF]; - for (c=0; c textstart) { + while (empty_line(input.lines + textend - 1) && textend > textstart) { #ifdef DEBUG - fprintf (stderr, "Killing trailing blank line in box body.\n"); + fprintf (stderr, "Killing trailing blank line in box body.\n"); #endif --textend; } } if (textstart > boxstart) { - for (j=boxstart; j textend) { - for (j=textend; j input.maxline) { input.maxline = input.lines[j].len - input.lines[j].invis; } } - memset (input.lines + input.anz_lines, 0, - (BMAX (textstart - boxstart, 0) + BMAX (boxend - textend, 0)) * - sizeof(line_t)); + memset(input.lines + input.anz_lines, 0, + (BMAX (textstart - boxstart, 0) + BMAX (boxend - textend, 0)) * + sizeof(line_t)); - #ifdef DEBUG - #if 0 - for (j=0; j LINE_MAX_BYTES) { - fprintf (stderr, "%s: internal error\n", PROJECT); + fprintf(stderr, "%s: internal error\n", PROJECT); return 1; } - *chars = (char **) calloc (height, sizeof(char *)); + *chars = (char **) calloc(height, sizeof(char *)); if (*chars == NULL) { - perror (PROJECT); + perror(PROJECT); return 2; } - for (j=0; j0; --j) - BFREE ((*chars)[j-1]); + perror(PROJECT); + for (/*empty*/; j > 0; --j) BFREE ((*chars)[j - 1]); BFREE (*chars); return 3; } - memset ((*chars)[j], ' ', width); + memset((*chars)[j], ' ', width); } return 0; @@ -154,7 +159,7 @@ int genshape (const size_t width, const size_t height, char ***chars) -void freeshape (sentry_t *shape) +void freeshape(sentry_t *shape) /* * Free all memory allocated by the shape and set the struct to * SENTRY_INITIALIZER. Do not free memory of the struct. @@ -164,8 +169,9 @@ void freeshape (sentry_t *shape) { size_t j; - for (j=0; jheight; ++j) + for (j = 0; j < shape->height; ++j) { BFREE (shape->chars[j]); + } BFREE (shape->chars); *shape = SENTRY_INITIALIZER; @@ -173,26 +179,27 @@ void freeshape (sentry_t *shape) -int isempty (const sentry_t *shape) +int isempty(const sentry_t *shape) /* * Return true if shape is empty. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - if (shape == NULL) + if (shape == NULL) { return 1; - else if (shape->chars == NULL) + } else if (shape->chars == NULL) { return 1; - else if (shape->width == 0 || shape->height == 0) + } else if (shape->width == 0 || shape->height == 0) { return 1; - else + } else { return 0; + } } -int isdeepempty (const sentry_t *shape) +int isdeepempty(const sentry_t *shape) /* * Return true if shape is empty, also checking if lines consist of spaces * only. @@ -202,13 +209,15 @@ int isdeepempty (const sentry_t *shape) { size_t j; - if (isempty (shape)) + if (isempty(shape)) { return 1; + } - for (j=0; jheight; ++j) { + for (j = 0; j < shape->height; ++j) { if (shape->chars[j]) { - if (strspn (shape->chars[j], " ") != shape->width) + if (strspn(shape->chars[j], " ") != shape->width) { return 0; + } } } @@ -217,7 +226,7 @@ int isdeepempty (const sentry_t *shape) -size_t highest (const sentry_t *sarr, const int n, ...) +size_t highest(const sentry_t *sarr, const int n, ...) /* * Return height (vert.) of highest shape in given list. * @@ -235,16 +244,17 @@ size_t highest (const sentry_t *sarr, const int n, ...) size_t max = 0; /* current maximum height */ #if defined(DEBUG) && 0 - fprintf (stderr, "highest (%d, ...)\n", n); + fprintf (stderr, "highest (%d, ...)\n", n); #endif va_start (ap, n); - for (i=0; i max) + if (!isempty(sarr + r)) { + if (sarr[r].height > max) { max = sarr[r].height; + } } } @@ -255,7 +265,7 @@ size_t highest (const sentry_t *sarr, const int n, ...) -size_t widest (const sentry_t *sarr, const int n, ...) +size_t widest(const sentry_t *sarr, const int n, ...) /* * Return width (horiz.) of widest shape in given list. * @@ -273,19 +283,20 @@ size_t widest (const sentry_t *sarr, const int n, ...) size_t max = 0; /* current maximum width */ #if defined(DEBUG) && 0 - fprintf (stderr, "widest (%d, ...)\n", n); + fprintf (stderr, "widest (%d, ...)\n", n); #endif va_start (ap, n); - for (i=0; i max) + if (!isempty(sarr + r)) { + if (sarr[r].width > max) { max = sarr[r].width; + } } } - + va_end (ap); return max; @@ -293,7 +304,7 @@ size_t widest (const sentry_t *sarr, const int n, ...) -shape_t leftmost (const int aside, const int cnt) +shape_t leftmost(const int aside, const int cnt) /* * Return leftmost existing shape in specification for side aside * (BTOP or BBOT), skipping cnt shapes. Corners are not considered. @@ -307,18 +318,21 @@ shape_t leftmost (const int aside, const int cnt) int c = 0; int s; - if (cnt < 0) + if (cnt < 0) { return ANZ_SHAPES; + } if (aside == BTOP) { s = 0; do { ++s; - while (s < SHAPES_PER_SIDE-1 && - isempty(opt.design->shape + north_side[s])) - ++s; - if (s == SHAPES_PER_SIDE-1) + while (s < SHAPES_PER_SIDE - 1 && + isempty(opt.design->shape + north_side[s])) { + ++s; + } + if (s == SHAPES_PER_SIDE - 1) { return ANZ_SHAPES; + } } while (c++ < cnt); return north_side[s]; } @@ -327,10 +341,12 @@ shape_t leftmost (const int aside, const int cnt) s = SHAPES_PER_SIDE - 1; do { --s; - while (s && isempty(opt.design->shape + south_side[s])) + while (s && isempty(opt.design->shape + south_side[s])) { --s; - if (!s) + } + if (!s) { return ANZ_SHAPES; + } } while (c++ < cnt); return south_side[s]; } @@ -340,7 +356,7 @@ shape_t leftmost (const int aside, const int cnt) -int empty_side (sentry_t *sarr, const int aside) +int empty_side(sentry_t *sarr, const int aside) /* * Return true if the shapes on the given side consist entirely out of * spaces - and spaces only, tabs are considered non-empty. @@ -356,11 +372,12 @@ int empty_side (sentry_t *sarr, const int aside) { int i; - for (i=0; icurrent_rule? opt.design->current_rule->line: 0, + fprintf(stderr, "%s: %s: line %d: %s\n", + PROJECT, yyfilename ? yyfilename : "(null)", + opt.design->current_rule ? opt.design->current_rule->line : 0, msg); errno = EINVAL; } -int strisyes (const char *s) +int strisyes(const char *s) /* * Determine if the string s has a contents indicating "yes". * @@ -86,26 +86,28 @@ int strisyes (const char *s) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - if (s == NULL) + if (s == NULL) { return 0; + } - if (!strncasecmp ("on", s, 3)) + if (!strncasecmp("on", s, 3)) { return 1; - else if (!strncasecmp ("yes", s, 4)) + } else if (!strncasecmp("yes", s, 4)) { return 1; - else if (!strncasecmp ("true", s, 5)) + } else if (!strncasecmp("true", s, 5)) { return 1; - else if (!strncmp ("1", s, 2)) + } else if (!strncmp("1", s, 2)) { return 1; - else if (!strncasecmp ("t", s, 2)) + } else if (!strncasecmp("t", s, 2)) { return 1; - else + } else { return 0; + } } -int strisno (const char *s) +int strisno(const char *s) /* * Determine if the string s has a contents indicating "no". * @@ -117,50 +119,53 @@ int strisno (const char *s) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - if (s == NULL) + if (s == NULL) { return 0; + } - if (!strncasecmp ("off", s, 4)) + if (!strncasecmp("off", s, 4)) { return 1; - else if (!strncasecmp ("no", s, 3)) + } else if (!strncasecmp("no", s, 3)) { return 1; - else if (!strncasecmp ("false", s, 6)) + } else if (!strncasecmp("false", s, 6)) { return 1; - else if (!strncmp ("0", s, 2)) + } else if (!strncmp("0", s, 2)) { return 1; - else if (!strncasecmp ("f", s, 2)) + } else if (!strncasecmp("f", s, 2)) { return 1; - else + } else { return 0; + } } -void concat_strings (char *dst, int max_len, int count, ...) +void concat_strings(char *dst, int max_len, int count, ...) /* * Concatenate a variable number of strings into a fixed-length buffer. * * dst Destination array * max_len Maximum resulting string length (including terminating NULL). * count Number of source strings. - * - * The concatenation process terminates when either the destination + * + * The concatenation process terminates when either the destination * buffer is full or all 'count' strings are processed. Null string * pointers are treated as empty strings. - * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - va_list va; + va_list va; const char *src; va_start (va, count); /* - * Sanity check. + * Sanity check. */ - if (max_len < 1) + if (max_len < 1) { return; + } if (max_len == 1 || count < 1) { *dst = '\0'; @@ -170,7 +175,7 @@ void concat_strings (char *dst, int max_len, int count, ...) /* * Loop over all input strings. */ - while (count-->0 && max_len > 1) { + while (count-- > 0 && max_len > 1) { /* * Grab an input string pointer. If it's NULL, skip it (eg. treat @@ -178,10 +183,11 @@ void concat_strings (char *dst, int max_len, int count, ...) */ src = va_arg (va, const char *); - if (src == NULL) + if (src == NULL) { continue; + } - /* + /* * Concatenate 'src' onto 'dst', as long as we have room. */ while (*src && max_len > 1) { @@ -200,7 +206,7 @@ void concat_strings (char *dst, int max_len, int count, ...) -int empty_line (const line_t *line) +int empty_line(const line_t *line) /* * Return true if line is empty. * @@ -212,22 +218,25 @@ int empty_line (const line_t *line) char *p; size_t j; - if (!line) + if (!line) { return 1; - if (line->text == NULL || line->len <= 0) + } + if (line->text == NULL || line->len <= 0) { return 1; + } - for (p=line->text, j=0; *p && jlen; ++j, ++p) { - if (*p != ' ' && *p != '\t' && *p != '\r') + for (p = line->text, j = 0; *p && j < line->len; ++j, ++p) { + if (*p != ' ' && *p != '\t' && *p != '\r') { return 0; + } } return 1; } -size_t expand_tabs_into (const char *input_buffer, const size_t in_len, - const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len) +size_t expand_tabs_into(const char *input_buffer, const size_t in_len, + const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len) /* * Expand tab chars in input_buffer and store result in text. * @@ -248,51 +257,56 @@ size_t expand_tabs_into (const char *input_buffer, const size_t in_len, * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - static char temp [LINE_MAX_BYTES*MAX_TABSTOP+1]; /* work string */ - size_t ii; /* position in input string */ - size_t io; /* position in work string */ - size_t jp; /* tab expansion jump point */ - size_t tabnum; /* number of tabs in input */ + static char temp[LINE_MAX_BYTES * MAX_TABSTOP + 1]; /* work string */ + size_t ii; /* position in input string */ + size_t io; /* position in work string */ + size_t jp; /* tab expansion jump point */ + size_t tabnum; /* number of tabs in input */ - *text = NULL; + *text = NULL; - for (ii=0, *tabpos_len=0; ii 0) { - *tabpos = (size_t *) calloc ((*tabpos_len) + 1, sizeof(size_t)); - if (*tabpos == NULL) { - return 0; /* out of memory */ - } - } + for (ii = 0, *tabpos_len = 0; ii < in_len; ++ii) { + if (input_buffer[ii] == '\t') { + (*tabpos_len)++; + } + } + if (opt.tabexp != 'k') { + *tabpos_len = 0; + } + if (*tabpos_len > 0) { + *tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t)); + if (*tabpos == NULL) { + return 0; /* out of memory */ + } + } - for (ii=0, io=0, tabnum=0; ii < in_len && ((int) io) < (LINE_MAX_BYTES*tabstop-1); ++ii) { - if (input_buffer[ii] == '\t') { - if (*tabpos_len > 0) { - (*tabpos)[tabnum++] = io; - } - for (jp=io+tabstop-(io%tabstop); io 0) { + (*tabpos)[tabnum++] = io; + } + for (jp = io + tabstop - (io % tabstop); io < jp; ++io) { + temp[io] = ' '; + } + } + else { + temp[io] = input_buffer[ii]; + ++io; + } + } + temp[io] = '\0'; - *text = (char *) strdup (temp); - if (*text == NULL) return 0; + *text = (char *) strdup(temp); + if (*text == NULL) { + return 0; + } - return io; + return io; } -void btrim (char *text, size_t *len) +void btrim(char *text, size_t *len) /* * Remove trailing whitespace from line. * @@ -302,7 +316,7 @@ void btrim (char *text, size_t *len) long idx = (long) (*len) - 1; while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r' - || text[idx] == '\t' || text[idx] == ' ')) + || text[idx] == '\t' || text[idx] == ' ')) /**/ { text[idx--] = '\0'; } @@ -312,7 +326,7 @@ void btrim (char *text, size_t *len) -char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip) +char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, int skip) /* * Return pointer to last occurrence of string s2 in string s1. * @@ -330,24 +344,29 @@ char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip char *p; int comp; - if (!s2 || *s2 == '\0') + if (!s2 || *s2 == '\0') { return (char *) s1; - if (!s1 || *s1 == '\0') + } + if (!s1 || *s1 == '\0') { return NULL; - if (skip < 0) + } + if (skip < 0) { skip = 0; + } - p = strrchr (s1, s2[0]); - if (!p) + p = strrchr(s1, s2[0]); + if (!p) { return NULL; + } while (p >= s1) { - comp = strncmp (p, s2, s2_len); + comp = strncmp(p, s2, s2_len); if (comp == 0) { - if (skip--) + if (skip--) { --p; - else + } else { return p; + } } else { --p; @@ -359,7 +378,7 @@ char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip -char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indentspc_len) +char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len) /* * Checks if tab expansion mode is "keep", and if so, calculates a new * indentation string based on the one given. The new string contains @@ -378,7 +397,7 @@ char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indents */ { size_t i; - char *result; + char *result; size_t result_len; if (opt.tabexp != 'k') { @@ -388,20 +407,20 @@ char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indents return NULL; } if (indentspc_len == 0) { - return (char *) strdup (""); + return (char *) strdup(""); } - result = (char *) malloc (indentspc_len + 1); + result = (char *) malloc(indentspc_len + 1); if (result == NULL) { - perror (PROJECT); + perror(PROJECT); return NULL; } - memset (result, (int)' ', indentspc_len); + memset(result, (int) ' ', indentspc_len); result[indentspc_len] = '\0'; result_len = indentspc_len; - - for (i=0; i < input.lines[lineno].tabpos_len - && input.lines[lineno].tabpos[i] < indentspc_len; ++i) + + for (i = 0; i < input.lines[lineno].tabpos_len + && input.lines[lineno].tabpos[i] < indentspc_len; ++i) /**/ { size_t tpos = input.lines[lineno].tabpos[i]; size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */ diff --git a/src/tools.h b/src/tools.h index 4e7e0bf..8e49928 100644 --- a/src/tools.h +++ b/src/tools.h @@ -5,12 +5,12 @@ * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2, as published * by the Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -27,30 +27,38 @@ #define TOOLS_H -#define BMAX(a,b) ((a)>(b)? (a):(b)) /* return the larger value */ +#define BMAX(a, b) ((a)>(b)? (a):(b)) /* return the larger value */ #define BFREE(p) { /* free memory and clear pointer */ \ - if (p) { \ - free (p); \ - (p) = NULL; \ - } \ + if (p) { \ + free (p); \ + (p) = NULL; \ + } \ } -int yyerror (const char *fmt, ...); -void regerror (char *msg); -int empty_line (const line_t *line); -size_t expand_tabs_into (const char *input_buffer, const size_t in_len, - const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len); -void btrim (char *text, size_t *len); -char* my_strnrstr (const char *s1, const char *s2, const size_t s2_len, - int skip); -int strisyes (const char *s); -int strisno (const char *s); +int yyerror(const char *fmt, ...); -void concat_strings (char *dst, int max_len, int count, ...); +void regerror(char *msg); + +int empty_line(const line_t *line); + +size_t expand_tabs_into(const char *input_buffer, const size_t in_len, + const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len); + +void btrim(char *text, size_t *len); + +char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, + int skip); + +int strisyes(const char *s); + +int strisno(const char *s); + +void concat_strings(char *dst, int max_len, int count, ...); + +char *tabbify_indent(const size_t lineno, char *indentspc, const size_t indentspc_len); -char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indentspc_len); #endif