/* This file is in the public domain. */ #include #include #include "es.h" void es_init(ES *e) { e->b = 0; e->a = 0; e->l = 0; } void es_done(ES *e) { free(e->b); es_init(e); } void es_clear(ES *e) { e->l = 0; } void es_append_1(ES *e, char c) { es_append_n(e,&c,1); } void es_append_n(ES *e, const void *d, int n) { if (n < 1) return; if (e->a < e->l+n) { e->a = e->l + n + 16; e->b = realloc(e->b,e->a); } bcopy(d,e->b+e->l,n); e->l += n; } int es_len(const ES *e) { return(e->l); } char *es_buf(const ES *e) { return(e->b); } char *es_buf_nul(ES *e) { es_append_1(e,'\0'); e->l --; return(e->b); } int es_pop_1(ES *e) { if (e->l < 1) return(-1); return(((unsigned char *)e->b)[--e->l]); } void es_pop_n(ES *e, int n) { if ((n < 0) || (n > e->l)) abort(); e->l -= n; } char *es_take(ES *e) { char *b; b = e->b; es_init(e); return(b); }