forked from Xevaquor/Dd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.dpr
219 lines (187 loc) · 4.13 KB
/
LinkedList.dpr
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
215
216
217
library LinkedList;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
DateUtils,
Math,
Types,
TOperativeUnit in 'TOperativeUnit.pas';
{$R *.res}
var
gHead : PElem;
procedure Append(item : TElemType); stdcall;
var
newNode : PElem;
iter : PElem;
begin
if gHead = nil then
begin
//if list is empty, create it
New(gHead);
gHead^.Next := nil;
gHead^.Val := item;
Exit;
end;
//otherwise, find tail and append new element to it
iter := gHead;
while(iter^.Next <> nil) do
begin
iter := iter^.Next;
end;
New(newNode);
newNode^.Next := nil;
newNode^.Val := item;
iter^.Next := newNode;
end;
procedure WriteEach; stdcall;
var
iter : PElem;
begin
//check if empty
if gHead = nil then
begin
writeln('Empty');
Exit;
end;
iter := gHead;
while iter <> nil do
begin
writeln(iter^.Val.FirstName);
iter := iter^.Next;
end;
end;
procedure Seed; stdcall;
var
bolek, stokrotka, michau : TOperative;
begin
bolek.FirstName := 'Lech';
bolek.NickName := 'Bolek';
bolek.DateOfBirth := Now;
stokrotka.FirstName := 'Monika';
stokrotka.NickName := 'Stokrotka';
stokrotka.DateOfBirth := Now;
michau.FirstName := 'Michal';
michau.NickName := 'Michau';
michau.DateOfBirth := Now;
Append(bolek);
Append(michau);
Append(stokrotka);
end;
//TODO: check if list is empty
procedure WriteToFile; stdcall;
var
f : File of TOperative;
iter : PElem;
begin
AssignFile(f, 'database.dat');
Rewrite(f);
try
begin
//go through all entries and save them to file
iter := gHead;
while iter <> nil do
begin
Write(f, iter^.Val);
iter := iter^.Next;
end;
end;
finally
CloseFile(f);
end;
end;
//TODO: Exception handling ?
procedure ReadFromFile; stdcall;
var
f : File of TOperative;
entry : TOperative;
begin
if not FileExists('database.dat') then
Exit;
AssignFile(f, 'database.dat');
Reset(f);
try
begin
while not EOF(f) do
begin
Read(f, entry);
Append(entry);
end;
end;
finally
CloseFile(f);
end;
end;
function GetHead : PElem; stdcall;
begin
Result := gHead;
end;
function EqualTOperatives(a, b : TOperative) : Boolean; stdcall;
begin
Result := (a.FirstName = b.FirstName) and
(a.LastName = b.LastName) and
(a.NickName = b.NickName) and
(CompareDate(a.DateOfBirth, b.DateOfBirth) = EqualsValue) and
(a.BirthPlace = b.BirthPlace);
end;
procedure UpdateOperative(old, new : TOperative); stdcall;
var
iter : PElem;
begin
iter := gHead;
while iter <> nil do
begin
if EqualTOperatives(old, iter^.Val) then
begin
iter^.Val := new;
Exit;
end;
iter := iter^.Next;
end;
end;
procedure Remove(op : TOperative); stdcall;
var
iter, prev : PElem;
begin
iter := gHead;
while iter <> nil do
begin
if EqualTOperatives(iter^.Val, op) then
begin
//TODO: Skrajne przypadki
//TODO: Memleak
if iter = gHead then
begin
gHead := iter^.Next;
dispose(iter);
Exit;
end;
prev^.Next := iter^.Next;
Dispose(iter);
Exit;
end;
prev := iter;
iter := iter^.Next;
end;
end;
exports
Append name 'Append',
WriteEach name 'WriteEach',
WriteToFile name 'WriteToFile',
ReadFromFile name 'ReadFromFile',
GetHead name 'GetHead',
EqualTOperatives name 'EqualTOperatives',
Remove name 'Remove',
Seed name 'Seed',
UpdateOperative name 'UpdateOperative';
begin
gHead := nil;
end.