unit BufferUnit;



interface

uses SysUtils;


type
  PURLOrMail = ^TURLOrMail;
//this will be a record for a linked list of string values
  TURLOrMail = Record
    Value: String;
    Next: PURLOrMail;
    Previous: PURLOrMail;
  end;
//Linked list embeded into a class with some routines
  TBuffer = class
    Public
      TheFile:String;
      First: PURLOrMail;
      Last: PURLOrMail;

      function GotIt(thaStr: String): Boolean;
      procedure Add(ThaValue: String);
      procedure Remove(URLOrMail: PURLOrMail);
      procedure Init(Filename: String);
      procedure SaveIt;
  end;

implementation

procedure TBuffer.Init(Filename: String);
var InFile: TextFile;
    TempRecord: PURLOrMail;
begin
    First := Nil;
    Last := Nil;
    TheFile := FileName;
    //should add errorhandling here
    try
        AssignFile(InFile, Filename);
        Reset(InFile);
        While not EOF(InFile) do
        begin
            // in case of no records in list
            If First = Nil Then
            begin
                New(TempRecord);
                Readln(InFile, TempRecord.Value);
                TempRecord.Next := Nil;
                TempRecord.Previous := Nil;
                First := TempRecord;
                Last := TempRecord;
            end
            // in case of records already in list
            else
            begin
                New(TempRecord);
                Readln(InFile, TempRecord.Value);
                TempRecord.Next := Nil;
                Last.Next := TempRecord;
                TempRecord.Previous := Last;
                Last := TempRecord;
            end;
        end;
        CloseFile(InFile);
    finally end;
end;

Procedure TBuffer.Add(ThaValue: String);
Begin
    //again in case of no records in list
    If First = Nil Then
    begin
        new(First);
        First := Last;
        First.Value := ThaValue;
        First.Previous := Nil;
    end
    //and in case of some records existing in list
    else
    begin
        New(Last.Next);
        Last.Next.Previous := Last;
        Last.Next.Next := Nil;
        Last := Last.Next;
        Last.Value := ThaValue;
    end;
End;

function TBuffer.GotIt(ThaStr:String):Boolean;
var
    RetVal: PURLOrMail;
Begin
    RetVal := First;
    Result := False;

    While Retval <> Nil do
    Begin
        If Retval.Value = ThaStr Then Result := True;
// There should be a break keyword in object pascal
// i have to loop to the end of list... maybe there is one,
// i'm only a delphi beginner...
    end;
end;

procedure TBuffer.Remove(URLOrMail: PURLOrMail);
Begin
// again i have to do some cumbersome
// beginning and end of list checking and dual doing everything... i hate it.
    If (URLOrMail.Previous <> Nil) and (URLOrMail.Next <> Nil) then
    begin
        URLOrMail.Previous.Next := URLOrMail.Next;
        URLOrMail.Next.Previous := URLOrMail.Previous;
        Dispose(URLOrMail);
    end
    else
    begin
        if (URLOrMail.Previous = Nil) and (URLOrMail.Next <> Nil) Then
        begin
            First := First.Next;
            First.Previous := Nil;
            Dispose(URLOrMail);
        end;
        if (URLOrMail.Previous <> Nil) and (URLOrMail.Next = Nil) Then
        begin
            Last := Last.Previous;
            Last.Next := Nil;
            Dispose(URLOrMail);
        end;
        if (URLOrMail.Next = Nil) and (URLOrMail.Previous = Nil) Then
        begin
            First := Nil;
            Last := Nil;
            Dispose(URLOrMail);
        end;
    end;
end;

procedure TBuffer.SaveIt;
var F:TextFile;
begin
    AssignFile(F , TheFile);
    //ReWrite deletes the file if it exsists... :)
    ReWrite(F);
    while last <> Nil do
    begin
        Writeln(F, Last.Value);
        Last := Last.Previous;
        Dispose(Last.Next);
        Last.Next := Nil;
    end;
    Writeln(F, First.Value);
    Dispose(First);
end;


end.
