unit uDataLocation;
interface
type
IDataLocation =
interface
['
{72C10C78-A39E-4005-8D53-57FD44A9B7F4}']
function ConfigFile(
out aConfigFileName :
string;
const aDefaultFileName :
string = '
') : Boolean;
function DataFileExt(aDataFileExt :
string) : IDataLocation;
function DataCmdParam(aDataCmdParam :
string) : IDataLocation;
function ConfigFileExt(aConfigFileExt :
string) : IDataLocation;
function ConfigCmdParam(aConfigCmdParam :
string) : IDataLocation;
function ConfigSection(aConfigSection :
string) : IDataLocation;
function DataFileKey(aDataFileKey :
string) : IDataLocation;
end;
type
TDataLocation =
class(TInterfacedObject, IDataLocation)
private
FAppName :
string;
FDataFile :
string;
FConfigFile :
string;
FDataFileExt :
string;
FDataCmdParam :
string;
FConfigFileExt :
string;
FConfigCmdParam :
string;
FConfigSection :
string;
FDataKey :
string;
FIsLocal : Boolean;
FIsUSB : Boolean;
function FindDataCmd : Boolean;
function FindConfigCmd : Boolean;
function FindLocalConfig : Boolean;
function FindLocalFile : Boolean;
function FindUserDataFile : Boolean;
private // IdcDataLocation
function ConfigFile(
out aConfigFileName :
string;
const aDefaultFileName :
string = '
') : Boolean;
function DataFileExt(aDataFileExt :
string) : IDataLocation;
function DataCmdParam(aDataCmdParam :
string) : IDataLocation;
function ConfigFileExt(aConfigFileExt :
string) : IDataLocation;
function ConfigCmdParam(aConfigCmdParam :
string) : IDataLocation;
function ConfigSection(aConfigSection :
string) : IDataLocation;
function DataFileKey(aDataFileKey :
string) : IDataLocation;
public
constructor Create;
end;
implementation
uses
SysUtils, Forms, IniFiles, ShlObj,
uUtils;
const
cDataFileExt = '
.zip';
cDataCmdParam = '
data';
cConfigFileExt = '
.ini';
cConfigCmd = '
config';
cConfigSection = '
Config';
cDataFileKey = '
Data';
function TDataLocation.ConfigCmdParam(aConfigCmdParam:
string): IDataLocation;
begin
Result := Self;
FConfigCmdParam := aConfigCmdParam;
end;
function TDataLocation.ConfigFile(
out aConfigFileName :
string;
const aDefaultFileName :
string = '
') : Boolean;
begin
Result := True;
if aDefaultFileName > '
'
then
FDataFile := aDefaultFileName;
try
If FindDataCmd
then exit;
If FindConfigCmd
then exit;
If FindLocalConfig
then exit;
If FindLocalFile
then exit;
If FindUserDataFile
then exit;
Result := False;
finally
aConfigFileName := FDataFile;
end;
end;
function TDataLocation.ConfigFileExt(aConfigFileExt:
string): IDataLocation;
begin
Result := Self;
FConfigFileExt := aConfigFileExt;
FConfigFileExt := ChangeFileExt(FAppName, FConfigFileExt);
end;
function TDataLocation.ConfigSection(aConfigSection:
string): IDataLocation;
begin
Result := Self;
FConfigSection := aConfigSection;
end;
constructor TDataLocation.Create;
begin
FAppName := Application.ExeName;
FDataFileExt := cDataFileExt;
FDataCmdParam := cDataCmdParam;
FConfigFileExt := cConfigFileExt;
FConfigCmdParam := cConfigCmd;
FConfigSection := cConfigSection;
FDataKey := cDataFileKey;
FIsUSB := False;
FIsLocal := True;
FDataFile := ChangeFileExt(FAppName, FDataFileExt);
FConfigFile := ChangeFileExt(FAppName, FConfigFileExt);
end;
function TDataLocation.DataCmdParam(aDataCmdParam:
string): IDataLocation;
begin
Result := Self;
FDataCmdParam := aDataCmdParam;
end;
function TDataLocation.DataFileExt(aDataFileExt :
string): IDataLocation;
begin
Result := Self;
FDataFileExt := aDataFileExt;
FDataFile := ChangeFileExt(FAppName, FDataFileExt);
end;
function TDataLocation.DataFileKey(aDataFileKey:
string): IDataLocation;
begin
Result := Self;
FDataKey := aDataFileKey;
end;
function TDataLocation.FindConfigCmd: Boolean;
var
aFileName :
string;
begin
Result := False;
if GetCmdLineSwitchValue(aFileName, FConfigCmdParam)
then
begin
FConfigFile := aFileName;
Result := FindLocalConfig;
end;
end;
function TDataLocation.FindDataCmd: Boolean;
var
aFileName :
string;
begin
Result := False;
if GetCmdLineSwitchValue(aFileName, FDataCmdParam)
then
begin
FDataFile := aFileName;
Result := FindLocalFile;
end;
end;
function TDataLocation.FindLocalConfig: Boolean;
var
aIniFile : TIniFile;
begin
Result := False;
if FileExists(FConfigFile)
then
begin
aIniFile := TIniFile.Create(FConfigFile);
try
FDataFile := aIniFile.ReadString(FConfigSection, FDataKey, FDataFile);
Result := FindLocalFile;
finally
aIniFile.Free;
end;
end;
end;
function TDataLocation.FindLocalFile: Boolean;
begin
Result := False;
if FIsLocal
then
Result := FileExists(FDataFile);
end;
function TDataLocation.FindUserDataFile: Boolean;
var
aDataPath :
string;
aFileName :
string;
begin
Result := False;
if not FIsUSB
then
begin
aFileName := ExtractFileName(FDataFile);
aDataPath := IncludeTrailingBackslash(GetSpecialFolder(CSIDL_APPDATA)) + ChangeFileExt(aFileName, '
');
FDataFile := IncludeTrailingBackslash(aDataPath) + aFileName;
Result := FileExists(FDataFile);
end;
end;
end.