forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDXCC.pas
214 lines (189 loc) · 5.48 KB
/
DXCC.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
unit DXCC;
interface
uses
Generics.Collections,
Classes;
type
TDXCCRec= class
public
prefixReg: string;
Entity: string;
Continent: string;
ITU: string;
CQ: string;
function GetString: string;
end;
TDXCC= class
private
DXCCList: TObjectList<TDXCCRec>;
procedure LoadDxCCList;
procedure Delimit(var AStringList: TStringList; const AText: string);
function SearchPrefix(out index : integer; const ACallPrefix : string) : Boolean;
public
constructor Create;
destructor Destroy; override;
function FindRec(out dxrec : TDXCCRec; const ACallsign : string) : Boolean;
function GetStationInfo(const ACallsign: string): string;
function Search(ACallsign: string): string;
end;
var
gDXCCList: TDXCC;
implementation
uses
SysUtils, Contnrs, log, PerlRegEx, Ini;
procedure TDXCC.LoadDxCCList;
var
slst, tl: TStringList;
i: integer;
AR: TDXCCRec;
begin
slst:= TStringList.Create;
tl:= TStringList.Create;
try
DXCCList:= TObjectList<TDXCCRec>.Create;
slst.LoadFromFile(ParamStr(1) + 'DXCC.LIST');
// The search algorithm walks this list in reverse order.
for i:= 0 to slst.Count-1 do begin
if slst.Strings[i].StartsWith('#') then continue;
self.Delimit(tl, slst.Strings[i]);
if (tl.Count = 7) then begin
// some expressions are ignored because they mask other entities
if tl.Strings[1].StartsWith('!ignore') then continue;
AR:= TDXCCRec.Create;
AR.prefixReg:= tl.Strings[1];
AR.Entity:= tl.Strings[2];
AR.Continent:= tl.Strings[3];
AR.ITU:= tl.Strings[4];
AR.CQ:= tl.Strings[5];
DXCCList.Add(AR);
end;
end;
finally
slst.Free;
tl.Free;
end;
end;
constructor TDXCC.Create;
begin
inherited Create;
LoadDxCCList;
end;
destructor TDXCC.Destroy;
begin
FreeAndNil(DXCCList);
end;
// search ARRL DXCC prefix records for given callsign prefix.
function TDXCC.SearchPrefix(out index : integer; const ACallPrefix : string) : Boolean;
var
reg: TPerlRegEx;
s: string;
i: integer;
begin
reg := TPerlRegEx.Create();
try
Result:= False;
reg.Subject := UTF8Encode(ACallPrefix);
for i:= DXCCList.Count - 1 downto 0 do begin
s:= '^(' + TDXCCRec(DXCCList.Items[i]).prefixReg + ')';
reg.RegEx:= UTF8Encode(s);
if Reg.Match then begin
index:= i;
Result:= True;
Break;
end;
end;
finally
reg.Free;
end;
end;
function TDXCC.FindRec(out dxrec : TDXCCRec; const ACallsign : string) : Boolean;
var
sP : string;
index : integer;
begin
dxrec:= nil;
// Use full call when extracting prefix, not user's call.
// Example: F6/W7SST should return 'F6' not 'W7' (station located within F6)
// Also, do not delete trailing letters of call or prefix to allow
// regular expressions to match correctly (e.g. RC2F, Kaliningrad).
sP:= ExtractPrefix(ACallsign, {DeleteTrailingLetters=} False);
// special case for KG4 prefix...
// 2x1 and 2x3 callsigns are US; 2x2 calls assumed to be Guantanamo Bay.
// (Special thanks to F6FVY for a code example on how to solve this.)
if ACallsign.StartsWith('KG4') and
not ACallsign.StartsWith('KG44') and
((Length(ACallsign) = 6) or (Length(ACallsign) = 4)) then
begin
// KG4abc problem ... this is hard coded
Result:= SearchPrefix(index, 'K');
end
else
Result:= SearchPrefix(index, sP);
if Result then
dxrec:= TDXCCRec(DXCCList.Items[index]);
end;
// return status bar information string.
function TDXCC.GetStationInfo(const ACallsign: string): string;
var
i : integer;
sP: string;
begin
Result:= 'Unknown';
// Use full call when extracting prefix, not user's call.
sP:= ExtractPrefix(ACallsign);
if SearchPrefix(i, sP) then
Result:= sP + ': ' + TDXCCRec(DXCCList[i]).GetString;
end;
function TDXCC.Search(ACallsign: string): string;
var
reg: TPerlRegEx;
i: integer;
s, sP: string;
begin
reg := TPerlRegEx.Create();
try
Result:= '';
// Use full call when extracting prefix, not user's call.
sP:= ExtractPrefix(ACallsign);
reg.Subject := UTF8Encode(sP);
for i:= DXCCList.Count - 1 downto 0 do begin
s:= '^(' + TDXCCRec(DXCCList.Items[i]).prefixReg + ')';
reg.RegEx:= UTF8Encode(s);
if Reg.Match then begin
Result:= sP + ': ' + TDXCCRec(DXCCList[i]).GetString;
Break;
end;
end;
finally
reg.Free;
end;
end;
procedure TDXCC.Delimit(var AStringList: TStringList; const AText: string);
const
DelimitChar: char= ';';
var
i, l: integer;
s: string;
begin
AStringList.Clear;
l:= Length(AText);
s:= '';
for i := 1 to l do begin
if(AText[i] = DelimitChar) or (i=l) then begin
AStringList.Add(s);
s:= '';
end
else
s:= s + AText[i];
end;
end;
{ TARRLRec }
function TDXCCRec.GetString: string;
begin
// make the long USA entry a little shorter (similar to N1MM)
if Entity = 'United States of America' then
Result:= Format('%s/United States; ITU Zone: %s; CQ Zone: %s', [Continent, ITU, CQ])
else
Result:= Format('%s/%s; ITU Zone: %s; CQ Zone: %s', [Continent, Entity, ITU, CQ]);
end;
end.