📄 csdfe.pas
字号:
unit CSDFE; // CS Data File Enginge
interface
uses
Classes, StrUtils, SysUtils ;
Type
TErrorTypes = (FileMissed, OpenFailed, ReadFailed, WriteFailed);
Errors = set of TErrorTypes ;
TCSConfigFile=Class //CS Config File
Private
LOCF:TStringList; // Lines of Config File
theFileName : String ;
ErrCode : Errors ;
FileAgeWhenLoad : Integer;
Function FindIdent(Ident:string):Integer;
function GetCurFileAge:Integer;
procedure SetCurFileAge(CurFileAge:Integer);
procedure SetFileName( pFileName : String );
public
constructor Create;
destructor Destroy;Override;
procedure Load;
procedure Save;
procedure WriteVar(Ident:string; Value:string);
function ReadVar(Ident:String):String;
procedure BindKey(Key:String;Action:String);overload;
function BindKey(Key:String):String;Overload;
function KeyAction(Key:String):String;
property FileName : String read theFileName write SetFileName ;
Property StringList:TStringList Read LOCF ;
Property ErrorCode : Errors read ErrCode;
property CurFileAge:integer read GetCurFileAge write SetCurFileAge;
end;
implementation
procedure TCSConfigFile.SetCurFileAge(CurFileAge:Integer);
begin
FileAgeWhenLoad := CurFileAge;
end;
function TCSConfigFile.GetCurFileAge:Integer;
begin
result:=FileAgeWhenLoad;
end;
procedure TCSConfigFile.SetFileName( pFileName : String );
begin
if FileExists(pFileName) then
theFileName := pFileName
else
begin
theFileName := '' ;
ErrCode := [FileMissed] ; // file not exists
end;
end;
function TCSConfigFile.FindIdent(Ident:string):Integer;
var
i: Integer;
s: String;
begin
//
for i :=0 to LOCF.Count - 1 do
begin
s := copy( LOCF.Strings[i],1,length(Ident)+1) ;
if ( s= (Ident+' ')) or ( s = (Ident+#9)) then
begin
Result:=i;
exit;
end;
end;
Result := -1;
end;
procedure TCSConfigFile.Load ;
begin
LOCF.LoadFromFile(theFilename);
FileAgeWhenLoad := FileAge(theFilename);
end;
procedure TCSConfigFile.Save;
var
CurFileAge : Integer;
begin
CurFileAge := FileAge(theFilename);
if CurFileAge <> FileAgeWhenLoad then
begin
FileAgeWhenLoad := CurFileAge;
LOCF.LoadFromFile(theFileName);
end
else
begin
LOCF.SaveToFile(theFileName);
FileAgeWhenLoad := FileAge(theFilename);
end;
end;
procedure TCSConfigFile.WriteVar(Ident:string; Value:string);
var
IdentPos:Integer;
begin
//
Value := AnsiQuotedStr(Value,'"');
IdentPos := FindIdent(Ident);
if IdentPos = -1 then
//找不到标识符,创建新的一行,定入标识符和值
LOCF.Add(Ident+' '+Value)
else
// 修改值
LOCF.Strings[IdentPos] := Ident + ' ' + Value;
end;
function TCSConfigFile.ReadVar(Ident:String):String;
var
IdentPos : Integer ;
begin
IdentPos := FindIdent(Ident);
if IdentPos = -1 then // 找不到标识符
Result := ''
else // 返回变量
begin
Result := AnsiDequotedStr(RightStr(LOCF.Strings[IdentPos], Length(LOCF.Strings[IdentPos])-Length(Ident)-1),'"');
end;
end;
procedure TCSConfigFile.BindKey(Key:String;Action:String);
var
IdentPos : Integer ;
begin
// 为 KEY 加上双引号
Key := AnsiQuotedStr(Key, '"');
IdentPos := FindIdent( 'bind ' + Key );
if IdentPos = -1 then // 这个键还没绑定
LOCF.Append( 'bind ' + Key + ' "' + Action + '"' )
else
LOCF[IdentPos] := ( 'bind ' + Key + ' "' + Action + '"' ) ;
// null;
end;
function TCSConfigFile.BindKey(Key:String):String;
var
IdentPos : Integer ;
begin
// 为 KEY 加上双引号
Key := AnsiQuotedStr(Key, '"');
IdentPos := FindIdent( 'bind ' + Key );
if IdentPos = -1 then // 这个键还没绑定
Result := ''
else
Result := AnsiDequotedStr(RightStr(LOCF.Strings[IdentPos], Length(LOCF.Strings[IdentPos])-Length(Key)-6),'"');
end;
function TCSConfigFile.KeyAction(Key:String):String;
var
IdentPos : Integer ;
begin
// 为 KEY 加上双引号
Key := AnsiQuotedStr(Key, '"');
IdentPos := FindIdent( Key );
if IdentPos = -1 then // 这个键还没绑定
Result := ''
else
Result := AnsiDequotedStr(Trim(RightStr(LOCF[IdentPos],Length(LOCF[IdentPos])-Length(Key))),'"');
end;
constructor TCSConfigFile.Create;
begin
LOCF := TStringList.Create;
end;
destructor TCSConfigFile.Destroy;
begin
LOCF.Free ;
end;
initialization
// TmpStrLst := TStringList.Create ;
finalization
// TmpStrLst.Free ;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -