1(* $Id: LRealConv.Mod,v 1.13 2003/04/06 12:11:15 mva Exp $ *) 2MODULE LRealConv; 3(* String to LONGREAL conversion functions. 4 Copyright (C) 2002 Michael van Acken 5 6 This module is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public License 8 as published by the Free Software Foundation; either version 2 of 9 the License, or (at your option) any later version. 10 11 This module is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with OOC. If not, write to the Free Software Foundation, 18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19*) 20 21IMPORT 22 SYSTEM, libc, CharClass, ConvTypes, Real0; 23 24(** 25 26 The regular expression for a signed fixed-point real number is 27 @samp{[+-]?\d+(\.\d* )?}. For the optional exponent part, it is 28 @samp{E[+-]?\d+}. 29 30*) 31 32TYPE 33 ConvResults* = ConvTypes.ConvResults; 34 (**One of @oconst{strAllRight}, @oconst{strOutOfRange}, 35 @oconst{strWrongFormat}, or @oconst{strEmpty}. *) 36 37CONST 38 strAllRight*=ConvTypes.strAllRight; 39 (**The string format is correct for the corresponding conversion. *) 40 strOutOfRange*=ConvTypes.strOutOfRange; 41 (**The string is well-formed but the value cannot be represented. *) 42 strWrongFormat*=ConvTypes.strWrongFormat; 43 (**The string is in the wrong format for the conversion. *) 44 strEmpty*=ConvTypes.strEmpty; 45 (**The given string is empty. *) 46 47CONST 48 maxValue = "17976931348623157"; 49 (* signifcant digits of the maximum value 1.7976931348623157D+308 *) 50 maxExp = 308; 51 (* maxium positive exponent of a normalized number *) 52 53PROCEDURE ScanReal*(inputCh: CHAR; 54 VAR chClass: ConvTypes.ScanClass; 55 VAR nextState: ConvTypes.ScanState); 56 BEGIN 57 Real0.ScanReal (inputCh, chClass, nextState); 58 END ScanReal; 59 60PROCEDURE FormatReal* (str: ARRAY OF CHAR): ConvResults; 61 BEGIN 62 RETURN Real0.FormatReal (str, maxExp, maxValue); 63 END FormatReal; 64 65PROCEDURE ValueReal*(str: ARRAY OF CHAR): LONGREAL; 66(* result is undefined if FormatReal(str) # strAllRight *) 67 VAR 68 i: LONGINT; 69 value: LONGREAL; 70 BEGIN 71 i := 0; 72 WHILE CharClass.IsWhiteSpace(str[i]) DO 73 (* skip our definition of whitespace *) 74 INC (i); 75 END; 76 IF libc.sscanf(SYSTEM.ADR(str[i]), "%lf", SYSTEM.ADR(value)) = 1 THEN 77 <*PUSH; Warnings:=FALSE*> 78 RETURN value (* syntax is ok *) 79 <*POP*> 80 ELSE 81 RETURN 0; (* error *) 82 END; 83 END ValueReal; 84 85PROCEDURE LengthFloatReal*(real: LONGREAL; sigFigs: INTEGER): INTEGER; 86 BEGIN 87<*PUSH; Assertions:=TRUE*> 88 ASSERT (FALSE) 89<*POP*> 90 END LengthFloatReal; 91 92PROCEDURE LengthEngReal*(real: LONGREAL; sigFigs: INTEGER): INTEGER; 93 BEGIN 94<*PUSH; Assertions:=TRUE*> 95 ASSERT (FALSE) 96<*POP*> 97 END LengthEngReal; 98 99PROCEDURE LengthFixedReal*(real: LONGREAL; place: INTEGER): INTEGER; 100 BEGIN 101<*PUSH; Assertions:=TRUE*> 102 ASSERT (FALSE) 103<*POP*> 104 END LengthFixedReal; 105 106END LRealConv. 107