Ensure more consistent formatting of tribute passages

Changes to be committed:
	modified:   src/files.c
This commit is contained in:
nhmall
2015-06-13 19:18:01 -04:00
parent e3850d8606
commit 58f3611ec1
+123 -48
View File
@@ -1,4 +1,4 @@
/* NetHack 3.6 files.c $NHDT-Date: 1434202413 2015/06/13 13:33:33 $ $NHDT-Branch: tribute_read $:$NHDT-Revision: 1.176 $ */ /* NetHack 3.6 files.c $NHDT-Date: 1434237479 2015/06/13 23:17:59 $ $NHDT-Branch: tribute_read $:$NHDT-Revision: 1.177 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -3387,7 +3387,9 @@ boolean wildcards;
* *
*/ */
STATIC_OVL char *FDECL(bookformat,(char *,char *,int,int *)); STATIC_DCL boolean FDECL(add_book_content,(char *,BOOLEAN_P));
STATIC_DCL char *FDECL(get_book_content,(char *,int));
STATIC_DCL void NDECL(free_book_content);
#define SECTIONSCOPE 1 #define SECTIONSCOPE 1
#define TITLESCOPE 2 #define TITLESCOPE 2
@@ -3407,7 +3409,7 @@ int tribpassage;
const char *badtranslation = "an incomprehensible foreign translation"; const char *badtranslation = "an incomprehensible foreign translation";
boolean matchedsection = FALSE, matchedtitle = FALSE; boolean matchedsection = FALSE, matchedtitle = FALSE;
winid tribwin = WIN_ERR; winid tribwin = WIN_ERR;
boolean grasped = FALSE; boolean grasped = FALSE, partial_line = FALSE;
/* check for mandatories */ /* check for mandatories */
if (!tribsection || !tribtitle) { if (!tribsection || !tribtitle) {
@@ -3447,8 +3449,12 @@ int tribpassage;
*line = *lastline = '\0'; *line = *lastline = '\0';
while (dlb_fgets(line, sizeof line, fp) != 0) { while (dlb_fgets(line, sizeof line, fp) != 0) {
linect++; linect++;
if ((endp = index(line, '\n')) != 0) if ((endp = index(line, '\n')) != 0) {
*endp = 0; *endp = '\0';
partial_line = FALSE;
} else {
partial_line = TRUE;
}
switch (line[0]) { switch (line[0]) {
case '%': case '%':
if (!strncmpi(&line[1], "section ", sizeof("section ") - 1)) { if (!strncmpi(&line[1], "section ", sizeof("section ") - 1)) {
@@ -3518,23 +3524,21 @@ int tribpassage;
/* comment only, next! */ /* comment only, next! */
break; break;
default: default:
if (matchedtitle && scope == PASSAGESCOPE && tribwin != WIN_ERR) { if (matchedtitle && scope == PASSAGESCOPE && tribwin != WIN_ERR)
if (strlen(line) > COLNO - 5) { add_book_content(line, partial_line);
char *s, partial[BUFSZ];
int cntxt = 0;
while ((s = bookformat(line, partial, COLNO-5, &cntxt)))
putstr(tribwin, 0, s);
} else
putstr(tribwin, 0, line);
Strcpy(lastline, line);
}
} }
} }
cleanup: cleanup:
(void) dlb_fclose(fp); (void) dlb_fclose(fp);
if (tribwin != WIN_ERR) { if (tribwin != WIN_ERR) {
char *s, buf[BUFSZ];
if (matchedtitle && scope == PASSAGESCOPE) { if (matchedtitle && scope == PASSAGESCOPE) {
while ((s = get_book_content(buf, COLNO-5))) {
putstr(tribwin, 0, s);
Strcpy(lastline, s);
}
free_book_content();
display_nhwindow(tribwin, FALSE); display_nhwindow(tribwin, FALSE);
/* put the final attribution line into message history, /* put the final attribution line into message history,
analogous to the summary line from long quest messages */ analogous to the summary line from long quest messages */
@@ -3554,46 +3558,117 @@ cleanup:
return grasped; return grasped;
} }
char * static char *content = 0;
bookformat(line, partial, maxlen, cntxt) static int strt = 0;
char *line, *partial; static int creditpass = 0;
int maxlen, *cntxt;
boolean
add_book_content(line, partial_line)
char *line;
boolean partial_line;
{ {
/* Try to do a little better at where we break the line */ int l;
char *txt = partial; char *new_content = 0;
int start, finish;
int ccnt, k, k2, lastbreak = 0;
if (!line || !partial || !maxlen) if (!content) {
l = strlen(line) + (partial_line ? 1 : 2);
content = (char *)alloc(l);
Strcpy(content, line);
if (!partial_line)
Strcat(content, " ");
strt = 0;
creditpass = 0;
} else {
l = strlen(content) + strlen(line) + (partial_line ? 1 : 2);
new_content = (char *)alloc(l);
if (!new_content) return FALSE;
Strcpy(new_content, content);
Strcat(new_content, line);
if (!partial_line)
Strcat(new_content, " ");
free(content);
content = new_content;
new_content = (char *)0;
l = strlen(content);
}
return TRUE;
}
char *
get_book_content(buf, bufsiz)
char *buf;
int bufsiz;
{
/* Try to do a little better at where we do or don't break the line */
char hold;
int k, this_line;
boolean creditline = FALSE;
static int this_passage = 0;
if (strt == -1 || !buf || !bufsiz)
return (char *)0; return (char *)0;
start = *cntxt; this_line = 0;
ccnt = start; k2 = 0; for (k = strt; content[k] && (content[k] != '[') &&
if (!line[ccnt]) this_line < (bufsiz - 2); ++k) {
return (char *)0; this_passage++;
this_line++;
while (line[ccnt] && k2 < maxlen) {
if (line[ccnt] == ' ')
lastbreak = ccnt;
k2++;
ccnt++;
} }
finish = ccnt; if (this_line >= (bufsiz - 2)) {
while(content[k] != ' ' &&
if (!line[ccnt]) { content[k] != '.' &&
if (k2 <= maxlen) content[k] != '?' &&
finish = ccnt; content[k] != '!')
} else --k;
finish = lastbreak; if (content[k] != ' ') k++;
hold = content[k];
k2 = 0; content[k] = '\0';
for (k = start; k < finish; ++k) { Strcpy(buf, &content[strt]);
partial[k2++] = line[k]; content[k] = hold;
if (content[k] == ' ') k++;
strt = k;
return buf;
} }
partial[k2] = '\0'; if (!content[k]) {
*cntxt = finish; /* found the trailing null */
Strcpy(buf, &content[strt]);
strt = -1;
return buf;
}
if (content[k] == '[') {
if (this_line > 0) {
/* There's some stuff to pass back */
hold = content[k];
content[k] = '\0';
Strcpy(buf, &content[strt]);
content[k] = hold;
strt = k;
return buf;
} else {
if (creditpass == 0) {
Strcpy(buf, "");
creditpass++;
return buf;
} else {
if (content[strt-1] == ' ') strt--;
if (content[strt-1] == ' ') strt--;
Strcpy(buf, &content[strt]);
strt = -1;
return buf;
}
}
}
strt = -1;
return (char *)0;
}
return partial; void
free_book_content()
{
if (content)
free(content);
content = (char *)0;
strt = -1;
} }
/* ---------- END TRIBUTE ----------- */ /* ---------- END TRIBUTE ----------- */