Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12870
  • 博文数量: 7
  • 博客积分: 280
  • 博客等级: 二等列兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 17:51
文章分类
文章存档

2008年(7)

我的朋友
最近访客

分类: C/C++

2008-06-04 14:00:09

读取lua中的变量
 

void load(char *filename, int *width, int *height)
{
    lua_State *L = lua_open();
    luaL_openlibs(L);

    if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))
        error(L, "cannot run configuration file: %s", lua_tostring(L, -1));

    lua_getglobal(L, "width");
    lua_getglobal(L, "height");
    if (!lua_isnumber(L, -2))
        error(L, "`width' should be a number\n");
    if (!lua_isnumber(L, -1))
        error(L, "`height' should be a number\n");
    *width = (int)lua_tonumber(L, -2);
    *height = (int)lua_tonumber(L, -1);

    lua_close(L);
}

 
 
执行Lua中的函数
 

void call_va(char *filename, const char *func, const char *sig, ...)
{
    va_list vl;
    int narg, nres;    /* number of arguments and results */

    lua_State *L = lua_open();
    luaL_openlibs(L);

    if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))
        error(L, "cannot run configuration file: %s", lua_tostring(L, -1));

    va_start(vl, sig);
    lua_getglobal(L, func);    /* get function */

    /* push arguments */
    narg = 0;
    while (*sig)
    {    /* push arguments */
        switch (*sig++)
        {
        case 'd':    /* double argument */
            lua_pushnumber(L, va_arg(vl, double));
            break;

        case 'i':    /* int argument */
            lua_pushnumber(L, va_arg(vl, int));
            break;

        case 's':    /* string argument */
            lua_pushstring(L, va_arg(vl, char *));
            break;

        case '>':
            goto endwhile;

        default:
            error(L, "invalid option (%c)", *(sig - 1));
        }
        narg++;
        luaL_checkstack(L, 1, "too many arguments");
    } endwhile:

    /* do the call */
    nres = strlen(sig);    /* number of expected results */
    if (lua_pcall(L, narg, nres, 0) != 0)    /* do the call */
        error(L, "error running function `%s': %s", func, lua_tostring(L, -1));

    /* retrieve results */
    nres = -nres;        /* stack index of first result */
    while (*sig)
    {    /* get results */
        switch (*sig++)
        {
        case 'd':    /* double result */
            if (!lua_isnumber(L, nres))
                error(L, "wrong result type");
            *va_arg(vl, double *) = lua_tonumber(L, nres);
            break;

        case 'i':    /* int result */
            if (!lua_isnumber(L, nres))
                error(L, "wrong result type");
            *va_arg(vl, int *) = (int)lua_tonumber(L, nres);
            break;

        case 's':    /* string result */
            if (!lua_isstring(L, nres))
                error(L, "wrong result type");
            *va_arg(vl, const char **) = lua_tostring(L, nres);
            break;

        default:
            error(L, "invalid option (%c)", *(sig - 1));
        }
        nres++;
    }
    va_end(vl);

    lua_close(L);
}

阅读(662) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~