home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / BTP20.ZIP / CRUNCH2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-06-10  |  8.7 KB  |  297 lines

  1. PROGRAM Crunch2;               { (c) 1993 John C. Leon   last updated 6/9/93 }
  2.  
  3. {Uses Step Next Extended to retrieve 5 records at a time, then Insert Extended
  4.  to insert 5 records at a time.}
  5.  
  6. {$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}
  7. {$X+}
  8.  
  9. USES
  10.    Crt, DOS, Objects {for FNameStr}, BTP;
  11.  
  12. CONST
  13.    NOTICE1 = 'Crunch2                   (C) 1993 John C. Leon.  All Rights Reserved.';
  14.    NOTICE2 = '----------------------------------------------------------------------';
  15.    NumRecordsinOp  :integer = 5; {MaxInsBufLength = (2+(2*Number of Insertions) + }
  16.    MaxInsBufLength = 20462;      { (MaxFixedRecLength*Number of Insertions)       }
  17.    OwnerName  : string = '';
  18.    NumInserted: longint = 0;
  19.  
  20. TYPE
  21.    POrgFile      = ^TOrgFile;
  22.    TOrgFile      = object(BFileExt)
  23.                       function BTExt(OpCode, Key: integer): integer; virtual;
  24.                       end;
  25.  
  26.    TCopyBuffer = record
  27.                     Count: word;
  28.                     Repeater: array[1..MaxInsBufLength-2] of byte;
  29.                     end;
  30.    PCopyFile     = ^TCopyFile;
  31.    TCopyFile     = object(BSized)
  32.                       ExtDBuffer: ^TCopyBuffer;
  33.                       constructor Init(UserFileName: FNameStr; OpenMode: integer;
  34.                                        OName: TOwnerName; BuffSize:integer);
  35.                       function BTExt(OpCode, Key: integer): integer; virtual;
  36.                       destructor Done; virtual;
  37.                       end;
  38.  
  39. VAR
  40.    OrgName, CopyName     : string[79];
  41.    OrgFile               : POrgFile;
  42.    CopyFile              : PCopyFile;
  43.    Counter,  Counter1,
  44.    CopyOfs, OrgOfs,
  45.    RecordLength,
  46.    Remainder, x, y       : integer;
  47.    NumberOps             : longint;
  48.    LoRecordLength,
  49.    HiRecordLength        : byte;
  50.  
  51.  
  52. function TOrgFile.BTExt(OpCode, Key: integer): integer;
  53. begin
  54.    {call ancestor method to set buffer lengths & to structure send buffer}
  55.    inherited BTExt(OpCode, Key);
  56.    BTExt   := Btrv(OpCode, PosBlk, ExtDBuffer^.Entire, DBufferLen,
  57.                    VarNotRequired, Key);
  58. end;
  59.  
  60. constructor TCopyFile.Init(UserFileName: FNameStr; OpenMode: integer;
  61.                                          OName: TOwnerName; BuffSize: integer);
  62. begin
  63.    inherited Init(UserFileName, OpenMode, OName, BuffSize);
  64.    ExtDBuffer := DBuffer;         { Force a record structure on the DBuffer. }
  65. end;
  66.  
  67. destructor TCopyFile.Done;
  68. begin
  69.    inherited Done;
  70. end;
  71.  
  72. function TCopyFile.BTExt(OpCode, Key: integer): integer;
  73. var
  74.    ExtBufLen: integer;
  75. begin
  76.    ExtBufLen := 2 + (2 * ExtDBuffer^.Count) + (Specs.RecLen * ExtDBuffer^.Count);
  77.    BTExt := Btrv(OpCode, PosBlk, ExtDBuffer^, ExtBufLen, KBuffer^, Key);
  78. end;
  79.  
  80. procedure VerifyTargetOverWrite;
  81. var
  82.    Response: string;
  83.    DirInfo : SearchRec;
  84. begin
  85. findfirst(CopyName, archive, DirInfo);
  86. if doserror = 0 then
  87.    begin
  88.    writeln;
  89.    write('Target file ', CopyName, ' exists.  Overwrite? (Y/N): ');
  90.    readln(Response);
  91.    Response[1] := upcase(Response[1]);
  92.    if Response[1] = 'N' then
  93.       begin
  94.       OrgFile^.Close;
  95.       dispose(OrgFile, Done);
  96.       writeln('Program aborted.');
  97.       halt(4);
  98.       end;
  99.    if Response[1] <> 'Y' then
  100.       begin
  101.       writeln;
  102.       writeln('Invalid response ... program aborted.');
  103.       OrgFile^.Close;
  104.       dispose(OrgFile, Done);
  105.       writeln('Program aborted');
  106.       halt(5);
  107.       end;
  108.    end;
  109. end;
  110.  
  111. procedure PrintNotices;
  112. begin
  113.    writeln(NOTICE1);
  114.    writeln(NOTICE2);
  115.    writeln;
  116. end;
  117.  
  118.  
  119. (* Begin MAIN program code *)
  120. (* ------------------------------------------------------------------------ *)
  121. BEGIN
  122.  
  123. if not IsBtrieveLoaded then
  124.    begin
  125.    writeln('Please load Btrieve before loading this program.');
  126.    halt(1);
  127.    end;
  128.  
  129. clrscr;
  130. PrintNotices;
  131.  
  132. write('Name of file to copy from: ');
  133. readln(OrgName);
  134. if OrgName = '' then
  135.    begin
  136.    writeln('No source file name entered ... program aborted');
  137.    halt(2);
  138.    end;
  139. for Counter := 1 to length(OrgName) do
  140.    OrgName[Counter] := upcase(OrgName[Counter]);
  141.  
  142. write('Name of file to create and populate from file ''', OrgName,''': ');
  143. readln(CopyName);
  144. if CopyName = '' then
  145.    begin
  146.    writeln('No target file name entered ... program aborted');
  147.    halt(3);
  148.    end;
  149. for Counter := 1 to length(CopyName) do
  150.    CopyName[Counter] := upcase(CopyName[Counter]);
  151.  
  152. { Open original file in read only mode }
  153. OrgFile  := new(POrgFile, Init(OrgName, ReadOnly, OwnerName));
  154. case BStatus of
  155.    51:  begin
  156.         dispose(OrgFile, Done);
  157.         write('Enter source file''s owner name: ');
  158.         readln(OwnerName);
  159.         if OwnerName = '' then
  160.            begin
  161.            writeln('Owner name not provided ... program aborted');
  162.            halt(6);
  163.            end;
  164.         OrgFile := new(POrgFile, Init(OrgName, ReadOnly, OwnerName));
  165.         if BStatus = 51 then
  166.            begin
  167.            writeln(OwnerName, ' not accepted by Btrieve as owner name.');
  168.            writeln('Program aborted.');
  169.            dispose(OrgFile, Done);
  170.            halt(6);
  171.            end;
  172.         end;
  173.     FileNotFound: begin
  174.                   writeln('Source file ', OrgName, ' not found.');
  175.                   writeln('Program aborted.');
  176.                   dispose(OrgFile, Done);
  177.                   halt(7);
  178.                   end;
  179.     0:    {Don't do anything if object initialized w/no errors, but}
  180.     else  {catch all other errors w/following code.}
  181.        begin
  182.         writeln('Error opening ', OrgName, '.  Status: ', BStatus);
  183.         writeln('Program aborted.');
  184.         halt(8);
  185.         end;
  186.     end;
  187.  
  188.  
  189. if OrgFile^.NumRecs = 0 then                     {don't proceed if empty file}
  190.    begin
  191.    writeln('No records in ', OrgName, '.  CRUNCH aborted.');
  192.    OrgFile^.Close;
  193.    dispose(OrgFile, Done);
  194.    halt(9);
  195.    end;
  196.  
  197. if (OrgFile^.Specs.FileFlags and VarLength) = VarLength then
  198.    begin
  199.    writeln(OrgName, ' is a variable length file.  Can''t process.');
  200.    OrgFile^.Close;
  201.    dispose(OrgFile, Done);
  202.    halt(10);
  203.    end;
  204.  
  205. RecordLength   := OrgFile^.Specs.RecLen;
  206. LoRecordLength := lo(RecordLength);
  207. HiRecordLength := hi(RecordLength);
  208.  
  209. {Set up required filter and extractor data fields in OrgFile^:
  210.    Max number of records to skip, # logic terms, # records to retrieve w/
  211.    each call, and number of fields to extract.}
  212.  
  213. OrgFile^.SetTerms(1, 0, 5, 1);
  214.  
  215. {Set up required minimum of one extractor spec in collection.  Note that
  216.  OrgFile's constructor initialized the collection.}
  217.  
  218. with OrgFile^.ExtractorSpec^ do
  219.    insert(new(PExtSpec, Init(OrgFile^.Specs.RecLen, 0)));
  220.  
  221. {Exit if target exists, and user doesn't wanna overwrite.}
  222. VerifyTargetOverWrite;
  223.  
  224. BStatus := CloneFile(OrgName, CopyName, Retain, '');
  225. if BStatus = Zero then
  226.    begin
  227.    writeln(CopyName, ' created successfully.');
  228.    writeln;
  229.    end
  230.    else
  231.    begin
  232.    writeln('Error creating ', CopyName, '.  Status = ', BStatus, '.');
  233.    OrgFile^.Close;
  234.    dispose(OrgFile, Done);
  235.    halt(11);
  236.    end;
  237.  
  238. {Open new copy of file in accelerated mode.}
  239. CopyFile := new(PCopyFile, Init(CopyName, Accel, '', sizeof(TExtDBuffer)));
  240.  
  241. OrgOfs   := 7;  {We know the length of record in this case, and don't
  242.                  care about position, so skip the six lead bytes of each
  243.                  record.}
  244. CopyOfs  := 1;
  245.  
  246. Remainder := OrgFile^.NumRecs MOD NumRecordsinOp;
  247. NumberOps := OrgFile^.NumRecs DIV NumRecordsinOp;
  248. if Remainder <> 0 then inc(NumberOps);
  249.  
  250. writeln('Total # records in ', OrgName, ': ', OrgFile^.NumRecs);
  251. write('Number records inserted in ', CopyName, ': ');
  252. x := wherex;
  253. y := wherey;
  254.  
  255. for Counter := 1 to NumberOps do
  256.    begin
  257.    BStatus := OrgFile^.BTExt(BStepNextExt, Zero);
  258.    NumRecordsinOp := OrgFile^.ExtDBuffer^.NumRecs; {# recs ret'd by StepNextExt}
  259.  
  260.    {Build buffer for insertion.}
  261.    for Counter1 := 1 to NumRecordsinOp do
  262.       begin
  263.       CopyFile^.ExtDBuffer^.Repeater[CopyOfs] := LoRecordLength;
  264.       inc(CopyOfs);
  265.       CopyFile^.ExtDBuffer^.Repeater[CopyOfs] := HiRecordLength;
  266.       inc(CopyOfs);
  267.       move(OrgFile^.ExtDBuffer^.Repeater[OrgOfs],
  268.            CopyFile^.ExtDBuffer^.Repeater[CopyOfs], RecordLength);
  269.       CopyOfs := CopyOfs + RecordLength;
  270.       OrgOfs  := OrgOfs + RecordLength + 6;
  271.       end;
  272.  
  273.    CopyFile^.ExtDBuffer^.Count := NumRecordsinOp;
  274.    BStatus := CopyFile^.BTExt(BInsertExt, Zero);
  275.    inc(NumInserted, NumRecordsinOp);
  276.    if (NumInserted) MOD 5 = 0 then
  277.       begin
  278.       gotoxy(x, y);
  279.       write(NumInserted);
  280.       end;
  281.    CopyOfs := 1;
  282.    OrgOfs  := 7;
  283.  
  284.    end; {for Counter := 1 to NumberOps}
  285.  
  286. gotoxy(1, y);
  287. clreol;
  288. writeln('Inserted ', NumInserted, ' records in ', CopyName);
  289. writeln('DONE...');
  290.  
  291. BStatus := OrgFile^.Close;
  292. BStatus := CopyFile^.Close;
  293. dispose(OrgFile, Done);
  294. dispose(CopyFile, Done);
  295.  
  296. END.
  297.