1. Martin Felis
  2. rbdl

Commits

Martin Felis  committed ad5aa9d

updated to newest luatables also in the default branch

  • Participants
  • Parent commits 5852dae
  • Branches default

Comments (0)

Files changed (3)

File addons/luamodel/luamodel.cc

View file
 SpatialVector get_spatial_vector (lua_State *L, const string &path, int index = -1) {
 	SpatialVector result (0., 0., 0., 0., 0., 0.);
 
-	std::vector<double> array = ltGetDoubleArrayAt (L, path.c_str(), index);
+	std::vector<double> array = ltGetDoubleVectorAt (L, path.c_str(), index);
 	if (array.size() != 6) {
 		cerr << "Invalid array size for spatial vector variable '" << path << "'." << endl;
 		abort();
 Vector3d get_vector3d (lua_State *L, const string &path, int index = -1) {
 	Vector3d result;
 
-	std::vector<double> array = ltGetDoubleArrayAt (L, path.c_str(), index);
+	std::vector<double> array = ltGetDoubleVectorAt (L, path.c_str(), index);
 	if (array.size() != 3) {
 		cerr << "Invalid array size for 3d vector variable '" << path << "'." << endl;
 		abort();
 		return result;
 	}
 
-	std::vector<double> array = ltGetDoubleArrayAt (L, path.c_str(), -1);
+	std::vector<double> array = ltGetDoubleVectorAt (L, path.c_str(), -1);
 	if (array.size() != 9) {
 		cerr << "Invalid array size for 3d matrix variable '" << path << "'." << endl;
 		abort();

File addons/luamodel/luatables.cc

View file
 
 using namespace std;
 
-static void bail(lua_State *L, const char *msg){
+void bail(lua_State *L, const char *msg){
 	std::cerr << msg << lua_tostring(L, -1) << endl;
 	abort();
 }
  * optional last parameter is used to ease iteration over multiple
  * elements.
  */
-bool get_table_from_path (lua_State *L, const char *path_str, int index = -1);
+bool get_table_from_path (lua_State *L, const char *path_str, const int index = -1);
 
 
 /* Proxy functions for ltXXXAt() calls */
 	return ltGetKeysAt (L, path_str, -1);
 }
 
-std::vector<double> ltGetDoubleArray (lua_State *L, const char *path_str) {
-	return ltGetDoubleArrayAt (L, path_str, -1);
+std::vector<double> ltGetDoubleVector (lua_State *L, const char *path_str) {
+	return ltGetDoubleVectorAt (L, path_str, -1);
 }
+
+bool ltGetDoubleArray (lua_State *L, const char *path_str, const unsigned int count, double *dest) {
+	return ltGetDoubleArrayAt (L, path_str, count, dest, -1);
+}
+
+bool ltIsNumber (lua_State *L, const char *path_str) {
+	return ltIsNumberAt (L, path_str, -1);
+}
+
+bool ltIsExisting (lua_State *L, const char *path_str) {
+	return ltIsExistingAt (L, path_str, -1);
+}
+
 /* Actual interesting code */
 
 bool ltOpenFile (lua_State **L, const char *filename) {
 	*L = NULL;
 }
 
-std::string ltGetStringAt (lua_State *L, const char *path_str, int index, const std::string &default_result) {
+std::string ltGetStringAt (lua_State *L, const char *path_str, const int index, const std::string &default_result) {
 	std::string result;
 
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-double ltGetDoubleAt (lua_State *L, const char *path_str, int index, const double &default_result) {
+double ltGetDoubleAt (lua_State *L, const char *path_str, const int index, const double &default_result) {
 	double result;
 	
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-size_t ltGetLengthAt (lua_State *L, const char *path_str, int index) {
+size_t ltGetLengthAt (lua_State *L, const char *path_str, const int index) {
 	size_t result = 0;
 	
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-std::vector<std::string> ltGetKeysAt (lua_State *L, const char *path_str, int index) {
+std::vector<std::string> ltGetKeysAt (lua_State *L, const char *path_str, const int index) {
 	std::vector<string> result;
 	
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-std::vector<double> ltGetDoubleArrayAt (lua_State *L, const char *path_str, int index) {
+std::vector<double> ltGetDoubleVectorAt (lua_State *L, const char *path_str, const int index) {
 	std::vector<double> result;
 	
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-bool ltIsNumber (lua_State *L, const char *path_str) {
-	return ltIsNumberAt (L, path_str, -1);
+bool ltGetDoubleArrayAt (lua_State *L, const char *path_str, const unsigned int count, double *dest, const int index) {
+	std::vector<double> result;
+	
+	int stack_top = lua_gettop(L);
+
+	if (!get_table_from_path(L, path_str, index)) 
+		return false;
+
+	if (!lua_istable(L, -1)) {
+		cout << "Error: value at " << path_str;
+		if (index > 0)
+			cout << "[" << index << "]";
+		cout << " is not a table!" << endl;
+
+		// clean up the stack
+		lua_pop (L, lua_gettop(L) - stack_top);
+		return false;
+	}
+
+	lua_pushnil(L);
+
+	int i = 1;
+	while (lua_next(L, -2)) {
+		if (lua_isnumber (L, -1)) {
+			result.push_back (lua_tonumber(L, -1));
+		} else {
+			cout << "Error: values at " << path_str;
+			if (index > 0)
+				cout << "[" << index << "]";
+			cout << " are not numbers only!" << endl;
+
+			// clean up the stack
+			lua_pop (L, lua_gettop(L) - stack_top);
+			return false;
+		}
+		lua_pop(L, 1);
+		i++;
+	}
+
+	// clean up the stack
+	lua_pop (L, lua_gettop(L) - stack_top);
+
+	if (result.size() >= count) {
+		for (unsigned int i = 0; i < count; i++) {
+			dest[i] = result[i];
+		}
+		return true;
+	}
+
+	cout << "Error: Tried to read " << count << " values at " << path_str;
+	if (index > 0)
+		cout << "[" << index << "]";
+	cout << " but only found " << result.size() << " elements!" << endl;
+
+	return false;
 }
 
-bool ltIsNumberAt (lua_State *L, const char *path_str, int index) {
+bool ltIsNumberAt (lua_State *L, const char *path_str, const int index) {
 	bool result = false;
 
 	int stack_top = lua_gettop(L);
 	return result;
 }
 
-bool ltIsExisting (lua_State *L, const char *path_str) {
-	return ltIsExistingAt (L, path_str, -1);
-}
-
 bool ltIsExistingAt (lua_State *L, const char *path_str, int index) {
 	int stack_top = lua_gettop(L);
 
  * optional last parameter is used to ease iteration over multiple
  * elements.
  */
-bool get_table_from_path (lua_State *L, const char *path_str, int index) {
+bool get_table_from_path (lua_State *L, const char *path_str, const int index) {
 	std::string path = path_str;
 	std::string token = path;
 
 		}
 
 		if (lua_isnil(L, -1)) {
+			cout << "Error: could not find table '" << path_str;
+			if (path.size() == 0 && index > 0)
+				cout << "[" << index << "]";
+			cout << "'." << endl;
+			
 			// clean up the stack
 			lua_pop (L, lua_gettop(L) - stack_top);
 			return false;

File addons/luamodel/luatables.h

View file
 #include <vector>
 #include <string>
 
-extern "C" {
-#include <lua.h>
+extern "C"
+{
+   #include <lua.h>
+   #include <lauxlib.h>
+   #include <lualib.h>
 }
 
 bool ltOpenFile (lua_State **L, const char *filename);
 void ltClose (lua_State **L);
 
-std::string ltGetStringAt (lua_State *L, const char *path_str, int index, const std::string &default_result = "");
+std::string ltGetStringAt (lua_State *L, const char *path_str, const int index, const std::string &default_result = "");
 std::string ltGetString (lua_State *L, const char *path_str, const std::string &default_result = "");
 
-double ltGetDoubleAt (lua_State *L, const char *path_str, int index, const double &default_result = 0.);
+double ltGetDoubleAt (lua_State *L, const char *path_str, const int index, const double &default_result = 0.);
 double ltGetDouble (lua_State *L, const char *path_str, const double &default_result = 0.);
 
-size_t ltGetLengthAt (lua_State *L, const char *path_str, int index);
+size_t ltGetLengthAt (lua_State *L, const char *path_str, const int index);
 size_t ltGetLength (lua_State *L, const char *path_str);
 
-std::vector<std::string> ltGetKeysAt (lua_State *L, const char *path_str, int index);
+std::vector<std::string> ltGetKeysAt (lua_State *L, const char *path_str, const int index);
 std::vector<std::string> ltGetKeys (lua_State *L, const char *path_str);
 
-std::vector<double> ltGetDoubleArrayAt (lua_State *L, const char *path_str, int index);
-std::vector<double> ltGetDoubleArray (lua_State *L, const char *path_str); 
+std::vector<double> ltGetDoubleVectorAt (lua_State *L, const char *path_str, const int index);
+std::vector<double> ltGetDoubleVector (lua_State *L, const char *path_str); 
+
+bool ltGetDoubleArrayAt (lua_State *L, const char *path_str, const unsigned int count, double *dest, const int index);
+bool ltGetDoubleArray (lua_State *L, const char *path_str, const unsigned int count, double *dest); 
 
 bool ltIsNumber (lua_State *L, const char *path_str);
 bool ltIsNumberAt (lua_State *L, const char *path_str, int index);