1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
/*********************/
/* reformat.c */
/* for Par 1.50 */
/* Copyright 1996 by */
/* Adam M. Costello */
/*********************/
/* This is ANSI C code. */
#include "reformat.h" /* Makes sure we're consistent with the */
/* prototype. Also includes "errmsg.h". */
#include "buffer.h" /* Also includes <stddef.h>. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#undef NULL
#define NULL ((void *) 0)
#ifdef DONTFREE
#define free(ptr)
#endif
typedef unsigned char wflag_t;
typedef struct word {
const char *chrs; /* Pointer to the characters in the word */
/* (NOT terminated by '\0'). */
struct word *prev, /* Pointer to previous word. */
*next, /* Pointer to next word. */
/* Supposing this word were the first... */
*nextline; /* Pointer to first word in next line. */
int score, /* Value of the objective function. */
length; /* Length of this word. */
wflag_t flags; /* Notable properties of this word. */
} word;
/* The following may be bitwise-OR'd together */
/* to set the flags field of a word: */
static const wflag_t
W_SHIFTED = 1, /* This word should have an extra space before */
/* it unless it's the first word in the line. */
W_CURIOUS = 2, /* This is a curious word (see par.doc). */
W_CAPITAL = 4; /* This is a capitalized word (see par.doc). */
#define isshifted(w) ( (w)->flags & 1)
#define iscurious(w) (((w)->flags & 2) != 0)
#define iscapital(w) (((w)->flags & 4) != 0)
static int checkcapital(word *w)
/* Returns 1 if *w is capitalized according to the definition */
/* in par.doc (assuming <cap> is 0), or 0 if not. */
{
const char *p, *end;
for (p = w->chrs, end = p + w->length; p < end && !isalnum(*p); ++p);
return p < end && !islower(*p);
}
static int checkcurious(word *w)
/* Returns 1 if *w is curious according to */
/* the definition in par.doc, or 0 if not. */
{
const char *start, *p;
char ch;
for (start = w->chrs, p = start + w->length; p > start; --p) {
ch = p[-1];
if (isalnum(ch)) return 0;
if (ch == '.' || ch == '?' || ch == '!' || ch == ':') break;
}
if (p <= start + 1) return 0;
--p;
do if (isalnum(*--p)) return 1;
while (p > start);
return 0;
}
static int simplebreaks(word *head, word *tail, int L, int last)
/* Chooses line breaks in a list of words which maximize the length of the */
/* shortest line. L is the maximum line length. The last line counts as a */
/* line only if last is non-zero. _head must point to a dummy word, and tail */
/* must point to the last word, whose next field must be NULL. Returns the */
/* length of the shortest line on success, -1 if there is a word of length */
/* greater than L, or L if there are no lines. */
{
word *w1, *w2;
int linelen, score;
if (!head->next) return L;
for (w1 = tail, linelen = w1->length;
w1 != head && linelen <= L;
linelen += isshifted(w1), w1 = w1->prev, linelen += 1 + w1->length) {
w1->score = last ? linelen : L;
w1->nextline = NULL;
}
for ( ; w1 != head; w1 = w1->prev) {
w1->score = -1;
for (linelen = w1->length, w2 = w1->next;
linelen <= L;
linelen += 1 + isshifted(w2) + w2->length, w2 = w2->next) {
score = w2->score;
if (linelen < score) score = linelen;
if (score >= w1->score) {
w1->nextline = w2;
w1->score = score;
}
}
}
return head->next->score;
}
static void normalbreaks(
word *head, word *tail, int L, int fit, int last, errmsg_t errmsg
)
/* Chooses line breaks in a list of words according to the policy */
/* in "par.doc" for <just> = 0 (L is <L>, fit is <fit>, and last is */
/* <last>). head must point to a dummy word, and tail must point */
/* to the last word, whose next field must be NULL. */
{
word *w1, *w2;
int tryL, shortest, score, target, linelen, extra, minlen;
*errmsg = '\0';
if (!head->next) return;
target = L;
/* Determine minimum possible difference between */
/* the lengths of the shortest and longest lines: */
if (fit) {
score = L + 1;
for (tryL = L; ; --tryL) {
shortest = simplebreaks(head,tail,tryL,last);
if (shortest < 0) break;
if (tryL - shortest < score) {
target = tryL;
score = target - shortest;
}
}
}
/* Determine maximum possible length of the shortest line: */
shortest = simplebreaks(head,tail,target,last);
if (shortest < 0) {
sprintf(errmsg,impossibility,1);
return;
}
/* Minimize the sum of the squares of the differences */
/* between target and the lengths of the lines: */
w1 = tail;
do {
w1->score = -1;
for (linelen = w1->length, w2 = w1->next;
linelen <= target;
linelen += 1 + isshifted(w2) + w2->length, w2 = w2->next) {
extra = target - linelen;
minlen = shortest;
if (w2)
score = w2->score;
else {
score = 0;
if (!last) extra = minlen = 0;
}
if (linelen >= minlen && score >= 0) {
score += extra * extra;
if (w1->score < 0 || score <= w1->score) {
w1->nextline = w2;
w1->score = score;
}
}
if (!w2) break;
}
w1 = w1->prev;
} while (w1 != head);
if (head->next->score < 0)
sprintf(errmsg,impossibility,2);
}
static void justbreaks(
word *head, word *tail, int L, int last, errmsg_t errmsg
)
/* Chooses line breaks in a list of words according to the */
/* policy in "par.doc" for <just> = 1 (L is <L> and last is */
/* <last>). head must point to a dummy word, and tail must */
/* point to the last word, whose next field must be NULL. */
{
word *w1, *w2;
int numgaps, extra, score, gap, maxgap, numbiggaps;
*errmsg = '\0';
if (!head->next) return;
/* Determine the minimum possible largest inter-word gap: */
w1 = tail;
do {
w1->score = L;
for (numgaps = 0, extra = L - w1->length, w2 = w1->next;
extra >= 0;
++numgaps, extra -= 1 + isshifted(w2) + w2->length, w2 = w2->next) {
gap = numgaps ? (extra + numgaps - 1) / numgaps : L;
if (w2)
score = w2->score;
else {
score = 0;
if (!last) gap = 0;
}
if (gap > score) score = gap;
if (score < w1->score) {
w1->nextline = w2;
w1->score = score;
}
if (!w2) break;
}
w1 = w1->prev;
} while (w1 != head);
maxgap = head->next->score;
if (maxgap >= L) {
strcpy(errmsg, "Cannot justify.\n");
return;
}
/* Minimize the sum of the squares of the numbers */
/* of extra spaces required in each inter-word gap: */
w1 = tail;
do {
w1->score = -1;
for (numgaps = 0, extra = L - w1->length, w2 = w1->next;
extra >= 0;
++numgaps, extra -= 1 + isshifted(w2) + w2->length, w2 = w2->next) {
gap = numgaps ? (extra + numgaps - 1) / numgaps : L;
if (w2)
score = w2->score;
else {
if (!last) {
w1->nextline = NULL;
w1->score = 0;
break;
}
score = 0;
}
if (gap <= maxgap && score >= 0) {
numbiggaps = extra % numgaps;
score += (extra / numgaps) * (extra + numbiggaps) + numbiggaps;
/* The above may not look like the sum of the squares of the numbers */
/* of extra spaces required in each inter-word gap, but trust me, it */
/* is. It's easier to prove graphically than algebraicly. */
if (w1->score < 0 || score <= w1->score) {
w1->nextline = w2;
w1->score = score;
}
}
if (!w2) break;
}
w1 = w1->prev;
} while (w1 != head);
if (head->next->score < 0)
sprintf(errmsg,impossibility,3);
}
char **reformat(
const char * const *inlines, const char * const *endline, int afp, int fs,
int hang, int prefix, int suffix, int width, int cap, int fit, int guess,
int just, int last, int Report, int touch, errmsg_t errmsg
)
{
int numin, affix, L, onfirstword = 1, linelen, numout, numgaps, extra, phase;
const char * const *line, **suffixes = NULL, **suf, *end, *p1, *p2;
char *q1, *q2, **outlines = NULL;
word dummy, *head, *tail, *w1, *w2;
buffer *pbuf = NULL;
/* Initialization: */
*errmsg = '\0';
dummy.next = dummy.prev = NULL;
dummy.flags = 0;
head = tail = &dummy;
numin = endline - inlines;
if (numin <= 0) {
sprintf(errmsg,impossibility,4);
goto rfcleanup;
}
/* Allocate space for pointers to the suffixes: */
suffixes = malloc(numin * sizeof (const char *));
if (!suffixes) {
strcpy(errmsg,outofmem);
goto rfcleanup;
}
/* Set the pointers to the suffixes, and create the words: */
affix = prefix + suffix;
L = width - prefix - suffix;
line = inlines, suf = suffixes;
do {
for (end = *line; *end; ++end);
if (end - *line < affix) {
sprintf(errmsg,
"Line %d shorter than <prefix> + <suffix> = %d + %d = %d\n",
line - inlines + 1, prefix, suffix, affix);
goto rfcleanup;
}
end -= suffix;
*suf = end;
p1 = *line + prefix;
for (;;) {
while (p1 < end && *p1 == ' ') ++p1;
if (p1 == end) break;
p2 = p1;
if (onfirstword) {
p1 = *line + prefix;
onfirstword = 0;
}
while (p2 < end && *p2 != ' ') ++p2;
w1 = malloc(sizeof (word));
if (!w1) {
strcpy(errmsg,outofmem);
goto rfcleanup;
}
w1->next = NULL;
w1->prev = tail;
tail = tail->next = w1;
w1->chrs = p1;
w1->length = p2 - p1;
w1->flags = 0;
p1 = p2;
}
++line, ++suf;
} while (line < endline);
/* If guess is 1, set flag values and merge words: */
if (guess) {
for (w1 = head, w2 = head->next; w2; w1 = w2, w2 = w2->next) {
if (checkcurious(w2)) w2->flags |= W_CURIOUS;
if (cap || checkcapital(w2)) {
w2->flags |= W_CAPITAL;
if (iscurious(w1))
if (w1->chrs[w1->length] && w1->chrs + w1->length + 1 == w2->chrs) {
w2->length += w1->length + 1;
w2->chrs = w1->chrs;
w2->prev = w1->prev;
w2->prev->next = w2;
if (iscapital(w1)) w2->flags |= W_CAPITAL;
else w2->flags &= ~W_CAPITAL;
if (isshifted(w1)) w2->flags |= W_SHIFTED;
else w2->flags &= ~W_SHIFTED;
free(w1);
}
else
w2->flags |= W_SHIFTED;
}
}
tail = w1;
}
/* Check for too-long words: */
if (Report)
for (w2 = head->next; w2; w2 = w2->next) {
if (w2->length > L) {
linelen = w2->length;
if (linelen > errmsg_size - 17)
linelen = errmsg_size - 17;
sprintf(errmsg, "Word too long: %.*s\n", linelen, w2->chrs);
goto rfcleanup;
}
}
else
for (w2 = head->next; w2; w2 = w2->next)
while (w2->length > L) {
w1 = malloc(sizeof (word));
if (!w1) {
strcpy(errmsg,outofmem);
goto rfcleanup;
}
w1->next = w2;
w1->prev = w2->prev;
w1->prev->next = w1;
w2->prev = w1;
w1->chrs = w2->chrs;
w2->chrs += L;
w1->length = L;
w2->length -= L;
w1->flags = 0;
if (iscapital(w2)) {
w1->flags |= W_CAPITAL;
w2->flags &= ~W_CAPITAL;
}
if (isshifted(w2)) {
w1->flags |= W_SHIFTED;
w2->flags &= ~W_SHIFTED;
}
}
/* Choose line breaks according to policy in "par.doc": */
if (just) justbreaks(head,tail,L,last,errmsg);
else normalbreaks(head,tail,L,fit,last,errmsg);
if (*errmsg) goto rfcleanup;
/* Change L to the length of the longest line if required: */
if (!just && touch) {
L = 0;
w1 = head->next;
while (w1) {
for (linelen = w1->length, w2 = w1->next;
w2 != w1->nextline;
linelen += 1 + isshifted(w2) + w2->length, w2 = w2->next);
if (linelen > L) L = linelen;
w1 = w2;
}
}
/* Construct the lines: */
pbuf = newbuffer(sizeof (char *), errmsg);
if (*errmsg) goto rfcleanup;
numout = 0;
w1 = head->next;
while (numout < hang || w1) {
if (w1)
for (w2 = w1->next, numgaps = 0, extra = L - w1->length;
w2 != w1->nextline;
++numgaps, extra -= 1 + isshifted(w2) + w2->length, w2 = w2->next);
linelen = suffix || just && (w2 || last) ?
L + affix :
w1 ? prefix + L - extra : prefix;
q1 = malloc((linelen + 1) * sizeof (char));
if (!q1) {
strcpy(errmsg,outofmem);
goto rfcleanup;
}
additem(pbuf, &q1, errmsg);
if (*errmsg) goto rfcleanup;
++numout;
q2 = q1 + prefix;
if (numout <= numin) memcpy(q1, inlines[numout - 1], prefix);
else if (numin > hang ) memcpy(q1, endline[-1], prefix);
else {
if (afp > prefix) afp = prefix;
memcpy(q1, endline[-1], afp);
q1 += afp;
while (q1 < q2) *q1++ = ' ';
}
q1 = q2;
if (w1) {
phase = numgaps / 2;
for (w2 = w1; ; ) {
memcpy(q1, w2->chrs, w2->length);
q1 += w2->length;
w2 = w2->next;
if (w2 == w1->nextline) break;
*q1++ = ' ';
if (just && (w1->nextline || last)) {
phase += extra;
while (phase >= numgaps) {
*q1++ = ' ';
phase -= numgaps;
}
}
if (isshifted(w2)) *q1++ = ' ';
}
}
q2 += linelen - affix;
while (q1 < q2) *q1++ = ' ';
q2 = q1 + suffix;
if (numout <= numin) memcpy(q1, suffixes[numout - 1], suffix);
else if (numin > hang ) memcpy(q1, suffixes[numin - 1], suffix);
else {
if (fs > suffix) fs = suffix;
memcpy(q1, suffixes[numin - 1], fs);
q1 += fs;
while(q1 < q2) *q1++ = ' ';
}
*q2 = '\0';
if (w1) w1 = w1->nextline;
}
q1 = NULL;
additem(pbuf, &q1, errmsg);
if (*errmsg) goto rfcleanup;
outlines = copyitems(pbuf,errmsg);
rfcleanup:
if (suffixes) free(suffixes);
while (tail != head) {
tail = tail->prev;
free(tail->next);
}
if (pbuf) {
if (!outlines)
for (;;) {
outlines = nextitem(pbuf);
if (!outlines) break;
free(*outlines);
}
freebuffer(pbuf);
}
return outlines;
}
|