File: CallLst.pas

package info (click to toggle)
morserunner 0.9%2Blinux-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,384 kB
  • sloc: pascal: 5,344; makefile: 4
file content (126 lines) | stat: -rw-r--r-- 2,742 bytes parent folder | download | duplicates (2)
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
//------------------------------------------------------------------------------
//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;

{$MODE Delphi}

interface

uses
  SysUtils, Classes, Ini;

procedure LoadCallList;
function PickCall: string;

var
  Calls: TStringList;




implementation


function CompareCalls(Item1, Item2: Pointer): Integer;
begin
  Result := StrComp(PChar(Item1), PChar(Item2));
end;

procedure 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;
  Data: string;
begin
  Calls.Clear;

  // try current directory first
  FileName := 'Master.dta';
  if not FileExists(FileName) then
     begin
	// try system location
	Filename := '/usr/share/morserunner/Master.dta';
	if not FileExists(FileName) then
	   begin
	      Writeln('Unable to load call file, default single call used');
	      Exit;
	   end;
     end;

  Writeln('Using calls from file: ', Filename);
  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(Data, Size - Position);
      ReadBuffer(Data[1], Length(Data));
    finally
      Free;
    end;


  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 PickCall: string;
var
  Idx: integer;
begin
  if Calls.Count = 0 then begin Result := 'DJ1TF'; Exit; end;

  Idx := Random(Calls.Count);
  Result := Calls[Idx];

  if Ini.RunMode = rmHst then Calls.Delete(Idx);
end;


initialization
  Calls := TStringList.Create;

finalization
  Calls.Free;

end.