void naStr_gcclean(struct naStr* str) { if(str->emblen == -1) naFree(str->data.ref.ptr); str->data.ref.ptr = 0; str->data.ref.len = 0; str->emblen = -1; }
// Handles multiple EOL conventions by using stdio's ungetc. Will not // work for other IO types without converting them to FILE* with // fdopen() or whatnot... static naRef f_readln(naContext ctx, naRef me, int argc, naRef* args) { naRef result; struct naIOGhost* g = argc==1 ? ioghost(args[0]) : 0; int i=0, c, sz = 128; char *buf; if(!g || g->type != &naStdIOType) naRuntimeError(ctx, "bad argument to readln()"); buf = naAlloc(sz); while(1) { c = getcguard(ctx, g->handle, buf); if(c == EOF || c == '\n') break; if(c == '\r') { int c2 = getcguard(ctx, g->handle, buf); if(c2 != EOF && c2 != '\n') if(EOF == ungetc(c2, g->handle)) break; break; } buf[i++] = c; if(i >= sz) buf = naRealloc(buf, sz *= 2); } result = c == EOF ? naNil() : naStr_fromdata(naNewString(ctx), buf, i); naFree(buf); return result; }
static void setlen(struct naStr* s, int sz) { if(s->data) naFree(s->data); s->len = sz; s->data = naAlloc(sz+1); s->data[sz] = 0; // nul terminate }
static void* threadtop(void* param) #endif { ThreadData* td = param; naCall(td->ctx, td->func, 0, 0, naNil(), naNil()); naFreeContext(td->ctx); naFree(td); return 0; }
// frees buffer before tossing an error static int getcguard(naContext ctx, FILE* f, void* buf) { int c; naModUnlock(); c = fgetc(f); naModLock(); if(ferror(f)) { naFree(buf); naRuntimeError(ctx, strerror(errno)); } return c; }
static void setlen(struct naStr* s, int sz) { if(s->emblen == -1 && DATA(s)) naFree(s->data.ref.ptr); if(sz > MAX_STR_EMBLEN) { s->emblen = -1; s->data.ref.len = sz; s->data.ref.ptr = naAlloc(sz+1); } else { s->emblen = sz; } DATA(s)[sz] = 0; // nul terminate }
void naStr_gcclean(struct naStr* str) { naFree(str->data); str->data = 0; str->len = 0; }
static void ghostDestroy(void* g) { struct naIOGhost* io = (struct naIOGhost*)g; io->type->destroy(io->handle); naFree(io); }
void naiGCHashClean(struct naHash* h) { naFree(h->rec); h->rec = 0; }