From: andy Date: Fri, 5 Dec 2003 01:49:02 +0000 (+0000) Subject: Added setsize() and subvec() library functions X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dc9c41e119223da65c1f158bef43532234f97853;p=simgear.git Added setsize() and subvec() library functions --- diff --git a/simgear/nasal/lib.c b/simgear/nasal/lib.c index b050aa70..f9768d52 100644 --- a/simgear/nasal/lib.c +++ b/simgear/nasal/lib.c @@ -46,6 +46,33 @@ static naRef pop(naContext c, naRef args) return naVec_removelast(v); } +static naRef setsize(naContext c, naRef args) +{ + naRef v = naVec_get(args, 0); + int sz = (int)naNumValue(naVec_get(args, 1)).num; + if(!naIsVector(v)) return naNil(); + naVec_setsize(v, sz); + return v; +} + +static naRef subvec(naContext c, naRef args) +{ + int i; + naRef nlen, result, v = naVec_get(args, 0); + int len = 0, start = (int)naNumValue(naVec_get(args, 1)).num; + nlen = naNumValue(naVec_get(args, 2)); + if(!naIsNil(nlen)) + len = (int)naNumValue(naVec_get(args, 2)).num; + if(!naIsVector(v) || start < 0 || start >= naVec_size(v) || len < 0) + return naNil(); + if(len == 0 || len > naVec_size(v) - start) len = naVec_size(v) - start; + result = naNewVector(c); + naVec_setsize(result, len); + for(i=0; isize++; } +void naVec_setsize(naRef vec, int sz) +{ + int i; + struct naVec* v = vec.ref.ptr.vec; + naRef* na = naAlloc(sizeof(naRef) * sz); + for(i=0; isize) ? v->array[i] : naNil(); + naFree(v->array); + v->array = na; + v->size = sz; + v->alloced = sz; +} + naRef naVec_removelast(naRef vec) { naRef o;