Apply new code format from CLion

This commit is contained in:
Thomas Jensen 2021-01-27 17:13:17 +01:00
parent dcd2328c77
commit f536d45f7a
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
6 changed files with 1022 additions and 897 deletions

View File

@ -33,6 +33,7 @@
#include "generate.h" #include "generate.h"
static int horiz_precalc(const sentry_t *sarr, static int horiz_precalc(const sentry_t *sarr,
size_t *topiltf, size_t *botiltf, size_t *hspace) size_t *topiltf, size_t *botiltf, size_t *hspace)
/* /*
@ -81,10 +82,15 @@ static int horiz_precalc (const sentry_t *sarr,
/* /*
* Compute number of existent shapes in top and in bottom part * Compute number of existent shapes in top and in bottom part
*/ */
tnumsh = 0; bnumsh = 0; tnumsh = 0;
bnumsh = 0;
for (i = 1; i < SHAPES_PER_SIDE - 1; ++i) { for (i = 1; i < SHAPES_PER_SIDE - 1; ++i) {
if (!isempty(sarr+north_side[i])) tnumsh++; if (!isempty(sarr + north_side[i])) {
if (!isempty(sarr+south_side[i])) bnumsh++; tnumsh++;
}
if (!isempty(sarr + south_side[i])) {
bnumsh++;
}
} }
#ifdef DEBUG #ifdef DEBUG
@ -136,8 +142,7 @@ static int horiz_precalc (const sentry_t *sarr,
if (!isempty(&(sarr[seite[i]]))) { if (!isempty(&(sarr[seite[i]]))) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_hspace < target_width || *res_hspace < target_width ||
twidth != bwidth) twidth != bwidth) {
{
iltf[i - 1] += sarr[seite[i]].width; iltf[i - 1] += sarr[seite[i]].width;
*res_hspace += sarr[seite[i]].width; *res_hspace += sarr[seite[i]].width;
} }
@ -163,8 +168,7 @@ static int horiz_precalc (const sentry_t *sarr,
if (!isempty(sarr + seite[i]) && sarr[seite[i]].elastic) { if (!isempty(sarr + seite[i]) && sarr[seite[i]].elastic) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_hspace < target_width || *res_hspace < target_width ||
twidth != bwidth) twidth != bwidth) {
{
iltf[i - 1] += sarr[seite[i]].width; iltf[i - 1] += sarr[seite[i]].width;
*res_hspace += sarr[seite[i]].width; *res_hspace += sarr[seite[i]].width;
} }
@ -187,8 +191,7 @@ static int horiz_precalc (const sentry_t *sarr,
if (sarr[seite[1]].elastic && sarr[seite[3]].elastic) { if (sarr[seite[1]].elastic && sarr[seite[3]].elastic) {
if (iltf[*stoggle - 1] == 0 || if (iltf[*stoggle - 1] == 0 ||
*res_hspace < target_width || *res_hspace < target_width ||
twidth != bwidth) twidth != bwidth) {
{
*res_hspace += sarr[seite[*stoggle]].width; *res_hspace += sarr[seite[*stoggle]].width;
iltf[*stoggle - 1] += sarr[seite[*stoggle]].width; iltf[*stoggle - 1] += sarr[seite[*stoggle]].width;
} }
@ -199,8 +202,7 @@ static int horiz_precalc (const sentry_t *sarr,
if (sarr[seite[i]].elastic) { if (sarr[seite[i]].elastic) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_hspace < target_width || *res_hspace < target_width ||
twidth != bwidth) twidth != bwidth) {
{
iltf[i - 1] += sarr[seite[i]].width; iltf[i - 1] += sarr[seite[i]].width;
*res_hspace += sarr[seite[i]].width; *res_hspace += sarr[seite[i]].width;
} }
@ -214,7 +216,6 @@ static int horiz_precalc (const sentry_t *sarr,
fprintf(stderr, "%s: internal error in horiz_precalc()\n", PROJECT); fprintf(stderr, "%s: internal error in horiz_precalc()\n", PROJECT);
return 1; return 1;
} }
} while (twidth != bwidth || twidth < target_width || bwidth < target_width); } while (twidth != bwidth || twidth < target_width || bwidth < target_width);
*hspace = twidth; /* return either one */ *hspace = twidth; /* return either one */
@ -272,10 +273,15 @@ static int vert_precalc (const sentry_t *sarr,
/* /*
* Compute number of existent shapes in left and right part (1..3) * Compute number of existent shapes in left and right part (1..3)
*/ */
lnumsh = 0; rnumsh = 0; lnumsh = 0;
rnumsh = 0;
for (i = 1; i < SHAPES_PER_SIDE - 1; ++i) { for (i = 1; i < SHAPES_PER_SIDE - 1; ++i) {
if (!isempty(sarr+west_side[i])) lnumsh++; if (!isempty(sarr + west_side[i])) {
if (!isempty(sarr+east_side[i])) rnumsh++; lnumsh++;
}
if (!isempty(sarr + east_side[i])) {
rnumsh++;
}
} }
lheight = 0; lheight = 0;
@ -320,8 +326,7 @@ static int vert_precalc (const sentry_t *sarr,
if (!isempty(&(sarr[seite[i]]))) { if (!isempty(&(sarr[seite[i]]))) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_vspace < target_height || *res_vspace < target_height ||
lheight != rheight) lheight != rheight) {
{
iltf[i - 1] += sarr[seite[i]].height; iltf[i - 1] += sarr[seite[i]].height;
*res_vspace += sarr[seite[i]].height; *res_vspace += sarr[seite[i]].height;
} }
@ -347,8 +352,7 @@ static int vert_precalc (const sentry_t *sarr,
if (!isempty(sarr + seite[i]) && sarr[seite[i]].elastic) { if (!isempty(sarr + seite[i]) && sarr[seite[i]].elastic) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_vspace < target_height || *res_vspace < target_height ||
lheight != rheight) lheight != rheight) {
{
iltf[i - 1] += sarr[seite[i]].height; iltf[i - 1] += sarr[seite[i]].height;
*res_vspace += sarr[seite[i]].height; *res_vspace += sarr[seite[i]].height;
} }
@ -371,8 +375,7 @@ static int vert_precalc (const sentry_t *sarr,
if (sarr[seite[1]].elastic && sarr[seite[3]].elastic) { if (sarr[seite[1]].elastic && sarr[seite[3]].elastic) {
if (iltf[*stoggle - 1] == 0 || if (iltf[*stoggle - 1] == 0 ||
*res_vspace < target_height || *res_vspace < target_height ||
lheight != rheight) lheight != rheight) {
{
*res_vspace += sarr[seite[*stoggle]].height; *res_vspace += sarr[seite[*stoggle]].height;
iltf[*stoggle - 1] += sarr[seite[*stoggle]].height; iltf[*stoggle - 1] += sarr[seite[*stoggle]].height;
} }
@ -383,8 +386,7 @@ static int vert_precalc (const sentry_t *sarr,
if (sarr[seite[i]].elastic) { if (sarr[seite[i]].elastic) {
if (iltf[i - 1] == 0 || if (iltf[i - 1] == 0 ||
*res_vspace < target_height || *res_vspace < target_height ||
lheight != rheight) lheight != rheight) {
{
iltf[i - 1] += sarr[seite[i]].height; iltf[i - 1] += sarr[seite[i]].height;
*res_vspace += sarr[seite[i]].height; *res_vspace += sarr[seite[i]].height;
} }
@ -398,7 +400,6 @@ static int vert_precalc (const sentry_t *sarr,
fprintf(stderr, "%s: internal error in vert_precalc()\n", PROJECT); fprintf(stderr, "%s: internal error in vert_precalc()\n", PROJECT);
return 1; return 1;
} }
} while (lheight != rheight || lheight < target_height || rheight < target_height); } while (lheight != rheight || lheight < target_height || rheight < target_height);
*vspace = lheight; /* return either one */ *vspace = lheight; /* return either one */
@ -426,9 +427,11 @@ static int vert_assemble (const sentry_t *sarr, const shape_t *seite,
result->chars[line] = (char *) calloc(1, result->width + 1); result->chars[line] = (char *) calloc(1, result->width + 1);
if (result->chars[line] == NULL) { if (result->chars[line] == NULL) {
perror(PROJECT); perror(PROJECT);
if ((long)--line >= 0) do { if ((long) --line >= 0) {
do {
BFREE (result->chars[line--]); BFREE (result->chars[line--]);
} while ((long) line >= 0); } while ((long) line >= 0);
}
return 1; /* out of memory */ return 1; /* out of memory */
} }
} }
@ -436,10 +439,12 @@ static int vert_assemble (const sentry_t *sarr, const shape_t *seite,
cshape = (seite == north_side) ? 0 : 2; cshape = (seite == north_side) ? 0 : 2;
for (j = 0; j < result->width; j += sarr[seite[cshape + 1]].width) { for (j = 0; j < result->width; j += sarr[seite[cshape + 1]].width) {
while (iltf[cshape] == 0) while (iltf[cshape] == 0) {
cshape += (seite == north_side) ? 1 : -1; cshape += (seite == north_side) ? 1 : -1;
for (line=0; line<result->height; ++line) }
for (line = 0; line < result->height; ++line) {
strcat(result->chars[line], sarr[seite[cshape + 1]].chars[line]); strcat(result->chars[line], sarr[seite[cshape + 1]].chars[line]);
}
iltf[cshape] -= sarr[seite[cshape + 1]].width; iltf[cshape] -= sarr[seite[cshape + 1]].width;
} }
@ -467,24 +472,27 @@ static void horiz_assemble (const sentry_t *sarr, const shape_t *seite,
cshape = 2; cshape = 2;
} }
for (j=0; j<sarr[ctop].height; ++j) for (j = 0; j < sarr[ctop].height; ++j) {
result->chars[j] = sarr[ctop].chars[j]; result->chars[j] = sarr[ctop].chars[j];
for (j=0; j<sarr[cbottom].height; ++j) }
for (j = 0; j < sarr[cbottom].height; ++j) {
result->chars[result->height - sarr[cbottom].height + j] = result->chars[result->height - sarr[cbottom].height + j] =
sarr[cbottom].chars[j]; sarr[cbottom].chars[j];
}
sc = 0; 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) { while (iltf[cshape] == 0) {
if (seite == east_side) if (seite == east_side) {
++cshape; ++cshape;
else } else {
--cshape; --cshape;
}
sc = 0; sc = 0;
} }
if (sc == sarr[seite[cshape+1]].height) if (sc == sarr[seite[cshape + 1]].height) {
sc = 0; sc = 0;
}
result->chars[j] = sarr[seite[cshape + 1]].chars[sc]; result->chars[j] = sarr[seite[cshape + 1]].chars[sc];
++sc; ++sc;
iltf[cshape] -= 1; iltf[cshape] -= 1;
@ -513,7 +521,9 @@ static int horiz_generate (sentry_t *tresult, sentry_t *bresult)
SHAPES_PER_SIDE, SW, SSW, S, SSE, SE); SHAPES_PER_SIDE, SW, SSW, S, SSE, SE);
rc = horiz_precalc(opt.design->shape, tiltf, biltf, &(tresult->width)); rc = horiz_precalc(opt.design->shape, tiltf, biltf, &(tresult->width));
if (rc) return rc; if (rc) {
return rc;
}
bresult->width = tresult->width; bresult->width = tresult->width;
#ifdef DEBUG #ifdef DEBUG
@ -533,12 +543,18 @@ static int horiz_generate (sentry_t *tresult, sentry_t *bresult)
tresult->chars = (char **) calloc(tresult->height, sizeof(char *)); tresult->chars = (char **) calloc(tresult->height, sizeof(char *));
bresult->chars = (char **) calloc(bresult->height, sizeof(char *)); bresult->chars = (char **) calloc(bresult->height, sizeof(char *));
if (tresult->chars == NULL || bresult->chars == NULL) return 1; if (tresult->chars == NULL || bresult->chars == NULL) {
return 1;
}
rc = vert_assemble(opt.design->shape, north_side, tiltf, tresult); rc = vert_assemble(opt.design->shape, north_side, tiltf, tresult);
if (rc) return rc; if (rc) {
return rc;
}
rc = vert_assemble(opt.design->shape, south_side, biltf, bresult); rc = vert_assemble(opt.design->shape, south_side, biltf, bresult);
if (rc) return rc; if (rc) {
return rc;
}
#if defined(DEBUG) && 1 #if defined(DEBUG) && 1
{ {
@ -585,7 +601,9 @@ static int vert_generate (sentry_t *lresult, sentry_t *rresult)
SHAPES_PER_SIDE, SE, ESE, E, ENE, NE); SHAPES_PER_SIDE, SE, ESE, E, ENE, NE);
rc = vert_precalc(opt.design->shape, leftiltf, rightiltf, &vspace); rc = vert_precalc(opt.design->shape, leftiltf, rightiltf, &vspace);
if (rc) return rc; if (rc) {
return rc;
}
lresult->height = vspace + lresult->height = vspace +
opt.design->shape[NW].height + opt.design->shape[SW].height; opt.design->shape[NW].height + opt.design->shape[SW].height;
@ -608,9 +626,13 @@ static int vert_generate (sentry_t *lresult, sentry_t *rresult)
#endif #endif
lresult->chars = (char **) calloc(lresult->height, sizeof(char *)); lresult->chars = (char **) calloc(lresult->height, sizeof(char *));
if (lresult->chars == NULL) return 1; if (lresult->chars == NULL) {
return 1;
}
rresult->chars = (char **) calloc(rresult->height, sizeof(char *)); rresult->chars = (char **) calloc(rresult->height, sizeof(char *));
if (rresult->chars == NULL) return 1; if (rresult->chars == NULL) {
return 1;
}
horiz_assemble(opt.design->shape, west_side, leftiltf, lresult); horiz_assemble(opt.design->shape, west_side, leftiltf, lresult);
horiz_assemble(opt.design->shape, east_side, rightiltf, rresult); horiz_assemble(opt.design->shape, east_side, rightiltf, rresult);
@ -652,10 +674,14 @@ int generate_box (sentry_t *thebox)
int i; int i;
rc = horiz_generate(&(thebox[0]), &(thebox[2])); rc = horiz_generate(&(thebox[0]), &(thebox[2]));
if (rc) goto err; if (rc) {
goto err;
}
rc = vert_generate(&(thebox[3]), &(thebox[1])); rc = vert_generate(&(thebox[3]), &(thebox[1]));
if (rc) goto err; if (rc) {
goto err;
}
return 0; /* all clear */ return 0; /* all clear */
@ -701,7 +727,8 @@ static int justify_line (line_t *line, int skew)
return 0; return 0;
} }
for (p=line->text; *p==' ' || *p=='\t'; ++p); for (p = line->text; *p == ' ' || *p == '\t'; ++p) {
}
newlen = line->len - (p - line->text); newlen = line->len - (p - line->text);
switch (opt.justify) { switch (opt.justify) {
@ -725,14 +752,16 @@ static int justify_line (line_t *line, int skew)
if (opt.design->indentmode == 't') { if (opt.design->indentmode == 't') {
shift = (input.maxline - input.indent - newlen + line->invis) / 2 + input.indent; shift = (input.maxline - input.indent - newlen + line->invis) / 2 + input.indent;
skew -= input.indent; skew -= input.indent;
if ((input.maxline-input.indent - newlen) % 2 && skew == 1) if ((input.maxline - input.indent - newlen) % 2 && skew == 1) {
++shift; ++shift;
} }
}
else { else {
shift = (input.maxline - newlen + line->invis) / 2; 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; ++shift;
} }
}
newtext = (char *) calloc(shift + newlen + line->invis + 1, sizeof(char)); newtext = (char *) calloc(shift + newlen + line->invis + 1, sizeof(char));
if (newtext == NULL) { if (newtext == NULL) {
@ -976,12 +1005,15 @@ int output_box (const sentry_t *thebox)
skip_start = 0; skip_start = 0;
skip_end = 0; skip_end = 0;
skip_left = 0; skip_left = 0;
if (empty_side (opt.design->shape, BTOP)) if (empty_side(opt.design->shape, BTOP)) {
skip_start = opt.design->shape[NW].height; 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; 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 (empty_side(opt.design->shape, BLEF)) {
skip_left = opt.design->shape[NW].width;
} /* could simply be 1, though */
#if defined(DEBUG) #if defined(DEBUG)
fprintf (stderr, "skip_start = %d; skip_end = %d; skip_left = %d; " fprintf (stderr, "skip_start = %d; skip_end = %d; skip_left = %d; "
"nol = %d;\n", skip_start, skip_end, skip_left, nol); "nol = %d;\n", skip_start, skip_end, skip_left, nol);
@ -1019,8 +1051,9 @@ int output_box (const sentry_t *thebox)
input.lines[ti].text, input.lines[ti].len, hpr-hpl); input.lines[ti].text, input.lines[ti].len, hpr-hpl);
#endif #endif
rc = justify_line(input.lines + ti, hpr - hpl); rc = justify_line(input.lines + ti, hpr - hpl);
if (rc) if (rc) {
return rc; return rc;
}
r = input.maxline - input.lines[ti].vischar; r = input.maxline - input.lines[ti].vischar;
trailspc[r] = '\0'; trailspc[r] = '\0';
restored_indent = tabbify_indent(ti, indentspc, indentspclen); restored_indent = tabbify_indent(ti, indentspc, indentspclen);

View File

@ -27,6 +27,7 @@
int generate_box(sentry_t *thebox); int generate_box(sentry_t *thebox);
int output_box(const sentry_t *thebox); int output_box(const sentry_t *thebox);

View File

@ -90,8 +90,9 @@ static int best_match (const line_t *line,
chkline.text = cs->chars[k]; chkline.text = cs->chars[k];
chkline.len = cs->width; chkline.len = cs->width;
if (empty_line (&chkline) && !(quality==0 && j==numw-1)) if (empty_line(&chkline) && !(quality == 0 && j == numw - 1)) {
continue; continue;
}
s = (char *) strdup(cs->chars[k]); s = (char *) strdup(cs->chars[k]);
if (s == NULL) { if (s == NULL) {
@ -110,15 +111,16 @@ static int best_match (const line_t *line,
break; break;
} }
} }
if (p) if (p) {
break; break;
} }
}
if (!p && cq) { if (!p && cq) {
if (*s == ' ') if (*s == ' ') {
memmove(s, s + 1, cq--); memmove(s, s + 1, cq--);
else if (s[cq-1] == ' ') } else if (s[cq - 1] == ' ') {
s[--cq] = '\0'; s[--cq] = '\0';
else { } else {
cq = 0; cq = 0;
break; break;
} }
@ -161,8 +163,9 @@ static int best_match (const line_t *line,
chkline.text = cs->chars[k]; chkline.text = cs->chars[k];
chkline.len = cs->width; chkline.len = cs->width;
if (empty_line (&chkline)) if (empty_line(&chkline)) {
continue; continue;
}
s = (char *) strdup(cs->chars[k]); s = (char *) strdup(cs->chars[k]);
if (s == NULL) { if (s == NULL) {
@ -181,15 +184,16 @@ static int best_match (const line_t *line,
break; break;
} }
} }
if (p) if (p) {
break; break;
} }
}
if (!p && cq) { if (!p && cq) {
if (*s == ' ') if (*s == ' ') {
memmove(s, s + 1, cq--); memmove(s, s + 1, cq--);
else if (s[cq-1] == ' ') } else if (s[cq - 1] == ' ') {
s[--cq] = '\0'; s[--cq] = '\0';
else { } else {
cq = 0; cq = 0;
break; break;
} }
@ -248,12 +252,14 @@ static int hmm (const int aside, const size_t follow,
aside==BTOP?"BTOP":"BBOT", follow, p[0], *ecs, cnt); aside==BTOP?"BTOP":"BBOT", follow, p[0], *ecs, cnt);
#endif #endif
if (p > ecs) /* last shape tried was too long */ if (p > ecs) { /* last shape tried was too long */
return 2; return 2;
}
sh = leftmost(aside, cnt); sh = leftmost(aside, cnt);
if (sh == ANZ_SHAPES) if (sh == ANZ_SHAPES) {
return 1; return 1;
}
cs = opt.design->shape + sh; cs = opt.design->shape + sh;
@ -261,10 +267,11 @@ static int hmm (const int aside, const size_t follow,
if (cmp == 0) { if (cmp == 0) {
if (p + cs->width == ecs) { if (p + cs->width == ecs) {
if (leftmost (aside, cnt+1) == ANZ_SHAPES) if (leftmost(aside, cnt + 1) == ANZ_SHAPES) {
return 0; /* good! all clear, it matched */ return 0; /* good! all clear, it matched */
else } else {
return 3; /* didn't use all shapes to do it */ return 3;
} /* didn't use all shapes to do it */
} }
if (cs->elastic) { if (cs->elastic) {
rc = hmm(aside, follow, p + cs->width, ecs, cnt); rc = hmm(aside, follow, p + cs->width, ecs, cnt);
@ -284,10 +291,11 @@ static int hmm (const int aside, const size_t follow,
fprintf (stderr, "hmm returned %d\n", rc); fprintf (stderr, "hmm returned %d\n", rc);
#endif #endif
} }
if (rc == 0) if (rc == 0) {
return 0; /* we're on the way back */ return 0; /* we're on the way back */
else } else {
return 4; /* can't continue on this path */ return 4;
} /* can't continue on this path */
} }
else { else {
return 5; /* no match */ return 5; /* no match */
@ -347,8 +355,7 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
} }
for (lcnt = 0; lcnt < mheight && lcnt < input.anz_lines for (lcnt = 0; lcnt < mheight && lcnt < input.anz_lines
&& line >= input.lines; ++lcnt) && line >= input.lines; ++lcnt) {
{
goeast = gowest = 0; goeast = gowest = 0;
#ifdef DEBUG #ifdef DEBUG
@ -366,11 +373,12 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
wcs = NULL; wcs = NULL;
if (gowest) { if (gowest) {
gowest = 0; gowest = 0;
if (*p == ' ' || *p == '\t') if (*p == ' ' || *p == '\t') {
++p; ++p;
else } else {
break; break;
} }
}
else { else {
p = line->text; p = line->text;
} }
@ -386,14 +394,17 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
} }
if (wcs) { if (wcs) {
for (q = wcs - 1; q >= line->text; --q) { for (q = wcs - 1; q >= line->text; --q) {
if (*q != ' ' && *q != '\t') if (*q != ' ' && *q != '\t') {
break; break;
} }
if (q >= line->text) }
if (q >= line->text) {
wcs = NULL; wcs = NULL;
} }
if (!wcs) }
if (!wcs) {
break; break;
}
p = wcs + cs->width; p = wcs + cs->width;
} }
@ -402,13 +413,13 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
/* or not NULL (if west side is not empty). In any case, p */ /* or not NULL (if west side is not empty). In any case, p */
/* points to where we start searching for the east corner. */ /* points to where we start searching for the east corner. */
#ifdef DEBUG #ifdef DEBUG
if (wcs) if (wcs) {
fprintf (stderr, "West corner shape matched at " fprintf (stderr, "West corner shape matched at "
"position %d.\n", wcs - line->text); "position %d.\n", wcs - line->text);
else } else {
fprintf (stderr, "West box side is empty.\n"); fprintf (stderr, "West box side is empty.\n");
}
#endif #endif
/* /*
* Look for east corner shape * Look for east corner shape
*/ */
@ -416,37 +427,37 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
ecs = my_strnrstr(p, cs->chars[follow], cs->width, goeast); ecs = my_strnrstr(p, cs->chars[follow], cs->width, goeast);
if (ecs) { if (ecs) {
for (q = ecs + cs->width; *q; ++q) { for (q = ecs + cs->width; *q; ++q) {
if (*q != ' ' && *q != '\t') if (*q != ' ' && *q != '\t') {
break; break;
} }
if (*q) }
if (*q) {
ecs = NULL; ecs = NULL;
} }
}
if (!ecs) { if (!ecs) {
if (goeast == 0) if (goeast == 0) {
break; break;
else { } else {
goeast = 0; goeast = 0;
gowest = 1; gowest = 1;
continue; continue;
} }
} }
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "East corner shape matched at position %d.\n", fprintf (stderr, "East corner shape matched at position %d.\n", ecs-line->text);
ecs-line->text);
#endif #endif
/* /*
* Check if text between corner shapes is valid * Check if text between corner shapes is valid
*/ */
mmok = !hmm(aside, follow, p, ecs, 0); mmok = !hmm(aside, follow, p, ecs, 0);
if (!mmok) if (!mmok) {
++goeast; ++goeast;
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "Text between corner shapes is %s.\n", fprintf (stderr, "Text between corner shapes is %s.\n", mmok? "VALID": "NOT valid");
mmok? "VALID": "NOT valid");
#endif #endif
} while (!mmok); } while (!mmok);
/* /*
@ -455,20 +466,23 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
if (mmok) { /* match found */ if (mmok) { /* match found */
if (!result_init) { if (!result_init) {
result_init = 1; result_init = 1;
if (aside == BTOP) if (aside == BTOP) {
*hstart = lcnt; *hstart = lcnt;
else } else {
*hend = (input.anz_lines - lcnt - 1) + 1; *hend = (input.anz_lines - lcnt - 1) + 1;
} }
if (aside == BTOP) }
if (aside == BTOP) {
*hend = lcnt + 1; *hend = lcnt + 1;
else } else {
*hstart = input.anz_lines - lcnt - 1; *hstart = input.anz_lines - lcnt - 1;
} }
}
else { else {
if (result_init) if (result_init) {
break; break;
} }
}
wcs = NULL; wcs = NULL;
ecs = NULL; ecs = NULL;
@ -523,8 +537,9 @@ static design_t *detect_design()
#endif #endif
hits = 0; hits = 0;
for (j=0; j<ANZ_SIDES; ++j) for (j = 0; j < ANZ_SIDES; ++j) {
empty[j] = empty_side(d->shape, j); empty[j] = empty_side(d->shape, j);
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "Empty sides: TOP %d, LEFT %d, BOTTOM %d, RIGHT %d\n", fprintf (stderr, "Empty sides: TOP %d, LEFT %d, BOTTOM %d, RIGHT %d\n",
empty[BTOP], empty[BLEF], empty[BBOT], empty[BRIG]); empty[BTOP], empty[BLEF], empty[BBOT], empty[BRIG]);
@ -533,80 +548,96 @@ static design_t *detect_design()
for (scnt = 0; scnt < ANZ_SHAPES; ++scnt) { for (scnt = 0; scnt < ANZ_SHAPES; ++scnt) {
switch (scnt) { switch (scnt) {
case NW: case SW: case NW:
case SW:
/* /*
* Try and find west corner shapes. Every non-empty shape * Try and find west corner shapes. Every non-empty shape
* line is searched for on every input line. A hit is * line is searched for on every input line. A hit is
* generated whenever a match is found. * generated whenever a match is found.
*/ */
if (empty[BLEF] || (empty[BTOP] && scnt == NW) if (empty[BLEF] || (empty[BTOP] && scnt == NW)
|| (empty[BBOT] && scnt == SW)) || (empty[BBOT] && scnt == SW)) {
break; break;
}
for (j = 0; j < d->shape[scnt].height; ++j) { for (j = 0; j < d->shape[scnt].height; ++j) {
shpln.text = d->shape[scnt].chars[j]; shpln.text = d->shape[scnt].chars[j];
shpln.len = d->shape[scnt].width; shpln.len = d->shape[scnt].width;
if (empty_line (&shpln)) if (empty_line(&shpln)) {
continue; continue;
for (s=shpln.text; *s==' ' || *s=='\t'; ++s); }
for (s = shpln.text; *s == ' ' || *s == '\t'; ++s) {
}
for (k = 0; k < d->shape[scnt].height; ++k) { for (k = 0; k < d->shape[scnt].height; ++k) {
a = k; a = k;
if (scnt == SW) if (scnt == SW) {
a += input.anz_lines - d->shape[scnt].height; a += input.anz_lines - d->shape[scnt].height;
if (a >= input.anz_lines) }
if (a >= input.anz_lines) {
break; 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; ++hits;
} }
} }
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "After %s corner check:\t%ld hits.\n", fprintf (stderr, "After %s corner check:\t%ld hits.\n", shape_name[scnt], hits);
shape_name[scnt], hits);
#endif #endif
break; break;
case NE: case SE: case NE:
case SE:
/* /*
* Try and find east corner shapes. Every non-empty shape * Try and find east corner shapes. Every non-empty shape
* line is searched for on every input line. A hit is * line is searched for on every input line. A hit is
* generated whenever a match is found. * generated whenever a match is found.
*/ */
if (empty[BRIG] || (empty[BTOP] && scnt == NE) if (empty[BRIG] || (empty[BTOP] && scnt == NE)
|| (empty[BBOT] && scnt == SE)) || (empty[BBOT] && scnt == SE)) {
break; break;
}
for (j = 0; j < d->shape[scnt].height; ++j) { for (j = 0; j < d->shape[scnt].height; ++j) {
shpln.text = d->shape[scnt].chars[j]; shpln.text = d->shape[scnt].chars[j];
shpln.len = d->shape[scnt].width; shpln.len = d->shape[scnt].width;
if (empty_line (&shpln)) if (empty_line(&shpln)) {
continue; continue;
}
for (s = shpln.text + shpln.len - 1; for (s = shpln.text + shpln.len - 1;
(*s == ' ' || *s == '\t') && shpln.len; (*s == ' ' || *s == '\t') && shpln.len;
--s, --(shpln.len)); --s, --(shpln.len)) {
}
for (k = 0; k < d->shape[scnt].height; ++k) { for (k = 0; k < d->shape[scnt].height; ++k) {
a = k; a = k;
if (scnt == SE) if (scnt == SE) {
a += input.anz_lines - d->shape[scnt].height; a += input.anz_lines - d->shape[scnt].height;
if (a >= input.anz_lines) }
if (a >= input.anz_lines) {
break; break;
}
for (p = input.lines[a].text + input.lines[a].len - 1; for (p = input.lines[a].text + input.lines[a].len - 1;
p >= input.lines[a].text && (*p == ' ' || *p == '\t'); p >= input.lines[a].text && (*p == ' ' || *p == '\t');
--p); --p) {
}
p = p - shpln.len + 1; p = p - shpln.len + 1;
if (p < input.lines[a].text) if (p < input.lines[a].text) {
continue; continue;
if (strncmp (p, shpln.text, shpln.len) == 0) }
if (strncmp(p, shpln.text, shpln.len) == 0) {
++hits; ++hits;
} }
} }
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "After %s corner check:\t%ld hits.\n", fprintf (stderr, "After %s corner check:\t%ld hits.\n", shape_name[scnt], hits);
shape_name[scnt], hits);
#endif #endif
break; break;
default: default:
if (isempty (d->shape+scnt)) if (isempty(d->shape + scnt)) {
continue; continue;
}
if ((scnt >= NNW && scnt <= NNE) if ((scnt >= NNW && scnt <= NNE)
|| (scnt >= SSE && scnt <= SSW)) { || (scnt >= SSE && scnt <= SSW)) {
@ -624,30 +655,37 @@ static design_t *detect_design()
for (j = 0; j < d->shape[scnt].height; ++j) { for (j = 0; j < d->shape[scnt].height; ++j) {
shpln.text = d->shape[scnt].chars[j]; shpln.text = d->shape[scnt].chars[j];
shpln.len = d->shape[scnt].width; shpln.len = d->shape[scnt].width;
if (empty_line (&shpln)) if (empty_line(&shpln)) {
continue; continue;
}
for (k = 0; k < d->shape[scnt].height; ++k) { for (k = 0; k < d->shape[scnt].height; ++k) {
a = k; a = k;
if (scnt >= SSE && scnt <= SSW) if (scnt >= SSE && scnt <= SSW) {
a += input.anz_lines - d->shape[scnt].height; a += input.anz_lines - d->shape[scnt].height;
if (a >= input.anz_lines) }
if (a >= input.anz_lines) {
break; break;
}
for (p = input.lines[a].text; for (p = input.lines[a].text;
*p == ' ' || *p == '\t'; ++p); *p == ' ' || *p == '\t'; ++p) {
}
p += d->shape[NW].width; p += d->shape[NW].width;
if (p - input.lines[a].text if (p - input.lines[a].text
>= (long) input.lines[a].len) >= (long) input.lines[a].len) {
continue; continue;
}
p = strstr(p, shpln.text); p = strstr(p, shpln.text);
if (p) { if (p) {
if (d->shape[scnt].elastic) { if (d->shape[scnt].elastic) {
p += shpln.len; p += shpln.len;
if (p - input.lines[a].text if (p - input.lines[a].text
>= (long) input.lines[a].len) >= (long) input.lines[a].len) {
continue; continue;
if (!strncmp (p, shpln.text, shpln.len)) }
if (!strncmp(p, shpln.text, shpln.len)) {
++hits; ++hits;
} }
}
else { else {
++hits; ++hits;
} }
@ -678,48 +716,55 @@ static design_t *detect_design()
* non-empty shape line. If so, generate a hit. * non-empty shape line. If so, generate a hit.
*/ */
if (((empty[BTOP] ? 0 : d->shape[NW].height) if (((empty[BTOP] ? 0 : d->shape[NW].height)
+ (empty[BBOT]? 0: d->shape[SW].height)) < input.anz_lines) + (empty[BBOT] ? 0 : d->shape[SW].height)) < input.anz_lines) {
{
for (k = empty[BTOP] ? 0 : d->shape[NW].height; for (k = empty[BTOP] ? 0 : d->shape[NW].height;
k < input.anz_lines - (empty[BBOT] ? 0 : d->shape[SW].height); k < input.anz_lines - (empty[BBOT] ? 0 : d->shape[SW].height);
++k) ++k) {
{ for (p = input.lines[k].text; *p == ' ' || *p == '\t'; ++p) {
for (p=input.lines[k].text; *p==' ' || *p=='\t'; ++p); }
for (scnt = WSW; scnt <= WNW; ++scnt) { for (scnt = WSW; scnt <= WNW; ++scnt) {
a = 0; a = 0;
if (isempty (d->shape + scnt)) if (isempty(d->shape + scnt)) {
continue; continue;
}
for (j = 0; j < d->shape[scnt].height; ++j) { for (j = 0; j < d->shape[scnt].height; ++j) {
shpln.text = d->shape[scnt].chars[j]; shpln.text = d->shape[scnt].chars[j];
shpln.len = d->shape[scnt].width; shpln.len = d->shape[scnt].width;
if (empty_line (&shpln)) if (empty_line(&shpln)) {
continue; continue;
for (s=shpln.text; *s==' ' || *s=='\t'; ++s); }
for (s = shpln.text; *s == ' ' || *s == '\t'; ++s) {
}
if (strncmp(p, s, shpln.len - (s - shpln.text)) == 0) { if (strncmp(p, s, shpln.len - (s - shpln.text)) == 0) {
++hits; ++hits;
a = 1; a = 1;
break; break;
} }
} }
if (a) if (a) {
break; break;
} }
}
for (scnt = ENE; scnt <= ESE; ++scnt) { for (scnt = ENE; scnt <= ESE; ++scnt) {
a = 0; a = 0;
if (isempty (d->shape + scnt)) if (isempty(d->shape + scnt)) {
continue; continue;
}
for (j = 0; j < d->shape[scnt].height; ++j) { for (j = 0; j < d->shape[scnt].height; ++j) {
shpln.text = d->shape[scnt].chars[j]; shpln.text = d->shape[scnt].chars[j];
shpln.len = d->shape[scnt].width; shpln.len = d->shape[scnt].width;
if (empty_line (&shpln)) if (empty_line(&shpln)) {
continue; continue;
}
for (p = input.lines[k].text + input.lines[k].len - 1; for (p = input.lines[k].text + input.lines[k].len - 1;
p >= input.lines[a].text && (*p == ' ' || *p == '\t'); p >= input.lines[a].text && (*p == ' ' || *p == '\t');
--p); --p) {
}
for (s = shpln.text + shpln.len - 1; for (s = shpln.text + shpln.len - 1;
(*s == ' ' || *s == '\t') && shpln.len; (*s == ' ' || *s == '\t') && shpln.len;
--s, --(shpln.len)); --s, --(shpln.len)) {
}
p = p - shpln.len + 1; p = p - shpln.len + 1;
if (strncmp(p, shpln.text, shpln.len) == 0) { if (strncmp(p, shpln.text, shpln.len) == 0) {
++hits; ++hits;
@ -727,15 +772,15 @@ static design_t *detect_design()
break; break;
} }
} }
if (a) if (a) {
break; break;
} }
} }
} }
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "After side checks:\t%ld hits.\n", hits); fprintf (stderr, "After side checks:\t%ld hits.\n", hits);
#endif #endif
if (hits > maxhits) { if (hits > maxhits) {
maxhits = hits; maxhits = hits;
res = d; res = d;
@ -743,11 +788,11 @@ static design_t *detect_design()
} }
#ifdef DEBUG #ifdef DEBUG
if (res) if (res) {
fprintf (stderr, "CHOOSING \"%s\" design (%ld hits).\n", fprintf (stderr, "CHOOSING \"%s\" design (%ld hits).\n", res->name, maxhits);
res->name, maxhits); } else {
else
fprintf (stderr, "NO DESIGN FOUND WITH EVEN ONE HIT!\n"); fprintf (stderr, "NO DESIGN FOUND WITH EVEN ONE HIT!\n");
}
#endif #endif
return res; return res;
@ -906,15 +951,17 @@ int remove_box()
#endif #endif
if (ws && we) { if (ws && we) {
did_something = 1; did_something = 1;
for (p=ws; p<we; ++p) for (p = ws; p < we; ++p) {
*p = ' '; *p = ' ';
} }
}
if (es && ee) { if (es && ee) {
for (p=es; p<ee; ++p) for (p = es; p < ee; ++p) {
*p = ' '; *p = ' ';
} }
} }
} }
}
/* /*
* Remove as many spaces from the left side of the line as the west side * Remove as many spaces from the left side of the line as the west side
@ -926,9 +973,10 @@ int remove_box()
size_t c; size_t c;
size_t widz = opt.design->shape[NW].width + opt.design->padding[BLEF]; size_t widz = opt.design->shape[NW].width + opt.design->padding[BLEF];
for (c = 0; c < widz; ++c) { for (c = 0; c < widz; ++c) {
if (input.lines[j].text[c] != ' ') if (input.lines[j].text[c] != ' ') {
break; break;
} }
}
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "memmove (\"%s\", \"%s\", %d);\n", fprintf (stderr, "memmove (\"%s\", \"%s\", %d);\n",
input.lines[j].text, input.lines[j].text + c, input.lines[j].text, input.lines[j].text + c,
@ -964,8 +1012,7 @@ int remove_box()
} }
if (textstart > boxstart) { if (textstart > boxstart) {
for (j=boxstart; j<textstart; ++j) for (j = boxstart; j < textstart; ++j) BFREE (input.lines[j].text);
BFREE (input.lines[j].text);
memmove(input.lines + boxstart, input.lines + textstart, memmove(input.lines + boxstart, input.lines + textstart,
(input.anz_lines - textstart) * sizeof(line_t)); (input.anz_lines - textstart) * sizeof(line_t));
input.anz_lines -= textstart - boxstart; input.anz_lines -= textstart - boxstart;
@ -973,8 +1020,7 @@ int remove_box()
boxend -= textstart - boxstart; boxend -= textstart - boxstart;
} }
if (boxend > textend) { if (boxend > textend) {
for (j=textend; j<boxend; ++j) for (j = textend; j < boxend; ++j) BFREE (input.lines[j].text);
BFREE (input.lines[j].text);
if (boxend < input.anz_lines) { if (boxend < input.anz_lines) {
memmove(input.lines + textend, input.lines + boxend, memmove(input.lines + textend, input.lines + boxend,
(input.anz_lines - boxend) * sizeof(line_t)); (input.anz_lines - boxend) * sizeof(line_t));
@ -1023,13 +1069,14 @@ void output_input (const int trim_only)
#ifdef DEBUG #ifdef DEBUG
fprintf (stderr, "output_input() - enter (trim_only=%d)\n", trim_only); fprintf (stderr, "output_input() - enter (trim_only=%d)\n", trim_only);
#endif #endif
for (j=0; j<input.anz_lines; ++j) for (j = 0; j < input.anz_lines; ++j) {
{ if (input.lines[j].text == NULL) {
if (input.lines[j].text == NULL)
continue; continue;
}
btrim(input.lines[j].text, &(input.lines[j].len)); btrim(input.lines[j].text, &(input.lines[j].len));
if (trim_only) if (trim_only) {
continue; continue;
}
indentspc = NULL; indentspc = NULL;
if (opt.tabexp == 'u') { if (opt.tabexp == 'u') {

View File

@ -39,12 +39,16 @@ char *shape_name[] = {
}; };
shape_t north_side[SHAPES_PER_SIDE] = {NW, NNW, N, NNE, NE}; /* clockwise */ shape_t north_side[SHAPES_PER_SIDE] = {NW, NNW, N, NNE, NE}; /* clockwise */
shape_t east_side[SHAPES_PER_SIDE] = { NE, ENE, E, ESE, SE };
shape_t south_side[SHAPES_PER_SIDE] = { SE, SSE, S, SSW, SW };
shape_t west_side[SHAPES_PER_SIDE] = { SW, WSW, W, WNW, NW };
shape_t corners[ANZ_CORNERS] = { NW, NE, SE, SW };
shape_t *sides[] = { north_side, east_side, south_side, west_side };
shape_t east_side[SHAPES_PER_SIDE] = {NE, ENE, E, ESE, SE};
shape_t south_side[SHAPES_PER_SIDE] = {SE, SSE, S, SSW, SW};
shape_t west_side[SHAPES_PER_SIDE] = {SW, WSW, W, WNW, NW};
shape_t corners[ANZ_CORNERS] = {NW, NE, SE, SW};
shape_t *sides[] = {north_side, east_side, south_side, west_side};
@ -64,11 +68,12 @@ shape_t findshape (const sentry_t *sarr, const int num)
int i; int i;
for (i = 0; i < num; ++i) { for (i = 0; i < num; ++i) {
if (isempty(sarr+i)) if (isempty(sarr + i)) {
continue; continue;
else } else {
break; break;
} }
}
return (shape_t) i; return (shape_t) i;
} }
@ -95,13 +100,14 @@ int on_side (const shape_t s, const int idx)
for (side = 0; side < ANZ_SIDES; ++side) { for (side = 0; side < ANZ_SIDES; ++side) {
for (i = 0; i < SHAPES_PER_SIDE; ++i) { for (i = 0; i < SHAPES_PER_SIDE; ++i) {
if (sides[side][i] == s) { if (sides[side][i] == s) {
if (found == idx) if (found == idx) {
return side; return side;
else } else {
++found; ++found;
} }
} }
} }
}
return ANZ_SIDES; return ANZ_SIDES;
} }
@ -141,8 +147,7 @@ int genshape (const size_t width, const size_t height, char ***chars)
(*chars)[j] = (char *) calloc(width + 1, sizeof(char)); (*chars)[j] = (char *) calloc(width + 1, sizeof(char));
if ((*chars)[j] == NULL) { if ((*chars)[j] == NULL) {
perror(PROJECT); perror(PROJECT);
for (/*empty*/; j>0; --j) for (/*empty*/; j > 0; --j) BFREE ((*chars)[j - 1]);
BFREE ((*chars)[j-1]);
BFREE (*chars); BFREE (*chars);
return 3; return 3;
} }
@ -164,8 +169,9 @@ void freeshape (sentry_t *shape)
{ {
size_t j; size_t j;
for (j=0; j<shape->height; ++j) for (j = 0; j < shape->height; ++j) {
BFREE (shape->chars[j]); BFREE (shape->chars[j]);
}
BFREE (shape->chars); BFREE (shape->chars);
*shape = SENTRY_INITIALIZER; *shape = SENTRY_INITIALIZER;
@ -180,15 +186,16 @@ int isempty (const sentry_t *shape)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/ */
{ {
if (shape == NULL) if (shape == NULL) {
return 1; return 1;
else if (shape->chars == NULL) } else if (shape->chars == NULL) {
return 1; return 1;
else if (shape->width == 0 || shape->height == 0) } else if (shape->width == 0 || shape->height == 0) {
return 1; return 1;
else } else {
return 0; return 0;
} }
}
@ -202,15 +209,17 @@ int isdeepempty (const sentry_t *shape)
{ {
size_t j; size_t j;
if (isempty (shape)) if (isempty(shape)) {
return 1; return 1;
}
for (j = 0; j < shape->height; ++j) { for (j = 0; j < shape->height; ++j) {
if (shape->chars[j]) { if (shape->chars[j]) {
if (strspn (shape->chars[j], " ") != shape->width) if (strspn(shape->chars[j], " ") != shape->width) {
return 0; return 0;
} }
} }
}
return 1; return 1;
} }
@ -243,10 +252,11 @@ size_t highest (const sentry_t *sarr, const int n, ...)
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
shape_t r = va_arg (ap, shape_t); shape_t r = va_arg (ap, shape_t);
if (!isempty(sarr + r)) { if (!isempty(sarr + r)) {
if (sarr[r].height > max) if (sarr[r].height > max) {
max = sarr[r].height; max = sarr[r].height;
} }
} }
}
va_end (ap); va_end (ap);
@ -281,10 +291,11 @@ size_t widest (const sentry_t *sarr, const int n, ...)
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
shape_t r = va_arg (ap, shape_t); shape_t r = va_arg (ap, shape_t);
if (!isempty(sarr + r)) { if (!isempty(sarr + r)) {
if (sarr[r].width > max) if (sarr[r].width > max) {
max = sarr[r].width; max = sarr[r].width;
} }
} }
}
va_end (ap); va_end (ap);
@ -307,18 +318,21 @@ shape_t leftmost (const int aside, const int cnt)
int c = 0; int c = 0;
int s; int s;
if (cnt < 0) if (cnt < 0) {
return ANZ_SHAPES; return ANZ_SHAPES;
}
if (aside == BTOP) { if (aside == BTOP) {
s = 0; s = 0;
do { do {
++s; ++s;
while (s < SHAPES_PER_SIDE - 1 && while (s < SHAPES_PER_SIDE - 1 &&
isempty(opt.design->shape + north_side[s])) isempty(opt.design->shape + north_side[s])) {
++s; ++s;
if (s == SHAPES_PER_SIDE-1) }
if (s == SHAPES_PER_SIDE - 1) {
return ANZ_SHAPES; return ANZ_SHAPES;
}
} while (c++ < cnt); } while (c++ < cnt);
return north_side[s]; return north_side[s];
} }
@ -327,10 +341,12 @@ shape_t leftmost (const int aside, const int cnt)
s = SHAPES_PER_SIDE - 1; s = SHAPES_PER_SIDE - 1;
do { do {
--s; --s;
while (s && isempty(opt.design->shape + south_side[s])) while (s && isempty(opt.design->shape + south_side[s])) {
--s; --s;
if (!s) }
if (!s) {
return ANZ_SHAPES; return ANZ_SHAPES;
}
} while (c++ < cnt); } while (c++ < cnt);
return south_side[s]; return south_side[s];
} }
@ -357,10 +373,11 @@ int empty_side (sentry_t *sarr, const int aside)
int i; int i;
for (i = 0; i < SHAPES_PER_SIDE; ++i) { for (i = 0; i < SHAPES_PER_SIDE; ++i) {
if (isdeepempty (sarr + sides[aside][i])) if (isdeepempty(sarr + sides[aside][i])) {
continue; continue;
else } else {
return 0; /* side is not empty */ return 0;
} /* side is not empty */
} }
return 1; /* side is empty */ return 1; /* side is empty */

View File

@ -86,22 +86,24 @@ int strisyes (const char *s)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/ */
{ {
if (s == NULL) if (s == NULL) {
return 0; return 0;
}
if (!strncasecmp ("on", s, 3)) if (!strncasecmp("on", s, 3)) {
return 1; return 1;
else if (!strncasecmp ("yes", s, 4)) } else if (!strncasecmp("yes", s, 4)) {
return 1; return 1;
else if (!strncasecmp ("true", s, 5)) } else if (!strncasecmp("true", s, 5)) {
return 1; return 1;
else if (!strncmp ("1", s, 2)) } else if (!strncmp("1", s, 2)) {
return 1; return 1;
else if (!strncasecmp ("t", s, 2)) } else if (!strncasecmp("t", s, 2)) {
return 1; return 1;
else } else {
return 0; return 0;
} }
}
@ -117,22 +119,24 @@ int strisno (const char *s)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/ */
{ {
if (s == NULL) if (s == NULL) {
return 0; return 0;
}
if (!strncasecmp ("off", s, 4)) if (!strncasecmp("off", s, 4)) {
return 1; return 1;
else if (!strncasecmp ("no", s, 3)) } else if (!strncasecmp("no", s, 3)) {
return 1; return 1;
else if (!strncasecmp ("false", s, 6)) } else if (!strncasecmp("false", s, 6)) {
return 1; return 1;
else if (!strncmp ("0", s, 2)) } else if (!strncmp("0", s, 2)) {
return 1; return 1;
else if (!strncasecmp ("f", s, 2)) } else if (!strncasecmp("f", s, 2)) {
return 1; return 1;
else } else {
return 0; return 0;
} }
}
@ -159,8 +163,9 @@ void concat_strings (char *dst, int max_len, int count, ...)
/* /*
* Sanity check. * Sanity check.
*/ */
if (max_len < 1) if (max_len < 1) {
return; return;
}
if (max_len == 1 || count < 1) { if (max_len == 1 || count < 1) {
*dst = '\0'; *dst = '\0';
@ -178,8 +183,9 @@ void concat_strings (char *dst, int max_len, int count, ...)
*/ */
src = va_arg (va, const char *); src = va_arg (va, const char *);
if (src == NULL) if (src == NULL) {
continue; continue;
}
/* /*
* Concatenate 'src' onto 'dst', as long as we have room. * Concatenate 'src' onto 'dst', as long as we have room.
@ -212,15 +218,18 @@ int empty_line (const line_t *line)
char *p; char *p;
size_t j; size_t j;
if (!line) if (!line) {
return 1; return 1;
if (line->text == NULL || line->len <= 0) }
if (line->text == NULL || line->len <= 0) {
return 1; return 1;
}
for (p = line->text, j = 0; *p && j < line->len; ++j, ++p) { for (p = line->text, j = 0; *p && j < line->len; ++j, ++p) {
if (*p != ' ' && *p != '\t' && *p != '\r') if (*p != ' ' && *p != '\t' && *p != '\r') {
return 0; return 0;
} }
}
return 1; return 1;
} }
@ -257,11 +266,13 @@ size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
*text = NULL; *text = NULL;
for (ii = 0, *tabpos_len = 0; ii < in_len; ++ii) { for (ii = 0, *tabpos_len = 0; ii < in_len; ++ii) {
if (input_buffer[ii] == '\t') if (input_buffer[ii] == '\t') {
(*tabpos_len)++; (*tabpos_len)++;
} }
if (opt.tabexp != 'k') }
if (opt.tabexp != 'k') {
*tabpos_len = 0; *tabpos_len = 0;
}
if (*tabpos_len > 0) { if (*tabpos_len > 0) {
*tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t)); *tabpos = (size_t *) calloc((*tabpos_len) + 1, sizeof(size_t));
if (*tabpos == NULL) { if (*tabpos == NULL) {
@ -274,9 +285,10 @@ size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
if (*tabpos_len > 0) { if (*tabpos_len > 0) {
(*tabpos)[tabnum++] = io; (*tabpos)[tabnum++] = io;
} }
for (jp=io+tabstop-(io%tabstop); io<jp; ++io) for (jp = io + tabstop - (io % tabstop); io < jp; ++io) {
temp[io] = ' '; temp[io] = ' ';
} }
}
else { else {
temp[io] = input_buffer[ii]; temp[io] = input_buffer[ii];
++io; ++io;
@ -285,7 +297,9 @@ size_t expand_tabs_into (const char *input_buffer, const size_t in_len,
temp[io] = '\0'; temp[io] = '\0';
*text = (char *) strdup(temp); *text = (char *) strdup(temp);
if (*text == NULL) return 0; if (*text == NULL) {
return 0;
}
return io; return io;
} }
@ -302,7 +316,7 @@ void btrim (char *text, size_t *len)
long idx = (long) (*len) - 1; long idx = (long) (*len) - 1;
while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r' while (idx >= 0 && (text[idx] == '\n' || text[idx] == '\r'
|| text[idx] == '\t' || text[idx] == ' ')) || text[idx] == '\t' || text[idx] == ' ')) /**/
{ {
text[idx--] = '\0'; text[idx--] = '\0';
} }
@ -330,25 +344,30 @@ char *my_strnrstr (const char *s1, const char *s2, const size_t s2_len, int skip
char *p; char *p;
int comp; int comp;
if (!s2 || *s2 == '\0') if (!s2 || *s2 == '\0') {
return (char *) s1; return (char *) s1;
if (!s1 || *s1 == '\0') }
if (!s1 || *s1 == '\0') {
return NULL; return NULL;
if (skip < 0) }
if (skip < 0) {
skip = 0; skip = 0;
}
p = strrchr(s1, s2[0]); p = strrchr(s1, s2[0]);
if (!p) if (!p) {
return NULL; return NULL;
}
while (p >= s1) { while (p >= s1) {
comp = strncmp(p, s2, s2_len); comp = strncmp(p, s2, s2_len);
if (comp == 0) { if (comp == 0) {
if (skip--) if (skip--) {
--p; --p;
else } else {
return p; return p;
} }
}
else { else {
--p; --p;
} }
@ -401,7 +420,7 @@ char *tabbify_indent (const size_t lineno, char *indentspc, const size_t indents
result_len = indentspc_len; result_len = indentspc_len;
for (i = 0; i < input.lines[lineno].tabpos_len for (i = 0; i < input.lines[lineno].tabpos_len
&& input.lines[lineno].tabpos[i] < indentspc_len; ++i) && input.lines[lineno].tabpos[i] < indentspc_len; ++i) /**/
{ {
size_t tpos = input.lines[lineno].tabpos[i]; size_t tpos = input.lines[lineno].tabpos[i];
size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */ size_t nspc = opt.tabstop - (tpos % opt.tabstop); /* no of spcs covered by tab */

View File

@ -38,20 +38,28 @@
int yyerror(const char *fmt, ...); int yyerror(const char *fmt, ...);
void regerror(char *msg); void regerror(char *msg);
int empty_line(const line_t *line); int empty_line(const line_t *line);
size_t expand_tabs_into(const char *input_buffer, const size_t in_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); const int tabstop, char **text, size_t **tabpos, size_t *tabpos_len);
void btrim(char *text, size_t *len); void btrim(char *text, size_t *len);
char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len, char *my_strnrstr(const char *s1, const char *s2, const size_t s2_len,
int skip); int skip);
int strisyes(const char *s); int strisyes(const char *s);
int strisno(const char *s); int strisno(const char *s);
void concat_strings(char *dst, int max_len, int count, ...); 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 #endif
/*EOF*/ /* vim: set cindent sw=4: */ /*EOF*/ /* vim: set cindent sw=4: */