/* Fold lines */
for (i=0; i<ncols; i++)
{
rd[i].w=dp[i].w;
if (dp[i].data==NULL)
{
rd[i].data=null_string; // Avoid complication of null in
// basic_print_row
continue; // Move on to next item
}
d=word_wrap(dp[i].data, "\n", "\n", dp[i].w.width, 0);
if ((rd[i].data=strdup(*d))==NULL)
{
fprintf(stderr, "text_table::print_row: fatal alloca failure\n");
exit(1);
}
else
{
/* Filter out CH_SUSPECT */
for(s=t=(char *) rd[i].data; *s!='\0'; s++)
{
if (*s==CH_SUSPECT)
continue;
*(t++)=*s;
}
*t='\0';
}
delete(d);
}
basic_print_row(ncols, rd, ' ', out); // Printing the rows is actually
// quite complex.
for (i=0; i<ncols; i++)
{
if (rd[i].data!=null_string)
free((void *) rd[i].data); // Free data
}
}
/* O(n^2) table width reduction algorithm, n is small in almost all cases */
static void shrink_widths(int ncols, struct rdata *cols, int mtw)
{
int i, j, tw, maxw;
for (tw=0, i=0; i<ncols; i++)
tw+=cols[i].w.width;
mtw-=ncols; // Take account of column seperators
/* Simply reduce the maximum width column width by one until
enougn has been trimed */
while (tw>mtw)
{
maxw=0; j=-1;
for (i=0; i<ncols; i++)
{
if (maxw<cols[i].w.width && cols[i].w.align!=ALIGN_DP)
{
j=i;
maxw=cols[i].w.width;
}
}
if (j==-1)
{
fprintf(stderr, "Can not shrink overwidth table\n");
continue;
}
cols[j].w.width--;
tw--;
}
}
/* Returns NULL or text message */
const char *text_table::print_table(int wd, FILE *out)
{
int i,j;
struct rdata *d;
const struct col_info *col;
if ((d=(struct rdata *) alloca(cols*sizeof(struct rdata)))==NULL)
{
cerr<<"text_table::print_table alloca failute (fatal)\n";
return "[Table omitted due to lack of memory]";
}
if (cols==0 || rows==0)
{
fputs("[empty tabel ignored]\n", out);
return "[Ignored empty table]";
}