Procedure Check_Last_Read(const fn:string; I:byte);
var x,y,fz    : longint;
    f         : file of Snc_File_Type;
    scan      : Snc_File_Type;
begin
assign(f,FN);
{$i-} reset(f); {$i+}
if ioresult<>0 then rewrite(F);
end;


Procedure Message_Area_List;
var msgarea:mcis;
    i:B_File_type;
    x,y:integer;
    fa:string[3];
begin
fillchar(msgarea,sizeof(msgarea),#0);
msgarea[1].mci:='~AN';
msgarea[2].mci:='~AT';
msgarea[3].mci:='~NM';
msgarea[4].mci:='~SP';
x:=1;
printfile(Config.Textfile_Dir+'marea.top',msgarea,false);
for x:=1 to total_Bases('MSGCONF1') do
begin
read_base('MSGCONF1',X,I);
msgarea[1].str:=strr(x);
msgarea[2].str:=i.name;
msgarea[3].str:=strr(total_msg(i.datafile));
msgarea[4].str:='none';
printfile(Config.Textfile_Dir+'marea.mid',msgarea,false);
end;

printfile(Config.Textfile_Dir+'marea.bot',msgarea,false);
PRINT('|B0|12Message Base ..-> ');
buflen:=3;
sreadln(fa);
buflen:=255;
if valu(fa) in [1..total_bases('MSGCONF1')] then
read_base('MSGCONF1',valu(fa),currentbase);
end;

Function Total_Bases(const fn:string):Byte;
Var Bf:File Of B_File_Type;
Begin
     Assign(Bf,config.board_dir+Fn+'.BF');
     {$I-} Reset(Bf); {$I+}
     If Ioresult<>0 then {$I-} Rewrite(Bf); {$I+}
     Total_Bases:=Filesize(Bf);
     close(bf);
end;

Function Read_Base(const fn:string; BaseNum:Byte; Var Base:B_File_Type):Boolean;
Var Bf:File Of B_File_Type;
    Temp:Boolean;
Begin
     Temp:=False;
     Assign(Bf,config.board_dir+Fn+'.BF');
     {$I-} Reset(Bf); {$I-}
     If Ioresult<>0 then {$I-} Rewrite(Bf); {$I+}
     if BaseNum <= Filesize(bf) then begin
     Temp:=True;
     seek(bf,basenum-1);
     read(Bf,base);
     end else Temp:=False;
     Close(Bf);
     read_base:=temp;
     currentbasenum:=basenum;
   end;

Procedure Write_Base(const fn:string; Base:B_File_Type);
Var Bf:File Of B_File_Type;
Begin
     Assign(Bf,config.board_dir+Fn+'.BF');
     {$I-} Reset(Bf); {$I+}
     If Ioresult<>0 then {$I-} Rewrite(Bf); {$I+}
     Seek(Bf,FileSize(Bf));
     Write(Bf,Base);
     Close(Bf);
end;

Procedure Modify_Base(const fn:string; Base:B_File_Type; BaseNum:Byte);
Var Bf:File Of B_File_Type;
    TB:B_File_Type;
Begin
     Assign(Bf,config.board_dir+Fn+'.BF');
     {$I-} Reset(Bf); {$I+}
     If Ioresult<>0 then {$I-} Rewrite(Bf); {$I+}
     if BaseNum <= Filesize(Bf) then begin
     FillChar(TB,sizeof(B_File_Type),#0);
     Seek(Bf,BaseNum-1);
     Write(Bf,Tb);
     Seek(Bf,BaseNum-1);
     Write(Bf,Base);
     end;
     close(bf);
     end;


Procedure Switch_Base(const fn:string; BaseA,BaseB:Byte);
Var Bf:File Of B_File_Type;
    B1:B_File_Type;
    B2:B_File_Type;
    B3:B_File_Type;
Begin
     Assign(Bf,config.board_dir+Fn+'.BF');
     {$I-} reset(Bf); {$I+}
     If Ioresult<>0 then {$I-} rewrite(bf); {$I+}
     If (BaseA <= Filesize(Bf)) and (BaseB <= Filesize(Bf)) then
     begin
       Fillchar(B3,Sizeof(B_File_Type),#0);
       Seek(Bf,BaseA-1);
       Read(Bf,B1);
       Seek(Bf,BaseB-1);
       Read(Bf,B2);
       Seek(Bf,BaseA-1);
       write(Bf,B3);
       Seek(Bf,BaseA-1);
       write(Bf,b2);
       Seek(Bf,BaseB-1);
       write(bf,b3);
       Seek(Bf,BaseB-1);
       write(bf,b1);
       end;
       close(bf);
     end;



Procedure Delete_Base(const fn:string; BaseNum:Byte);
Var Bf:File Of B_File_Type;
    Temp:B_File_Type;
    Count:Byte;
    RecNum:Byte;
Begin
     Assign(Bf,config.board_dir+fn+'.BF');
     {$I-} reset(bf); {$I+}
     if ioresult<>0 then {$I-} rewrite(Bf); {$I+}
     If BaseNum <= Filesize(Bf) then
     Begin
       for Count:=BaseNum to Filesize(Bf)-1 do begin
        seek(bf,count);
        read(bf,Temp);
        Seek(bf,count-1);
        write(bf,Temp);
       end;
      Seek(bf,filesize(bf));
      truncate(bf);
     end;
   close(bf);
 end;



Procedure Insert_Base(const fn:string; Base:B_File_Type; BaseNum:Byte);
Var Bf:File Of B_File_Type;  { I hate Base Inserting! }
    Base2:B_File_Type;       { This May Or May Not Work }
    Count:Byte;
    RecNum:Byte;
Begin
     Write_Base(Fn,Base);
     For Count:=BaseNum To Total_Bases(fn) do
     Switch_Base(Fn,BaseNum+Count,Total_Bases(fn));
end;


Function Total_Msg(const fn:string):Integer;
Var Nf:File Of M_Ndx_Type;
Begin
     Assign(Nf,config.board_dir+Fn+'.NDX');
     {$I-} Reset(Nf); {$I+}
     If Ioresult<>0 then {$I-} Rewrite(Nf); {$I+}
     Total_Msg:=Filesize(Nf);
     Close(Nf);
End;


Function Read_Ndx(const fn:string; Num:Longint; var Ndx:M_Ndx_Type):Boolean;
var
   NF:File Of M_NDX_Type;
   TV:Boolean;
begin
     TV:=False;
     Fillchar(Ndx,sizeof(Ndx),#0);
     Assign(Nf,config.board_dir+fn+'.NDX');
     {$I-} reset(nf); {$I+}
     If Ioresult<>0 then {$I-} rewrite(nf); {$I+}
     if filesize(nf)=0 then begin
                            read_ndx:=false;
                            close(nf);
                            exit;
                            end;
     If Num <= 0 Then Num:=1;
     If Num-1  <= filesize(nf) then
        Begin
             Seek(Nf,Num-1);
             Read(Nf,Ndx);
             TV:=True;
        end else tv:=false;
    read_ndx:=tv;
    close(Nf);
      end;


Procedure Write_Ndx(const fn:string; Ndx:M_Ndx_Type);
Var NF:File of M_Ndx_Type;
Begin
     Assign(Nf,config.board_dir+Fn+'.NDX');
     {$I-} reset(Nf); {$I+}
     If Ioresult<>0 then {$I-} Rewrite(Nf); {$I+}
     Seek(Nf,filesize(Nf));
     Write(Nf,Ndx);
     close(nf);
   End;

Procedure Read_Body(const fn:string; Ndx:M_Ndx_Type);
Var Bf:Text;
    TmpVar:Integer;
    TmpVar2:String;
Begin
    Assign(Bf,config.board_dir+Fn+'.BOD');
    {$I-} reset(Bf); {$I+}
    if ioresult<>0 then {$I-} rewrite(Bf); {$I+}
    TextSeek(Bf,Ndx.Filepos);
    For TmpVar:=1 to Ndx.Numlines do
     begin
          readln(bf,TmpVar2);
          if length(tmpvar2)=80 then print(tmpvar2) else
          println(tmpVar2);
     end;
   close(Bf);
   end;

Procedure Write_Body(const Fn:String; var Ndx:M_Ndx_Type);
Var Bf:Text;
    y,TempVar:Integer;
    s:string[80];
Begin
     Assign(Bf,config.board_dir+fn+'.BOD');
     {$I-} Reset(Bf); {$I+}
     If Ioresult<>0 then {$I-} rewrite(Bf); {$I+}
     Ndx.Filepos:=Textfilesize(Bf);
     TextSeek(Bf,Ndx.Filepos);
     append(bf);
     ndx.numlines:=totallines;
     For TempVar:=1 to Ndx.Numlines do begin
                                       s:=M_Buff^[TempVar];
                                       for y:=80 downto 1 do
                                       if s[y]=#0 then dec(s[0]);
                                       Writeln(Bf,s);
                                       end;
     close(Bf);
     end;
