xref: /titanic_41/usr/src/lib/libast/common/sfio/sfgetd.c (revision e5ba14ff435beeefdaa2e6649e175c74afe02c76)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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
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 
40 	SFMTXSTART(f,-1.);
41 
42 	if((sign = sfgetc(f)) < 0 || (exp = (int)sfgetu(f)) < 0)
43 		SFMTXRETURN(f, -1.);
44 
45 	if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
46 		SFMTXRETURN(f, -1.);
47 
48 	SFLOCK(f,0);
49 
50 	v = 0.;
51 	for(;;)
52 	{	/* fast read for data */
53 		if(SFRPEEK(f,s,p) <= 0)
54 		{	f->flags |= SF_ERROR;
55 			v = -1.;
56 			goto done;
57 		}
58 
59 		for(ends = s+p; s < ends; )
60 		{	c = *s++;
61 			v += SFUVALUE(c);
62 			v = ldexpl(v,-SF_PRECIS);
63 			if(!(c&SF_MORE))
64 			{	f->next = s;
65 				goto done;
66 			}
67 		}
68 		f->next = s;
69 	}
70 
71 done:
72 	v = ldexpl(v,(sign&02) ? -exp : exp);
73 	if(sign&01)
74 		v = -v;
75 
76 	SFOPEN(f,0);
77 	SFMTXRETURN(f, v);
78 }
79