/* takes ownership of the string k, and object v */ int btDict_add( btDict *_this, btString* k, btObject* v) { int hi, lo, mid, res, ipos; int i; int idx = _this->len++; if (idx >= _this->dictsize) { int newsize = QUANTIZE(idx+1); _this->key = btrealloc( _this->key, sizeof(*_this->key)*newsize); _this->value = btrealloc( _this->value, sizeof(*_this->value)*newsize); _this->dictsize = newsize; } /* binary search */ ipos = hi = idx; lo = 0; while (lo < hi) { mid = (lo + hi) / 2; res = btString_cmp(k, &_this->key[mid]); if (res == 0) { return -1; /* key is already in the dictionary */ } if (res < 0) ipos = hi = mid; if (res > 0) lo = mid+1; } for (i=idx-1; i>=ipos; i--) { _this->key[i+1] = _this->key[i]; _this->value[i+1] = _this->value[i]; } btString_create( &_this->key[ipos]); btString_setbuf( &_this->key[ipos], k->buf, k->len); if (k->allocated) btfree(k); _this->value[ipos] = v; return 0; }
int kStringBuffer_grow( kStringBuffer *sb, int len) { sb->bufsize += len; sb->buf = btrealloc( sb->buf, sb->bufsize); bt_assert(sb->buf); return 0; }
/* the list takes ownership of the object v */ int btList_add( btList *_this, btObject *v) { int idx = _this->len++; bt_assert(_this->parent.t == BT_LIST); if (idx >= _this->listsize) { int newsize = QUANTIZE(idx+1); _this->list = btrealloc( _this->list, sizeof(*_this->list)*newsize); _this->listsize = newsize; } _this->list[idx] = v; return 0; }
int bts_strstream_write(btStream*bts, char *buf, int len) { btStrStream *ss = (btStrStream *)bts; if (ss->bts.type != BTSTRSTREAM) return -1; if (ss->len + len > ss->max) { /* resize buffer */ ss->max = ss->len + len + 100; ss->buf = btrealloc(ss->buf, ss->max); } memcpy(ss->buf + ss->len, buf, len); ss->len += len; return 0; }
int btFileSet_addfile( btFileSet *fs, const char *path, _int64 len) { int ifile=fs->nfiles++; btFile *f; fs->file = btrealloc(fs->file, sizeof(btFile *)*fs->nfiles); bt_assert(fs->file); f=btcalloc(1,sizeof(btFile)); bt_assert(f); f->path = strdup(path); f->len = len; if (ifile > 0) { btFile *lf = fs->file[ifile-1]; f->start = lf->start + lf->len; } fs->file[ifile]=f; return 0; }
int btFileSet_addfile( btFileSet *fs, const char *path, _int64 len) { int ifile=fs->nfiles++; btFile *f; #if 0 printf("addfile( ..., %s, %d)\n", path, len); #endif fs->file = btrealloc(fs->file, sizeof(btFile *)*fs->nfiles); DIE_UNLESS(fs->file); f=btcalloc(1,sizeof(btFile)); DIE_UNLESS(f); f->path = strdup(path); f->len = len; if (ifile > 0) { btFile *lf = fs->file[ifile-1]; f->start = lf->start + lf->len; } fs->file[ifile]=f; return 0; }