forked from VE3NEA/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 12
/
CallLst.pas
149 lines (119 loc) · 2.9 KB
/
CallLst.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
//------------------------------------------------------------------------------
//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/.
//------------------------------------------------------------------------------
unit CallLst;
interface
uses
Classes;
type
// simple calllist. contains a TStringList of callsigns.
TCallList = class
protected
Calls: TStringList;
public
constructor Create;
destructor Destroy; override;
procedure LoadCallList;
function IsEmpty : Boolean;
procedure Clear();
function PickCall : string;
end;
implementation
uses
SysUtils, Ini;
function CompareCalls(Item1, Item2: Pointer): Integer;
begin
Result := StrComp(PChar(Item1), PChar(Item2));
end;
// reads callsigns from Master.dta file
procedure TCallList.LoadCallList;
const
Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/';
CHRCOUNT = Length(Chars);
INDEXSIZE = Sqr(CHRCOUNT) + 1;
INDEXBYTES = INDEXSIZE * SizeOf(Integer);
var
i: integer;
P, Pe: PChar;
L: TList;
FileName: string;
FFileSize: integer;
FIndex: array[0..INDEXSIZE-1] of integer;
DataNew: AnsiString;
Data: String;
begin
Calls.Clear;
FileName := ExtractFilePath(ParamStr(0)) + 'Master.dta';
if not FileExists(FileName) then Exit;
with TFileStream.Create(FileName, fmOpenRead) do
try
FFileSize := Size;
if FFileSize < INDEXBYTES then Exit;
ReadBuffer(FIndex, INDEXBYTES);
if (FIndex[0] <> INDEXBYTES) or (FIndex[INDEXSIZE-1] <> FFileSize)
then Exit;
SetLength(DataNew, Size - Position);
ReadBuffer(DataNew[1], Length(DataNew));
finally
Free;
end;
Data:= string(DataNew); //Modify By BG4FQD for unicode
L := TList.Create;
try
//list pointers to calls
L.Capacity := 20000;
P := @Data[1];
Pe := P + Length(Data);
while P < Pe do
begin
L.Add(TObject(P));
P := P + StrLen(P) + 1;
end;
//delete dupes
L.Sort(CompareCalls);
for i:=L.Count-1 downto 1 do
if StrComp(PChar(L[i]), PChar(L[i-1])) = 0
then L[i] := nil;
//put calls to Lst
Calls.Capacity := L.Count;
for i:=0 to L.Count-1 do
if L[i] <> nil then
Calls.Add(PChar(L[i]));
finally
L.Free;
end;
end;
function TCallList.IsEmpty : Boolean;
begin
Result := Calls.Count = 0;
end;
procedure TCallList.Clear();
begin
Calls.Clear;
end;
// returns a single callsign
function TCallList.PickCall : string;
var
Idx: integer;
begin
if Calls.Count = 0 then begin
Result := 'P29SX';
Exit;
end;
Idx := Random(Calls.Count);
Result := Calls[Idx];
if Ini.RunMode = rmHst then
Calls.Delete(Idx);
end;
constructor TCallList.Create;
begin
Calls := TStringList.Create;
end;
destructor TCallList.Destroy;
begin
Calls.Clear;
Calls.Free;
end;
end.