Advertisement
voxner

Untitled

May 30th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #include <lua.h>
  6. #include <lauxlib.h>
  7. #include <lualib.h>
  8.  
  9.  
  10. int main(){
  11.  
  12.     int status = 0;
  13.  
  14.     lua_State *L;
  15.  
  16.     /*
  17.      * All Lua contexts are held in this structure. We work with it almost
  18.      * all the time.
  19.      */
  20.     L = luaL_newstate();
  21.  
  22.     luaL_openlibs(L); /* Load Lua libraries */
  23.  
  24.     /* Load the file containing the script we are going to run */
  25.  
  26.     setLuaPath(L, "/path/to/mydir/");
  27.  
  28.     status = luaL_loadfile(L, "my.lua");
  29.     printf("%s:%d: Reached status %d\n",__func__, __LINE__,
  30.             status);
  31.     if (status) {
  32.         /* If something went wrong, error message is at the top of */
  33.         /* the stack */
  34.         printf("Couldn't load file: %s\n", lua_tostring(L, -1));
  35.         exit(1);
  36.     }
  37.     return ;
  38. }       /* -----  end of function get_radio1_profilename  ----- */
  39.  
  40. int setLuaPath( lua_State* L, const char* path )
  41. {
  42.     char *cur_path;
  43.     size_t len;
  44.     char* mypath;
  45.  
  46.     lua_getglobal( L, "package" );
  47.     lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
  48.  
  49.     cur_path = lua_tostring( L, -1); // grab path string from top of stack
  50.     mypath = malloc(strlen(cur_path) + strlen(path) + 9);
  51.     strcpy(mypath,cur_path);
  52.     strcat(mypath,";");
  53.     strcat(mypath,path);
  54.     strcat(mypath,"?.lua;");
  55.  
  56.     lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
  57.     lua_pushstring( L, mypath) ;
  58.     lua_setfield( L, -2, "path" );
  59.     lua_pop( L, 1 );
  60.     return 0; // all done!
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement