Pushok icon

My little lua module

Pushok | PRO | 04/20/11 11:56:35 AM UTC | 0 ⭐ | 538 👁️ | Never ⏰ | []
C |

1.34 KB

|

None

|

0 👍

/

0 👎

#include "windows.h"
 
extern "C" {
#include "math.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "lua/lua.h"
#include "lua/lauxlib.h"
#include "lua/lualib.h"
}
#define PROJECT_TABLENAME "luamodule"
#ifdef WIN32
#define LUA_API __declspec(dllexport)
#else
#define LUA_API
#endif
 
const char* DecToBin(lua_State *Lua, double num)
{
    if(num == 0) return "0";
    char *bt = new char[1024];
    bt[0] = '\0';
    num = (long long) num;
    while(1)
    {
        lua_pushnumber(Lua, (int)num % 2);
        strcat(bt,lua_tostring(Lua, lua_gettop(Lua)));
        if(((double)num / 2) == 0.5)
        {
            for(int i=0,j=strlen(bt)-1;i<floor(double(strlen(bt)/2));i++,j--)
            {
                char it = bt[i];
                bt[i] = bt[j];
                bt[j] = it;
            }
            return bt;
        }
        num = floor(double(num/2));
    }
}
 
extern "C" {
    int LUA_API luaopen_luamodule (lua_State *L);
}
 
static int sleepLua (lua_State *L) {
    Sleep((DWORD)lua_tonumber(L, 1));
    return 0;
}
 
static int dosomething(lua_State *L)
{
    lua_createtable(L, 1,1);
    return 0;
}
 
static int dectobin (lua_State *L) {
    lua_pushstring(L, DecToBin(L, lua_tonumber(L, 1)));
    return 1;
}
 
int LUA_API luaopen_luamodule (lua_State *L) {
    struct luaL_reg driver[] = {
        {"sleepLua", sleepLua}, 
        {"dectobin", dectobin},
        {NULL, NULL},
    };
    luaL_openlib (L, "luamodule", driver, 0);
    return 1;
}

Comments