home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume1 / othello1 / part01 / InOutExtensions.mod next >
Text File  |  1987-06-08  |  7KB  |  282 lines

  1. IMPLEMENTATION MODULE InOutExtensions ;
  2.  
  3. (* written by            *)
  4. (*    Jesse M. Heines       *)
  5. (*    University of Lowell *)
  6.  
  7. (* Version 1.2     1/28/87 *)
  8.  
  9.  
  10.  
  11. FROM CharCodes IMPORT
  12.    EscapeCh ;
  13.  
  14. FROM InOut IMPORT
  15.    termCH, Read, Write, WriteInt ;
  16.  
  17. FROM MathLib0 IMPORT
  18.    log10 ;
  19.  
  20. FROM SCTermStream IMPORT
  21.    CreateInOutput ;
  22.  
  23. FROM Streams IMPORT
  24.    Delete, Stream ;
  25.  
  26. FROM SYSTEM IMPORT
  27.    MAXINT, MININT ;
  28.  
  29. FROM TextIO IMPORT
  30.    EndOfLine, ReadCHAR ;
  31.  
  32.  
  33.  
  34. PROCEDURE GetEscapeSequence
  35.    (VAR c : ARRAY OF CHAR ) ;   (* characters read from the terminal *)
  36.  
  37. (* This procedure is designed to read as escape sequnce from the terminal   *)
  38. (* without waiting for the user to press RETURN.  It begins by reading a    *)
  39. (* single from the terminal.  If the character read is ESCape, a second     *)
  40. (* character is read.  If the second characters is '[', a third character   *)
  41. (* is read.  Any characters not read are set to 0C.                         *)
  42.  
  43. VAR 
  44.    SingleCharStream : Stream ;   (* terminal stream in single character mode *)
  45.  
  46. BEGIN
  47.  
  48.       (* Initialization *)
  49.  
  50.    c[0] := 0C ;
  51.    c[1] := 0C ;
  52.    c[2] := 0C ;
  53.  
  54.       (* Open input stream *)
  55.  
  56.    SingleCharStream := CreateInOutput () ;
  57.  
  58.       (* Read characters *)
  59.  
  60.    c[0] := ReadCHAR (SingleCharStream) ;
  61.    IF c[0] = EscapeCh THEN
  62.       c[1] := ReadCHAR (SingleCharStream) ;
  63.       IF c[1] = '[' THEN
  64.      c[2] := ReadCHAR (SingleCharStream) ;
  65.       END ;
  66.    END ;
  67.  
  68.       (* Close input stream *)
  69.  
  70.    Delete (SingleCharStream) ;
  71.  
  72. END GetEscapeSequence ;
  73.  
  74.  
  75.  
  76. PROCEDURE GetOneChar
  77.    (VAR c : CHAR ) ;   (* character read from the terminal *)
  78.  
  79. (* This procedure gets a single character from the terminal without *)
  80. (* waiting for the user to press RETURN.                            *)
  81.  
  82. VAR 
  83.    SingleCharStream : Stream ;   (* terminal stream in single character mode *)
  84.  
  85. BEGIN
  86.  
  87.    SingleCharStream := CreateInOutput () ;
  88.    c := ReadCHAR (SingleCharStream) ;
  89.    Delete (SingleCharStream) ;
  90.  
  91. END GetOneChar ;
  92.  
  93.  
  94.  
  95. PROCEDURE ReadLn ;
  96.  
  97. (* This procedure reads data from the current input stream until an end  *)
  98. (* of line character (EOL), a null character (0C), or a CTRL/D character *)
  99. (* (4C) is read.  The data read is not saved.  The purpose of this       *)
  100. (* procedure is to skip the rest of the current line to prepare for      *)
  101. (* reading the next line of input.                                       *)
  102.  
  103. BEGIN
  104.  
  105.       (* Read as long as character read is not a terminating condition *)
  106.  
  107.    WHILE (NOT (EndOfLine (termCH))) AND (termCH # 0C) AND (termCH # 4C) DO
  108.       Read (termCH) ;
  109.    END ;
  110.  
  111. END ReadLn ;
  112.  
  113.  
  114.  
  115. PROCEDURE ReadLine
  116.    (VAR line : ARRAY OF CHAR) ;   (* the line read *)
  117.  
  118. (* This procedure reads a line of data from the current input stream.     *)
  119. (* Reading is terminated when the end of line character (EOL) is reached, *)
  120. (* the array into which characters are being read becomes full, a null    *)
  121. (* character is read, or a CTRL/D character (4C) is read.                 *)
  122.  
  123. VAR 
  124.    k       : CARDINAL ;   (* loop index *)
  125.    linelen : CARDINAL ;   (* number of characters read thus far *)
  126.  
  127. BEGIN
  128.  
  129.       (* Initialization *)
  130.  
  131.    Read (termCH) ;
  132.    linelen := 0 ;
  133.  
  134.       (* Read as long as character read is not a terminating condition *)
  135.  
  136.    WHILE (NOT (EndOfLine (termCH))) AND (linelen <= HIGH(line)) AND 
  137.      (termCH # 0C) AND (termCH # 4C) DO
  138.       line[linelen] := termCH ;
  139.       Read (termCH) ;
  140.       INC (linelen) ;
  141.    END ;
  142.  
  143.       (* Zero out the rest of the line *)
  144.  
  145.    FOR k := linelen TO HIGH(line) DO
  146.       line[k] := 0C ;
  147.    END ;
  148.  
  149. END ReadLine ;
  150.  
  151.  
  152.  
  153. PROCEDURE WriteFormattedReal
  154.    (r              : REAL ;         (* the real number to write *)
  155.     width          : CARDINAL ;     (* size of output field *)
  156.     ndecimalplaces : CARDINAL ) ;   (* number of decimal places to write *)
  157.  
  158. (* This procedure writes a real number to the output stream and places that *)
  159. (* number in a field "width" spaces wide.  The field will be filled with    *)
  160. (* blanks to pad it to the appropriate size.  (The number is right justi-   *)
  161. (* fied in the field.)  WriteFormattedReal will output numbers in standard  *)
  162. (* format with the specified number of decimal places.                      *)
  163.  
  164. VAR
  165.    k          : CARDINAL ;   (* loop index *)
  166.    length     : CARDINAL ;   (* minimum field width needed to write number *)
  167.    placevalue : REAL ;       (* value of place currently being printed *)
  168.    placedigit : [0..9] ;     (* digit in place currently being printed *)
  169.  
  170. BEGIN
  171.  
  172.       (* Compute minimum number of spaces needed to write number *)
  173.  
  174.    IF r > 0.0 THEN
  175.       IF r >= 1.0 THEN
  176.      length := TRUNC (log10 (r)) + 2 + ndecimalplaces ;
  177.       ELSE
  178.      length := 2 + ndecimalplaces ;
  179.       END ;
  180.    ELSE
  181.       IF r <= -1.0 THEN
  182.      length := TRUNC (log10 (ABS(r))) + 3 + ndecimalplaces ;
  183.       ELSE
  184.      length := 3 + ndecimalplaces ;
  185.       END ;
  186.    END ;
  187.  
  188.       (* Output padding spaces if necessary *)
  189.  
  190.    FOR k := length + 1 TO width DO
  191.       Write (' ') ;
  192.    END ;
  193.  
  194.       (* Write integer part *)
  195.    
  196.    IF (r >= FLOAT (MININT)) AND (r <= FLOAT (MAXINT)) THEN
  197.       WriteInt (TRUNC(r), 0) ;
  198.  
  199.    ELSE
  200.       IF r < 0.0 THEN
  201.      Write ('-') ;
  202.      r := ABS (r) ;
  203.       END ;
  204.  
  205.       placevalue := Power (10.0,TRUNC(log10(r))) ;
  206.  
  207.       FOR k := TRUNC(log10(r)) TO 0 BY -1 DO
  208.      placedigit := TRUNC (r/placevalue) ;
  209.      Write (CHR (48 + placedigit)) ;
  210.      r := r - placevalue * FLOAT(placedigit) ;
  211.      placevalue := placevalue / 10.0 ;
  212.       END ;
  213.  
  214.    END ;
  215.  
  216.       (* Write decimal point *)
  217.    
  218.    Write ('.') ;
  219.  
  220.       (* Write fractional part *)
  221.  
  222.    r := r - FLOAT(TRUNC(r)) ;
  223.  
  224.    FOR k := 1 TO ndecimalplaces DO
  225.       r := 10.0 * r ;
  226.       IF k < ndecimalplaces THEN
  227.      WriteInt (TRUNC(r), 0) ;
  228.       ELSE
  229.      WriteInt (RoundDigit(r), 0) ;
  230.       END ;
  231.       r := r - FLOAT(TRUNC(r)) ;
  232.    END ;
  233.  
  234. END WriteFormattedReal ;
  235.  
  236.  
  237.  
  238. PROCEDURE Power
  239.    (n : REAL ;       (* number to compute the Power of *)
  240.     x : CARDINAL )   (* Power to raise number to *)
  241.    : REAL ;          (* result type *)
  242.  
  243. (* This procedure computes n to the x and returns the result as a real *)
  244. (* number.  It is not exported.                                        *)
  245.  
  246. VAR
  247.    k    : CARDINAL ;   (* loop index *)
  248.    temp : REAL ;       (* intermediate result *)
  249.  
  250. BEGIN
  251.    temp := 1.0 ;
  252.    FOR k := 1 TO x DO
  253.       temp := n * temp ;
  254.    END ;
  255.    RETURN temp ;
  256. END Power ;
  257.  
  258.  
  259.  
  260. PROCEDURE RoundDigit
  261.    (n : REAL )   (* real number to round *)
  262.    : INTEGER ;   (* return type *)
  263.  
  264. (* This procedure rounds a real number to the nearest 1 and returns the *)
  265. (* resultant value as an integer.  It is not exported.                  *)
  266.  
  267. BEGIN
  268.    IF ABS (n - FLOAT(TRUNC(n))) < 0.5 THEN
  269.       RETURN TRUNC(n) ;
  270.    ELSE
  271.       IF n > 0.0 THEN
  272.      RETURN TRUNC(n) + 1 ;
  273.       ELSE
  274.      RETURN TRUNC(n) - 1 ;
  275.       END ;
  276.    END ;
  277. END RoundDigit ;
  278.  
  279.  
  280.  
  281. END InOutExtensions.
  282.