Lua 基础

来源:互联网 发布:淘宝促销海报 编辑:程序博客网 时间:2024/06/05 15:51
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "lua.h"#include "lualib.h"#include "lauxlib.h"#include <stdarg.h>#define config_file "config.lua";//#pragma comment(lib, "Lua.lib")void load(lua_State* L, const char* fname, int* w, int* h);void stackDump(lua_State* L);void GetColor();char* getfield_1(lua_State* L, char* key);void Fun();void call_va(const char* func, const char* sig, ...);int main(){Fun();getchar();return 0;}void aaa(){lua_State* L = luaL_newstate();luaL_openlibs(L);lua_pushnil(L);int topindex = lua_gettop(L);printf("栈顶元素索引:%d\n", topindex);stackDump(L);lua_pushfstring(L, "dsdfs", "sdfsdfsdf");topindex = lua_gettop(L);printf("栈顶元素索引:%d\n", topindex);stackDump(L);lua_pushinteger(L, 100);topindex = lua_gettop(L);printf("栈顶元素索引:%d\n", topindex);stackDump(L);lua_pushnil(L);topindex = lua_gettop(L);printf("栈顶元素索引:%d\n", topindex);stackDump(L);lua_pushnil(L);topindex = lua_gettop(L);printf("栈顶元素索引:%d\n", topindex);stackDump(L);lua_close(L);}//遍历栈中所有元素void stackDump(lua_State* L){int i;int top = lua_gettop(L);for(int i=1;i<=top;i++){int t = lua_type(L, i);switch (t){case LUA_TSTRING:{printf("%s", lua_tostring(L, i));break;}case LUA_TBOOLEAN:{printf(lua_toboolean(L, i) ? "true" : "false");break;}case LUA_TNUMBER:{printf("%g", lua_tonumber(L, i));break;}default:printf("%s", lua_typename(L, t));break;}printf("\n");}}//值类型操作void load(lua_State* L,const char* fname,int* w,int* h){if(luaL_loadfile(L,fname) || lua_pcall(L,0,0,0)){luaL_error(L, "cannot run config.file:%s", lua_tostring(L, -1));}lua_getglobal(L,"width");lua_getglobal(L,"height");if(!lua_isnumber(L,-2)){luaL_error(L, "should be a number:%s\n", lua_tostring(L, -1));}if (!lua_isnumber(L, -1)){luaL_error(L, "should be a number:%s\n", lua_tostring(L, -1));}*w = lua_tointeger(L,-2);*h = lua_tointeger(L,-1);}void GetColor(){lua_State* L = luaL_newstate();luaL_openlibs(L);if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot run config.file:%s", lua_tostring(L, -1));}int r= get_table(L,"background","r");printf("rgb r:%d",r);lua_close(L);}int get_table(lua_State* L,char* tbName,char* key){lua_getglobal(L,tbName);if(!lua_istable(L,-1)){lua_error(L,strcat(tbName," is not find"));}//double r= atof(getfield_1(L, key))*255;int value=lua_getfield(L,-1,key);printf("%f\n",value);return value;}#define MAX_COLOR 255//假设table位于栈顶int getfield(lua_State* L,const char* key){int result;lua_pushstring(L,key);lua_gettable(L,-2);if(!lua_isnumber(L,-1)){lua_error(L,"invalid component in background color");}result = (int)(lua_tonumber(L, -1)*MAX_COLOR);lua_pop(L,1);return result;}char* getfield_1(lua_State* L, char* key){lua_pushstring(L, key);lua_gettable(L, -2);if (lua_istable(L,-1)){lua_error(L, "invalid component in background color");}char* result = lua_tostring(L,-1);int length = lua_rawlen(L, -1);printf("string=%s,length=%d\n",result,length);lua_pop(L,-1);return result;}struct ColorTable{char* name;unsigned char red, green, blue;}colortable[]={{"WHITE",MAX_COLOR,MAX_COLOR,MAX_COLOR},{ "RED",MAX_COLOR,0,0},{ "GREEN",0,MAX_COLOR,0 },{ "BLUE",0,0,MAX_COLOR },{ "NULL",0,0,0}};void setfield(lua_State* L,const char* index,int value){lua_pushstring(L,index);lua_pushnumber(L,value);lua_settable(L,-3);lua_pushnumber(L,(double)value/MAX_COLOR);lua_setfield(L,-2,index);}void setcolor(lua_State* L, struct ColorTable* ct){lua_newtable(L);setfield(L,"r",ct->red);setfield(L, "g", ct->green);setfield(L, "b", ct->blue);lua_setglobal(L,ct->name);}void setAllColor(){lua_State* L = luaL_newstate();int i = 0;while (colortable[i].name!=NULL){setcolor(L,&colortable[i++]);}}void getcolor(lua_State* L,char* colorname){lua_getglobal(L, "background");if(lua_isstring(L,-1)){const char* name = lua_tostring(L, -1);int i=0;for(int i=0;colortable[i].name!=NULL;i++){if(strcmp(colorname,colortable[i].name)==0){break;}}if(colortable[i].name==NULL){lua_error(L, "color name is null error:%s", colorname);}else{int red = colortable[i].red;int green = colortable[i].green;int blue = colortable[i].blue;}}else if(lua_istable(L,-1)){int red = getfield(L, "r");int green = getfield(L, "g");int blue = getfield(L,"b");}else{lua_error(L, "invalid value for 'background'");}}void Fun(){lua_State* L = luaL_newstate();luaL_openlibs(L);if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot run config.file:%s", lua_tostring(L, -1));return;}double a;call_va(L,"xk_fun","dd>d",100.0,20.0,&a);printf("result=%d",a);lua_close(L);}void call_va(lua_State* L,const char* func,const char* sig,...){va_list vl;int narg, nres;va_start(vl, sig);lua_getglobal(L,func);for(narg=0;(*sig)!=0;narg++){lua_checkstack(L, 1, "too many aruments");switch (*sig++){case 'd':lua_pushnumber(L, va_arg(vl, double));break;case 'i':lua_pushinteger(L, va_arg(vl, int));break;case 's':lua_pushstring(L,va_arg(vl,char*));break;case '>':goto endargs;default:lua_error(L, "invalid optioanl(%c)", *(sig - 1));break;}}endargs:{nres = strlen(sig);if (lua_pcall(L, narg, nres, 0) != 0){lua_error(L, "error calling '%s':%s", func, lua_tostring(L, -1));}nres = -nres;while (*sig){switch (*sig++){case 'd':if (!lua_isnumber(L, nres)){lua_error(L, "wrong result type");}*va_arg(vl, double*) = lua_tonumber(L, nres);break;case 'i':if (!lua_isinteger(L, nres)){lua_error(L, "wrong result type");}*va_arg(vl, int*) = lua_tointeger(L, nres);break;case 's':if (!lua_isstring(L, nres)){lua_error(L, "wrong result type");}*va_arg(vl, const char**) = lua_tostring(L, nres);break;default:lua_error(L, "invalid option (%c)", *(sig - 1));break;}nres++;}va_end(vl);}}














0 0
原创粉丝点击