first commit
This commit is contained in:
commit
f02530e13e
2 changed files with 267 additions and 0 deletions
174
tinybindings.cpp
Normal file
174
tinybindings.cpp
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
#include <tiny/tdf.h>
|
||||
#include <lua5.1/lua.h>
|
||||
#include <lua5.1/lualib.h>
|
||||
#include <lua5.1/lauxlib.h>
|
||||
|
||||
#define TDFLUASTART (lua_State*L) { tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1); std::vector<std::string>path;\
|
||||
int n=lua_gettop(L); for(int i=2;i<=n;i++){ path.push_back(luaL_checkstring(L,i)); }try{
|
||||
|
||||
#define TDFLUAEND }catch(tiny::TDF_ERR&e){lua_pushnil(L);lua_pushstring(L,e.what());return 2;}return 1;}
|
||||
|
||||
|
||||
#define TDFLUASETSTART (lua_State*L) { tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1); std::vector<std::string>path;\
|
||||
int n=lua_gettop(L);for(int i=2;i<n;i++){ path.push_back(luaL_checkstring(L,i)); }try{
|
||||
|
||||
#define TDFLUASETEND return 0; }catch(tiny::TDF_ERR&e){lua_pushstring(L,e.what());lua_error(L);return 0; }}
|
||||
|
||||
|
||||
static int luatdfread(lua_State*L){
|
||||
const char*filepath=luaL_checkstring(L,1);
|
||||
tiny::TDF_FILE*file=new tiny::TDF_FILE();
|
||||
file->filepath=(char*)filepath;
|
||||
try{
|
||||
file->read();
|
||||
lua_pushlightuserdata(L,file);
|
||||
return 1;
|
||||
}catch(tiny::TDF_ERR&e){
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L,e.what());
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
static int luatdfsave(lua_State*L){
|
||||
tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1);
|
||||
file->save();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int luatdfclose(lua_State*L){
|
||||
tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1);
|
||||
file->close();
|
||||
delete file;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//get
|
||||
|
||||
|
||||
static int luatdfgetint
|
||||
TDFLUASTART int val=file->getint(path); lua_pushinteger(L,val); TDFLUAEND
|
||||
|
||||
static int luatdfgetfloat
|
||||
TDFLUASTART float val=file->getfloat(path); lua_pushnumber(L,val); TDFLUAEND
|
||||
|
||||
static int luatdfgetstring
|
||||
TDFLUASTART std::string val=file->getstring(path); lua_pushstring(L,val.c_str()); TDFLUAEND
|
||||
|
||||
static int luatdfgetbool
|
||||
TDFLUASTART bool val=file->getbool(path); lua_pushboolean(L,val); TDFLUAEND
|
||||
|
||||
static int luatdfgetchar
|
||||
TDFLUASTART char val=file->getchar(path); char str[2]={val,0}; lua_pushstring(L,str); TDFLUAEND
|
||||
|
||||
static int luatdfgetpointer
|
||||
TDFLUASTART std::vector<std::string>val=file->getpointer(path); lua_newtable(L); for(size_t i=0;i<val.size();i++){ lua_pushstring(L,val[i].c_str()); lua_rawseti(L,-2,i+1);} TDFLUAEND
|
||||
|
||||
static int luatdfgetclass
|
||||
TDFLUASTART boost::unordered_map<std::string,tiny::TDF_DATA>*val=file->getclass(path); lua_pushlightuserdata(L,val); TDFLUAEND
|
||||
|
||||
|
||||
//set
|
||||
|
||||
|
||||
static int luatdfsetint
|
||||
TDFLUASETSTART int val=luaL_checkinteger(L,n); file->setint(path,val); TDFLUASETEND
|
||||
|
||||
static int luatdfsetfloat
|
||||
TDFLUASETSTART float val=luaL_checknumber(L,n); file->setfloat(path,val); TDFLUASETEND
|
||||
|
||||
static int luatdfsetstring
|
||||
TDFLUASETSTART const char*val=luaL_checkstring(L,n); file->setstring(path,val); TDFLUASETEND
|
||||
|
||||
static int luatdfsetblock
|
||||
TDFLUASETSTART const char*val=luaL_checkstring(L,n); file->setblock(path,val); TDFLUASETEND
|
||||
|
||||
static int luatdfsetbool
|
||||
TDFLUASETSTART bool val=lua_toboolean(L,n); file->setbool(path,val); TDFLUASETEND
|
||||
|
||||
static int luatdfsetchar
|
||||
TDFLUASETSTART const char*str=luaL_checkstring(L,n); file->setchar(path,str[0]); TDFLUASETEND
|
||||
|
||||
|
||||
|
||||
static int luatdfdefined(lua_State*L){
|
||||
tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1);
|
||||
std::vector<std::string>path;
|
||||
int n=lua_gettop(L);
|
||||
for(int i=2;i<=n;i++){
|
||||
path.push_back(luaL_checkstring(L,i));
|
||||
}
|
||||
bool val=file->defined(path);
|
||||
lua_pushboolean(L,val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int luatdfsetpointer(lua_State*L){
|
||||
tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1);
|
||||
std::vector<std::string>path;
|
||||
int n=lua_gettop(L);
|
||||
for(int i=2;i<n;i++){
|
||||
path.push_back(luaL_checkstring(L,i));
|
||||
}
|
||||
luaL_checktype(L,n,LUA_TTABLE);
|
||||
std::vector<std::string>val;
|
||||
int len=lua_objlen(L,n);
|
||||
for(int i=1;i<=len;i++){
|
||||
lua_rawgeti(L,n,i);
|
||||
val.push_back(luaL_checkstring(L,-1));
|
||||
lua_pop(L,1);
|
||||
}
|
||||
try{
|
||||
file->setpointer(path,val);
|
||||
return 0;
|
||||
}catch(tiny::TDF_ERR&e){
|
||||
lua_pushstring(L,e.what());
|
||||
lua_error(L);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int luatdfdefine(lua_State*L){
|
||||
tiny::TDF_FILE*file=(tiny::TDF_FILE*)lua_touserdata(L,1);
|
||||
luaL_checktype(L,2,LUA_TTABLE);
|
||||
const char*name=luaL_checkstring(L,3);
|
||||
std::vector<std::string>defines;
|
||||
int len=lua_objlen(L,2);
|
||||
for(int i=1;i<=len;i++){
|
||||
lua_rawgeti(L,2,i);
|
||||
defines.push_back(luaL_checkstring(L,-1));
|
||||
lua_pop(L,1);
|
||||
}
|
||||
file->define(defines,(char*)name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg tdflib[]={
|
||||
{"read",luatdfread},
|
||||
{"close",luatdfclose},
|
||||
{"getint",luatdfgetint},
|
||||
{"getfloat",luatdfgetfloat},
|
||||
{"getstring",luatdfgetstring},
|
||||
{"getbool",luatdfgetbool},
|
||||
{"getchar",luatdfgetchar},
|
||||
{"getpointer",luatdfgetpointer},
|
||||
{"getclass",luatdfgetclass},
|
||||
{"defined",luatdfdefined},
|
||||
{"setint",luatdfsetint},
|
||||
{"setfloat",luatdfsetfloat},
|
||||
{"setstring",luatdfsetstring},
|
||||
{"setblock",luatdfsetblock},
|
||||
{"setbool",luatdfsetbool},
|
||||
{"setchar",luatdfsetchar},
|
||||
{"setpointer",luatdfsetpointer},
|
||||
{"define",luatdfdefine},
|
||||
{"save",luatdfsave},
|
||||
{NULL,NULL}
|
||||
};
|
||||
|
||||
extern "C" int luaopentdf(lua_State*L){
|
||||
luaL_register(L,"tdf",tdflib);
|
||||
return 1;
|
||||
}
|
||||
93
tinylua.lua
Normal file
93
tinylua.lua
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
--tiny lua class
|
||||
|
||||
|
||||
local tdf = require("tdf")
|
||||
|
||||
local filemt = {}
|
||||
|
||||
function filemt:close()
|
||||
tdf.close(self.handle)
|
||||
end
|
||||
|
||||
function filemt:save()
|
||||
tdf.save(self.handle)
|
||||
end
|
||||
|
||||
function filemt:defined(path)
|
||||
return tdf.defined(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:getint(path)
|
||||
return tdf.getint(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:getfloat(path)
|
||||
return tdf.getfloat(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:getstring(path)
|
||||
return tdf.getstring(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:getbool(path)
|
||||
return tdf.getbool(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:getchar(path)
|
||||
local c = tdf.getchar(self.handle, unpack(path))
|
||||
return string.byte(c)
|
||||
end
|
||||
|
||||
function filemt:getpointer(path)
|
||||
return {tdf.getpointer(self.handle, unpack(path))}
|
||||
end
|
||||
|
||||
function filemt:getclass(path)
|
||||
return tdf.getclass(self.handle, unpack(path))
|
||||
end
|
||||
|
||||
function filemt:setint(path, value)
|
||||
tdf.setint(self.handle, unpack(path), value)
|
||||
end
|
||||
|
||||
function filemt:setfloat(path, value)
|
||||
tdf.setfloat(self.handle, unpack(path), value)
|
||||
end
|
||||
|
||||
function filemt:setstring(path, value)
|
||||
tdf.setstring(self.handle, unpack(path), value)
|
||||
end
|
||||
|
||||
function filemt:setblock(path, value)
|
||||
tdf.setblock(self.handle, unpack(path), value)
|
||||
end
|
||||
|
||||
function filemt:setbool(path, value)
|
||||
tdf.setbool(self.handle, unpack(path), value)
|
||||
end
|
||||
|
||||
function filemt:setchar(path, value)
|
||||
tdf.setchar(self.handle, unpack(path), string.char(value))
|
||||
end
|
||||
|
||||
function filemt:setpointer(path, tbl)
|
||||
tdf.setpointer(self.handle, unpack(path), unpack(tbl))
|
||||
end
|
||||
|
||||
function filemt:define(names, classname)
|
||||
tdf.define(self.handle, names, classname)
|
||||
end
|
||||
|
||||
local function open(filepath)
|
||||
local ok, handle = pcall(tdf.read, filepath)
|
||||
if not ok then
|
||||
error(handle)
|
||||
end
|
||||
local obj = {handle = handle}
|
||||
setmetatable(obj, {__index = filemt})
|
||||
return obj
|
||||
end
|
||||
|
||||
return {
|
||||
open = open
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue