16,748
社区成员
发帖
与我相关
我的任务
分享
unit Properties;
interface
uses
Classes, SysUtils, Dialogs;
type
TProperties = class(TObject)
private
FStringList: TStringList;
FFileName: string;
public
Constructor Create( fileName: string );
Destructor Destroy;override;
procedure Load( fileName: string );
function getValues( key: string ):string;
procedure setValues( key, value: string );
procedure append( key, value: string );
procedure save;
end;
implementation
{ TProperties }
procedure TProperties.append(key, value: string);
begin
FStringList.Add( key + '='+ value );
end;
constructor TProperties.Create(fileName: string);
begin
FStringList:= TStringList.Create;
FFileName:= fileName;
Load( fileName );
end;
destructor TProperties.Destroy;
begin
FStringList.Free;
inherited;
end;
function TProperties.getValues(key: string): string;
var
intI: Integer;
begin
for intI:=0 to FStringList.Count-1 do
begin
if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then
begin
if Pos( key, FStringList.Strings[ intI ] ) > 0 then
Result:= Copy( FStringList.Strings[ intI ], Pos( '=', FStringList.Strings[ intI ] ) + 1 , length( FStringList.Strings[ intI ] ) );
end;
end;
end;
procedure TProperties.Load(fileName: string);
begin
FStringList.Clear;
if FileExists( fileName ) then
FStringList.LoadFromFile( fileName );
end;
procedure TProperties.save;
begin
FStringList.SaveToFile( FFileName );
end;
procedure TProperties.setValues(key, value: string);
var
intI: Integer;
begin
for intI:=0 to FStringList.Count-1 do
begin
if ( FStringList.Strings[ intI ] <> '') and ( Copy( FStringList.Strings[ intI ], 0, 1 ) <> '#' ) then
begin
if Pos( key, FStringList.Strings[ intI ] ) > 0 then
FStringList.Strings[ intI ] := Copy( FStringList.Strings[ intI ], 0, Pos( '=', FStringList.Strings[ intI ] ) ) + value;
end;
end;
end;
end.
//去掉每行中无用数据
function CutComma(sData: string): string;
var
I: Integer;
NewData: string;
begin
Result := '';
NewData := Trim(sData);
for I := 1 to Length(NewData) do
begin
if (NewData[I] in [' ', #9]) then
begin
Delete(NewData, I, 1);
end;
end;
Result := NewData;
end;
{==================================================================
输入参数:Param1 文件对应项如 c3p0.minPoolSize
返回值:
修改记录:
===================================================================}
function GetValueFromPro(Param1: string): string;
var
s, sRtn: string;
fFileName: string;
i: Integer;
sList: TStringList;
begin
sRtn := '';
fFileName := ExtractFilePath(Application.ExeName) + 'properties.txt';
sList := TStringList.Create;
sList.LoadFromFile(fFileName); //将文件内容读入内存
s := Param1;
for i := 0 to sList.Count - 1 do
begin
if Pos(s, sList[i]) > 0 then //如果存在对应项
begin
sRtn := Copy(CutComma(sList[i]),
Pos(s, sList[i])+2,
Length(CutComma(sList[i])) - (Pos(s, sList[i])+1)
);
break;
end;
end;
Result := sRtn;
end;