📄 tablet.mx
字号:
TABheader(mb, stk, pci); t = tableReports[rnr]; if (!isScalarVector(t)) throw(MAL, "tablet.print", "Only scalars expected"); else TABshowRow(t); if (t->tbotbrk == 0) { LINE(t->fd, t->rowwidth); } else stream_write(t->fd, t->tbotbrk, 1, strlen(t->tbotbrk)); return MAL_SUCCEED;}strTABpage(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ Client c = MCgetClient(); int rnr = c - mal_clients; Tablet *t; TABformatPrepare(mb, stk, pci); t = tableReports[rnr]; if (t->ttopbrk == 0) { LINE(t->fd, t->rowwidth); } else stream_write(t->fd, t->ttopbrk, 1, strlen(t->ttopbrk)); if (!isBATVector(t)) throw(MAL, "tablet.print", "Only aligned BATs expected"); else { t->pageLimit = 20; t->firstrow = t->lastrow = 0; TABshowPage(t); } return MAL_SUCCEED;}strTABsetProperties(int *ret, str *prop){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ if (tableReports[rnr] == 0) throw(MAL, "tablet.properties", "Format definition missing"); CLEAR(tableReports[rnr]->properties); tableReports[rnr]->properties = !strNil(*prop) ? GDKstrdup(*prop) : 0; return MAL_SUCCEED;}strTABdump(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ (void) mb; (void) stk; (void) pci; /* fool compiler */ throw(MAL, "tablet.report", "Not yet implemented");}strTABfinishReport(int *ret){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ if (tableReports[rnr] == 0) throw(MAL, "tablet.finish", "Header information missing"); clearTable(tableReports[rnr]); GDKfree(tableReports[rnr]); tableReports[rnr] = 0; return MAL_SUCCEED;}strTABsetStream(int *ret, stream **s){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ if (tableReports[rnr] == 0) throw(MAL, "tablet.setStream", "Header information missing"); tableReports[rnr]->fd = *s; return MAL_SUCCEED;}strTABsetPivot(int *ret, int *bid){ Client c = MCgetClient(); int rnr = c - mal_clients; BAT *b; (void) ret; /* fool compiler */ if ((b = BATdescriptor(*bid)) == NULL) { throw(MAL, "tablet.setPivot", "Pivot BAT missing"); } tableReports[rnr]->pivot = b; BBPunfix(b->batCacheid); return MAL_SUCCEED;}strTABsetDelimiter(int *ret, str *sep){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ if (tableReports[rnr] == 0) throw(MAL, "tablet.setDelimiters", "Header information missing"); tableReports[rnr]->sep = !strNil(*sep) ? GDKstrdup(*sep) : 0; return MAL_SUCCEED;}strTABsetColumn(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ (void) mb; (void) stk; (void) pci; /* fool compiler */ throw(MAL, "tablet.setColumn", "Not yet implemented");}strTABsetColumnName(int *ret, int *idx, str *s){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); tableReports[rnr]->columns[*idx].name = !strNil(*s) ? GDKstrdup(*s) : 0; return MAL_SUCCEED;}strTABsetTableBracket(int *ret, str *lbrk, str *rbrk){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, MAXARG); tableReports[rnr]->ttopbrk = !strNil(*lbrk) ? GDKstrdup(*lbrk) : 0; tableReports[rnr]->tbotbrk = !strNil(*rbrk) ? GDKstrdup(*rbrk) : 0; return MAL_SUCCEED;}strTABsetRowBracket(int *ret, str *lbrk, str *rbrk){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, MAXARG); tableReports[rnr]->rlbrk = !strNil(*lbrk) ? GDKstrdup(*lbrk) : 0; tableReports[rnr]->rrbrk = !strNil(*rbrk) ? GDKstrdup(*rbrk) : 0; return MAL_SUCCEED;}strTABsetColumnBracket(int *ret, int *idx, str *lbrk, str *rbrk){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); tableReports[rnr]->columns[*idx].lbrk = !strNil(*lbrk) ? GDKstrdup(*lbrk) : 0; tableReports[rnr]->columns[*idx].rbrk = !strNil(*rbrk) ? GDKstrdup(*rbrk) : 0; return MAL_SUCCEED;}strTABsetColumnNull(int *ret, int *idx, str *nullstr){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); tableReports[rnr]->columns[*idx].nullstr = !strNil(*nullstr) ? GDKstrdup(*nullstr) : 0; return MAL_SUCCEED;}strTABsetColumnWidth(int *ret, int *idx, int *width){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); tableReports[rnr]->columns[*idx].maxwidth = *width; return MAL_SUCCEED;}strTABsetColumnPosition(int *ret, int *idx, int *first, int *width){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; (void) first; /* fool compiler */ tableReports[rnr]->columns[*idx].fieldwidth = *width; tableReports[rnr]->columns[*idx].fieldstart = *width; makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); return MAL_SUCCEED;}strTABsetColumnDecimal(int *ret, int *idx, int *scale, int *prec){ Client c = MCgetClient(); int rnr = c - mal_clients; (void) ret; /* fool compiler */ makeTableSpace(rnr, (*idx >= MAXARG ? *idx : MAXARG)); if (*prec > *scale) throw(MAL, "tablet.setColumnDecimal", "Illegal range"); tableReports[rnr]->columns[*idx].precision = *prec; tableReports[rnr]->columns[*idx].scale = *scale; return MAL_SUCCEED;}strTABfirstPage(int *ret){ (void) ret; /* fool compiler */ throw(MAL, "tablet.firstPage", "Not yet implemented");}strTABlastPage(int *ret){ (void) ret; /* fool compiler */ throw(MAL, "tablet.lastPage", "Not yet implemented");}strTABnextPage(int *ret){ (void) ret; /* fool compiler */ throw(MAL, "tablet.nextPage", "Not yet implemented");}strTABprevPage(int *ret){ (void) ret; /* fool compiler */ throw(MAL, "tablet.prevPage", "Not yet implemented");}strTABgetPage(int *ret, int *pnr){ (void) ret; (void) pnr; /* fool compiler */ throw(MAL, "tablet.getPage", "Not yet implemented");}strTABgetPageCnt(int *ret){ (void) ret; /* fool compiler */ throw(MAL, "tablet.getPageCnt", "Not yet implemented");}static ptrbun_tail(BAT *b, size_t nr){ REGISTER size_t _i = BUNindex((b), BUNfirst((b))); return (ptr) BUNtail(b, BUNptr(b, _i + nr));}static BAT *void_bat_create(int adt, size_t nr){ BAT *b = BATnew(TYPE_void, adt, BATTINY); /* check for correct structures */ if (b == NULL) return b; if (BATmirror(b)) BATseqbase(b, 0); if (nr > (size_t) REMAP_PAGE_MAXSIZE) BATmmap(b, STORE_MMAP, STORE_MMAP, STORE_MMAP); if (nr > BATTINY) b = BATextend(b, nr); return b;}static char *sep_dup(char *sep){ size_t len = strlen(sep); char *res = GDKmalloc(len*2 + 1), *result = res; char *end = sep + len; while (sep < end) { if (*sep == '\\') { ++sep; switch (*sep++) { case 'r': *res++ = '\r'; break; case 'n': *res++ = '\n'; break; case 't': *res++ = '\t'; break; } } else { *res++ = *sep++; } } *res = '\0'; return result;}ptr *TABLETstrFrStr(Column *c, char *s, char *e){ int len = (int) (e - s + 1); /* 64bit: should check for overflow */ if (c->len < len){ c->len = len; c->data = GDKrealloc(c->data,len); } if (s == e) { *(char*)c->data = 0; } else if (GDKstrFromStr(c->data, (unsigned char *) s, e - s) < 0) { return NULL; } return c->data;}ptr *TABLETadt_frStr(void *extra, int type, char *s, char *e, char quote){ ptr *res = NULL; Column *c = extra; if (s == NULL || (!(type == TYPE_str && quote) && strcmp(s, "nil") == 0)) { ptr src = ATOMnilptr(type); int len = ATOMlen(ATOMtype(type), src); if (c->len < len) { c->len = len; c->data = GDKrealloc(c->data,len); } memcpy(c->data, src, len); } else if (type == TYPE_str) { return TABLETstrFrStr(c, s, e); } else { (void) (*BATatoms[type].atomFromStr) (s, &c->len, (ptr) &c->data); res = c->data; } return res;}intTABLETadt_toStr(void *extra, char **buf, int *len, int type, ptr a){ (void) extra; /* fool compiler */ if (type == TYPE_str) { char *dst, *src = a; int l; if (GDK_STRNIL(src)) { src = "nil"; } l = strlen(src); if (l + 3 > *len) { GDKfree(buf); *len = 2 * l + 3; *buf = GDKmalloc(*len); memset(*buf,0,*len); } dst = *buf; dst[0] = '"'; strncpy(dst + 1, src, l); dst[l + 1] = '"'; dst[l + 2] = 0; return l + 2; } else { return (*BATatoms[type].atomToStr) (buf, len, a); }}static size_tcreate_loadformat(Tablet * as, BAT *names, BAT *seps, BAT *types){ size_t p; size_t nr_attrs = BATcount(names); Column *fmt = as->format = (Column *) GDKmalloc(sizeof(Column) * (nr_attrs + 1)); as->offset = 0; as->nr_attrs = nr_attrs; /* assert(as->nr_attrs == nr_attrs); *//* i.e. it fits */ for (p = 0; p < nr_attrs; p++) { fmt[p].name = (char *) bun_tail(names, p); fmt[p].sep = sep_dup((char *) bun_tail(seps, p)); fmt[p].seplen = (int) strlen(fmt[p].sep); fmt[p].type = (char *) bun_tail(types, p); fmt[p].adt = ATOMindex(fmt[p].type); if (fmt[p].adt < 0) { GDKerror("create_loadformat: %s has unknown type %s (using str instead).\n", fmt[p].name, fmt[p].name); fmt[p].adt = TYPE_str; } fmt[p].tostr = &TABLETadt_toStr; fmt[p].frstr = &TABLETadt_frStr; fmt[p].extra = fmt+p; fmt[p].data = NULL; fmt[p].len = 0; if (tablet_debug) stream_printf(GDKerr, "%s\n", fmt[p].name); } return as->nr_attrs;}staticsize_tcreate_dumpformat(Tablet * as, BAT *names, BAT *seps, BAT *bats){ size_t p; size_t nr_attrs = BATcount(bats); Column *fmt = as->format = (Column *) GDKmalloc(sizeof(Column) * (nr_attrs + 1)); as->offset = 0; as->nr_attrs = nr_attrs; /* assert(as->nr_attrs == nr_attrs); *//* i.e. it fits */ for (p = 0; p < nr_attrs; p++) { BAT *b = (BAT *) BATdescriptor(*(bat *) bun_tail(bats, p)); if (!b) return ~(size_t) 0; /* (size_t)(-1) */ fmt[p].name = NULL; if (names) fmt[p].name = (char *) bun_tail(names, p); fmt[p].sep = sep_dup((char *) bun_tail(seps, p)); fmt[p].seplen = (int) strlen(fmt[p].sep); fmt[p].type = ATOMname(b->ttype); fmt[p].adt = (b)->ttype; fmt[p].tostr = &TABLETadt_toStr; fmt[p].frstr = &TABLETadt_frStr; fmt[p].extra = fmt+p; fmt[p].data = NULL; fmt[p].len = 0; BBPunfix(b->batCacheid); } return as->nr_attrs;}voidTABLETdestroy_format(Tablet * as){ size_t p; Column *fmt = as->format; for (p = 0; p < as->nr_attrs; p++) { if (fmt[p].c) BBPunfix(fmt[p].c->batCacheid); GDKfree(fmt[p].sep); if (fmt[p].data) GDKfree(fmt[p].data); } GDKfree(fmt);}size_tTABLETassign_BATs(Tablet * as, BAT *bats){ Column *fmt = as->format; size_t res = as->nr; size_t i; for (i = 0; i < as->nr_attrs; i++) { BAT *b = (BAT *) BATdescriptor(*(bat *) bun_tail(bats, i)); fmt[i].c = (b); if (res == ~(size_t) 0 || BATcount(fmt[i].c) < res) res = BATcount(fmt[i].c); } as->nr = res; return res;}static oidcheck_BATs(Tablet * as){ Column *fmt = as->format; size_t i = 0; size_t cnt; oid base; if (fmt[i].c == NULL) i++; cnt = BATcount(fmt[i].c); base = fmt[i].c->hseqbase; if (!BAThdense(fmt[i].c) || as->nr != cnt ) return oid_nil; for (i = 0; i < as->nr_attrs; i++) { BAT *b; size_t offset; b = fmt[i].c; if (b == NULL) continue; offset = BUNindex(b, BUNfirst(b)) + as->offset; if (BATcount(b) != cnt || !BAThdense(b) || b->hseqbase != base) return oid_nil; fmt[i].p = BUNptr(b, offset); } return base;}intTABLETcreate_bats(Tablet * as){ Column *fmt = as->format; size_t i; size_t nr = 0; if (as->nr != ~(size_t)0) nr = as->nr; for (i = 0; i < as->nr_attrs; i++) { fmt[i].c = void_bat_create(fmt[i].adt, nr); if (!fmt[i].c) { GDKerror("ASCIIcreate_bats: Failed to create bat of size %d\n", as->nr); return -1; } } return 0;}BAT *TABLETcollect_bats(Tablet * as){ BAT *bats = BATnew(TYPE_str, TYPE_bat, as->nr_attrs); Column *fmt = as->format; size_t i; size_t cnt = BATcount(fmt[0].c); if (bats == NULL) return NULL; for (i = 0; i < as->nr_attrs; i++) { BUNins(bats, (ptr) fmt[i].name, (ptr) &fmt[i].c->batCacheid, FALSE); BATsetaccess(fmt[i].c, BAT_READ); if (cnt != BATcount(fmt[i].c)) { GDKerror("Error: counts are not equal\n"); return NULL; } } return bats;}static INLINE intmyisspace(int s){ return s == ' ' || s == '\t' || s == '\n';}static INLINE char *lstrip(char *s){ while (myisspace((int) *s)) { s++; } return s;}static INLINE char *rstrip(char *e){ e--; while (myisspace((int) *e)) { e--;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -