diff --git a/src/files.c b/src/files.c index 1d0211111..0748bf94d 100644 --- a/src/files.c +++ b/src/files.c @@ -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. */ /* 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 TITLESCOPE 2 @@ -3407,7 +3409,7 @@ int tribpassage; const char *badtranslation = "an incomprehensible foreign translation"; boolean matchedsection = FALSE, matchedtitle = FALSE; winid tribwin = WIN_ERR; - boolean grasped = FALSE; + boolean grasped = FALSE, partial_line = FALSE; /* check for mandatories */ if (!tribsection || !tribtitle) { @@ -3447,8 +3449,12 @@ int tribpassage; *line = *lastline = '\0'; while (dlb_fgets(line, sizeof line, fp) != 0) { linect++; - if ((endp = index(line, '\n')) != 0) - *endp = 0; + if ((endp = index(line, '\n')) != 0) { + *endp = '\0'; + partial_line = FALSE; + } else { + partial_line = TRUE; + } switch (line[0]) { case '%': if (!strncmpi(&line[1], "section ", sizeof("section ") - 1)) { @@ -3518,23 +3524,21 @@ int tribpassage; /* comment only, next! */ break; default: - if (matchedtitle && scope == PASSAGESCOPE && tribwin != WIN_ERR) { - if (strlen(line) > COLNO - 5) { - 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); - } + if (matchedtitle && scope == PASSAGESCOPE && tribwin != WIN_ERR) + add_book_content(line, partial_line); } } cleanup: (void) dlb_fclose(fp); if (tribwin != WIN_ERR) { + char *s, buf[BUFSZ]; 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); /* put the final attribution line into message history, analogous to the summary line from long quest messages */ @@ -3554,46 +3558,117 @@ cleanup: return grasped; } -char * -bookformat(line, partial, maxlen, cntxt) -char *line, *partial; -int maxlen, *cntxt; +static char *content = 0; +static int strt = 0; +static int creditpass = 0; + +boolean +add_book_content(line, partial_line) +char *line; +boolean partial_line; { - /* Try to do a little better at where we break the line */ - char *txt = partial; - int start, finish; - int ccnt, k, k2, lastbreak = 0; + int l; + char *new_content = 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; - start = *cntxt; - ccnt = start; k2 = 0; - if (!line[ccnt]) - return (char *)0; - - while (line[ccnt] && k2 < maxlen) { - if (line[ccnt] == ' ') - lastbreak = ccnt; - k2++; - ccnt++; + this_line = 0; + for (k = strt; content[k] && (content[k] != '[') && + this_line < (bufsiz - 2); ++k) { + this_passage++; + this_line++; } - finish = ccnt; - - if (!line[ccnt]) { - if (k2 <= maxlen) - finish = ccnt; - } else - finish = lastbreak; - - k2 = 0; - for (k = start; k < finish; ++k) { - partial[k2++] = line[k]; + if (this_line >= (bufsiz - 2)) { + while(content[k] != ' ' && + content[k] != '.' && + content[k] != '?' && + content[k] != '!') + --k; + if (content[k] != ' ') k++; + hold = content[k]; + content[k] = '\0'; + Strcpy(buf, &content[strt]); + content[k] = hold; + if (content[k] == ' ') k++; + strt = k; + return buf; } - partial[k2] = '\0'; - *cntxt = finish; + if (!content[k]) { + /* 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 ----------- */