MODULE SpaltenSatz;
(*$E MOS *)

(*
 * F�gt entweder die Texte zweier Dateien nebeneinander zusammen
 * oder bereitet einen Text f�r zweispaltigen Druck vor.
 *)

IMPORT GEMDOSIO;
FROM InOut IMPORT Write, WriteLn, WriteString, WriteInt, WriteCard,
        ReadString, ReadCard, ReadInt;
IMPORT Files, Text;
FROM Strings IMPORT Append, DelTrailingBlanks, Copy;

(*
 * Mischt zwei Textfiles so, daž die Zeilen der 2. Datei rechts neben
 * der 1. erscheinen.
 *)

TYPE String = ARRAY [0..255] OF CHAR;

VAR spc, s1, s2, s3: String;
    ok, second, onefile, buffered: BOOLEAN;
    f1, f2, f3: Files.File;
    n: INTEGER;
    ofs, lps, line: INTEGER;
    linebuf: ARRAY [1..200] OF String;

PROCEDURE flush;
  VAR l: INTEGER;
  BEGIN
    FOR l:= 1 TO line DO
      Text.WriteString (f3, linebuf[l]);
      Text.WriteLn (f3);
    END;
    Text.WritePg (f3);
    line:= 1
  END flush;

BEGIN
  FOR n:= 0 TO SIZE (spc)-1 DO
    spc[n]:= ' '
  END;
  WriteString ("1. Datei? ");
  ReadString (s1);
  Files.Open (f1, s1, Files.readSeqTxt);
  IF Files.State (f1) < 0 THEN
    WriteString ('Fehler!');
    WriteLn;
    RETURN
  END;
  WriteString ("2. Datei (oder nix)? ");
  ReadString (s1);
  second:= s1[0] # '';
  onefile:= ~second;
  IF second THEN
    Files.Open (f2, s1, Files.readSeqTxt);
    IF Files.State (f2) < 0 THEN
      WriteString ('Fehler!');
      WriteLn;
      Files.Close (f1);
      RETURN
    END;
  END;
  WriteString ("Ausgabedatei? ");
  ReadString (s1);
  Files.Create (f3, s1, Files.writeSeqTxt, Files.replaceOld);
  IF Files.State (f3) < 0 THEN
    WriteString ('Fehler!');
    WriteLn;
    Files.Close (f2);
    Files.Close (f1);
    RETURN
  END;
  WriteString ("Spaltenoffset? ");
  ReadInt (ofs);
  IF ofs = 0 THEN Files.Close (f3); Files.Close (f2); Files.Close (f1); RETURN END;
  WriteString ("Zeilenanzahl pro Seite (0 f. unendlich)? ");
  ReadInt (lps);
  IF onefile & (lps = 0) THEN
    WriteString ("Bei 1-File-Spaltendruck nicht erlaubt!");
    WriteLn;
    Files.Close (f3); Files.Close (f2); Files.Close (f1); RETURN
  END;
  buffered:= onefile OR (lps > 0);
  line:= 1;
  LOOP
    Text.ReadFromLine (f1, s1);
    IF ~Files.EOF (f1) THEN Text.ReadLn (f1) END;
    s2:= '';
    IF onefile THEN
      IF second THEN
        s2:= s1;
        s1:= linebuf[line]
      END;
    ELSIF second THEN
      Text.ReadFromLine (f2, s2);
      second:= ~Files.EOF (f2);
      IF second THEN
        Text.ReadLn (f2);
        second:= ~Files.EOF (f2);
      END;
    END;
    Copy (spc, 0, ofs-INT(LENGTH (s1)), s3, ok);
    Append (s3, s1, ok);
    IF LENGTH(s2) # 0 THEN s1[ofs]:= 0C END;
    Append (s2, s1, ok);
    DelTrailingBlanks (s1);
    IF buffered THEN
      linebuf[line]:= s1;
      IF line = lps THEN
        IF onefile THEN
          IF ~second THEN
            second:= TRUE;
            line:= 1;
          ELSE
            second:= FALSE;
            flush;
          END
        ELSE
          flush;
        END
      ELSE
        INC (line)
      END
    ELSE
      Text.WriteString (f3, s1);
      Text.WriteLn (f3);
    END;
    IF Files.EOF (f1) THEN EXIT END;
  END;
  IF buffered THEN
    DEC (line);
    flush;
  END;
  Files.Close (f3);
  Files.Close (f2);
  Files.Close (f1);
END SpaltenSatz.
