mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-06-14 13:56:41 +02:00
Bugfix: Horizontal box parts were not correctly detected if the west box
side was empty (reported: Tobias Buchal) Bugfix: boxes could hang in detect_horiz() due to goeast/west confusion
This commit is contained in:
parent
532bd941b3
commit
3f2f687128
120
src/remove.c
120
src/remove.c
@ -4,7 +4,7 @@
|
|||||||
* Date created: June 23, 1999 (Wednesday, 20:59h)
|
* Date created: June 23, 1999 (Wednesday, 20:59h)
|
||||||
* Author: Copyright (C) 1999 Thomas Jensen
|
* Author: Copyright (C) 1999 Thomas Jensen
|
||||||
* tsjensen@stud.informatik.uni-erlangen.de
|
* tsjensen@stud.informatik.uni-erlangen.de
|
||||||
* Version: $Id: remove.c,v 1.3 1999/07/20 18:57:16 tsjensen Exp tsjensen $
|
* Version: $Id: remove.c,v 1.4 1999/08/21 16:03:31 tsjensen Exp tsjensen $
|
||||||
* Language: ANSI C
|
* Language: ANSI C
|
||||||
* World Wide Web: http://home.pages.de/~jensen/boxes/
|
* World Wide Web: http://home.pages.de/~jensen/boxes/
|
||||||
* Purpose: Box removal, i.e. the deletion of boxes
|
* Purpose: Box removal, i.e. the deletion of boxes
|
||||||
@ -25,6 +25,9 @@
|
|||||||
* Revision History:
|
* Revision History:
|
||||||
*
|
*
|
||||||
* $Log: remove.c,v $
|
* $Log: remove.c,v $
|
||||||
|
* Revision 1.4 1999/08/21 16:03:31 tsjensen
|
||||||
|
* Bugfix: When matching vertical side shape lines, ignore empty shape lines
|
||||||
|
*
|
||||||
* Revision 1.3 1999/07/20 18:57:16 tsjensen
|
* Revision 1.3 1999/07/20 18:57:16 tsjensen
|
||||||
* Added GNU GPL disclaimer
|
* Added GNU GPL disclaimer
|
||||||
* Does not kill leading/trailing blank lines anymore when !opt.killblank
|
* Does not kill leading/trailing blank lines anymore when !opt.killblank
|
||||||
@ -50,7 +53,7 @@
|
|||||||
#include "remove.h"
|
#include "remove.h"
|
||||||
|
|
||||||
static const char rcsid_remove_c[] =
|
static const char rcsid_remove_c[] =
|
||||||
"$Id: remove.c,v 1.3 1999/07/20 18:57:16 tsjensen Exp tsjensen $";
|
"$Id: remove.c,v 1.4 1999/08/21 16:03:31 tsjensen Exp tsjensen $";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -98,7 +101,7 @@ static int best_match (const line_t *line,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find match for WEST side
|
* Find match for WEST side (TODO: nicht wenn BLEF leer)
|
||||||
*/
|
*/
|
||||||
quality = 0;
|
quality = 0;
|
||||||
cs = opt.design->shape + WNW;
|
cs = opt.design->shape + WNW;
|
||||||
@ -315,7 +318,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 of the box
|
* Detect which part of the input belongs to the top/bottom of the box
|
||||||
*
|
*
|
||||||
* aside part of box to detect (BTOP or BBOT)
|
* aside part of box to detect (BTOP or BBOT)
|
||||||
* hstart index of first line of detected box part (result)
|
* hstart index of first line of detected box part (result)
|
||||||
@ -324,9 +327,10 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
* We assume the horizontal parts of the box to be in one piece, i.e. no
|
* We assume the horizontal parts of the box to be in one piece, i.e. no
|
||||||
* blank lines inserted. Lines may be missing, though. Lines may not be
|
* blank lines inserted. Lines may be missing, though. Lines may not be
|
||||||
* duplicated. They may be shifted left and right by inserting whitespace,
|
* duplicated. They may be shifted left and right by inserting whitespace,
|
||||||
* but whitespace which is part of the box must not have been deleted.
|
* but whitespace which is part of the box must not have been deleted
|
||||||
* Unfortunately, they may even differ in length as long as each line is
|
* (unless it's because an entire box side is empty). Box part lines may
|
||||||
* in itself a valid horizontal box line.
|
* even differ in length as long as each line is in itself a valid
|
||||||
|
* horizontal box line.
|
||||||
*
|
*
|
||||||
* RETURNS: == 0 success (hstart & hend are set)
|
* RETURNS: == 0 success (hstart & hend are set)
|
||||||
* != 0 error
|
* != 0 error
|
||||||
@ -337,18 +341,20 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
size_t follow; /* possible box line */
|
size_t follow; /* possible box line */
|
||||||
sentry_t *cs; /* current shape */
|
sentry_t *cs; /* current shape */
|
||||||
line_t *line; /* currently processed input line */
|
line_t *line; /* currently processed input line */
|
||||||
size_t lcnt; /* index of currently proc.inp.line */
|
size_t lcnt; /* line counter */
|
||||||
char *p; /* middle line part scanner */
|
char *p = NULL; /* middle line part scanner */
|
||||||
char *q; /* space check rover */
|
char *q; /* space check rover */
|
||||||
char *wcs = NULL; /* west corner shape position */
|
char *wcs = NULL; /* west corner shape position */
|
||||||
char *ecs = NULL; /* east corner shape position */
|
char *ecs = NULL; /* east corner shape position */
|
||||||
char *ecs_save; /* temp copy of ecs */
|
|
||||||
int mmok = 0; /* true if middle match was ok */
|
int mmok = 0; /* true if middle match was ok */
|
||||||
size_t mheight; /* regular height of box part */
|
size_t mheight; /* regular height of box part */
|
||||||
int result_init = 0; /* true if hstart was set */
|
int result_init = 0; /* true if hstart etc. was init. */
|
||||||
int goeast, gowest;
|
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;
|
*hstart = *hend = 0;
|
||||||
|
nowside = empty_side (opt.design->shape, BLEF);
|
||||||
|
|
||||||
mheight = opt.design->shape[sides[aside][0]].height;
|
mheight = opt.design->shape[sides[aside][0]].height;
|
||||||
if (aside == BTOP) {
|
if (aside == BTOP) {
|
||||||
@ -375,12 +381,27 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
/*
|
/*
|
||||||
* Look for west corner shape
|
* Look for west corner shape
|
||||||
*/
|
*/
|
||||||
|
if (!goeast) {
|
||||||
|
if (nowside) {
|
||||||
|
wcs = NULL;
|
||||||
|
if (gowest) {
|
||||||
|
gowest = 0;
|
||||||
|
if (*p == ' ' || *p == '\t')
|
||||||
|
++p;
|
||||||
|
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) {
|
if (gowest) {
|
||||||
wcs = strstr (wcs+1, cs->chars[follow]);
|
|
||||||
gowest = 0;
|
gowest = 0;
|
||||||
|
wcs = strstr (wcs+1, cs->chars[follow]);
|
||||||
}
|
}
|
||||||
else if (!wcs) {
|
else {
|
||||||
wcs = strstr (line->text, cs->chars[follow]);
|
wcs = strstr (line->text, cs->chars[follow]);
|
||||||
}
|
}
|
||||||
if (wcs) {
|
if (wcs) {
|
||||||
@ -391,22 +412,27 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
if (q >= line->text)
|
if (q >= line->text)
|
||||||
wcs = NULL;
|
wcs = NULL;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
if (!wcs)
|
||||||
if (wcs)
|
break;
|
||||||
fprintf (stderr, "West corner shape matched at position %d.\n",
|
|
||||||
wcs - line->text);
|
|
||||||
else
|
|
||||||
fprintf (stderr, "West corner shape not found.\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
p = wcs + cs->width;
|
p = wcs + cs->width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* 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
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Look for east corner shape
|
* Look for east corner shape
|
||||||
*/
|
*/
|
||||||
if (wcs) {
|
|
||||||
cs = opt.design->shape + sides[aside][aside==BTOP?SHAPES_PER_SIDE-1:0];
|
cs = opt.design->shape + sides[aside][aside==BTOP?SHAPES_PER_SIDE-1:0];
|
||||||
ecs_save = ecs;
|
|
||||||
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) {
|
||||||
@ -417,38 +443,36 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
ecs = NULL;
|
ecs = NULL;
|
||||||
}
|
}
|
||||||
if (!ecs) {
|
if (!ecs) {
|
||||||
gowest = 1;
|
if (goeast == 0)
|
||||||
|
break;
|
||||||
|
else {
|
||||||
goeast = 0;
|
goeast = 0;
|
||||||
ecs = ecs_save;
|
gowest = 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (ecs)
|
|
||||||
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);
|
||||||
else
|
|
||||||
fprintf (stderr, "East corner shape not found.\n");
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if text between corner shapes is valid
|
* Check if text between corner shapes is valid
|
||||||
*/
|
*/
|
||||||
if (wcs && ecs) {
|
|
||||||
mmok = !hmm (aside, follow, p, ecs, 0);
|
mmok = !hmm (aside, follow, p, ecs, 0);
|
||||||
#ifdef DEBUG
|
|
||||||
fprintf (stderr, "Text between corner shapes is%s valid.\n",
|
|
||||||
mmok? "": " NOT");
|
|
||||||
#endif
|
|
||||||
if (!mmok)
|
if (!mmok)
|
||||||
++goeast;
|
++goeast;
|
||||||
}
|
#ifdef DEBUG
|
||||||
|
fprintf (stderr, "Text between corner shapes is %s.\n",
|
||||||
|
mmok? "VALID": "NOT valid");
|
||||||
|
#endif
|
||||||
|
|
||||||
} while (!mmok && wcs);
|
} while (!mmok);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Proceed to next line
|
* Proceed to next line
|
||||||
*/
|
*/
|
||||||
if (wcs && ecs && 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)
|
||||||
@ -465,8 +489,11 @@ static int detect_horiz (const int aside, size_t *hstart, size_t *hend)
|
|||||||
if (result_init)
|
if (result_init)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
wcs = NULL;
|
wcs = NULL;
|
||||||
ecs = NULL;
|
ecs = NULL;
|
||||||
|
p = NULL;
|
||||||
|
mmok = 0;
|
||||||
|
|
||||||
if (aside == BTOP) {
|
if (aside == BTOP) {
|
||||||
++follow;
|
++follow;
|
||||||
@ -826,16 +853,32 @@ int remove_box()
|
|||||||
*/
|
*/
|
||||||
boxstart = 0;
|
boxstart = 0;
|
||||||
textstart = 0;
|
textstart = 0;
|
||||||
|
if (empty_side (opt.design->shape, BTOP)) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
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
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "----> First line of box is %d, ", boxstart);
|
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 body (text) is %d.\n", textstart);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Phase 2: Find out how many lines belong to the bottom of the 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;
|
||||||
|
boxend = input.anz_lines;
|
||||||
|
#ifdef DEBUG
|
||||||
|
fprintf (stderr, "----> Bottom box side is empty: boxend == textend == %d.\n",
|
||||||
|
input.anz_lines);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
textend = 0;
|
textend = 0;
|
||||||
boxend = 0;
|
boxend = 0;
|
||||||
detect_horiz (BBOT, &textend, &boxend);
|
detect_horiz (BBOT, &textend, &boxend);
|
||||||
@ -847,6 +890,7 @@ int remove_box()
|
|||||||
fprintf (stderr, "----> Last line of box body (text) is %d, ", textend-1);
|
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 is %d.\n", boxend-1);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Phase 3: Iterate over body lines, removing box sides where applicable
|
* Phase 3: Iterate over body lines, removing box sides where applicable
|
||||||
@ -865,12 +909,12 @@ int remove_box()
|
|||||||
}
|
}
|
||||||
else if (m == 0) {
|
else if (m == 0) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "line %2d: no side match\n", j);
|
fprintf (stderr, "\033[00;33;01mline %2d: no side match\033[00m\n", j);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "\033[00;33mline %2d: west: %d (\'%c\') to "
|
fprintf (stderr, "\033[00;33;01mline %2d: west: %d (\'%c\') to "
|
||||||
"%d (\'%c\') [len %d]; east: %d (\'%c\') to %d (\'%c\')"
|
"%d (\'%c\') [len %d]; east: %d (\'%c\') to %d (\'%c\')"
|
||||||
" [len %d]\033[00m\n", j,
|
" [len %d]\033[00m\n", j,
|
||||||
ws? ws-input.lines[j].text:0, ws?ws[0]:'?',
|
ws? ws-input.lines[j].text:0, ws?ws[0]:'?',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user