Hi,
My requirement is to load an embedded lua script file using C api. I am
unable to load the script file using C apis like lual_loadfile or
lual_dofile because the path is not properly setup as I use relative
names. I have tried adding my path to package.path but still i get
"Cannot open <file>" error. I cannot modify luaconf.h file so changing
LUA_DEFAULT_PATH is out of question.
I set package.path using C-APIs & then try to use relative filenames but
it doesn't work. Is there any other way of specifying the path (from C)
so that lua C apis work with relative names? Absolute path works fine.
I can see that the code sets the package.path to
"./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua*;/path/to/mydir/?.lua;*
but still luaL_loadfile fails... [1]
My code is based on [2].
      37     L = luaL_newstate();
      38     luaL_openlibs(L); /* Load Lua libraries */
      39     setLuaPath(L, "/path/to/mydir/");
      40     status = luaL_loadfile(L, "my.lua");
      41     if (status) {
      42         printf("Couldn't load file: %s\n", lua_tostring(L, -1));
      43         exit(1);
      44     }
      48 int setLuaPath( lua_State* L, const char* path )
      49 {
      50     char *cur_path;
      51     size_t len;
      52     char* mypath;
      53     lua_getglobal( L, "package" );
      54     lua_getfield( L, -1, "path" );
      55     cur_path = lua_tostring( L, -1);
      56     mypath = malloc(strlen(cur_path) + strlen(path) + 9);
      57     strcpy(mypath,cur_path);
      58     strcat(mypath,";");
      59     strcat(mypath,path);
      60     strcat(mypath,"?.lua;");
      61     lua_pop( L, 1 );
      62     lua_pushstring( L, mypath) ;
      63     lua_setfield( L, -2, "path" );
      64     lua_pop( L, 1 );
      65     return 0;
      66 }
[1] http://pastebin.com/xPjx8Vpy
[2]
http://stackoverflow.com/questions/4125971/setting-the-global-lua-path-variable-from-c-c
Thanks for your time,
Prasad