summaryrefslogtreecommitdiff
path: root/examples/apps/ide/src/unitlist.pas
blob: b8ca59adc316b004114d1ad6c911592942fe8f34 (plain)
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
unit UnitList;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpg_base;

type
  TUnit = class(TObject)
  private
    FFilename: TfpgString;
    FOpened: Boolean;
    function GetUnitName: TfpgString;
  public
    constructor Create;
    property FileName: TfpgString read FFilename write FFilename;
    property UnitName: TfpgString read GetUnitName;
    property Opened: Boolean read FOpened write FOpened;
  end;


  TUnitList = class(TObject)
  private
    FList: TList;
    function GetItems(AIndex: integer): TUnit;
    procedure SetItems(AIndex: integer; const AValue: TUnit);
  public
    constructor Create;
    destructor  Destroy; override;
    function    Count: integer;
    function    FindByName(const AUnitName: TfpgString): TUnit;
    function    FileExists(const AFilename: TfpgString): Boolean;
    procedure   Add(NewUnit: TUnit);
    procedure   Clear;
    procedure   Delete(AIndex: integer);
    property    Items[AIndex: integer]: TUnit read GetItems write SetItems; default;
  end;


implementation

uses
  fpg_utils;

{ TUnitList }

function TUnitList.GetItems(AIndex: integer): TUnit;
begin
  Result := TUnit(FList[AIndex]);
end;

procedure TUnitList.SetItems(AIndex: integer; const AValue: TUnit);
begin
  FList[AIndex] := AValue;
end;

constructor TUnitList.Create;
begin
  FList := TList.Create;
  inherited Create;
end;

destructor TUnitList.Destroy;
begin
  Clear;
  FList.Free;
  inherited Destroy;
end;

function TUnitList.Count: integer;
begin
  Result := FList.Count;
end;

function TUnitList.FindByName(const AUnitName: TfpgString): TUnit;
var
  l: Integer;
  r: Integer;
  m: Integer;
  cmp: Integer;
begin
  l := 0;
  r := Count-1;
  m := 0;
  while l <= r do
  begin
    m := (l+r) shr 1;
    Result := Items[m];
    cmp := AnsiCompareText(AUnitName, Result.UnitName);
    if cmp < 0 then
      r := m-1
    else if cmp > 0 then
      l := m+1
    else
      exit;
  end;
  Result := nil;
end;

function TUnitList.FileExists(const AFilename: TfpgString): Boolean;
var
  i: integer;
begin
  Result := False;
  for i := 0 to Count-1 do
  begin
    Result := Items[i].FileName = AFilename;
    if Result then
      Exit;
  end;
end;

procedure TUnitList.Add(NewUnit: TUnit);
var
  l: Integer;
  r: Integer;
  m: Integer;
  cmp: Integer;
begin
  l := 0;
  r := Count-1;
  m := 0;
  while l <= r do
  begin
    m := (l+r) shr 1;
    cmp := AnsiCompareText(NewUnit.UnitName, Items[m].UnitName);
    if cmp < 0 then
      r := m-1
    else if cmp > 0 then
      l := m + 1
    else
      break;
  end;
  if (m < Count) and (AnsiCompareText(NewUnit.UnitName, Items[m].UnitName) > 0) then
    inc(m);
  FList.Insert(m, NewUnit);
end;

procedure TUnitList.Clear;
var
  i: integer;
begin
  for i := Count-1 downto 0 do
    TUnit(FList[i]).Free;
  FList.Clear;
end;

procedure TUnitList.Delete(AIndex: integer);
begin
  TUnit(FList[AIndex]).Free;
  FList.Delete(AIndex);
end;

{ TUnit }

function TUnit.GetUnitName: TfpgString;
begin
  Result := fpgExtractFileName(Filename);
end;

constructor TUnit.Create;
begin
  inherited Create;
  FOpened := False;
end;

end.