shithub: lua9

Download patch

ref: dec3b0a9522014394993a1365673f434ca5ccfe6
parent: 98b14048ff38d318b446a1428a763a6b4bbb7a18
author: telephil9 <telephil9@gmail.com>
date: Sun Oct 25 16:05:59 EDT 2020

Add a function to retrieve a table of points from lua

	checkpoints() read a table at given index from the stack
	and return a list of points along with the number of points read.

--- a/geometry.c
+++ b/geometry.c
@@ -1,4 +1,5 @@
 #include <draw.h>
+#include <stdlib.h>
 #include <lua.h>
 #include <lualib.h>
 #include <lauxlib.h>
@@ -88,4 +89,25 @@
 	if(lua_istable(L, index) == 0)
 		return ZP;
 	return getpoint(L, index);
+}
+
+Point*
+checkpoints(lua_State *L, int index, int *np)
+{
+	Point *p;
+	int i;
+
+	if(lua_istable(L, index) == 0)
+		luaL_argerror(L, index, "table of points expected");
+	*np = luaL_len(L, index);
+	if(*np == 0)
+		luaL_argerror(L, index, "table of points is empty");
+	p = calloc(*np, sizeof(Point));
+	if(p == nil)
+		luaL_error(L, "out of memory");
+	for(i = 1; i <= *np; i++){
+		lua_rawgeti(L, index, i);
+		p[i-1] = checkpoint(L, lua_gettop(L));
+	}
+	return p;
 }
--- a/ldraw.h
+++ b/ldraw.h
@@ -24,6 +24,7 @@
 Point checkpoint(lua_State *L, int index);
 Point getpoint(lua_State *L, int index);
 Point optpoint(lua_State *L, int index);
+Point* checkpoints(lua_State *L, int index, int *np);
 
 /* libs */
 int openlibdraw(lua_State *L);