1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"sfhdr.h"
23 
24 /*	Read a portably coded double value
25 **
26 **	Written by Kiem-Phong Vo
27 */
28 
29 #if __STD_C
sfgetd(Sfio_t * f)30 Sfdouble_t sfgetd(Sfio_t* f)
31 #else
32 Sfdouble_t sfgetd(f)
33 Sfio_t*	f;
34 #endif
35 {
36 	reg uchar	*s, *ends, c;
37 	reg int		p, sign, exp;
38 	Sfdouble_t	v;
39 	SFMTXDECL(f); /* declare a local stream variable for multithreading */
40 
41 	SFMTXENTER(f,-1.);
42 
43 	if((sign = sfgetc(f)) < 0 || (exp = (int)sfgetu(f)) < 0)
44 		SFMTXRETURN(f, -1.);
45 
46 	if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
47 		SFMTXRETURN(f, -1.);
48 
49 	SFLOCK(f,0);
50 
51 	v = 0.;
52 	for(;;)
53 	{	/* fast read for data */
54 		if(SFRPEEK(f,s,p) <= 0)
55 		{	f->flags |= SF_ERROR;
56 			v = -1.;
57 			goto done;
58 		}
59 
60 		for(ends = s+p; s < ends; )
61 		{	c = *s++;
62 			v += SFUVALUE(c);
63 			v = ldexpl(v,-SF_PRECIS);
64 			if(!(c&SF_MORE))
65 			{	f->next = s;
66 				goto done;
67 			}
68 		}
69 		f->next = s;
70 	}
71 
72 done:
73 	v = ldexpl(v,(sign&02) ? -exp : exp);
74 	if(sign&01)
75 		v = -v;
76 
77 	SFOPEN(f,0);
78 	SFMTXRETURN(f, v);
79 }
80