/* $Id: text-table.cc,v 1.14 1997/04/13 04:26:42 dps Exp $ */ /* Ascii table layout */ #ifdef HAVE_CONFIG_H #include "config.h" #endif /* HAVE_CONFIG_H */ #ifdef __GNUC__ #define alloca __builtin_alloca #else #if HAVE_ALLOCA_H #include #else /* Do not have alloca.h */ #ifdef _AIX #pragma alloca #else /* not _AIX */ extern "C" char *alloca(int); #endif /* _AIX */ #endif /* HAVE_ALLOCA_H */ #endif /* __GNUC__ */ #include #include #include #ifdef HAVE_STRING_H #include #endif /* HAVE_STRING_H */ #ifdef HAVE_STRINGS_H #include #endif /* HAVE_STRINGS_H */ #ifdef HAVE_CTYPE_H #include #endif /* HAVE_CTYPE_H */ #include "tblock.h" #include "text-table.h" #include "lib.h" struct rdata { struct wd_info w; const char *data; }; /* Print out a number of spaces */ static inline void p_sp(int n, FILE *out) { int i; for (i=0; i=0) { n=scan_num(s); if (n.dot_pos!=-1) { p_sp(cols[i].w.dp_col-n.dot_pos, out); fwrite((void *) s, sizeof(char), t-s, out); p_sp(nsp-cols[i].w.dp_col+n.dot_pos, out); al=ALIGN_DP; } } switch(al) { case ALIGN_DP: break; case ALIGN_LEFT: fwrite((void *) s, sizeof(char), t-s, out); p_sp(nsp, out); break; case ALIGN_CENTER: p_sp(nsp/2, out); fwrite((void *) s, sizeof(char), t-s, out); p_sp(nsp-(nsp/2), out); // Avoid rounding related problems break; case ALIGN_RIGHT: p_sp(nsp, out); fwrite((void *) s, sizeof(char), t-s, out); break; default: fprintf(stderr,"basic_print_row: Invalid alignment\n"); break; } } fputc(sep, out); // Seperate columns by a space /* Update lptr[i] */ if (*t=='\n') t++; // Advance past newlines; lptr[i]=t; } fputc('\n', out); // Print out a newline } } /* Split the elements of a row */ static void print_row(int ncols, const struct rdata *dp, FILE *out) { static const char null_string[]={'\0'}; struct rdata *rd; tblock *d; char *s, *t; int i; /* Allocate structures */ if ((rd=(struct rdata *) alloca(ncols*sizeof(rdata)))==NULL) { fprintf(stderr, "print_row: fatal alloca failure\n"); exit(1); } /* Fold lines */ for (i=0; imtw) { maxw=0; j=-1; for (i=0; inext) d[i].w=find_width(rows, col->data); shrink_widths(cols, d, wd); for (i=0; inext) d[j].data=(col->data)[i]; print_row(cols, d, out); } return NULL; } /* Set */ int text_table::set(int c, int r, const char *s) { struct col_info *col; int i; if (c<0 || c>=cols || r<0 || r>=rows) { cerr<<"Invalid request to set "<next) ; if (col!=NULL) { if (col->data[r]!=NULL) free((void *) col->data[r]); col->data[r]=strdup(s); } return 1; } /* Constructor */ text_table::text_table(int c, int r) { int i, j; struct col_info *col, **nptr; cols=c; rows=r; cdata=NULL; // Hardenning against cols=0 for (nptr=&cdata, i=0; inext=NULL; nptr=&(col->next); if ((col->data= (const char **) malloc(rows*(sizeof(const char *))))==NULL) { cerr<<"text_table::constructor: malloc failure (fatal)\n"; exit(1); } for (j=0; jdata)[j]=NULL; } } /* Destructor */ text_table::~text_table() { int i; struct col_info *col, *nxt; for (col=cdata; col!=NULL;) { for (i=0; idata[i]==NULL) free((void *) col->data[i]); nxt=col->next; free(col); col=nxt; } }