Menu

[r4300]: / branches / parsnip / Src / Main / CS.ExternalProgs.DiffViewer.pas  Maximize  Restore  History

Download this file

198 lines (169 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a class to manage specification and execution of a user defined
* file diff viewer application.
}
unit CS.ExternalProgs.DiffViewer;
interface
type
TDiffViewer = class(TObject)
strict private
type
TTestFiles = record
public
type
TTestFileID = (tfiLeft, tfiRight);
public
class function Name(FileID: TTestFileID): string; static;
class procedure Create; static;
class procedure Delete; static;
end;
var
fExePath: string;
fParams: string;
fChanged: Boolean;
procedure SetExePath(const AExePath: string);
procedure SetParams(const AParams: string);
class function ParamStr(const Tplt, FileName1, FileName2: string): string;
procedure LoadProps;
procedure SaveProps;
class function InternalExecute(const AExePath, AParams, AFileName1,
AFileName2: string): Boolean;
public
constructor Create;
destructor Destroy; override;
function Execute(const AFileName1, AFileName2: string): Boolean;
function Test(const AExePath, AParams: string): Boolean;
property ExePath: string read fExePath write SetExePath;
property Params: string read fParams write SetParams;
end;
implementation
uses
// Delphi
SysUtils,
IOUtils,
Windows,
ShellAPI,
// Project
UConsts,
UIOUtils,
USettings,
UStrUtils,
USystemInfo;
{ TDiffViewer }
constructor TDiffViewer.Create;
begin
inherited Create;
LoadProps;
end;
destructor TDiffViewer.Destroy;
begin
if fChanged then
SaveProps;
TTestFiles.Delete;
inherited;
end;
function TDiffViewer.Execute(const AFileName1, AFileName2: string): Boolean;
begin
Result := InternalExecute(fExePath, fParams, AFileName1, AFileName2);
end;
class function TDiffViewer.InternalExecute(const AExePath, AParams, AFileName1,
AFileName2: string): Boolean;
begin
if StrIsBlank(AFileName1) or StrIsBlank(AFileName2) then
Exit(False);
if not TFile.Exists(AFileName1, False)
or not TFile.Exists(AFileName2, False) then
Exit(False);
if (AExePath = EmptyStr) or (AParams = EmptyStr) then
Exit(False);
if not TFile.Exists(AExePath, False) then
Exit(False);
Result := ShellExecute(
0,
'open',
PChar(StrQuoteSpaced(AExePath)),
PChar(ParamStr(AParams, AFileName1, AFileName2)),
PChar(TSystemFolders.Temp),
SW_SHOWNORMAL
) > 32;
end;
procedure TDiffViewer.LoadProps;
var
Storage: ISettingsSection;
begin
Storage := Settings.ReadSection(ssExternalApps);
fExePath := Storage.GetString('DiffViewerPath');
fParams := Storage.GetString('DiffViewerParams');
end;
class function TDiffViewer.ParamStr(const Tplt, FileName1, FileName2: string):
string;
begin
Result := StrReplace(StrReplace(Tplt, '%1', FileName1), '%2', FileName2);
end;
procedure TDiffViewer.SaveProps;
var
Storage: ISettingsSection;
begin
Storage := Settings.EmptySection(ssExternalApps);
Storage.SetString('DiffViewerPath', fExePath);
Storage.SetString('DiffViewerParams', fParams);
Storage.Save;
end;
procedure TDiffViewer.SetExePath(const AExePath: string);
begin
if StrSameText(AExePath, fExePath) then
Exit;
fExePath := StrTrim(AExePath);
fChanged := True;
end;
procedure TDiffViewer.SetParams(const AParams: string);
begin
if StrSameText(AParams, fParams) then
Exit;
fParams := StrTrim(AParams);
fChanged := True;
end;
function TDiffViewer.Test(const AExePath, AParams: string): Boolean;
begin
TTestFiles.Create;
Result := InternalExecute(
AExePath, AParams, TTestFiles.Name(tfiLeft), TTestFiles.Name(tfiRight)
);
end;
{ TDiffViewer.TTestFiles }
class procedure TDiffViewer.TTestFiles.Create;
resourcestring
sLeftContents = 'One' + EOL + 'Three' + EOL + 'Five' + EOL + 'Seven';
sRightContents = 'One' + EOL + 'Five' + EOL + 'Six' + EOL + 'Seven';
const
Contents: array[TTestFileID] of string = (sLeftContents, sRightContents);
var
FileID: TTestFileID;
begin
for FileID := Low(TTestFileID) to High(TTestFileID) do
TFileIO.WriteAllText(Name(FileID), Contents[FileID], TEncoding.Default);
end;
class procedure TDiffViewer.TTestFiles.Delete;
var
FileID: TTestFileID;
begin
for FileID := Low(TTestFileID) to High(TTestFileID) do
TFile.Delete(Name(FileID));
end;
class function TDiffViewer.TTestFiles.Name(FileID: TTestFileID): string;
const
Stubs: array[TTestFileID] of string = ('Left', 'Right');
begin
Result := IncludeTrailingPathDelimiter(TSystemFolders.Temp) +
'CodeSnip-Diff-Test-' + Stubs[FileID] + '.txt';
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.