📄 tablet.mx
字号:
throw(MAL, "tablet.load", "BAT missing"); } if ((types = BATdescriptor(*typeid)) == NULL) { BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); throw(MAL, "tablet.load", "BAT missing"); } tablet_load(&bn, names, seps, types, *filename, nr); if (bn == NULL) throw(MAL, "tablet.load", "Failed to load"); BBPincref(*ret, TRUE); *ret = bn->batCacheid; BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); BBPunfix(types->batCacheid); return MAL_SUCCEED;}strCMDtablet_dump(int *ret, int *nameid, int *sepid, int *bids, str *filename, int *nr){ BAT *names, *seps, *bats; (void) ret; if ((names = BATdescriptor(*nameid)) == NULL) { throw(MAL, "tablet.dump", "BAT missing"); } if ((seps = BATdescriptor(*sepid)) == NULL) { BBPunfix(names->batCacheid); throw(MAL, "tablet.dump", "BAT missing"); } if ((bats = BATdescriptor(*bids)) == NULL) { BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); throw(MAL, "tablet.dump", "BAT missing"); } tablet_dump(names, seps, bats, *filename, nr); BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); BBPunfix(bats->batCacheid); return MAL_SUCCEED;}strCMDtablet_input(int *ret, int *nameid, int *sepid, int *typeid, stream *s, int *nr){ BAT *names, *seps, *types, *bn; if ((names = BATdescriptor(*nameid)) == NULL) { throw(MAL, "tablet.load", "BAT missing"); } if ((seps = BATdescriptor(*sepid)) == NULL) { BBPunfix(names->batCacheid); throw(MAL, "tablet.load", "BAT missing"); } if ((types = BATdescriptor(*typeid)) == NULL) { BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); throw(MAL, "tablet.load", "BAT missing"); } tablet_input(&bn, names, seps, types, (void **) s, nr); if (bn == NULL) { BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); BBPunfix(types->batCacheid); throw(MAL, "tablet.load", "Failed to load"); } BBPincref(*ret, TRUE); *ret = bn->batCacheid; BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); BBPunfix(types->batCacheid); return MAL_SUCCEED;}strCMDtablet_output(int *ret, int *nameid, int *sepid, int *bids, void **s){ BAT *names, *seps, *bats; (void) ret; if ((names = BATdescriptor(*nameid)) == NULL) { throw(MAL, "tablet.output", "BAT missing"); } if ((seps = BATdescriptor(*sepid)) == NULL) { BBPunfix(names->batCacheid); throw(MAL, "tablet.output", "BAT missing"); } if ((bats = BATdescriptor(*bids)) == NULL) { BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); throw(MAL, "tablet.output", "BAT missing"); } tablet_output(names, seps, bats, s); BBPunfix(names->batCacheid); BBPunfix(seps->batCacheid); BBPunfix(bats->batCacheid); return MAL_SUCCEED;}@milPROC test_tablet() : void := { #tablet_debug(1); fp := fopen ( "format", "w+" ); fputs( "key,\",\",str\n", fp); fputs( "a_io_test,\"\\n\",dbl\n", fp); fclose( fp ); fp := fopen ( "data", "w+" ); fputs( "test, 0.12345\n", fp); fputs( "second line, 9.0\n", fp); fclose( fp ); bats := load_data(load_format("format"),"data",10); bats.print; a := bats.find("key").reverse().join( bats.find("a_io_test") ); a.print; dump_data(load_format("format"), bats, "data", -1); fp := fopen ( "data", "r" ); s := fgets( fp ); s.print(); fclose( fp );}@+ Tablet reportThe routines to manage the table descriptor@cstatic voidclearColumn(Column * c){ CLEAR(c->batname); CLEAR(c->name); CLEAR(c->sep); c->width = 0; c->tabs = 0; c->c = c->p = 0; /* keep nullstr and brackets */}static voidclearTable(Tablet * t){ unsigned int i; for (i = 0; i < t->nr_attrs; i++) clearColumn(t->columns + i); CLEAR(t->ttopbrk); CLEAR(t->tbotbrk); CLEAR(t->rlbrk); CLEAR(t->rrbrk); CLEAR(t->properties); CLEAR(t->title); CLEAR(t->footer); CLEAR(t->sep); t->rowwidth = 0; t->nr_attrs = 0; t->firstrow = t->lastrow = 0; /* keep brackets and stream */}@-Expansion of a table report descriptor should notmean loosing its content.@cstatic voidmakeTableSpace(int rnr, unsigned int acnt){ Tablet *t = 0; assert(rnr >= 0 && rnr < MAL_MAXCLIENTS); t = tableReports[rnr]; if (t == 0) { int len = sizeof(Tablet) + acnt * sizeof(Column); t = tableReports[rnr] = (Tablet *) GDKmalloc(len); memset((char *) t, 0, len); t->max_attrs = acnt; } if (t && t->max_attrs < acnt) { Tablet *tn; int len = sizeof(Tablet) + acnt * sizeof(Column); tn = tableReports[rnr] = (Tablet *) GDKmalloc(len); memset((char *) tn, 0, len); memcpy((char *) tn, (char *) t, sizeof(Tablet) + t->max_attrs * sizeof(Column)); GDKfree(t); tn->max_attrs = acnt; }}@-Binding variables also involves setting the defaultformatting scheme. These are based on the storage type onlyat this point. The variables should be either all BATsor all scalars. But this is to be checked just beforeprinting.@cstatic intisScalarVector(Tablet * t){ unsigned int i; for (i = 0; i < t->nr_attrs; i++) if (t->columns[i].c) return 0; return 1;}static intisBATVector(Tablet * t){ unsigned int i, cnt; if (t->columns[0].c == 0) return 0; cnt = BATcount(t->columns[0].c); for (i = 0; i < t->nr_attrs; i++) if (t->columns[i].c == 0) return 0; else if (BATcount(t->columns[i].c) != cnt) return 0; return 1;}static strbindVariable(Tablet * t, unsigned int anr, str nme, int tpe, ptr val, int *k){ Column *c; char *buf; int tpeStore; c = t->columns + anr; tpeStore = ATOMstorage(tpe); c->type = GDKstrdup(ATOMname(tpeStore)); c->adt = tpe; c->name = GDKstrdup(nme); if (c->rbrk == 0) c->rbrk = GDKstrdup(","); c->width = strlen(nme); /* plus default bracket(s) */ if (anr >= t->nr_attrs) t->nr_attrs = anr + 1; buf = (char *) GDKmalloc(BUFSIZ); memset(buf,0,BUFSIZ); if (tpe == TYPE_bat) { BAT *b; int bid = *(int *) val; if ((b = BATdescriptor(bid)) == NULL) { throw(MAL, "tablet.bindVariable", "Cannot access descriptor"); } c->c = b; /* the first column should take care of leader text size */ if (c->c) setTabwidth(c); } else if (val) { if (ATOMstorage(tpe) == TYPE_str || ATOMstorage(tpe) > TYPE_str) val = *(str *) val; /* V5 */ (*BATatoms[tpe].atomToStr) (&buf, k, val); c->width = MAX(c->width, strlen(buf)); if (c->lbrk) c->width += strlen(c->lbrk); if (c->rbrk) c->width += strlen(c->rbrk); } GDKfree(buf); c->p = val; if (c->scale) c->width = c->scale + 2; /* decimal point and '-' */ c->width += (c->rbrk ? strlen(c->rbrk) : 0) + (c->lbrk ? strlen(c->lbrk) : 0); if (c->maxwidth && c->maxwidth < c->width) c->width = c->maxwidth; if (t->columns == c) c->width += t->rlbrk ? strlen(t->rlbrk) : 0; c->tabs = 1 + (c->width) / 8; t->rowwidth += 8 * c->tabs; *k = c->width; if (c->c) BBPunfix(c->c->batCacheid); return NULL;}@+ Actual printing@cvoidTABshowHeader(Tablet * t){ unsigned int i; char *prop = "name", *p, *q; if (t->title) stream_write(t->fd, t->title, 1, strlen(t->title)); else { LINE(t->fd, t->rowwidth); } p = t->properties ? t->properties : prop; while (p) { q = strchr(p, ','); if (q) *q = 0; stream_write(t->fd, "# ", 1, 2); for (i = 0; i < t->nr_attrs; i++) { Column *c = t->columns + i; unsigned int len; str prop = 0; int u = 0, v = 0; if (strcmp(p, "name") == 0) prop = c->name; else if (strcmp(p, "type") == 0) prop = c->type; else if (!isScalar(c) && c->c) { if (strcmp(p, "bat") == 0) { prop = BBPname(c->c->batCacheid); } if (strcmp(p, "name") == 0) { prop = GDKstrdup(c->c->tident); } if (strcmp(p, "base") == 0) { char buf[BUFSIZ]; sprintf(buf, SZFMT, (size_t)c->c->hseqbase); prop = GDKstrdup(buf); } if (strcmp(p, "sorted") == 0) { if (BATtordered(c->c)&1) prop = GDKstrdup("true"); else prop = GDKstrdup("false"); } if (strcmp(p, "dense") == 0) { if (BATtdense(c->c)) prop = GDKstrdup("true"); else prop = GDKstrdup("false"); } if (strcmp(p, "key") == 0) { if (c->c->tkey) prop = GDKstrdup("true"); else prop = GDKstrdup("false"); } if (strcmp(p, "min") == 0) {@-@= setAggrcase TYPE_@1: { @1 m; char buf[BUFSIZ]; BAT@2(c->c,&m); sprintf(buf,@3,m); prop= GDKstrdup(buf);}break;@c switch (c->adt) { @:setAggr(int,min,"%d")@ @:setAggr(lng,min,"%lld")@ @:setAggr(sht,min,"%d")@ @:setAggr(dbl,min,"%f")@ default: prop = GDKstrdup(""); } } if (strcmp(p, "max") == 0) { switch (c->adt) { @:setAggr(int,max,"%d")@ @:setAggr(lng,max,"%lld")@ @:setAggr(sht,max,"%d")@ @:setAggr(dbl,max,"%f")@ default: prop = GDKstrdup(""); } } } len = prop ? strlen(prop) : 0; if (c->maxwidth && len > c->maxwidth) len = c->maxwidth; if (c->lbrk) stream_write(t->fd, c->lbrk, 1, u = strlen(c->lbrk)); stream_write(t->fd, prop, len, 1); if (c->rbrk && i + 1 < t->nr_attrs) stream_write(t->fd, c->rbrk, 1, v = strlen(c->rbrk)); if (c == t->columns) len += t->rlbrk ? strlen(t->rlbrk) : 0; TABS(t->fd, c->tabs - ((len + u + v) / 8)); if (prop) { GDKfree(prop); prop = 0; } } stream_write(t->fd, "# ", 1, 2); stream_write(t->fd, p, 1, strlen(p)); stream_write(t->fd, "\n", 1, 1); if (q) { *q = ','; p = q + 1; } else p = 0; } if (t->tbotbrk == 0) { LINE(t->fd, t->rowwidth); } else stream_write(t->fd, t->tbotbrk, 1, strlen(t->tbotbrk));}voidTABshowRow(Tablet * t){ unsigned int i = 0; unsigned int m = 0; int zero = 0; char *buf = 0; Column *c = t->columns + i; unsigned int len; int u = 0, v = 0; buf = (char *) GDKmalloc(m = t->rowwidth); if (t->rlbrk) stream_printf(t->fd, "%s", t->rlbrk); for (i = 0; i < t->nr_attrs; i++) { c = t->columns + i; u = 0; v = 0; if (c->p) (*BATatoms[c->adt].atomToStr) (&buf, &zero, c->p); m= (unsigned int) zero; if (strcmp(buf, "nil") == 0 && c->nullstr && strlen(c->nullstr) < m) strcpy(buf, c->nullstr); if (c->precision) { if (strcmp(buf, "nil") == 0) { snprintf(buf, m, "%*s", c->scale + (c->precision ? 1 : 0), "nil"); } else switch (c->adt) { case TYPE_int: { int vi = *(int *) c->p, vj = vi, m = 1; int k; for (k = c->precision; k > 0; k--) { vi /= 10; m *= 10; } snprintf(buf, m, "%*d.%d", c->scale - c->precision, vi, vj % m); } } } len = strlen(buf); if (c->maxwidth && len > c->maxwidth) len = c->maxwidth; if (c->lbrk) stream_write(t->fd, c->lbrk, 1, u = strlen(c->lbrk)); stream_write(t->fd, buf, 1, len); if (c->rbrk) { v = strlen(c->rbrk); if (i + 1 < t->nr_attrs) { stream_write(t->fd, c->rbrk, 1, v); } else if (*c->rbrk != ',') stream_write(t->fd, c->rbrk, 1, v); } if (c == t->columns) len += t->rlbrk ? strlen(t->rlbrk) : 0; TABS(t->fd, c->tabs - ((len + u + v - 1) / 8)); } if (t->rrbrk) stream_printf(t->fd, "%s\n", t->rrbrk); GDKfree(buf);}voidTABshowRange(Tablet * t, lng first, lng last){ lng i; size_t j; oid k; i = BATcount(t->columns[0].c); if (last < 0 || last > i) last = i; if (first <= 0) first = 0; for (i = first; i < last; i++) { if (t->pivot) { k = *(oid *) BUNtail(t->pivot, BUNptr(t->pivot, t->pivot->hseqbase + i)); } else k = (oid) i; for (j = 0; j < t->nr_attrs; j++) { BAT *b = t->columns[j].c; int base = t->columns[j].base; t->columns[j].p = BUNtail(b, BUNptr(b, base + k)); } TABshowRow(t); }}static voidTABshowPage(Tablet * t){ /* if( t->ttopbrk==0) { LINE(t->fd,t->rowwidth); } else stream_printf(t->fd, "%s\n", t->ttopbrk); */ TABshowRange(t, 0, -1); if (t->tbotbrk == 0) { LINE(t->fd, t->rowwidth); } else stream_printf(t->fd, "%s\n", t->tbotbrk);}@+ V4 stuffThe remainder is a patched copy of material from gdk_storage.@ctypedef int (*strFcn) (str *s, int *len, ptr val);#define printfcn(b) ((b->ttype==TYPE_void && b->tseqbase==oid_nil)?\ print_nil:BATatoms[b->ttype].atomToStr)static intprint_nil(char **dst, int *len, ptr dummy){ (void) dummy; /* fool compiler */ if (*len < 3) { if (*dst) GDKfree(*dst); *dst = (char *) GDKmalloc(*len = 40); } strcpy(*dst, "nil"); return 3;}static intsetTabwidth(Column * c){ strFcn tostr = printfcn(c->c); size_t cnt = BATcount(c->c); int ret = 0; unsigned int max; int t = BATttype(c->c); char *buf = 0; char *title = c->c->tident; if (strcmp(c->c->tident, "t") == 0) { title = GDKmalloc(strlen(BATgetId(c->c)) + 7); snprintf(title, 20, "%s", BATgetId(c->c)); } c->base = BUNindex(c->c, BUNfirst(c->c)); c->type = GDKstrdup(ATOMname(c->c->ttype)); c->adt = c->c->ttype; buf = (char *) GDKmalloc(ret = strLen(title)); max = MAX((int) strlen(c->type), ret); if (c->nullstr) max = MAX(max, strlen(c->nullstr)); if (c->lbrk) max += strlen(c->lbrk); if (c->rbrk) max += strlen(c->rbrk); if (t >= 0 && t < GDKatomcnt && tostr) { size_t off = BUNindex(c->c, BUNfirst(c->c)); size_t j, i, probe = MIN(10000, MAX(200, cnt / 100)); for (i = 0; i < probe; i++) { if (i >= cnt) break; j = off + ((cnt < probe) ? i : (rand() % cnt)); (*tostr) (&buf, &ret, BUNtail(c->c, BUNptr(c->c, j))); max = MAX(max, strlen(buf)); } } c->width = max; c->name = GDKstrdup(title); return c->width;}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -