home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / SRC / REALIO.MOD < prev    next >
Text File  |  1996-09-04  |  9KB  |  253 lines

  1. IMPLEMENTATION MODULE RealIO;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*              Real I/O using windows.                 *)
  6.         (*                                                      *)
  7.         (*  Programmer:         P. Moylan                       *)
  8.         (*  Last edited:        4 September 1996                *)
  9.         (*  Status:             Working                         *)
  10.         (*      More care needed in handling the case where     *)
  11.         (*       the field size is too small.                   *)
  12.         (*      Seems to be a loss of accuracy when writing     *)
  13.         (*       E-format numbers; for example 123.456E7 is     *)
  14.         (*       written as 1234559999.  I'm not yet sure       *)
  15.         (*       where this is happening, but suspect that it   *)
  16.         (*       involves some deep error analysis.             *)
  17.         (*                                                      *)
  18.         (********************************************************)
  19.  
  20. FROM Windows IMPORT
  21.     (* type *)  Window, ColumnRange,
  22.     (* proc *)  SaveCursor, SetCursor, EditString, ReadChar, LookaheadChar,
  23.                 ReadCharWithoutEcho, WriteString;
  24.  
  25. FROM Conversions IMPORT
  26.     (* proc *)  LongRealToString, StringToLongReal;
  27.  
  28. FROM Keyboard IMPORT
  29.     (* proc *)  InKey, PutBack;
  30.  
  31. (************************************************************************)
  32.  
  33. CONST
  34.     EndMarker = CHR(0);  tab = CHR(9);  CR = CHR(13);
  35.  
  36. TYPE
  37.     CharSet = SET OF CHAR;
  38.  
  39. (************************************************************************)
  40. (*                              REAL OUTPUT                             *)
  41. (************************************************************************)
  42.  
  43. PROCEDURE WriteLongReal (w: Window;  number: LONGREAL;  places: CARDINAL);
  44.  
  45.     (* Writes the second argument as a decimal number, right-justified  *)
  46.     (* in a field of "places" places.                                   *)
  47.  
  48.     VAR buffer: ARRAY [0..79] OF CHAR;
  49.  
  50.     BEGIN
  51.         LongRealToString (number, buffer, places);
  52.         WriteString (w, buffer);
  53.     END WriteLongReal;
  54.  
  55. (************************************************************************)
  56.  
  57. PROCEDURE WriteReal (w: Window;  number: REAL;  places: CARDINAL);
  58.  
  59.     (* Like WriteLongReal, except for argument type.    *)
  60.  
  61.     BEGIN
  62.         WriteLongReal (w, VAL(LONGREAL,number), places);
  63.     END WriteReal;
  64.  
  65. (************************************************************************)
  66. (*                      REAL INPUT FROM KEYBOARD                        *)
  67. (************************************************************************)
  68.  
  69. PROCEDURE ReadRealString (w: Window;  VAR (*OUT*) string: ARRAY OF CHAR);
  70.  
  71.     (* Reads in a character string from the keyboard, stopping at the   *)
  72.     (* first character which cannot form part of the external           *)
  73.     (* representation of an unsigned real number.  Leading blanks are   *)
  74.     (* skipped and not echoed.                                          *)
  75.  
  76.     CONST Blanks = CharSet {" ", tab, CR};
  77.  
  78.     VAR nextchar: CHAR;
  79.         position: CARDINAL;
  80.  
  81.     (********************************************************************)
  82.  
  83.     PROCEDURE GetNextChar;
  84.  
  85.         (* Stores nextchar, reads new nextchar from keyboard.  Returns  *)
  86.         (* result = EndMarker if we run out of space.                   *)
  87.  
  88.         BEGIN
  89.             IF position <= HIGH(string) THEN
  90.                 ReadChar (w, nextchar);
  91.                 string[position] := nextchar;  INC (position);
  92.                 nextchar := LookaheadChar(w);
  93.             ELSE
  94.                 nextchar := EndMarker;
  95.             END (*IF*);
  96.         END GetNextChar;
  97.  
  98.     (********************************************************************)
  99.  
  100.     BEGIN
  101.         position := 0;
  102.  
  103.         (* Skip leading spaces and tabs.        *)
  104.  
  105.         WHILE LookaheadChar(w) IN Blanks DO
  106.             ReadCharWithoutEcho (w, nextchar);
  107.         END (*WHILE*);
  108.         nextchar := LookaheadChar(w);
  109.  
  110.         (* Read the sign, if present.  We also permit spaces or tabs    *)
  111.         (* after the sign.                                              *)
  112.  
  113.         IF (nextchar = '-') OR (nextchar = '+') THEN
  114.             REPEAT
  115.                 GetNextChar;
  116.             UNTIL (nextchar <> " ") AND (nextchar <> tab);
  117.         END (*IF*);
  118.  
  119.         (* Read the part before the decimal point.      *)
  120.  
  121.         WHILE nextchar IN CharSet {"0".."9"} DO
  122.             GetNextChar;
  123.         END (*WHILE*);
  124.  
  125.         (* Now the part after the decimal point, if any.        *)
  126.  
  127.         IF nextchar = "." THEN
  128.             GetNextChar;
  129.             WHILE nextchar IN CharSet {"0".."9"} DO
  130.                 GetNextChar;
  131.             END (*WHILE*);
  132.         END (*IF*);
  133.  
  134.         (* Check for Ennn part. *)
  135.  
  136.         IF (nextchar = "E") OR (nextchar = "e") THEN
  137.             GetNextChar;
  138.             IF (nextchar = "+") OR (nextchar = "-") THEN
  139.                 GetNextChar;
  140.             END (*IF*);
  141.             WHILE nextchar IN CharSet {"0".."9"} DO
  142.                 GetNextChar;
  143.             END (*WHILE*);
  144.         END (*IF*);
  145.  
  146.         (* Ensure that string is properly terminated.  *)
  147.  
  148.         IF position <= HIGH(string) THEN
  149.             string[position] := EndMarker;
  150.         END (*IF*);
  151.  
  152.     END ReadRealString;
  153.  
  154. (************************************************************************)
  155.  
  156. PROCEDURE ReadLongReal (w: Window): LONGREAL;
  157.  
  158.     (* Reads and converts an unsigned numeric string from the keyboard. *)
  159.  
  160.     VAR InputString: ARRAY [0..79] OF CHAR;
  161.  
  162.     BEGIN
  163.         ReadRealString (w, InputString);
  164.         RETURN StringToLongReal (InputString);
  165.     END ReadLongReal;
  166.  
  167. (************************************************************************)
  168.  
  169. PROCEDURE ReadReal (w: Window): REAL;
  170.  
  171.     (* Like ReadLongReal, except for argument type.     *)
  172.  
  173.     BEGIN
  174.         RETURN VAL(REAL, ReadLongReal (w));
  175.     END ReadReal;
  176.  
  177. (************************************************************************)
  178.  
  179. PROCEDURE ReadBufferedLongReal (w: Window;  fieldsize: CARDINAL): LONGREAL;
  180.  
  181.     (* Like ReadLongReal, but allows the user to edit within a field of *)
  182.     (* the specified size.                                              *)
  183.  
  184.     VAR buffer: ARRAY ColumnRange OF CHAR;
  185.         row, column: CARDINAL;
  186.         value: LONGREAL;
  187.  
  188.     BEGIN
  189.         SaveCursor (w, row, column);
  190.         buffer := "";
  191.         EditString (w, buffer, fieldsize);
  192.         value := StringToLongReal (buffer);
  193.         SetCursor (w, row, column);
  194.         WriteLongReal (w, value, fieldsize);
  195.         RETURN value;
  196.     END ReadBufferedLongReal;
  197.  
  198. (************************************************************************)
  199.  
  200. PROCEDURE ReadBufferedReal (w: Window;  fieldsize: CARDINAL): REAL;
  201.  
  202.     (* Like ReadBufferedLongReal, except for argument type.     *)
  203.  
  204.     BEGIN
  205.         RETURN VAL(REAL, ReadBufferedLongReal (w, fieldsize));
  206.     END ReadBufferedReal;
  207.  
  208. (************************************************************************)
  209.  
  210. PROCEDURE EditLongReal (w: Window;  VAR (*INOUT*) variable: LONGREAL;
  211.                                                         width: CARDINAL);
  212.  
  213.     (* Displays the current value of "variable" at the current cursor   *)
  214.     (* position in window w, using a field width of "width" characters, *)
  215.     (* and gives the user the option of altering the value.             *)
  216.  
  217.     CONST Esc = CHR(27);
  218.  
  219.     VAR buffer: ARRAY ColumnRange OF CHAR;
  220.         row, column: CARDINAL;  ch: CHAR;
  221.  
  222.     BEGIN
  223.         SaveCursor (w, row, column);
  224.         LongRealToString (variable, buffer, width);
  225.         EditString (w, buffer, width);
  226.         ch := InKey();  PutBack(ch);
  227.         IF ch <> Esc THEN
  228.             variable := StringToLongReal (buffer);
  229.         END (*IF*);
  230.         SetCursor (w, row, column);
  231.         WriteLongReal (w, variable, width);
  232.     END EditLongReal;
  233.  
  234. (************************************************************************)
  235.  
  236. PROCEDURE EditReal (w: Window;  VAR (*INOUT*) variable: REAL;
  237.                                                         width: CARDINAL);
  238.  
  239.     (* Like EditLongReal, except for argument type.     *)
  240.  
  241.     VAR temp: LONGREAL;
  242.  
  243.     BEGIN
  244.         temp := VAL(LONGREAL,variable);
  245.         EditLongReal (w, temp, width);
  246.         variable := VAL(REAL,temp);
  247.     END EditReal;
  248.  
  249. (************************************************************************)
  250.  
  251. END RealIO.
  252. 
  253.