ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/portio.c
(Generate patch)

Comparing ray/src/common/portio.c (file contents):
Revision 2.6 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 2.26 by greg, Fri Feb 19 18:00:29 2021 UTC

# Line 4 | Line 4 | static const char      RCSid[] = "$Id$";
4   /*
5   * Portable i/o for binary files
6   *
7 < * External symbols declared in standard.h
7 > * External symbols declared in rtio.h
8   */
9  
10 < /* ====================================================================
11 < * The Radiance Software License, Version 1.0
12 < *
13 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
14 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
15 < *
16 < * Redistribution and use in source and binary forms, with or without
17 < * modification, are permitted provided that the following conditions
18 < * are met:
19 < *
20 < * 1. Redistributions of source code must retain the above copyright
21 < *         notice, this list of conditions and the following disclaimer.
22 < *
23 < * 2. Redistributions in binary form must reproduce the above copyright
24 < *       notice, this list of conditions and the following disclaimer in
25 < *       the documentation and/or other materials provided with the
26 < *       distribution.
27 < *
28 < * 3. The end-user documentation included with the redistribution,
29 < *           if any, must include the following acknowledgment:
30 < *             "This product includes Radiance software
31 < *                 (http://radsite.lbl.gov/)
32 < *                 developed by the Lawrence Berkeley National Laboratory
33 < *               (http://www.lbl.gov/)."
34 < *       Alternately, this acknowledgment may appear in the software itself,
35 < *       if and wherever such third-party acknowledgments normally appear.
36 < *
37 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
38 < *       and "The Regents of the University of California" must
39 < *       not be used to endorse or promote products derived from this
40 < *       software without prior written permission. For written
41 < *       permission, please contact [email protected].
42 < *
43 < * 5. Products derived from this software may not be called "Radiance",
44 < *       nor may "Radiance" appear in their name, without prior written
45 < *       permission of Lawrence Berkeley National Laboratory.
46 < *
47 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
48 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
51 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
54 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
55 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
56 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
57 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 < * SUCH DAMAGE.
59 < * ====================================================================
60 < *
61 < * This software consists of voluntary contributions made by many
62 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
63 < * information on Lawrence Berkeley National Laboratory, please see
64 < * <http://www.lbl.gov/>.
65 < */
10 > #include "copyright.h"
11  
12 < #include <stdio.h>
12 > #include "rtio.h"
13  
14 < #ifndef frexp
70 < extern double  frexp();
71 < #endif
72 < #ifndef ldexp
73 < extern double  ldexp();
74 < #endif
14 > #include <math.h>
15  
16  
17 < void
18 < putstr(s, fp)                   /* write null-terminated string to fp */
19 < register char  *s;
20 < register FILE  *fp;
17 > int
18 > putstr(                         /* write null-terminated string to fp */
19 >        char  *s,
20 >        FILE  *fp
21 > )
22   {
23          do
24                  putc(*s, fp);
25          while (*s++);
26 +
27 +        return(ferror(fp) ? EOF : 0);
28   }
29  
30  
31 < void
32 < putint(i, siz, fp)              /* write a siz-byte integer to fp */
33 < long  i;
34 < register int  siz;
35 < register FILE  *fp;
31 > int
32 > putint(                         /* write a siz-byte integer to fp */
33 >        long  i,
34 >        int  siz,
35 >        FILE  *fp
36 > )
37   {
38 <        while (siz--)
39 <                putc((int)(i>>(siz<<3) & 0xff), fp);
38 >        while (siz > sizeof(long)) {
39 >                putc((i<0)*0xff, fp);
40 >                siz--;
41 >        }
42 >        siz <<= 3;
43 >        while ((siz -= 8) > 0)
44 >                putc((int)(i>>siz & 0xff), fp);
45 >
46 >        return(putc((int)(i & 0xff), fp) == EOF ? EOF : 0);
47   }
48  
49  
50 < void
51 < putflt(f, fp)                   /* put out floating point number */
52 < double  f;
53 < FILE  *fp;
50 > int
51 > putflt(                         /* put out floating point number */
52 >        double  f,
53 >        FILE  *fp
54 > )
55   {
56 +        long  m;
57          int  e;
58  
59 <        putint((long)(frexp(f,&e)*0x7fffffff), 4, fp);
60 <        putint((long)e, 1, fp);
59 >        m = frexp(f, &e) * 0x7fffffff;
60 >        if (e > 127) {                  /* overflow */
61 >                m = m > 0 ? (long)0x7fffffff : -(long)0x7fffffff;
62 >                e = 127;
63 >        } else if (e < -128) {          /* underflow */
64 >                m = 0;
65 >                e = 0;
66 >        }
67 >        putint(m, 4, fp);
68 >        return(putint(e, 1, fp));
69   }
70  
71  
72 + size_t
73 + putbinary(                      /* fwrite() replacement for small objects */
74 +        const void *p,
75 +        size_t elsiz,
76 +        size_t nel,
77 +        FILE *fp)
78 + {
79 +        const char      *s = (const char *)p;
80 +        size_t          nbytes = elsiz*nel;
81 +
82 +        if (nbytes > 128)
83 +                return(fwrite(p, elsiz, nel, fp));
84 +        
85 +        while (nbytes-- > 0)
86 +                if (putc(*s++, fp) == EOF)
87 +                        return((elsiz*nel - nbytes)/elsiz);
88 +
89 +        return(nel);
90 + }
91 +
92 +
93   char *
94 < getstr(s, fp)                   /* get null-terminated string */
95 < char  *s;
96 < register FILE  *fp;
94 > getstr(                         /* get null-terminated string */
95 >        char  *s,
96 >        FILE  *fp
97 > )
98   {
99 <        register char  *cp;
100 <        register int  c;
99 >        char  *cp;
100 >        int  c;
101  
102          cp = s;
103          while ((c = getc(fp)) != EOF)
# Line 126 | Line 109 | register FILE  *fp;
109  
110  
111   long
112 < getint(siz, fp)                 /* get a siz-byte integer */
113 < int  siz;
114 < register FILE  *fp;
112 > getint(                         /* get a siz-byte integer */
113 >        int  siz,
114 >        FILE  *fp
115 > )
116   {
117 <        register int  c;
118 <        register long  r;
117 >        int  c;
118 >        long  r;
119  
120          if ((c = getc(fp)) == EOF)
121                  return(EOF);
122 <        r = 0x80&c ? -1<<8|c : c;               /* sign extend */
122 >        r = c;
123 >        if (c & 0x80)           /* sign extend? */
124 >                r |= -256L;
125          while (--siz > 0) {
126                  if ((c = getc(fp)) == EOF)
127                          return(EOF);
# Line 147 | Line 133 | register FILE  *fp;
133  
134  
135   double
136 < getflt(fp)                      /* get a floating point number */
137 < FILE  *fp;
136 > getflt(                         /* get a floating point number */
137 >        FILE  *fp
138 > )
139   {
140          long    l;
141          double  d;
142  
143          l = getint(4, fp);
144 +        if (l == EOF && feof(fp))       /* EOF? */
145 +                return((double)EOF);
146 +        if (l == 0) {
147 +                getc(fp);               /* exactly zero -- ignore exponent */
148 +                return(0.0);
149 +        }
150          d = (l + (l > 0 ? .5 : -.5)) * (1./0x7fffffff);
151          return(ldexp(d, (int)getint(1, fp)));
152 + }
153 +
154 +
155 + size_t
156 + getbinary(                      /* fread() replacement for small objects */
157 +        void *p,
158 +        size_t elsiz,
159 +        size_t nel,
160 +        FILE *fp)
161 + {
162 +        char    *s = (char *)p;
163 +        size_t  nbytes = elsiz*nel;
164 +        int     c;
165 +
166 +        if (nbytes > 128)
167 +                return(fread(p, elsiz, nel, fp));
168 +        
169 +        while (nbytes-- > 0) {
170 +                if ((c = getc(fp)) == EOF)
171 +                        return((elsiz*nel - nbytes)/elsiz);
172 +                *s++ = c;
173 +        }
174 +        return(nel);
175   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines