用lua做配置文件
可以用lua做程序的配置文件。LuaPlus提供了读写lua脚本文件的功能,我们可以将配置信息以Table的形式存储在lua脚本中,然后利用LuaPlus进行读取和更改。
一、创建配置文件
配置文件可以用文字编辑器创建,也可以在C++代码中创建。我们先在C++中创建。利用CreateTable创建一个名为"Window"的表格,利用LuaObject的Set*函数设置表格内容,最后用DumpObject函数保存到文件。代码如下:
void
SaveTable()
{
LuaStateOwnerstate;
LuaObjectmyTable = state -> GetGlobals().CreateTable( " Window " );
myTable.SetInteger( " width " , 640 );
myTable.SetInteger( " height " , 480 );
myTable.SetString( " title " , " MyFirstWindow " );
myTable.SetBoolean( " enabled " , true );
myTable.SetInteger( " alpha " , 128 );
myTable.SetString( " backgroundimage " , " bg.jpg " );
state -> DumpObject( " FirstWindow.lua " , " Window " ,state -> GetGlobals()[ " Window " ], 0 );
}
{
LuaStateOwnerstate;
LuaObjectmyTable = state -> GetGlobals().CreateTable( " Window " );
myTable.SetInteger( " width " , 640 );
myTable.SetInteger( " height " , 480 );
myTable.SetString( " title " , " MyFirstWindow " );
myTable.SetBoolean( " enabled " , true );
myTable.SetInteger( " alpha " , 128 );
myTable.SetString( " backgroundimage " , " bg.jpg " );
state -> DumpObject( " FirstWindow.lua " , " Window " ,state -> GetGlobals()[ " Window " ], 0 );
}
二、读取配置文件
Lua配置文件的加载和其它脚本文件一样,可以用DoFile函数,然后可以用GetGlobals加索引的形式获取表格LuaObject对象,最后用[ ]访问表格的元素。代码如下:
void
ReadShowTable()
{
LuaStateOwnerstate;
state -> DoFile( " FirstWindow.lua " );
LuaObjectmyTable = state -> GetGlobals()[ " Window " ];
// getWindowTable'sitem
printf( " WindowTableinFirstWindow.lua: " );
printf( " width=%d " ,myTable[ " width " ].GetInteger());
printf( " height=%d " ,myTable[ " height " ].GetInteger());
printf( " title=%s " ,myTable[ " title " ].GetString());
printf( " enabled=%s " ,myTable[ " enabled " ].GetBoolean() ? " true " : " false " );
printf( " alpha=%d " ,myTable[ " alpha " ].GetInteger());
printf( " backgroundimage=%s " ,myTable[ " backgroundimage " ].GetString());
}
{
LuaStateOwnerstate;
state -> DoFile( " FirstWindow.lua " );
LuaObjectmyTable = state -> GetGlobals()[ " Window " ];
// getWindowTable'sitem
printf( " WindowTableinFirstWindow.lua: " );
printf( " width=%d " ,myTable[ " width " ].GetInteger());
printf( " height=%d " ,myTable[ " height " ].GetInteger());
printf( " title=%s " ,myTable[ " title " ].GetString());
printf( " enabled=%s " ,myTable[ " enabled " ].GetBoolean() ? " true " : " false " );
printf( " alpha=%d " ,myTable[ " alpha " ].GetInteger());
printf( " backgroundimage=%s " ,myTable[ " backgroundimage " ].GetString());
}
三、更改配置文件
读取配置文件的过程和上一节类似,更改与保存和第一节类似。代码如下:
void
RewriteTableContents()
{
LuaStateOwnerstate;
state -> DoFile( " FirstWindow.lua " );
LuaObjectmyTable = state -> GetGlobals()[ " Window " ];
myTable.SetInteger( " width " , 800 );
myTable.SetInteger( " height " , 600 );
printf( " newwidth=%d,height=%d " ,myTable[ " width " ].GetInteger(),myTable[ " height " ].GetInteger());
state -> DumpObject( " FirstWindow.lua " , " Window " ,state -> GetGlobals()[ " Window " ]);
}
{
LuaStateOwnerstate;
state -> DoFile( " FirstWindow.lua " );
LuaObjectmyTable = state -> GetGlobals()[ " Window " ];
myTable.SetInteger( " width " , 800 );
myTable.SetInteger( " height " , 600 );
printf( " newwidth=%d,height=%d " ,myTable[ " width " ].GetInteger(),myTable[ " height " ].GetInteger());
state -> DumpObject( " FirstWindow.lua " , " Window " ,state -> GetGlobals()[ " Window " ]);
}
下面是main函数:
int
_tmain(
int
argc,_TCHAR
*
argv[])
{
printf( " nowcreatetableandsaveinFirstWindow.lua: " );
SaveTable();
printf( " nowreadtablefromFirstWindow.lua: " );
ReadShowTable();
printf( " nowretrievethetable,changethe"width"and"height"value,thensave: " );
RewriteTableContents();
printf( " now,re-readfromFirstWindow.lua: " );
ReadShowTable();
return 0 ;
}
{
printf( " nowcreatetableandsaveinFirstWindow.lua: " );
SaveTable();
printf( " nowreadtablefromFirstWindow.lua: " );
ReadShowTable();
printf( " nowretrievethetable,changethe"width"and"height"value,thensave: " );
RewriteTableContents();
printf( " now,re-readfromFirstWindow.lua: " );
ReadShowTable();
return 0 ;
}
工程的设置和上一篇文章一样。
F7编译后CTRL+F5即可看到运行结果。
值得注意的是,table可以嵌套table。这样我们便可以实现复杂的配置文件。