-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.c
72 lines (63 loc) · 2.2 KB
/
show.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "show.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char b[2048];
static void* xmalloc(size_t s) {
char *r = malloc(s);
if(!r) { fprintf(stderr, "error: malloc failed\n"); exit(1); }
return r;
}
static char* xstrdup(char *s) {
char *r = strdup(s);
if(!r) { fprintf(stderr, "error: strdup failed\n"); exit(1); }
return r;
}
/* n: number of columns
r: number of rows
c: column names
t: column types
v: column data
d: deleted rows
: returns string representation of table
caller should free result */
char* show(int n,int r,char **c,int *t,void **v,int *d) {
int i,j,k,p=4;
char *a;
col *cc = xmalloc(n*sizeof(col));
/* string representation of column data */
for(i=0;i<n;i++) {
cc[i] = (col){c[i],t[i],v[i],xmalloc(r*sizeof(char*)),0};
switch(t[i]) {
case 1: for(j=0;j<r;j++) { if(d&&d[j]) continue; sprintf(b,"%d",((int*)cc[i].v)[j]); cc[i].o[j]=xstrdup(b); } break;
case 2: for(j=0;j<r;j++) { if(d&&d[j]) continue; sprintf(b,"%0.*g",15,((double*)cc[i].v)[j]); cc[i].o[j]=xstrdup(b); } break;
case 3: for(j=0;j<r;j++) { if(d&&d[j]) continue; sprintf(b,"%c",((char*)cc[i].v)[j]); cc[i].o[j]=xstrdup(b); } break;
case 4: for(j=0;j<r;j++) { if(d&&d[j]) continue; cc[i].o[j]=xstrdup(((char**)cc[i].v)[j]); } break;
default: fprintf(stderr,"error: invalid column type [%d]\n", t[i]); return 0;
}
}
/* lengths */
for(i=0;i<n;i++) {
cc[i].m=strlen(cc[i].c);
for(j=0;j<r;j++) { if(d&&d[j]) continue; k=strlen(cc[i].o[j]); if(cc[i].m<k) cc[i].m=k; }
p+=(r+2)*(cc[i].m+n+1);
}
/* build result */
a=xmalloc(p);
k=0;
for(i=0;i<n;i++) k+=sprintf(a+k,"%-*s%s",cc[i].m,cc[i].c,i==n-1?"":" ");
k+=sprintf(a+k,"\n");
for(i=0;i<n;i++) { for(j=0;j<cc[i].m;j++) k+=sprintf(a+k,"-"); if(i!=n-1) k+=sprintf(a+k," "); }
k+=sprintf(a+k,"\n");
for(i=0;i<r;i++) {
if(d&&d[i]) continue;
for(j=0;j<n;j++) {
if(cc[j].t==4) k+=sprintf(a+k,"%-*s%s",cc[j].m,cc[j].o[i],j==n-1?"":" ");
else k+=sprintf(a+k,"%*s%s",cc[j].m,cc[j].o[i],j==n-1?"":" ");
}
k+=sprintf(a+k,"\n");
}
for(i=0;i<n;i++) { for(j=0;j<r;j++) { if(d&&d[j]) continue; free(cc[i].o[j]); } free(cc[i].o); }
free(cc);
return a;
}