Table code

From: James Turner (turnerjh@XTN.NET)
Date: 07/12/98


Here's some code (that uses the buffer and Varray code no less) to
display tables in a friendly way.

--cut here--
struct table {
  Varray *entries;
  int max_columns;
};

typedef struct table Table;

Table *
new_table(int columns)
{
  Table *t;

  t = mud_malloc(sizeof(*t));
  t->entries = v_create(12);
  t->max_columns = columns;

  return t;
}

void
add_table_entry(Table *t, char *val)
{
  v_push(t->entries, (void *)mud_strdup(val));
}

void
send_table(Table *t, CHAR_DATA *ch)
{
  int maxlen = 0;
  int cols, i, clen;
  Buffer *b;

  b = new_buffer(1024);

  clen = v_len(t->entries);

  for (i = 0; i < clen; i++)
    maxlen = MAX(maxlen, mud_strlen((const char *)t->entries->data[i]));

  if (t->max_columns)
    cols = MIN(t->max_columns, 76/(maxlen + 1));
  else
    cols = 76 / (maxlen + 1);

  for (i = 0; i < clen; i++) {
    bprintf(b, "%-*s ", maxlen, t->entries->data[i]);
    if (!((i+1) % cols))
      bprintf(b, "\r\n");
    mud_free(t->entries->data[i]);
  }

  bprintf(b, "\r\n");
  page_string(ch->desc, b->p, 1);
  free_buffer(b);
  v_free(t->entries);
  mud_free(t);
}


--cut here--

--
James Turner                turnerjh@pattern.net         UIN: 1102038
                            http://www.vuse.vanderbilt.edu/~turnerjh/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST