|
|
Delphi 对象封装 for Delphi
演示封装一个DELPHI的TMemIniFile(内存INI文件类)对象到Lua的完整代码
封装后的类名称为 TIniFile
对象方法为:
TIniFile.Open(FileName) - 打开INI,成功返回TRUE
TIniFile.Clear() - 清空INI文件
TIniFile.UpdateFile() - 更新到磁盘文件
TIniFile.DeleteKey(Section, Ident) - 删除指定节下的指定键
TIniFile.EraseSection(Section) - 删除指定节的所有数据
TIniFIle.SectionExists(Section) - 检查指定节是否存在
TIniFile.ValueExists(Section, Ident) - 检查指定节下的指定键是否存在
TIniFile.ReadBoolean(Section, Ident, Default) - 读取BOOL值,格式为节名、键名,缺省值
TIniFile.WriteBoolean(Section, Ident, Value) - 写入BOOL值,格式为节名、键名,值
TIniFile.ReadInteger(Section, Ident, Default) - 读取整数
TIniFile.WriteInteger(Section, Ident, Value) - 写入整数
TIniFile.ReadFloat(Section, Ident, Default) - 读取浮点数
TIniFile.WriteFloat(Section, Ident, Value) - 写入浮点数
TIniFile.ReadString(Section, Ident, Default) - 读取字符串
TIniFile.WriteString(Section, Ident, Value) - 写入字符串
对象数据为:
TIniFile.FileName - 返回INI文件名
此代码参考了云风的《编程感悟-我的游戏之旅》部分代码,其他代码都是
我在云风代码的研究基础上写的 ;)
由于代码比较长,就不贴上来了,下面有完整代码下载! [em14]
语法上已经很象DELPHI的OBJECT语法了 [em3]感兴趣的朋友拿去研究下哈 [em5]
附录1:类的申明
type
TScriptIniFile = class(TScriptObject)
fMemIniFile: TMemIniFile;
constructor Create; override;
destructor Destroy; override;
function GetScriptParams(l: p_lua_State): Integer; override;
function SetScriptParams(l: p_lua_State): Integer; override;
function Open(const FileName: String): Boolean;
private
fFileName: String;
end;
附录2:主执行代码,懂LUA的朋友应该能看懂:
var
l: p_lua_State;
s: string;
begin
l := lua_open();
luaopen_base(l);
Lua_Startup(l);
Lua_RegisterClass('TIniFile', TScriptIniFile);
// 创建INI文件对象
lua_dostring(l, 'obj = create_object("TIniFile")');
// 打开名为 Hello.ini 的配置文件
lua_dostring(l, 'obj.Open("Hello.ini")');
lua_dostring(l, 'obj.WriteBoolean("Sec", "Boolean", true)');
lua_dostring(l, 'obj.WriteInteger("Sec", "Integer", 1024)');
lua_dostring(l, 'obj.WriteFloat("Sec", "Float", 1.234)');
lua_dostring(l, 'obj.WriteString("Sec", "String", "LuaObject")');
// true
lua_dostring(l, 'print(obj.ReadBoolean("Sec", "Boolean", false))');
// 1024
lua_dostring(l, 'print(obj.ReadInteger("Sec", "Integer", -1))');
// 1.234
lua_dostring(l, 'print(obj.ReadFloat("Sec", "Float", -1))');
// LuaObject
lua_dostring(l, 'print(obj.ReadString("Sec", "String", ""))');
// true
lua_dostring(l, 'print(obj.SectionExists("Sec"))');
// false
lua_dostring(l, 'print(obj.SectionExists("Sec1"))');
// true
lua_dostring(l, 'print(obj.ValueExists("Sec", "Boolean"))');
// false
lua_dostring(l, 'print(obj.ValueExists("Sec", "Boolean1"))');
repeat
readln(s);
lua_dostring(l, pchar(s));
until s = '';
lua_dostring(l, 'obj.release()');
lua_close(l);
end.
附录3:程序运行完毕后保存的 hello.ini 文件的内容:
[Sec]
Boolean=1
Integer=1024
Float=1.234
String=LuaObject
|
|