summaryrefslogtreecommitdiff
path: root/packages/amunits/src/utilunits/linklist.pas
blob: f56913c0140e68953a1954a451e8e391804d7b52 (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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
{
    This file is part of the Free Pascal run time library.

    A file in Amiga system run time library.
    Copyright (c) 1998-2003 by Nils Sjoholm
    member of the Amiga RTL development team.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{$I useamigasmartlink.inc}
{$ifdef use_amiga_smartlink}
    {$smartlink on}
{$endif use_amiga_smartlink}

unit linklist;

{
   A unit for an easy way to use exec linked lists
   for Amiga. Can also be used for other platforms
   as it is. I hope.
   27 Oct 1998.

   Added the define use_amiga_smartlink.
   13 Jan 2003.

   nils.sjoholm@mailbox.swipnet.se
}

interface

uses
{$ifdef Amiga}
   Exec,amigalib,
{$endif}
   strings;

{ $define showall}

{$ifndef Amiga}
type

    pNode = ^tNode;
    tNode = record
        ln_Succ: pNode;
        ln_Pred: pNode;
        ln_Type: byte;
        ln_Pri : shortint;
        ln_Name: pchar;
        end;

    pList = ^tList;
    tList = record
        lh_Head: pNode;
        lh_Tail: pNode;
        lh_TailPred: pNode;
        lh_Type: byte;
        l_pad: byte;
        end;

{$endif}

type
    pFPCNode = ^tFPCNode;
    tFPCNode = record
        ln_Succ   : pNode;
        ln_Pred   : pNode;
        ln_Type   : Byte;
        ln_Pri    : Shortint;
        ln_Name   : PChar;
{
   Increase this record if you need more information
   just add your own to the record. Don't forget to
   change the functions or add your own functions.
}
        ln_Size   : Longint;
        end;

{$ifndef Amiga}
procedure NewList (list: pList);
procedure AddHead(list : pList; node : pNode);
procedure AddTail(list : pList; node : pNode);
procedure Insert(list : pList; node, lnode: pNode);
procedure Remove(node : pNode);
function RemHead(list : pList): pNode;
function RemTail(list : pList): pNode;
{$endif}

FUNCTION AddNewNode(VAR fpclist : pList; Str : PChar): pFPCNode;
FUNCTION AddNewNode(VAR fpclist : pList; Str : String): pFPCNode;
PROCEDURE ClearList(VAR fpclist : pList);
PROCEDURE CreateList(VAR fpclist : pList);
FUNCTION CopyList(fpclist : pList): pList;
PROCEDURE DeleteNode(ANode : pFPCNode);
PROCEDURE DestroyList(VAR fpclist : pList);
FUNCTION FindNodeData(fpclist : pList; data : PChar): pFPCNode;
FUNCTION FindNodeData(fpclist : pList; data : String): pFPCNode;
FUNCTION GetFirstNode(fpclist : pList): pFPCNode;
FUNCTION GetLastNode(fpclist : pList): pFPCNode;
FUNCTION GetNextNode( ANode : pFPCNode): pFPCNode;
FUNCTION GetNodeData(Anode : pFPCNode): PChar;
FUNCTION GetNodeNumber(fpclist : pList; num : Longint): pFPCNode;
FUNCTION GetPrevNode( ANode : pFPCNode): pFPCNode;
FUNCTION InsertNewNode(var fpclist : pList; data : PChar; Anode : pFPCNode): pFPCNode;
FUNCTION InsertNewNode(var fpclist : pList; data : String; Anode : pFPCNode): pFPCNode;
PROCEDURE ListToBuffer(fpclist : pList; VAR buf : PChar);
FUNCTION MergeLists(firstlist , secondlist : pList): pList;
PROCEDURE MoveNodeBottom(var fpclist: pList; ANode : pFPCNode);
PROCEDURE MoveNodeDown(VAR fpclist : pList; ANode : pFPCNode);
PROCEDURE MoveNodeTop(VAR fpclist: pList; ANode : pFPCNode);
PROCEDURE MoveNodeUp(VAR fpclist : pList; ANode : pFPCNode);
FUNCTION NodesInList(fpclist :  pList): Longint;
PROCEDURE PrintList(fpclist : pList);
PROCEDURE RemoveDupNode( VAR fpclist :  pList);
PROCEDURE RemoveLastNode(VAR fpclist : pList);
FUNCTION SizeOfList(fpclist : pList): Longint;
PROCEDURE SortList(VAR fpclist: pList);
FUNCTION UpDateNode(ANode : pFPCNode; data : PChar): BOOLEAN;
FUNCTION UpDateNode(ANode : pFPCNode; data : String): BOOLEAN;

function FileToList(thefile : PChar; var thelist : pList): boolean;
function FileToList(thefile : String; var thelist : pList): boolean;
function ListToFile(TheFile : PChar; thelist : pList): Boolean;
function ListToFile(TheFile : String; thelist : pList): Boolean;

implementation

{$ifndef Amiga}
procedure NewList (list: pList);
begin
    list^.lh_Head     := pNode(@list^.lh_Tail);
    list^.lh_Tail     := NIL;
    list^.lh_TailPred := pNode(@list^.lh_Head)
end;


procedure AddHead(list : pList; node : pNode);
begin
    node^.ln_Succ := list^.lh_Head;
    node^.ln_Pred := pNode(@list^.lh_Head);
    list^.lh_Head^.ln_Pred := node;
    list^.lh_Head := node;
end;

procedure AddTail(list : pList; node : pNode);
begin
    node^.ln_Succ := pNode(@list^.lh_Tail);
    node^.ln_Pred := list^.lh_TailPred;
    list^.lh_TailPred^.ln_Succ := node;
    list^.lh_TailPred := node;
end;

procedure Insert(list : pList; node : pNode; lnode: pNode);
begin
    {*
     *  Insert node after lnode.  If lnode = NIL then insert
     *  at head of list.
     *}

    if (lnode = NIL) then lnode := pNode(@list^.lh_Head);
    node^.ln_Pred := lnode;
    node^.ln_Succ := lnode^.ln_Succ;
    lnode^.ln_Succ := node;
    node^.ln_Succ^.ln_Pred := node;
end;

procedure Remove(node : pNode);
begin
    node^.ln_Succ^.ln_Pred := node^.ln_Pred;
    node^.ln_Pred^.ln_Succ := node^.ln_Succ;
    node^.ln_Succ := NIL;
    node^.ln_Pred := NIL;
end;

function RemHead(list : pList): pNode;
var
    node : pNode;
begin
    node := list^.lh_Head;
    if (node^.ln_Succ <> NIL) then begin
        node^.ln_Succ^.ln_Pred := node^.ln_Pred;
        node^.ln_Pred^.ln_Succ := node^.ln_Succ;
        node^.ln_Succ := NIL;
        node^.ln_Pred := NIL;
    end else node := NIL;
    RemHead := node;
end;

function RemTail(list : pList): pNode;
var
    node : pNode;
begin
    node := list^.lh_TailPred;
    if (node^.ln_Pred <> NIL) then Remove(node)
       else node := NIL;
    RemTail := node;
end;

{$endif}

FUNCTION AddNewNode(VAR fpclist : pList; Str : PChar): pFPCNode;
VAR
   tempnode : pFPCNode;
BEGIN
   New(tempnode);
   tempnode^.ln_Name := StrAlloc(StrLen(Str)+1);
   IF tempnode^.ln_Name <>  NIL THEN BEGIN
      StrCopy(tempnode^.ln_Name,Str);
      tempnode^.ln_Size := 0;
      tempnode^.ln_Type := 0;
      tempnode^.ln_Pri  := 0;
      AddTail(fpclist,pNode(tempnode));
      AddNewNode := tempnode;
   END ELSE BEGIN
      AddNewNode := NIL;
   END;
END;

FUNCTION AddNewNode(VAR fpclist : pList; Str : String): pFPCNode;
VAR
   tempnode : pFPCNode;
BEGIN
   New(tempnode);
   tempnode^.ln_Name := StrAlloc(Length(Str)+1);
   IF tempnode^.ln_Name <>  NIL THEN BEGIN
      StrPCopy(tempnode^.ln_Name,Str);
      tempnode^.ln_Size := 0;
      tempnode^.ln_Type := 0;
      tempnode^.ln_Pri  := 0;
      AddTail(fpclist,pNode(tempnode));
      AddNewNode := tempnode;
   END ELSE BEGIN
      AddNewNode := NIL;
   END;
END;

PROCEDURE ClearList(VAR fpclist : pList);
VAR
   tempnode : pFPCNode;
   dummy    : pNode;
BEGIN
   WHILE fpclist^.lh_Head <> @fpclist^.lh_Tail DO BEGIN
       tempnode := pFPCNode(fpclist^.lh_Head);
       if tempnode <> nil then begin
           if tempnode^.ln_Name <> nil then begin
              StrDispose(tempnode^.ln_Name);
           end;
           dummy := RemHead(fpclist);
           Dispose(tempnode);
       end;
   END;
END;

FUNCTION CopyList(fpclist : pList): pList;
VAR
    templist : pList;
    tempnode : pFPCNode;
    i, dummy : Longint;
    added    : pFPCNode;
BEGIN
    CreateList(templist);
    i := NodesInList(fpclist);
    tempnode := pFPCNode(fpclist^.lh_Head);
    FOR dummy := 1 TO i DO BEGIN
       added := AddNewNode(templist,tempnode^.ln_Name);
       tempnode := pFPCNode(tempnode^.ln_Succ);
    END;
    IF added = NIL THEN BEGIN
       CopyList := NIL;
    END ELSE BEGIN
       CopyList := templist;
    END;
END;

PROCEDURE CreateList(VAR fpclist : pList);
BEGIN
    New(fpclist);
    NewList(fpclist);
END;

PROCEDURE DeleteNode(ANode : pFPCNode);
BEGIN
   IF Assigned(ANode)THEN BEGIN
       IF Assigned(ANode^.ln_Name)THEN BEGIN
            StrDispose(ANode^.ln_Name);
       END;
       Remove(pNode(ANode));
       Dispose(ANode);
   END;
END;

{ remove all nodes, list is killed }
PROCEDURE DestroyList(VAR fpclist : pList);
VAR
   tempnode : pFPCNode;
   dummy    : pNode;
BEGIN
 WHILE fpclist^.lh_Head <> @fpclist^.lh_Tail DO BEGIN
       tempnode := pFPCNode(fpclist^.lh_Head);
       if Assigned(tempnode) then begin
           if Assigned(tempnode^.ln_Name) then begin
              {$ifdef showall}
                  write('releasing ');
                  writeln(tempnode^.ln_Name);
              {$endif}
              StrDispose(tempnode^.ln_Name);
           end;
           dummy := RemHead(fpclist);
           {$ifdef showall}
              writeln('Disposing node');
           {$endif}
           Dispose(tempnode);
       end;
   END;
   if Assigned(fpclist) then begin
      {$ifdef showall}
          writeln('Disposing of list');
      {$endif}
      Dispose(fpclist);
      fpclist := nil;
   end;
END;

FUNCTION FindNodeData(fpclist : pList; data : PChar): pFPCNode;
VAR
    temp : pFPCNode;
    result : pFPCNode;
BEGIN
    result := NIL;
    IF fpclist^.lh_Head^.ln_Succ <> NIL THEN BEGIN
        temp := pFPCNode(fpclist^.lh_Head);
        WHILE (temp^.ln_Succ <> NIL) DO BEGIN
            IF (StrIComp(temp^.ln_Name,data)=0) THEN BEGIN
                result := temp;
                break;
            END;
            temp := pFPCNode(temp^.ln_Succ);
        END;
    END;
    FindNodeData := result;
END;

FUNCTION FindNodeData(fpclist : pList; data : String): pFPCNode;
VAR
    temp : pFPCNode;
    result : pFPCNode;
    p : PChar;
BEGIN
    result := NIL;
    p := StrAlloc(length(data)+1);
    StrPCopy(p,data);
    IF fpclist^.lh_Head^.ln_Succ <> NIL THEN BEGIN
        temp := pFPCNode(fpclist^.lh_Head);
        WHILE (temp^.ln_Succ <> NIL) DO BEGIN
            IF (StrIComp(temp^.ln_Name,p)=0) THEN BEGIN
                result := temp;
                break;
            END;
            temp := pFPCNode(temp^.ln_Succ);
        END;
    END;
    StrDispose(p);
    FindNodeData := result;
END;

FUNCTION GetFirstNode(fpclist : pList): pFPCNode;
var
    head : pFPCNode;
BEGIN
    head := pFPCNode(fpclist^.lh_Head);
    if head^.ln_Succ <> nil then begin
        GetFirstNode := pFPCNode(head);
    end else GetFirstNode := nil;
END;

FUNCTION GetLastNode(fpclist : pList): pFPCNode;
var
    tail : pFPCNode;
BEGIN
    tail := pFPCNode(fpclist^.lh_TailPred);
    if tail^.ln_Pred <> nil then begin
        GetLastNode := pFPCNode(tail);
    end else GetLastNode := nil;
END;

FUNCTION GetNextNode( ANode : pFPCNode): pFPCNode;
var
    next : pFPCNode;
BEGIN
    next := pFPCNode(ANode^.ln_Succ);
    if next^.ln_Succ <> nil then begin
       GetNextNode := pFPCNode(next);
    end else GetNextNode := nil;
END;

FUNCTION GetNodeData(Anode : pFPCNode): PChar;
BEGIN
   IF ANode <> NIL THEN BEGIN
       IF ANode^.ln_Name <> NIL THEN BEGIN
           GetNodeData := ANode^.ln_Name;
       END ELSE BEGIN
           GetNodeData := NIL;
       END;
   END;
END;

FUNCTION GetNodeNumber(fpclist : pList; num : Longint): pFPCNode;
VAR
   dummy : Longint;
   tempnode : pFPCNode;
BEGIN
    IF num <= NodesInList(fpclist) then begin
       tempnode := pFPCNode(fpclist^.lh_Head);
       FOR dummy := 1 TO num DO BEGIN
          tempnode := pFPCNode(tempnode^.ln_Succ);
       END;
       GetNodeNumber := pFPCNode(tempnode);
    END ELSE BEGIN
       GetNodeNumber := NIL;
    END;
END;

FUNCTION GetPrevNode( ANode : pFPCNode): pFPCNode;
var
    prev : pFPCNode;
BEGIN
    prev := pFPCNode(ANode^.ln_Pred);
    if prev^.ln_Pred <> nil then begin
       GetPrevNode := pFPCNode(prev);
    end else GetPrevNode := nil;
END;

FUNCTION InsertNewNode(var fpclist : pList; data : PChar; Anode : pFPCNode): pFPCNode;
VAR
    dummy    : pFPCNode;
BEGIN
    dummy := AddNewNode(fpclist,data);
    IF dummy <> NIL THEN BEGIN
        IF (ANode <> NIL) THEN BEGIN
            Remove(pNode(dummy));
{$ifdef Amiga}
            ExecInsert(fpclist,pNode(dummy),pNode(Anode));
{$else}
            Insert(fpclist,pNode(dummy),pNode(Anode));
{$endif}
        END;
        InsertNewNode := dummy;
    END ELSE begin
        InsertNewNode := NIL;
    END;
END;

FUNCTION InsertNewNode(var fpclist : pList; data : String; Anode : pFPCNode): pFPCNode;
VAR
    dummy    : pFPCNode;
BEGIN
    dummy := AddNewNode(fpclist,data);
    IF dummy <> NIL THEN BEGIN
        IF (ANode <> NIL) THEN BEGIN
            Remove(pNode(dummy));
{$ifdef Amiga}
            ExecInsert(fpclist,pNode(dummy),pNode(Anode));
{$else}
            Insert(fpclist,pNode(dummy),pNode(Anode));
{$endif}
        END;
        InsertNewNode := dummy;
    END ELSE begin
        InsertNewNode := NIL;
    END;
END;

PROCEDURE ListToBuffer(fpclist : pList; VAR buf : PChar);
VAR
   i     : Longint;
   dummy : Longint;
   tempnode : pFPCNode;
BEGIN
   buf[0] := #0;
   i := NodesInList(fpclist);
   tempnode := pFPCNode(fpclist^.lh_Head);
   FOR dummy := 1 TO i DO BEGIN
      IF tempnode^.ln_Name <> NIL THEN BEGIN
         strcat(buf,tempnode^.ln_Name);
         IF dummy < i THEN BEGIN
            StrCat(buf,PChar(';'#0));
         END;
      END;
      tempnode := pFPCNode(tempnode^.ln_Succ);
   END;
END;

FUNCTION MergeLists(firstlist , secondlist : pList): pList;
VAR
    templist : pList;
    tempnode : pFPCNode;
    i, dummy : Longint;
    added    : pFPCNode;
BEGIN
    CreateList(templist);
    i := NodesInList(firstlist);
    tempnode := pFPCNode(firstlist^.lh_Head);
    FOR dummy := 0 TO i DO BEGIN
       added := AddNewNode(templist,tempnode^.ln_Name);
       tempnode := pFPCNode(tempnode^.ln_Succ);
    END;
    IF added = NIL THEN BEGIN
       MergeLists := NIL;
    END ELSE BEGIN
       i := NodesInList(secondlist);
       tempnode := pFPCNode(secondlist^.lh_Head);
       FOR dummy := 0 TO i DO BEGIN
          added := AddNewNode(templist,tempnode^.ln_Name);
          tempnode := pFPCNode(tempnode^.ln_Succ);
       END;
       IF added = NIL THEN BEGIN
          MergeLists := NIL;
       END ELSE BEGIN
          MergeLists := templist;
       END;
    END;
END;

{ move a node to the bottom of the list }
PROCEDURE MoveNodeBottom(var fpclist: pList; ANode : pFPCNode);

BEGIN
    IF ANode^.ln_Succ <> NIL THEN BEGIN
        Remove(pNode(ANode));
        AddTail(fpclist,pNode(ANode));
    END;
END;

{ move a node down the list }
PROCEDURE MoveNodeDown(VAR fpclist : pList; ANode : pFPCNode);
VAR
    suc : pFPCNode;
BEGIN
    suc := pFPCNode(ANode^.ln_Succ);
    IF (ANode <> NIL) AND (suc <> NIL) THEN BEGIN
        Remove(pNode(ANode));
{$ifdef Amiga}
        ExecInsert(fpclist,pNode(ANode),pNode(suc));
{$else}
        Insert(fpclist,pNode(ANode),pNode(suc));
{$endif}
    END;
END;

{ move a node up to the top of the list }
PROCEDURE MoveNodeTop(VAR fpclist: pList; ANode : pFPCNode);
BEGIN
    IF ANode^.ln_Pred <> NIL THEN BEGIN
        Remove(pNode(ANode));
        AddHead(fpclist,pNode(ANode));
    END;
END;

{ move a node up the list }
PROCEDURE MoveNodeUp(VAR fpclist : pList; ANode : pFPCNode);
VAR
    prev : pFPCNode;
BEGIN
    prev := pFPCNode(Anode^.ln_Pred);
    IF (ANode <> NIL) AND (prev <> NIL) THEN BEGIN
        prev := pFPCNode(prev^.ln_Pred);
        Remove(pNode(ANode));
{$ifdef Amiga}
        ExecInsert(fpclist,pNode(ANode),pNode(prev));
{$else}
        Insert(fpclist,pNode(ANode),pNode(prev));
{$endif}
    END;
END;

FUNCTION NodesInList(fpclist :  pList): Longint;
VAR
   tempnode : pFPCNode;
   i        : Longint;
BEGIN
    i := 0;
    tempnode := pFPCNode(fpclist^.lh_Head);
    WHILE tempnode^.ln_Succ <> NIL DO BEGIN
        tempnode := pFPCNode(tempnode^.ln_Succ);
        INC(i);
    END;
    NodesInList := i;
END;

PROCEDURE PrintList(fpclist : pList);
VAR
   i     : Longint;
   dummy : Longint;
   tempnode : pFPCNode;
BEGIN

   i := NodesInList(fpclist);

   tempnode := pFPCNode(fpclist^.lh_Head);
   FOR dummy := 1 TO i DO BEGIN
       IF tempnode^.ln_Name <> NIL THEN BEGIN
          WriteLN(tempnode^.ln_Name);
       END;
       tempnode := pFPCNode(tempnode^.ln_Succ);
   END;
END;

PROCEDURE RemoveDupNode( VAR fpclist :  pList);
VAR
   tempnode : pFPCNode;
   nextnode : pFPCNode;
BEGIN
    tempnode := pFPCNode(fpclist^.lh_Head);

    WHILE tempnode^.ln_Succ <> NIL DO BEGIN
         nextnode := pFPCNode(tempnode^.ln_Succ);
        IF (StrIComp(tempnode^.ln_Name,nextnode^.ln_Name)=0) THEN BEGIN
            DeleteNode(tempnode);
        END;
        tempnode := nextnode;
    END;
END;

PROCEDURE RemoveLastNode(VAR fpclist : pList);
VAR
   tempnode : pFPCNode;
   dummy    : pNode;
BEGIN
   tempnode := pFPCNode(fpclist^.lh_TailPred);
   if tempnode^.ln_Name <> nil then begin
      StrDispose(tempnode^.ln_Name);
   end;
   dummy := RemTail(fpclist);
   Dispose(tempnode);
END;

{ get the total size allocated by list }
{ size is WITH ';' between the strings }
FUNCTION SizeOfList(fpclist : pList): Longint;
VAR
   i     : Longint;
   dummy : Longint;
   tempnode : pFPCNode;
   tsize    : Longint;
BEGIN
   tsize := 0;
   i := NodesInList(fpclist);

    tempnode := pFPCNode(fpclist^.lh_Head);
    FOR dummy := 1 TO i DO BEGIN
        IF tempnode^.ln_Name <> NIL THEN BEGIN
            tsize := tsize + (StrLen(tempnode^.ln_Name)+1)
        END;
        tempnode := pFPCNode(tempnode^.ln_Succ);
    END;
    SizeOfList := tsize;
END;

{ sort the list using a bubble sort }
PROCEDURE SortList(VAR fpclist: pList);

VAR
    notfinished : BOOLEAN;
    first, second : pFPCNode;
    n,i : Longint;

BEGIN
    IF fpclist^.lh_Head^.ln_Succ <> NIL THEN BEGIN
        notfinished := True;
        i := NodesInList(fpclist);
        WHILE (notfinished) DO BEGIN
            notfinished := FALSE;
            first := pFPCNode(fpclist^.lh_Head);
            IF first <> NIL THEN BEGIN
                n := 1;
                second := pFPCNode(first^.ln_Succ);
                WHILE n <> i DO BEGIN
                    n := n + 1;
                    IF (StrIComp(first^.ln_Name,second^.ln_Name)>0) THEN BEGIN
                        Remove(pNode(first));
{$ifdef Amiga}
                        ExecInsert(fpclist,pNode(first),pNode(second));
{$else}
                        Insert(fpclist,pNode(first),pNode(second));
{$endif}
                        notfinished := True;
                    END ELSE
                        first := second;
                    second := pFPCNode(first^.ln_Succ);
                END;
            END;
        END;
    END;
END;

FUNCTION UpDateNode(ANode : pFPCNode; data : PChar): BOOLEAN;
VAR
   result : BOOLEAN;
BEGIN
    IF ANode^.ln_Succ <> NIL THEN BEGIN
        IF ANode^.ln_Name <> NIL THEN BEGIN
            StrDispose(ANode^.ln_Name);
            ANode^.ln_Name := StrAlloc(StrLen(data)+1);
            IF ANode^.ln_Name <> NIL THEN BEGIN
                StrCopy(ANode^.ln_Name,data);
                result := True;
            END ELSE BEGIN
                result := FALSE;
            END;
         END;
     END;
     UpDateNode := result;
END;

FUNCTION UpDateNode(ANode : pFPCNode; data : String): BOOLEAN;
VAR
   result : BOOLEAN;
BEGIN
    IF ANode^.ln_Succ <> NIL THEN BEGIN
        IF ANode^.ln_Name <> NIL THEN BEGIN
            StrDispose(ANode^.ln_Name);
            ANode^.ln_Name := StrAlloc(Length(data)+1);
            IF ANode^.ln_Name <> NIL THEN BEGIN
                StrPCopy(ANode^.ln_Name,data);
                result := True;
            END ELSE BEGIN
                result := FALSE;
            END;
         END;
     END;
     UpDateNode := result;
END;

function FileToList(thefile : PChar; var thelist : pList): boolean;
begin
    FileToList := FileToList(strpas(thefile), thelist);
end;

function FileToList(thefile : String; var thelist : pList): boolean;
var
   Inf : Text;
   tempnode : pFPCNode;
   buffer : PChar;
   buf : Array [0..500] of Char;
begin
   buffer := @buf;
   Assign(Inf, thefile);
   {$I-}
   Reset(Inf);
   {$I+}
   if IOResult = 0 then begin
      while not eof(Inf) do begin
      { I don't want end of lines here (for use with amiga listviews)
        just change this if you need newline characters.
      }
         Read(Inf, buffer);
         tempnode := AddNewNode(thelist,buffer);
         Readln(inf, buffer);
      end;
      CLose(Inf);
      FileToList := true;
   end else FileToList := false;
end;

function ListToFile(TheFile : PChar; thelist : pList): Boolean;
begin
    ListToFile := ListToFile(strpas(TheFile), thelist);
end;

function ListToFile(TheFile : String; thelist : pList): Boolean;
VAR
    Out      : Text;
    i        : Longint;
    dummy    : Longint;
    tempnode : pFPCNode;
begin
    Assign(Out, TheFile);
    {$I-}
    Rewrite(Out);
    {$I+}
    if IOResult = 0 then begin
       i := NodesInList(thelist);
       IF i > 0 THEN BEGIN
          tempnode := pFPCNode(thelist^.lh_Head);
          FOR dummy := 1 TO i DO BEGIN
             IF tempnode^.ln_Name <> NIL THEN BEGIN
                {
                  Have to check the strlen here, if it's an
                  empty pchar fpc will write out a #0
                }
                if strlen(tempnode^.ln_Name) > 0 then
                   WriteLN(Out,tempnode^.ln_Name)
                else writeln(Out);
             END;
             tempnode := pFPCNode(tempnode^.ln_Succ);
          END;
        END;
        Close(Out);
        ListToFile := True;
    END Else ListToFile := False;
END;


end.