Pascal: Converting strings to numeric variables (1 of 2)

Apple Pascal doesn't allow you to edit integer or real variable inputs.
If the wrong data is entered by mistake, the program will be stuck with
that data; worse yet, the system may crash.

The STRINGSTUF intrinsic unit is designed to avoid this problem. All data is
entered in the form of strings, then converted to the appropriate data format
by the unit. This allows you to edit the input data while it is still in
string form. STRINGSTUF is located on the no-longer distributed Apple
Graphics software package for the Apple II. If you're unable to locate a
copy of this software, refer to the article that contains the STRINGSTUF
code and instructions on its use: Apple Orchard, Vol. 1, #3, p. 59. The
article is by Jo and Frank Kellner.

The STRINGSTUF unit and accompanying demo program are designed to run in
versions of Pascal released later than version 1.0. Long integers are not
supported in STRINGSTUF.

Locate STRINGSTUF in any unused segment number between 17 and 31. Install the
unit in SYSTEM.LIBRARY, following the instructions in the Pascal Operating
System Reference Manual on pp. 186-193.

(*$S+*)
(*$LPprinter:*) (* Get a compile listing..optional *)

Unit STRINGSTUF; intrinsic code 26;

Interface

Type String255=String[255];

Function STRFP (Var STR:String255;
Var FP:real): boolean;
Function Strint (Var STR:String255;
Var INT:integer): boolean;

Implementation

Function STRFP; (* String to Real *)

CONST MaxReal=1.70E37; (* Max/10 *)
MinReal=1.2E-37; (* Min/10 *)

VAR DEC,DEX,EDP,INX,LEN: integer;
DP,EX,IM,MN,MX,SN: boolean;
CH: CHAR;
Numeric,Exponent,Modifier: Set Of CHAR;

Procedure Terminate;
Var I: integer;
Begin
If MX Then DEX:=-DEX;
EDP:=EDP+DEX-DEC;
If EDP<0
Then For I:=1 To -EDP DO
If FP>=MinReal Then FP:=FP/10.0
Else FP:=0 (* Underflow => 0 *)
Else For I:=1 To EDP Do
If FP<=MaxReal Then FP:=FP*10.0
Else exit (STRFP); (* Overflow *)
If MN Then FP:=-FP;
STRFP:=True;
exit (STRFP) (* Successful conversion *)
End;

Procedure Search;
Begin
While INX<=LEN Do
IF STR[INX] in Numeric
Then Begin
(*$R-*)
While (INX>1) and
(STR[INX-1] in Exponent+Modifier)
(*$R+*)
Do INX:=INX-1;
Exit(Search) (* Found start of number *)
End
Else INX:=INX+1;
Exit (STRFP) (* Non-numeric string *)
End;

Published Date: Feb 18, 2012