Lua 基础

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值