-
Notifications
You must be signed in to change notification settings - Fork 5
/
FilterClass_U.pas
150 lines (122 loc) · 3.4 KB
/
FilterClass_U.pas
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
unit FilterClass_U;
interface
uses Classes, IniFiles;
type
{$M+}
{ TFilterData }
TFilterData = class(TObject)
private
FRun: string;
FEdit: string;
FExtensions: string;
FRunParams: string;
FEditParams: string;
public
constructor Create;
procedure Assign(Source: TFilterData);
published
property Extensions: string read FExtensions write FExtensions;
property Edit: string read FEdit write FEdit;
property EditParams: string read FEditParams write FEditParams;
property Run: string read FRun write FRun;
property RunParams: string read FRunParams write FRunParams;
end;
{$M-}
var
Filters: TStringList;
procedure Filters_LoadFromFile;
procedure Filters_SaveToFile;
function Filters_GetFilterByFilename(const AFileName: string): TFilterData;
// nil if not Founded
implementation
uses SysUtils, System.TypInfo, Windows, CommonU;
const
sFiltersFileName = 'Filters.ini';
var FPropList: PPropList; FPropCount: Integer;
{ TFilterData }
procedure TFilterData.Assign(Source: TFilterData);
begin
for var i := 0 to FPropCount - 1 do
begin
var vProp := FPropList[i];
SetStrProp(Self, vProp, GetStrProp(Source, vProp));
end;
end;
constructor TFilterData.Create;
begin
for var i := 0 to FPropCount - 1 do
begin
SetStrProp(Self, FPropList[i], '');
end;
end;
procedure Filters_LoadFromFile;
begin
var vIniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + sFiltersFileName);
with vIniFile do
try
var Sections := TStringList.Create;
ReadSections(Sections);
for var vFilterName in Sections do
begin
var vFilterData := TFilterData.Create;
for var i := 0 to FPropCount - 1 do
begin
var vProp := FPropList[i];
SetStrProp(vFilterData, vProp, ReadString(vFilterName, string(vProp.Name), ''));
end;
Filters.AddObject(vFilterName, vFilterData)
end;
finally
Free;
end;
end;
procedure Filters_SaveToFile;
begin
var vFilename := ExtractFilePath(ParamStr(0)) + sFiltersFileName;
var vFilenameNew := ExtractFilePath(ParamStr(0)) + 'new-' + sFiltersFileName;
var vIniFile := TIniFile.Create(vFilenameNew);
with vIniFile do
try
for var i := 0 to Filters.Count - 1 do
begin
var vFilterName := Filters[i];
for var j := 0 to FPropCount - 1 do
begin
var vProp := FPropList[j];
vIniFile.WriteString(vFilterName, string(vProp.Name), GetStrProp(TFilterData(Filters.Objects[i]), vProp));
end;
end;
finally
Free;
end;
if (FileExists(vFilename) and not DeleteFile(PChar(vFilename))) or
not RenameFile(vFilenameNew, vFilename) then
RaiseLastOSError;
end;
function Filters_GetFilterByFilename(const AFileName: string): TFilterData;
// nil if not Founded
var
i: Integer;
vFilterData: TFilterData;
begin
Result := nil;
for i := 0 to Filters.Count - 1 do
begin
vFilterData := TFilterData(Filters.Objects[i]);
if MyMatchesExtensions(AFileName, vFilterData.Extensions) then
begin
Result := vFilterData;
Break;
end;
end;
end;
initialization
FPropCount := PTypeData(GetTypeData(TFilterData.ClassInfo))^.PropCount;
GetMem(FPropList, SizeOf(PPropInfo) * FPropCount);
GetPropInfos(TFilterData.ClassInfo, FPropList);
Filters := TStringList.Create;
Filters_LoadFromFile;
finalization
//Filters.Free;
//FreeMem(FPropList);
end.