Re: Is there a better way to do this?
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:vJq6k.3$Vq.2@newsfe05.lga...
I am trying to write an interface to a LUA compiler in C++. I know there
are some out there, I downloaded 5 or 6 and couldn't get any to work
correctly (they would compile in DevC++, not MSVC++, wouldn't work with LUA
library I had, etc...) so I'm having to write my own.
Right now I'm working on trying to simply run a lua file with parameters.
Steps are not that difficult.
1. Load the lua file
2. Push the parameters using functions
3. Execute the code.
Well I'm trying to make this simple, and optimally I'd like something
like:
Lua L;
L.Execute( "MyFile.lua", "parm1", "parm2", "parm3" );
Of course the number of the paramters is not fixed, but va_arg is a
nightmare and it is suggested not to do it in C++. So far I've come close
with:
LuaExecute( L, "MyFile.lua")( "parm1" )( "parm2" )( "parm3" );
which is kinda ugly and could use some improvments. Any suggestions?
[SNIP]
For now I've decided to use this ugly to code, easy to use kludge:
int ExecuteFile( const std::string& FileName, const std::string& Parm1 =
"\xFF", const std::string& Parm2 = "\xFF",
const std::string& Parm3 = "\xFF", const std::string& Parm4 =
"\xFF", const std::string& Parm5 = "\xFF")
{
int ReturnCode = luaL_loadfile( L, FileName.c_str() );
if ( ReturnCode )
{
std::cerr << "Error loading file: " << FileName << "\n";
return ReturnCode;
}
int ParmCount = 0;
if ( Parm1 != "\xFF" )
{
lua_pushstring( L, Parm1.c_str() );
++ParmCount;
}
if ( Parm2 != "\xFF" )
{
lua_pushstring( L, Parm2.c_str() );
++ParmCount;
}
if ( Parm3 != "\xFF" )
{
lua_pushstring( L, Parm3.c_str() );
++ParmCount;
}
if ( Parm4 != "\xFF" )
{
lua_pushstring( L, Parm4.c_str() );
++ParmCount;
}
if ( Parm5 != "\xFF" )
{
lua_pushstring( L, Parm5.c_str() );
++ParmCount;
}
ReturnCode = lua_pcall(L, ParmCount, LUA_MULTRET, 0);
if ( ReturnCode )
std::cerr << "Error executing file: " << FileName << "\n";
return ReturnCode;
}
If in the future I get an error because I'm trying to use 6 parameters, I'll
just modify the code to add Parm6
I'm not happy with this, but it works.