Program Merge;

{***************************************************************************}

{* SUB-PROGRAMS IN ORDER OF APPEARANCE

{*

{* PROCEDURE DeleteTempFiles

{* PROCEDURE DoSortFile

{***************************************************************************}

{*

{* GLOBAL DEFINITIONS

{*

{* USES UNIT Croft,

{* USES UNIT Crt;

{*

{* GLOBAL VARIABLE SortFile,

{* GLOBAL VARIABLE File1,

{* GLOBAL VARIABLE File2,

{* GLOBAL VARIABLE File3: string;

{* GLOBAL VARIABLE F1,

{* GLOBAL VARIABLE F2,

{* GLOBAL VARIABLE F3,

{* GLOBAL VARIABLE SF,

{* GLOBAL VARIABLE TF1,

{* GLOBAL VARIABLE TF2,

{* GLOBAL VARIABLE TF3: text;

{* GLOBAL VARIABLE alpha: integer;

{* GLOBAL VARIABLE InStr,

{* GLOBAL VARIABLE InStr2,

{* GLOBAL VARIABLE DataStr1,

{* GLOBAL VARIABLE DataStr2: string;

{* GLOBAL VARIABLE CheckPage,

{* GLOBAL VARIABLE CheckName: boolean;

{* GLOBAL VARIABLE Page1,

{* GLOBAL VARIABLE Page2: integer;

{* GLOBAL VARIABLE TempStr: array[1..10] of string;

{* GLOBAL VARIABLE TempQuoteStr: string;

{*

{***************************************************************************}

{* SUB-PROGRAMS with LOCAL DEFINITIONS and other SUB-PROGRAMS CALLED

{*

{* PROCEDURE DeleteTempFiles

{* PROCEDURE DoSortFile

{* PROGRAM MAIN

{*   calls PROCEDURE DoSortFile

{*   calls PROCEDURE DeleteTempFiles

{***************************************************************************}

{* SUB-PROGRAMS IN ALPHABETICAL ORDER

{*

{* PROCEDURE DeleteTempFiles

{* PROCEDURE DoSortFile

{***************************************************************************}

uses

  Croft,

  Crt;



var

  SortFile,

  File1,

  File2,

  File3: string;

  F1,

  F2,

  F3,

  SF,

  TF1,

  TF2,

  TF3: text;

  alpha: integer;

  InStr,

  InStr2,

  DataStr1,

  DataStr2: string;

  CheckPage,

  CheckName: boolean;

  Page1,

  Page2: integer;

  TempStr: array[1..10] of string;

  TempQuoteStr: string;



Procedure DeleteTempFiles;

{***************************************************************************}

begin

  FileDel('MERGE1.TMP',DumInt);

  FileDel('MERGE2.TMP',DumInt);

  FileDel('MERGE3.TMP',DumInt);

end;



Procedure DoSortFile;

{***************************************************************************}

begin

  Page1 := 0;

  Page2 := 0;

  ;

  clr;

  Say('Choose file to be sorted...');

  SortFile := GetDirFileOnly('*.DAT');

  Assign(SF, SortFile);

  Assign(TF1, 'MERGE1.TMP');

  Assign(TF2, 'MERGE2.TMP');

  Reset(SF);

  Rewrite(TF1);

  Rewrite(TF2);

  repeat

    alpha := 0;

    repeat

      Inc(alpha);

      readln(SF, TempStr[alpha]);

    until (EOF(SF)) or (alpha = 10);

    if alpha <> 10 then

      begin

        BeepLn;

        Say('Unexpected end of file in '+SortFile+'!');

        PoliteHalt;

      end;

    TempQuoteStr := TempStr[8];

    GetQuote(TempQuoteStr);

    if TempQuoteStr[6] = '1' then

      begin

        for alpha := 1 to 10 do

          WrLn2T(TF1, TempStr[alpha]);

        Inc(Page1);

      end

    else

      if TempQuoteStr[6] = '2' then

        begin

          for alpha := 1 to 10 do

            WrLn2T(TF2, TempStr[alpha]);

            Inc(Page2);

        end

      else

        begin

          BeepLn;

          Say('One of the sheets is not numbered "1" or "2"!');

          Say('Sheet '+TempStr[1]);

          PoliteHalt;

        end;

  until EOF(SF);

  if Page1 <> Page2 then

    begin

      BeepLn;

      Say('The number of 1st pages does not equal the number of 2nd pages!');

      PoliteHalt;

    end;

  Close(SF);

  Close(TF1);

  Close(TF2);

  Reset(TF1);

  Reset(TF2);

  Assign(TF3, 'MERGE3.TMP');

  Rewrite(TF3);

  repeat

    readln(TF1, TempStr[1]);

    repeat

      readln(TF2, TempStr[2]);

      if EOF(TF2) then

        begin

          close(TF2);

          reset(TF2);

        end;

    until TempStr[2] = TempStr[1];

    WrLn2T(TF3, TempStr[2]);

    for alpha := 2 to 10 do

      begin

        readln(TF1);

        readln(TF2, TempStr[alpha]);

        WrLn2T(TF3, TempStr[alpha]);

      end;

  until EOF(TF1);

  close(TF1);

  close(TF2);

  close(TF3);

end;



{***************************************************************************}

{***************************************************************************}

{***************************************************************************}

begin

  clr;

  Say('MERGE.EXE by 2Lt David Croft, 31st TES.');

  writeln;

  Say('MERGE is normally to be used with SCAN.EXE and TRANS2.EXE to create');

  Say('*.DAT files that have more than 120 responses on them.');

  writeln;

  Say('There are two ways to use MERGE:');

  writeln;

  Say('1) all the first pages should be run through the scanner using');

  Say('SCAN.EXE.  Next, all the second pages are run through IN THE SAME');

  Say('ORDER as the first pages to create a SEPARATE *.DAT file.');

  Say('Finally, MERGE is used to create a third *.DAT file with the ');

  Say('combined number of responses;');

  writeln;

  Say('2) all the first and second pages in no particular order are run');

  Say('through the scanner to create ONE double-sized *.DAT file.');

  Say('Each 1st and 2nd page pair has the NAME section filled out the same');

  Say('and the SPECIAL CODE P block is filled out either "1" or "2" for');

  Say('the appropriate page.  MERGE then sorts and splits the file into');

  Say('two temporary files and treats them as in the first case.');

  writeln;

  Say(

    'The header information (Name, Sex, etc.) will be that of the 1st page.'

      );

  writeln;

  wait;

  clr;

  Say('Do you have a *.IN file of mixed 1st and 2nd pages with SPECIAL');

  if AskYN('CODE P indicating page and the NAME on each page?','Y') then

    begin

      DoSortFile;

      CheckPage := false;

      CheckName := false;

      File1 := 'MERGE1.TMP';

      File2 := 'MERGE3.TMP';

      Clr;

    end

  else

    begin

      if AskYN('Check SPECIAL CODE P on answer sheet for proper page?','Y') then

        CheckPage := true

      else

        CheckPage := false;

      if AskYN('Check NAME on answer sheet for proper sequencing?','Y') then

        CheckName := true

      else

        CheckName := false;

      writeln;

      writeln('Select the first page...');

      File1 := GetDirFileOnly('*.DAT');

      writeln;

      writeln('Select the second page...');

      File2 := GetDirFileOnly('*.DAT');

    end;

  writeln;

  writeln('Select the merged file name...');

  File3 := GetDirFile('*.DAT');

  Assign(F1, File1);

  Assign(F2, File2);

  Assign(F3, File3);

  Reset(F1);

  Reset(F2);

  Rewrite(F3);

  repeat

    readln(F1, InStr);

    readln(F2, InStr2);

    if CheckName then

      if InStr <> InStr2 then

        begin

          writeln(#7);

          Say('Names do not match.  Survey sheets out of sequence.');

          Say('First sheet  = '+InStr);

          Say('Second sheet = '+InStr2);

          PoliteHalt;

        end;

    WrLn2T(F3, InStr);

    for alpha := 1 to 7 do

      begin

        readln(F1, InStr);

        readln(F2, InStr2);

        WrLn2T(F3, InStr);

      end;

    GetQuote(InStr);

    GetQuote(InStr2);

    if CheckPage then

      if (InStr[6] <> '1') or (InStr2[6] <> '2') then

        begin

          writeln(#7);

          Say('Special Code P for Page 1: "'+InStr[6]+'" should be "1".');

          Say('Special Code P for Page 2: "'+InStr2[6]+'" should be "2".');

          PoliteHalt;

        end;

    readln(F1, DataStr1);

    readln(F2, DataStr2);

    GetQuote(DataStr1);

    GetQuote(DataStr2);

    Wr2T  (F3, '"'+DataStr1);

    WrLn2T(F3, DataStr2+'"');

    readln(F1);

    readln(F2);

    WrLn2T(F3,'\');

  until EOF(F1);

  close(F1);

  close(F2);

  close(F3);

  DeleteTempFiles;

  wait;

end.