Source: src/cl_luawrapper.cc
|
|
|
|
//some LUA extenstions
#include "cl_luawrapper.h"
void LUA_WRAPPER::init()
{
lstate = lua_open();
luaopen_base(lstate);
luaopen_table(lstate);
luaopen_io(lstate);
luaopen_string(lstate);
luaopen_math(lstate);
luaopen_debug(lstate);
help_data.clear();
help_desc.clear();
lua_atpanic(lstate, lpanic);
}
void LUA_WRAPPER::done()
{
lua_close(lstate);
}
void LUA_WRAPPER::reg(int (*f)(lua_State*),const char *name)
{
reg(name, f);
}
void LUA_WRAPPER::reg(const char *name, int (*f)(lua_State*))
{
lua_register(lstate, name, f);
help_data[name] = "No help";
help_desc[name] = "";
}
void LUA_WRAPPER::reg(int (*f)(lua_State*), const char *name, string dsc, string hlp)
{
reg(name, f, dsc, hlp);
}
void LUA_WRAPPER::reg(const char *name, int (*f)(lua_State*), string dsc, string hlp)
{
lua_register(lstate, name, f);
help_desc[name] = dsc;
help_data[name] = hlp;
}
string LUA_WRAPPER::list_functions()
{
string result;
map ::iterator i;
for(i=help_data.begin(); i!=help_data.end(); i++) {
result = result + i->first;
result = result + "\n";
}
return result;
}
string LUA_WRAPPER::list_desc_functions()
{
string result;
map ::iterator i;
for(i=help_data.begin(); i!=help_data.end(); i++) {
result = result + i->first + "\t" +
help_desc[i->first] + "\n";
}
return result;
}
string LUA_WRAPPER::help_function(string s)
{
string result;
result = s + "\t" + help_desc[s] + "\n" +
help_data[s] + "\n";
return result;
}
int LUA_WRAPPER::get_int(char *name)
{
return lget_int(lstate, name);
}
//string variable
const char *LUA_WRAPPER::get_str(char *name)
{
return lget_str(lstate, name);
}
//string variable from array
const char *LUA_WRAPPER::get_str(char *name, int index)
{
return lget_str(lstate, name, index);
}
void LUA_WRAPPER::dofile( const char * filename )
{
lua_dofile(lstate, filename);
}
int LUA_WRAPPER::do_alleg_file(string fname)
{
PACKFILE *fp;
char buf[LUA_MAX_BUF];
char n[LUA_MAX_STACK];
long int size;
fp = pack_fopen(fname.c_str(), "r");
if(!fp) {
error("Unable to open %s",fname.c_str());
return 0;
}
size = pack_fread(buf, LUA_MAX_BUF, fp);
pack_fclose(fp);
lua_dobuffer(lstate, buf, size, n);
return 1;
}
void LUA_WRAPPER::exec(char *cmd)
{
lua_dobuffer(lstate, cmd, strlen(cmd), cmd);
}
int LUA_WRAPPER::argc() //return no. of arguments
{
return lget_argc(lstate);
}
string LUA_WRAPPER::get_str()
{
string s;
s = lget_arg_str(lstate);
return s;
}
int LUA_WRAPPER::get_int()
{
int i;
i = lget_arg_int(lstate);
return i;
}
Generated by: georgik on armada on Sat Jul 24 07:07:15 2004, using kdoc 2.0a54. |