Pushok

My little lua module

Apr 20th, 2011
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include "windows.h"
  2.  
  3. extern "C" {
  4. #include "math.h"
  5. #include "string.h"
  6. #include "stdio.h"
  7. #include "stdlib.h"
  8. #include "lua/lua.h"
  9. #include "lua/lauxlib.h"
  10. #include "lua/lualib.h"
  11. }
  12. #define PROJECT_TABLENAME "luamodule"
  13. #ifdef WIN32
  14. #define LUA_API __declspec(dllexport)
  15. #else
  16. #define LUA_API
  17. #endif
  18.  
  19. const char* DecToBin(lua_State *Lua, double num)
  20. {
  21.     if(num == 0) return "0";
  22.     char *bt = new char[1024];
  23.     bt[0] = '\0';
  24.     num = (long long) num;
  25.     while(1)
  26.     {
  27.         lua_pushnumber(Lua, (int)num % 2);
  28.         strcat(bt,lua_tostring(Lua, lua_gettop(Lua)));
  29.         if(((double)num / 2) == 0.5)
  30.         {
  31.             for(int i=0,j=strlen(bt)-1;i<floor(double(strlen(bt)/2));i++,j--)
  32.             {
  33.                 char it = bt[i];
  34.                 bt[i] = bt[j];
  35.                 bt[j] = it;
  36.             }
  37.             return bt;
  38.         }
  39.         num = floor(double(num/2));
  40.     }
  41. }
  42.  
  43. extern "C" {
  44.     int LUA_API luaopen_luamodule (lua_State *L);
  45. }
  46.  
  47. static int sleepLua (lua_State *L) {
  48.     Sleep((DWORD)lua_tonumber(L, 1));
  49.     return 0;
  50. }
  51.  
  52. static int dosomething(lua_State *L)
  53. {
  54.     lua_createtable(L, 1,1);
  55.     return 0;
  56. }
  57.  
  58. static int dectobin (lua_State *L) {
  59.     lua_pushstring(L, DecToBin(L, lua_tonumber(L, 1)));
  60.     return 1;
  61. }
  62.  
  63. int LUA_API luaopen_luamodule (lua_State *L) {
  64.     struct luaL_reg driver[] = {
  65.         {"sleepLua", sleepLua},
  66.         {"dectobin", dectobin},
  67.         {NULL, NULL},
  68.     };
  69.     luaL_openlib (L, "luamodule", driver, 0);
  70.     return 1;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment