PROGRAM Crunch1;              { (c) 1991 John C. Leon   last updated 11/4/91 }

{Handles ONLY standard, fixed length Btrieve files.}

{$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}

USES
   BTP;

VAR
   OrgName, CopyName : string[79];
   OrgFile, CopyFile : PBFixed;
   Counter           : word;

(* Begin MAIN program code *)
(* ------------------------------------------------------------------------ *)
BEGIN

write('Name of file to copy from: ');
readln(OrgName);
for Counter := 1 to length(OrgName) do
   OrgName[Counter] := upcase(OrgName[Counter]);

write('Name of file to create and populate from file ''', OrgName,''': ');
readln(CopyName);
for Counter := 1 to length(CopyName) do
   CopyName[Counter] := upcase(CopyName[Counter]);

{ Open original file in read only mode }
OrgFile := new(PBFixed, Init(OrgName, ReadOnly));

if BStatus <> Zero then
   writeln('Error opening ', OrgName)
   else

   begin                     {if original file exists and no error on open op}

   if OrgFile^.NumRecs = 0 then            {don't proceed if empty file}
      begin
      writeln('No records in ', OrgName, '.  CRUNCH1 aborted.');
      halt;
      end;

   if (OrgFile^.Specs.FileFlags and 1) = 1 then    {don't do var length files}
      begin
      writeln(OrgName, ' is a variable length file.  Can''t process.');
      halt;
      end;

   {Create copy of original, using precisely the same specs.}
   BStatus := CloneFile(OrgName, CopyName);
   if BStatus = Zero then
      writeln(CopyName, ' created successfully.')
      else
      begin
      writeln('Error creating ', CopyName, '.  Status = ', BStatus, '.');
      halt;
      end;

   {Open new copy of file in accelerated mode.}
   CopyFile := new(PBFixed, Init(CopyName, Accel));

   {Main loop...read a record, write a record.}
   for Counter:= 1 to OrgFile^.NumRecs do
      begin
      BStatus := OrgFile^.BT(BStepNext, Zero);
      CopyFile^.DBuffer := OrgFile^.DBuffer;
      BStatus := CopyFile^.BT(BInsert, Zero);
      if (Counter MOD 5) = 0 then
         writeln('Inserted total of ', Counter, ' records');
      end;
   if (Counter MOD 5) <> 0 then
      writeln('Inserted total of ', Counter, ' records');
   writeln('DONE...');

   BStatus := OrgFile^.Close;
   BStatus := CopyFile^.Close;

   dispose(OrgFile, Done);
   dispose(CopyFile, Done);

   end;  {if BStatus <> Zero}

END.
