aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/gctod.x
blob: ff58555bc94b0bfdbfece20c385b692dffb76562 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<ctype.h>
include	<chars.h>
include	<lexnum.h>

define	OCTAL		8
define	DECIMAL		10
define	HEX		16


# GCTOD -- General character string to double precision real.  Any legal
# number, e.g., integer, floating point, complex, or character constant,
# is decoded and returned as a double.

int procedure gctod (str, ip, odval)

char	str[ARB]		# input string
int	ip			# pointer into input string
double	odval			# output double

char	ch
double	dval
complex	xval
long	lval
int	ip_save, radix, nchars, vtype
int	ctox(), cctoc(), ctod(), gctol(), lexnum()

begin
	vtype = TY_DOUBLE				# val to be returned
	while (IS_WHITE (str[ip]))
	    ip = ip + 1

	ip_save = ip
	ch = str[ip]					# first nonwhite

	if (ch == '(') {				# complex number?
	    if (ctox (str, ip, xval) <= 0)
		return (0)				# not a number
	    else
		vtype = TY_COMPLEX

	} else if (ch == SQUOTE || ch == ESCAPE) {
	    if (cctoc (str, ip, ch) <= 0)		# character constant?
		return (0)
	    else
		dval = ch

	} else {				# determine type of number
	    switch (lexnum (str, ip, nchars)) {
	    case LEX_OCTAL:
		radix = OCTAL
	    case LEX_DECIMAL:
		radix = DECIMAL
	    case LEX_HEX:
		radix = HEX
	    case LEX_REAL:
		radix = TY_REAL
	    default:
		return (0)
	    }

	    if (radix == TY_REAL)		# perform the conversion
		nchars = ctod (str, ip, dval)
	    else {
		nchars = gctol (str, ip, lval, radix)
		dval = lval
		if (IS_INDEFL (lval))
		    dval = INDEFD
	    }
	}

	if (vtype == TY_COMPLEX) {
	    odval = xval
	    if (IS_INDEFX (xval))
		odval = INDEFD
	} else
	    odval = dval

	return (ip - ip_save)
end