… | |
… | |
287 | |
287 | |
288 | return buf; |
288 | return buf; |
289 | } |
289 | } |
290 | |
290 | |
291 | // expand one level of cfpod |
291 | // expand one level of cfpod |
|
|
292 | // end == -2, till '\n' |
|
|
293 | // end == -1, till '>' |
292 | // end == -1, till eos |
294 | // end == 0, till eos |
293 | // end == 0, one level of '>' |
|
|
294 | // end >= 1, this many levels of " >>>" |
295 | // end >= 1, levels of " >>>" |
295 | static const char * |
296 | static const char * |
296 | expand_cfpod (const player *pl, dynbuf_text &buf, const char *cfpod, int end) |
297 | expand_cfpod (const player *pl, dynbuf_text &buf, const char *cfpod, int end = 0) |
297 | { |
298 | { |
298 | static dynbuf_text nest(128, 128); |
299 | static dynbuf_text nest(128, 128); |
299 | |
300 | |
300 | for (;;) |
301 | for (;;) |
301 | switch (char ch = *cfpod++) |
302 | switch (char ch = *cfpod++) |
302 | { |
303 | { |
303 | case 0: |
304 | case 0: |
304 | return cfpod - 1; |
305 | return cfpod - 1; |
305 | |
306 | |
306 | case ' ': |
307 | case ' ': |
|
|
308 | // verbatim block? |
|
|
309 | if (!buf.size () && !end) |
|
|
310 | { |
|
|
311 | --cfpod; |
|
|
312 | goto verbatim; |
|
|
313 | } |
|
|
314 | |
307 | if (end <= 0) |
315 | if (end < 1) |
308 | goto passthrough; |
316 | goto passthrough; |
309 | |
317 | |
310 | for (int i = 0; i < end; ++i) |
318 | for (int i = 0; i < end; ++i) |
311 | if (cfpod [i] != '>') |
319 | if (cfpod [i] != '>') |
312 | goto passthrough; |
320 | goto passthrough; |
313 | |
321 | |
314 | return cfpod + end; |
322 | return cfpod + end; |
315 | |
323 | |
316 | case '>': |
324 | case '>': |
317 | if (end) |
325 | if (end != -1) |
318 | { |
326 | { |
319 | buf << ">"; |
327 | buf << ">"; |
320 | break; |
328 | break; |
321 | } |
329 | } |
322 | |
330 | |
… | |
… | |
328 | |
336 | |
329 | case '<': |
337 | case '<': |
330 | buf << "<"; |
338 | buf << "<"; |
331 | break; |
339 | break; |
332 | |
340 | |
|
|
341 | case '\r': |
|
|
342 | buf << '\n'; |
|
|
343 | break; |
|
|
344 | |
333 | case '\n': |
345 | case '\n': |
|
|
346 | if (end == -2) |
|
|
347 | return cfpod; |
|
|
348 | |
|
|
349 | // \n\ => newline, tt |
|
|
350 | // \n\n => \n\n |
|
|
351 | // \n => single space |
|
|
352 | |
|
|
353 | // two or more newlines will be copied verbatim, one is replaced by space |
334 | if (*cfpod == ' ') |
354 | if (*cfpod == '\n') |
335 | goto passthrough; |
|
|
336 | else if (*cfpod == '\n') |
|
|
337 | { |
355 | { |
338 | --cfpod; |
356 | --cfpod; |
339 | while (*cfpod == '\n') |
357 | do |
340 | buf << *cfpod++; |
358 | buf << *cfpod++; |
341 | |
359 | while (*cfpod == '\n'); |
342 | goto skip; |
|
|
343 | } |
360 | } |
344 | |
361 | else if (*cfpod != ' ') |
345 | buf << ' '; |
362 | buf << ' '; |
|
|
363 | |
|
|
364 | // verbatim block? |
|
|
365 | if (*cfpod == ' ') |
|
|
366 | goto verbatim; |
|
|
367 | |
346 | break; |
368 | break; |
347 | |
369 | |
348 | case 'B': case 'C': case 'E': case 'G': |
370 | case 'B': case 'C': case 'E': case 'G': |
349 | case 'H': case 'I': case 'T': case 'U': |
371 | case 'H': case 'I': case 'T': case 'U': |
350 | case 'Z': |
372 | case 'Z': |
351 | { |
373 | { |
352 | int len = 0; |
374 | int end = 0; |
353 | |
375 | |
354 | while (cfpod [len] == '<') |
376 | while (cfpod [end] == '<') |
355 | ++len; |
377 | ++end; |
356 | |
378 | |
357 | if (!len) |
379 | if (!end) |
358 | goto passthrough; |
380 | goto passthrough; |
359 | |
381 | |
360 | if (len == 1 || cfpod [len] != ' ') |
382 | if (end == 1 || cfpod [end] != ' ') |
361 | { |
383 | { |
362 | len = 0; |
384 | end = -1; |
363 | ++cfpod; |
385 | ++cfpod; |
364 | } |
386 | } |
365 | else |
387 | else |
366 | cfpod += len + 1; |
388 | cfpod += end + 1; |
367 | |
389 | |
368 | const char *pfx, *sfx; |
390 | const char *pfx, *sfx; |
369 | |
391 | |
370 | switch (ch) |
392 | switch (ch) |
371 | { |
393 | { |
… | |
… | |
378 | |
400 | |
379 | case 'H': // hint |
401 | case 'H': // hint |
380 | { |
402 | { |
381 | if (pl->hintmode) |
403 | if (pl->hintmode) |
382 | { |
404 | { |
383 | cfpod = expand_cfpod (pl, nest, cfpod, len); |
405 | cfpod = expand_cfpod (pl, nest, cfpod, end); |
384 | nest.clear (); |
406 | nest.clear (); |
385 | |
407 | |
386 | if (pl->hintmode == 1) |
408 | if (pl->hintmode == 1) |
387 | buf << "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>"; |
409 | buf << "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>"; |
388 | else |
410 | else |
… | |
… | |
399 | } |
421 | } |
400 | break; |
422 | break; |
401 | |
423 | |
402 | case 'G': // gender |
424 | case 'G': // gender |
403 | { |
425 | { |
404 | expand_cfpod (pl, nest, cfpod, len); |
426 | cfpod = expand_cfpod (pl, nest, cfpod, end); |
405 | nest << '\0'; // zero-terminate |
427 | nest << '\0'; // zero-terminate |
406 | |
428 | |
407 | const char *str = nest.linearise (); |
429 | const char *str = nest.linearise (); |
408 | const char *sep = strchr (str, '|'); |
430 | const char *sep = strchr (str, '|'); |
409 | |
431 | |
410 | if (sep) |
432 | if (sep) |
411 | { |
433 | { |
412 | if (pl->gender || 1) |
434 | if (pl->gender) |
413 | buf.add (sep + 1, nest.size () - (sep - str) - 2); |
435 | buf.add (sep + 1, nest.size () - (sep - str) - 2); |
414 | else |
436 | else |
415 | buf.add (str, sep - str); |
437 | buf.add (str, sep - str); |
416 | } |
438 | } |
417 | |
439 | |
… | |
… | |
420 | goto skip; |
442 | goto skip; |
421 | } |
443 | } |
422 | } |
444 | } |
423 | |
445 | |
424 | buf << pfx; |
446 | buf << pfx; |
425 | cfpod = expand_cfpod (pl, buf, cfpod, len); |
447 | cfpod = expand_cfpod (pl, buf, cfpod, end); |
426 | buf << sfx; |
448 | buf << sfx; |
427 | } |
449 | } |
428 | |
450 | |
429 | break; |
451 | break; |
430 | |
452 | |
431 | default: |
453 | default: |
432 | passthrough: |
454 | passthrough: |
433 | buf << ch; |
455 | buf << ch; |
434 | skip: |
456 | skip: |
435 | break; |
457 | break; |
|
|
458 | |
|
|
459 | verbatim: |
|
|
460 | // elements cannot currently span multiple lines |
|
|
461 | do |
|
|
462 | { |
|
|
463 | buf << "<tt>"; |
|
|
464 | cfpod = expand_cfpod (pl, buf, cfpod, -2); |
|
|
465 | buf << "</tt>\n"; |
|
|
466 | } |
|
|
467 | while (*cfpod == ' '); |
|
|
468 | |
|
|
469 | while (*cfpod == '\n') |
|
|
470 | buf << *cfpod++; |
|
|
471 | |
|
|
472 | break; |
436 | } |
473 | } |
437 | } |
474 | } |
438 | |
475 | |
439 | dynbuf_text * |
476 | dynbuf_text * |
440 | player::expand_cfpod (const char *cfpod) const |
477 | player::expand_cfpod (const char *cfpod) const |
441 | { |
478 | { |
442 | static dynbuf_text buf(1024, 1024); |
479 | static dynbuf_text buf(1024, 1024); |
443 | buf.clear (); |
480 | buf.clear (); |
444 | |
481 | |
445 | ::expand_cfpod (this, buf, cfpod, -1); |
482 | ::expand_cfpod (this, buf, cfpod); |
446 | |
483 | |
447 | return &buf; |
484 | return &buf; |
448 | } |
485 | } |
449 | |
486 | |