-- AsciiToTASCII.mesa
-- created by Haeberli: 10-Feb-82 19:06:58

DIRECTORY
  AltoDefs,
  Inline,
  IODefs,
  StreamDefs;

AsciiToTASCII: PROGRAM IMPORTS AltoDefs, Inline, IODefs, StreamDefs =
  {

  bitsLeft: [0..8] ← 8;
  byte: [0..256) ← 0;
  inDh: StreamDefs.DiskHandle;
  outDh: StreamDefs.DiskHandle;
  maxLength: CARDINAL = 256;
  record: ARRAY [0..maxLength) OF AltoDefs.BYTE;
  length: [0..maxLength] ← 0;

  StartRecord: PROCEDURE = {length ← 0};

  EndRecord: PROCEDURE = {IF length # 0 THEN {l: CARDINAL ← length; outDh.put[outDh, Inline.HighByte[l]]; outDh.put[outDh, Inline.LowByte[l]]; FOR i: CARDINAL IN [0..length) DO outDh.put[outDh, record[i]];  ENDLOOP;};};

  Put: PROCEDURE [byte: AltoDefs.BYTE] = {record[length] ← byte; length ← length + 1; IF length = maxLength THEN {EndRecord[]; StartRecord[];};};

  Initialize: PROCEDURE = {bitsLeft ← 8; byte ← 0};

  Flush: PROCEDURE [out: StreamDefs.DiskHandle] = {

    FOR i: CARDINAL IN [0..6) UNTIL bitsLeft = 0 DO
      IF bitsLeft = 0 THEN {Put[byte]; bitsLeft ← 8; byte ← 0; };
      byte ← Inline.BITSHIFT[byte, 1];
      bitsLeft ← bitsLeft - 1;
      ENDLOOP;
    Put[byte];

    EndRecord[];

    Initialize[];
    };

  Truncate: PROCEDURE [out: StreamDefs.DiskHandle, ch: CHARACTER] = {
    tch: [0..64) = IF ch = IODefs.CR THEN 63 ELSE Inline.BITAND[(2000B + LOOPHOLE[ch, INTEGER] - 240B), 77B];
    rtch: WORD ← Inline.BITSHIFT[tch, 10];


    FOR i: CARDINAL IN [0..6) DO
      IF bitsLeft = 0 THEN {Put[byte]; bitsLeft ← 8; byte ← 0; };
      byte ← Inline.BITSHIFT[byte, 1];
      byte ← Inline.BITOR[byte, Inline.BITSHIFT[rtch, -15]];
      bitsLeft ← bitsLeft - 1;
      rtch ← Inline.BITSHIFT[rtch, 1];
      ENDLOOP;

    };

  Transcribe: PROCEDURE [inName, outName: STRING] = {
    inDh ← StreamDefs.NewByteStream[
      name: inName, access: StreamDefs.Read];
    outDh ← StreamDefs.NewByteStream[
      name: outName, access: StreamDefs.WriteAppend];

    inDh.reset[inDh];
    outDh.reset[outDh];

    StartRecord[];
    Put[53B]; Put[200B];
    Put[0B]; Put[0B];
    Put[0B]; Put[0B];
    Put[340B]; Put[0B];
    Put[0B]; Put[0B];
    Put[0B]; Put[20B];
    Put[0B]; Put[4B];
    Put[0B]; Put[312B];
    Put[370B]; Put[264B];
    EndRecord[];

    Initialize[];
    UNTIL inDh.endof[inDh] DO Truncate[outDh, inDh.get[inDh]] ENDLOOP;
    Flush[outDh];

    inDh.destroy[inDh];
    outDh.destroy[outDh];
    };

  {

  Transcribe["MouseTestPatterns.txt", "MouseTestPatterns.tas"]; };

  }.