From cbe58e1e595938c00ea8e6ec1379e2fb999eb404 Mon Sep 17 00:00:00 2001 From: Rick L Bird Date: Thu, 5 May 2011 17:24:23 -0400 Subject: [PATCH] PennMUSH 1.8.3p11 Author: captdeaf@gmail.com Date: Fri Dec 11 07:47:38 2009 +0000 Made table() support <, > and - as alignment options for field width. Fixes #136 --- src/funlist.c | 70 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 11 deletions(-) diff --git a/src/funlist.c b/src/funlist.c index cbc7340..3df4e27 100644 --- a/src/funlist.c +++ b/src/funlist.c @@ -2673,12 +2673,14 @@ FUNCTION(fun_table) size_t col = 0; size_t offset, col_len; char sep, osep, *cp, *t; + char aligntype = '<'; + char *fwidth; ansi_string *as; if (!delim_check(buff, bp, nargs, args, 5, &osep)) return; if ((nargs == 5) && !*args[4]) - osep = 0; + osep = '\0'; if (!delim_check(buff, bp, nargs, args, 4, &sep)) return; @@ -2693,11 +2695,17 @@ FUNCTION(fun_table) line_length = 2; } if (nargs > 1) { - if (!is_integer(args[1])) { + fwidth = args[1]; + if ((*fwidth) == '<' || + (*fwidth) == '>' || + (*fwidth) == '-') { + aligntype = *(fwidth++); + } + if (!is_integer(fwidth)) { safe_str(T(e_ints), buff, bp); return; } - field_width = parse_integer(args[1]); + field_width = parse_integer(fwidth); if (field_width < 1) field_width = 1; if (field_width >= BUFFER_LEN) @@ -2724,14 +2732,33 @@ FUNCTION(fun_table) col_len = strlen(t); if (col_len > field_width) col_len = field_width; - safe_ansi_string(as, offset, col_len, buff, bp); - if (safe_fill(' ', field_width - col_len, buff, bp)) { - free_ansi_string(as); - return; + switch (aligntype) { + case '<': + safe_ansi_string(as, offset, col_len, buff, bp); + if (safe_fill(' ', field_width - col_len, buff, bp)) { + free_ansi_string(as); + return; + } + break; + case '>': + safe_fill(' ', field_width - col_len, buff, bp); + if (safe_ansi_string(as, offset, col_len, buff, bp)) { + free_ansi_string(as); + return; + } + break; + case '-': + safe_fill(' ', (field_width - col_len) / 2, buff, bp); + safe_ansi_string(as, offset, col_len, buff, bp); + if (safe_fill(' ', (field_width - col_len + 1) / 2, buff, bp)) { + free_ansi_string(as); + return; + } + break; } - col = field_width + !!osep; + col = field_width + (osep != '\0'); while (cp) { - col += field_width + !!osep; + col += field_width + (osep != '\0'); if (col > line_length) { if (NEWLINE_ONE_CHAR) safe_str("\n", buff, bp); @@ -2749,9 +2776,30 @@ FUNCTION(fun_table) col_len = strlen(t); if (col_len > field_width) col_len = field_width; - safe_ansi_string(as, offset, col_len, buff, bp); - if (safe_fill(' ', field_width - col_len, buff, bp)) + switch (aligntype) { + case '<': + safe_ansi_string(as, offset, col_len, buff, bp); + if (safe_fill(' ', field_width - col_len, buff, bp)) { + free_ansi_string(as); + return; + } + break; + case '>': + safe_fill(' ', field_width - col_len, buff, bp); + if (safe_ansi_string(as, offset, col_len, buff, bp)) { + free_ansi_string(as); + return; + } + break; + case '-': + safe_fill(' ', (field_width - col_len) / 2, buff, bp); + safe_ansi_string(as, offset, col_len, buff, bp); + if (safe_fill(' ', (field_width - col_len + 1) / 2, buff, bp)) { + free_ansi_string(as); + return; + } break; + } } free_ansi_string(as); } -- 2.30.2