From 7d631e495996e9e49b97df9053f503b21b1db8ba Mon Sep 17 00:00:00 2001 From: andy Date: Wed, 15 Mar 2006 19:42:37 +0000 Subject: [PATCH] Fix broken checkin in iolib.c. Also add the 'bits' library, which has a buf() function needed to make convenient use of io.read(). --- simgear/nasal/Makefile.am | 1 + simgear/nasal/bitslib.c | 94 +++++++++++++++++++++++++++++++++++++++ simgear/nasal/iolib.c | 4 +- 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 simgear/nasal/bitslib.c diff --git a/simgear/nasal/Makefile.am b/simgear/nasal/Makefile.am index 95021747..e31ad2f3 100644 --- a/simgear/nasal/Makefile.am +++ b/simgear/nasal/Makefile.am @@ -15,6 +15,7 @@ libsgnasal_a_SOURCES = \ mathlib.c \ iolib.c \ iolib.h \ + bitslib.c \ misc.c \ nasal.h \ parse.c parse.h \ diff --git a/simgear/nasal/bitslib.c b/simgear/nasal/bitslib.c new file mode 100644 index 00000000..0cc7c52e --- /dev/null +++ b/simgear/nasal/bitslib.c @@ -0,0 +1,94 @@ +#include +#include "data.h" + +// Note that this currently supports a maximum field width of 32 +// bits (i.e. an unsigned int). Using a 64 bit integer would stretch +// that beyond what is representable in the double result, but +// requires portability work. + +#define BIT(s,l,n) s[l-1-((n)>>3)] & (1<<((n)&7)) +#define CLRB(s,l,n) s[l-1-((n)>>3)] &= ~(1<<((n)&7)) +#define SETB(s,l,n) s[l-1-((n)>>3)] |= 1<<((n)&7) + +static unsigned int fld(naContext c, unsigned char* s, + int slen, int bit, int flen) +{ + int i; + unsigned int fld = 0; + if(bit + flen > 8*slen) naRuntimeError(c, "bitfield out of bounds"); + for(i=0; i 8*slen) naRuntimeError(c, "bitfield out of bounds"); + for(i=0; i 0 ? args[0].ref.ptr.str : 0; + int bit = argc > 1 ? (int)naNumValue(args[1]).num : -1; + int len = argc > 2 ? (int)naNumValue(args[2]).num : -1; + unsigned int f; + if(!s || !MUTABLE(args[0]) || bit < 0 || len < 0) + naRuntimeError(c, "missing/bad argument to fld/sfld"); + f = fld(c, s->data, s->len, bit, len); + if(!sign) return naNum(f); + if(f & (1 << (len-1))) f |= ~((1< 0 ? args[0].ref.ptr.str : 0; + int bit = argc > 1 ? (int)naNumValue(args[1]).num : -1; + int len = argc > 2 ? (int)naNumValue(args[2]).num : -1; + naRef val = argc > 3 ? naNumValue(args[3]) : naNil(); + if(!argc || !MUTABLE(args[0])|| bit < 0 || len < 0 || IS_NIL(val)) + naRuntimeError(c, "missing/bad argument to setfld"); + setfld(c, s->data, s->len, bit, len, (unsigned int)val.num); + return naNil(); +} + +static naRef f_buf(naContext c, naRef me, int argc, naRef* args) +{ + naRef len = argc ? naNumValue(args[0]) : naNil(); + if(IS_NIL(len)) naRuntimeError(c, "missing/bad argument to buf"); + return naStr_buf(naNewString(c), (int)len.num); +} + +static struct func { char* name; naCFunction func; } funcs[] = { + { "sfld", f_sfld }, + { "fld", f_fld }, + { "setfld", f_setfld }, + { "buf", f_buf }, +}; + +naRef naBitsLib(naContext c) +{ + naRef namespace = naNewHash(c); + int i, n = sizeof(funcs)/sizeof(struct func); + for(i=0; itype != &naStdIOType) naRuntimeError(ctx, "bad argument to readln()"); buf = naAlloc(sz); while(1) { - char c = getcguard(ctx, g->handle, buf); + c = getcguard(ctx, g->handle, buf); if(c == EOF || c == '\n') break; if(c == '\r') { char c2 = getcguard(ctx, g->handle, buf); -- 2.39.5