MCPcopy Create free account
hub / github.com/NetHack/NetHack / append_text_buffer

Function append_text_buffer

win/X11/wintext.c:358–412  ·  view source on GitHub ↗

Append a line to the text buffer. */

Source from the content-addressed store, hash-verified

356
357/* Append a line to the text buffer. */
358void
359append_text_buffer(struct text_buffer *tb, const char *str, boolean concat)
360{
361 char *copy;
362 int length;
363
364 if (!tb->text)
365 panic("append_text_buffer: null text buffer");
366
367 if (str) {
368 length = strlen(str);
369 } else {
370 length = 0;
371 }
372
373 if (length + tb->text_last + 1 >= tb->text_size) {
374/* we need to go to a bigger buffer! */
375#ifdef VERBOSE
376 printf(
377 "append_text_buffer: text buffer growing from %d to %d bytes\n",
378 tb->text_size, 2 * tb->text_size);
379#endif
380 copy = (char *) alloc((unsigned) tb->text_size * 2);
381 (void) memcpy(copy, tb->text, tb->text_last);
382 free(tb->text);
383 tb->text = copy;
384 tb->text_size *= 2;
385 }
386
387 if (tb->num_lines) { /* not first --- append a newline */
388 char appchar = '\n';
389
390 if (concat && !strchr("!.?'\")", tb->text[tb->text_last - 1])) {
391 appchar = ' ';
392 tb->num_lines--; /* offset increment at end of function */
393 }
394
395 *(tb->text + tb->text_last) = appchar;
396 tb->text_last++;
397 }
398
399 if (str) {
400 (void) memcpy((tb->text + tb->text_last), str, length + 1);
401 if (length) {
402 /* Remove all newlines. Otherwise we have a confused line count. */
403 copy = (tb->text + tb->text_last);
404 while ((copy = strchr(copy, '\n')) != (char *) 0)
405 *copy = ' ';
406 }
407
408 tb->text_last += length;
409 }
410 tb->text[tb->text_last] = '\0';
411 tb->num_lines++;
412}
413
414/* Initialize text buffer. */
415void

Callers 2

adjust_status_fancyFunction · 0.85
add_to_text_windowFunction · 0.85

Calls 3

printfFunction · 0.85
panicFunction · 0.50
allocFunction · 0.50

Tested by

no test coverage detected