]> git.mxchange.org Git - simgear.git/blobdiff - simgear/nasal/lib.c
Clamp pitch values rather than just dumping an error message.
[simgear.git] / simgear / nasal / lib.c
index 1ed3837bd6549fbd400d361c87f7177e48218576..7597742cf8e0cf1b5acb92c7eaeac883f1ef1a0e 100644 (file)
@@ -1,14 +1,14 @@
 #include "nasal.h"
 
-#ifndef _MSC_VER
 // No need to include <string.h> just for this:
-static int strlen(char* s)
+// It needs a funny name because MSVC wants to treat "strlen" as a
+// special symbol.  Ugh...
+static int StrLen(char* s)
 {
     char* s0 = s;
     while(*s) s++;
     return s - s0;
 }
-#endif
 
 static naRef size(naContext c, naRef args)
 {
@@ -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; i<len; i++)
+        naVec_set(result, i, naVec_get(v, start + i));
+    return result;
+}
+
 static naRef delete(naContext c, naRef args)
 {
     naRef h = naVec_get(args, 0);
@@ -118,7 +145,8 @@ static naRef typeOf(naContext c, naRef args)
     else if(naIsVector(r)) t = "vector";
     else if(naIsHash(r)) t = "hash";
     else if(naIsFunc(r)) t = "func";
-    r = naStr_fromdata(naNewString(c), t, strlen(t));
+    else if(naIsGhost(r)) t = "ghost";
+    r = naStr_fromdata(naNewString(c), t, StrLen(t));
     return r;
 }
 
@@ -128,6 +156,8 @@ static struct func funcs[] = {
     { "keys", keys }, 
     { "append", append }, 
     { "pop", pop }, 
+    { "setsize", setsize }, 
+    { "subvec", subvec }, 
     { "delete", delete }, 
     { "int", intf },
     { "num", num },
@@ -144,7 +174,7 @@ naRef naStdLib(naContext c)
     for(i=0; i<n; i++) {
         naRef code = naNewCCode(c, funcs[i].func);
         naRef name = naStr_fromdata(naNewString(c),
-                                    funcs[i].name, strlen(funcs[i].name));
+                                    funcs[i].name, StrLen(funcs[i].name));
         naHash_set(namespace, name, naNewFunc(c, code));
     }
     return namespace;