Source

rbdl / addons / luamodel / luatables.cc

The dev branch has multiple heads

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * luatables
 * Copyright (c) 2011-2012 Martin Felis <martin.felis@iwr.uni-heidelberg.de>
 * 
 * (zlib license)
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 *    1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 
 *    2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 
 *    3. This notice may not be removed or altered from any source
 *    distribution.
 */

#include "luatables.h"

#include <iostream>
#include <cstdlib>
#include <vector>
#include <sstream>

extern "C"
{
   #include <lua.h>
   #include <lauxlib.h>
   #include <lualib.h>
}

using namespace std;

void bail(lua_State *L, const char *msg){
	std::cerr << msg << lua_tostring(L, -1) << endl;
	abort();
}

/** \brief Puts a lua value at a given path with optional index onto the stack.
 *
 * This function allows to navigate tables by specifying the path to an
 * element just as within lua, e.g. "model.bodies[2].inertia[2][3]". The
 * optional last parameter is used to ease iteration over multiple
 * elements.
 */
bool get_table_from_path (lua_State *L, const char *path_str, const int index = -1);


/* Proxy functions for ltXXXAt() calls */
std::string ltGetString (lua_State *L, const char *path_str, const std::string &default_result) {
	return ltGetStringAt (L, path_str, -1, default_result);
}

double ltGetDouble (lua_State *L, const char *path_str, const double &default_result) {
	return ltGetDoubleAt (L, path_str, -1, default_result);
}

size_t ltGetLength (lua_State *L, const char *path_str) {
	return ltGetLengthAt (L, path_str, -1);
}

std::vector<std::string> ltGetKeys (lua_State *L, const char *path_str) {
	return ltGetKeysAt (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 = luaL_newstate();
	luaL_openlibs(*L);

	if (luaL_loadfile (*L, filename) || lua_pcall (*L, 0, 1, 0)) {
		bail (*L, "Error running file: ");
		return false;
	}

	return true;
}

void ltClose (lua_State **L) {
	lua_close(*L);
	*L = NULL;
}

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);

	if (!get_table_from_path(L, path_str, index)) 
		return default_result;

	if (!lua_isstring(L, -1)) {
		cout << "Error: value at " << path_str;
		if (index > 0)
			cout << "[" << index << "]";
		cout << " is not a string!" << endl;

		return default_result;
	}

	result = lua_tostring(L, -1);

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return 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);

	if (!get_table_from_path(L, path_str, index)) 
		return default_result;

	if (!lua_isnumber(L, -1)) {
		cout << "Error: value at " << path_str;
		if (index > 0)
			cout << "[" << index << "]";
		cout << " is not a number!" << endl;

		return default_result;
	}

	result = lua_tonumber(L, -1);

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return result;
}

size_t ltGetLengthAt (lua_State *L, const char *path_str, const int index) {
	size_t result = 0;
	
	int stack_top = lua_gettop(L);

	if (!get_table_from_path(L, path_str, index)) 
		return result;

	result = lua_objlen (L, -1);

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return result;
}

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);

	if (!get_table_from_path(L, path_str, index)) 
		return result;

	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 result;
	}

	lua_pushnil(L);

	int i = 1;
	while (lua_next(L, -2) != 0) {
		if (lua_isnumber(L, -2)) {
			// if top value is a number we convert it to a string using lua
			// facilities. Note: lua_tostring modifies the value and can thus
			// confuse the call of lua_next()!
			lua_pushvalue(L, -2);
			result.push_back(lua_tostring(L, -1));
			lua_pop(L, 1);
		} else if (lua_isstring(L, -2)) {
			result.push_back (lua_tostring(L, -2));
		}
		else {
			cout << "Error: values at " << path_str;
			if (index > 0)
				cout << "[" << index << "]";
			cout << " is not a string!" << endl;

			// clean up the stack
			lua_pop (L, lua_gettop(L) - stack_top);
			return std::vector<std::string>();
		}

		lua_pop(L, 1);
		i++;
	}

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return result;
}

std::vector<double> ltGetDoubleVectorAt (lua_State *L, const char *path_str, const int index) {
	std::vector<double> result;
	
	int stack_top = lua_gettop(L);

	if (!get_table_from_path(L, path_str, index)) 
		return result;

	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 result;
	}

	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 std::vector<double>();
		}
		lua_pop(L, 1);
		i++;
	}

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return result;
}

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, const int index) {
	bool result = false;

	int stack_top = lua_gettop(L);

	if (!get_table_from_path(L, path_str, index)) 
		result = false;

	if (lua_isnumber(L, -1)) {
		result = true;
	}

	// clean up the stack
	lua_pop (L, lua_gettop(L) - stack_top);

	return result;
}

bool ltIsExistingAt (lua_State *L, const char *path_str, int index) {
	int stack_top = lua_gettop(L);

	if (!get_table_from_path(L, path_str, index)) {
		return false;
	}

	lua_pop (L, lua_gettop(L) - stack_top);
	return true;
}

/** \brief Extracts a single token from a path string */
static std::string path_get_next_token (std::string &path) {
	std::string token = path;

	bool is_index = false;
	bool have_bracket = false;

	if (token.find(".") != std::string::npos) {
		token = token.substr(0, token.find("."));
	}

	if (token.find("[") != std::string::npos) {
		have_bracket = true;

		if (token.find("[") == 0) {
			token = token.substr (token.find("[") + 1, token.find("]") - 1);
			path = path.substr (token.size() + 2, path.size());
		} else {
			token = token.substr (0, token.find("["));
			path = path.substr (token.size(), path.size());
		}
	} else {
		if (path.size() > token.size())
			path = path.substr (token.size() + 1, path.size());
		else
			path = "";
	}

	if (path[0] == '.')
		path = path.substr (1, path.size());

	return token;
}

/** \brief Puts a lua value at a given path with optional index onto the stack.
 *
 * This function allows to navigate tables by specifying the path to an
 * element just as within lua, e.g. "model.bodies[2].inertia[2][3]". The
 * optional last parameter is used to ease iteration over multiple
 * elements.
 */
bool get_table_from_path (lua_State *L, const char *path_str, const int index) {
	std::string path = path_str;
	std::string token = path;

	// backup of the current stack
	int stack_top = lua_gettop(L);

	do {
		token = path_get_next_token (path);

		istringstream convert (token);
		int token_int;
		if (!(convert >> token_int)) 
			lua_pushstring(L, token.c_str());
		else
			lua_pushnumber (L, token_int);

		lua_gettable (L, -2);

		if (path.size() == 0 && index > 0) {
			lua_pushnumber (L, index);
			lua_gettable (L, -2);
		}

		if (lua_isnil(L, -1)) {
			// clean up the stack
			lua_pop (L, lua_gettop(L) - stack_top);
			return false;
		}

	} while (path.size() > 0);

	return true;
}