Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / mini_isam / isdatconv.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isdatconv.c /main/3 1995/10/23 11:37:16 rswiston $                                                   */
28 #ifndef lint
29 static char sccsid[] = "@(#)isdatconv.c 1.5 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isdatconv.c
37  *
38  * Description:
39  *      Conversion function between machine dependent and the X/OPEN 
40  *      machine independent formats.
41  *
42  * Some pieces of code may not be very "structured", but they result in
43  * optimized code with the -O compiler option.
44  *
45  */
46
47 #include "isam_impl.h"
48
49 #define BLANK   ' '
50
51 /* conversion functions for sparc architecture */
52
53 /* ldlong() - Load a long integer from a potentially  unaligned address */
54
55 long 
56 ldlong(p)
57     register char       *p;
58 {
59     int i;
60 #if LONG_BIT == 64
61     register unsigned long val;
62 #else
63     register unsigned int val;
64 #endif
65
66     val = 0;
67     for (i=0; i<LONGSIZE ; i++)  
68         val = (val << 8) + *((unsigned char *)p++);
69     
70     return ((long)val);
71 }
72
73 /* stlong() - Store a long integer at a potentially unaligned address */
74
75 int
76 stlong(val, p)
77     register long       val;
78     register char       *p;
79 {
80     int i;
81     p += LONGSIZE;
82
83     for (i=0; i<LONGSIZE ; i++)
84         *--p = (val >> 8*i) & 255;
85
86     return(0);
87 }
88
89 /* ldint() - Load a short integer from a potentially  unaligned address */
90
91 short
92 ldint(p)
93     register char       *p;
94 {
95     register unsigned int val;
96
97     val = *((unsigned char *)p++);
98     val = (val << 8) + *((unsigned char *)p++);
99
100     return ((short)val);
101 }
102
103
104 /* ldunshort - load a unshort integer : for 64K record length */
105
106 u_short
107 ldunshort(p)
108     register char       *p;
109 {
110     register unsigned int val;
111
112     val = *((unsigned char *)p++);
113     val = (val << 8) + *((unsigned char *)p++);
114
115     return ((u_short)val);
116 }
117
118 /* stint() - Store a short integer at a potentially unaligned address */
119
120 int
121 stint(val, p)
122     register short      val;
123     register char       *p;
124 {
125     p += SHORTSIZE;
126     *--p = val & 255;
127     *--p = (val >> 8) & 255;
128
129     return(0);
130 }
131
132 /* ldchar() - Load character field */
133
134 int
135 ldchar(src, len, dst)
136     char                *src;
137     register char       *dst;
138     int                 len;
139 {
140     register char       *p;
141
142     if (len <= 0)
143         return;
144
145     /* Load the entire string. */
146     memcpy((void *) dst, (const void *) src, len);
147
148     /* Remove trailing blanks. */
149     p = dst + len;
150     while (--p >= dst) {
151         if (*p != BLANK) {
152             break;
153         }
154     }
155
156     *++p = '\0';
157 }
158
159 int
160 stchar(src, dst, len)
161     register char       *src;
162     register char       *dst;
163     register int        len;
164 {
165     register char       c;
166
167     if (len <= 0)
168         return;
169
170     /* Copy up to NULL character. */
171     do {
172         if ((c = *src++) == '\0') 
173             break;
174         *dst++ = c;
175     } while (--len > 0);
176     
177     /* Pad with blanks. */
178     if (len > 0)
179         (void) memset((void *) dst, BLANK, len);
180 }
181
182 /* ldchar2() - Load character field (C style, NULL padded) */
183  
184 int
185 ldchar2(src, len, dst)
186     char                *src;
187     register char       *dst;
188     int                 len;
189 {
190     register char       *p;
191  
192     if (len <= 0)
193         return;
194  
195     /* Load the entire string. */
196     memcpy((void *) dst, (const void *) src, len);
197     *(dst + len) = NULL;
198 }
199  
200 int
201 stchar2(src, dst, len)
202     register char       *src;
203     register char       *dst;
204     register int        len;
205 {
206     register char       c;
207  
208     if (len <= 0)
209         return;
210  
211     /* Copy up to a NULL character. */
212     do {
213         if ((c = *src++) == '\0')
214             break;
215         *dst++ = c;
216     } while (--len > 0);
217     
218     /* Pad with NULLs. */
219     if (len > 0)
220         memset(dst, 0, len);
221 }
222
223 /* ldfloat() - Load a float number from a potentially  unaligned address */
224
225 float
226 ldfloat(p)
227     register char       *p;
228 {
229     union {
230         float fval;
231         int   ival;
232     } uval;
233     register unsigned int val;
234
235     val = *((unsigned char *)p++);
236     val = (val << 8) + *((unsigned char *)p++);
237     val = (val << 8) + *((unsigned char *)p++);
238     val = (val << 8) + *((unsigned char *)p++);
239
240     uval.ival = val;
241     return (uval.fval);
242 }
243
244 /* stfloat() - Store a float number at a potentially unaligned address */
245
246 int
247 stfloat(f, p)
248     float               f;                   /* Bug - it is passed as double */
249     register char       *p;
250 {
251     register unsigned   val;
252     union {
253         float fval;
254         int   ival;
255     } uval;
256
257     uval.fval = f;                           /* This fixes compiler bug */
258     val = uval.ival;
259
260     p += LONGSIZE;
261     *--p = val & 255;
262     *--p = (val >> 8) & 255;
263     *--p = (val >> 16) & 255;
264     *--p = (val >> 24) & 255;
265
266     return(0);
267 }
268
269 #if sparc | mc68000    /* MRJ */
270
271 /* ldbld() - Load a double float number from a potentially unaligned address */
272
273 double
274 lddbl(p)
275     register char       *p;
276 {
277     double val;
278
279     memcpy((void *)&val, (const void *) p, DOUBLESIZE);
280     return (val);
281 }
282
283 /* stdbl() - Store a double float number at a potentially unaligned address */
284
285 int
286 stdbl(val, p)
287     double              val;
288     register char       *p;
289 {
290     memcpy ( p,(char *)&val, DOUBLESIZE);
291 }
292
293 #else      /* 386i -- do it the long way round....  */
294
295 /* ldbld() - Load a double float number from a potentially unaligned address */
296
297 double
298 lddbl(p)
299     register char       *p;
300 {
301     union {
302         double rval;
303         char   sval[DOUBLESIZE];
304     } x;
305
306     char  *q;
307     int  i;
308
309     q  =  x.sval;
310     p +=  DOUBLESIZE;
311    
312     for (i=0; i<DOUBLESIZE; i++)
313         *q++ = *--p;
314     return (x.rval);
315 }
316
317 /* stdbl() - Store a double float number at a potentially unaligned address */
318
319 int
320 stdbl(val, p)
321     double              val;
322     register char       *p;
323 {
324     union {
325         double rval;
326         char   sval[DOUBLESIZE];
327     } x;
328
329     char  *q;
330     int  i;
331
332     x.rval = val;
333     q  =  x.sval;
334     p +=  DOUBLESIZE;
335    
336     for (i=0; i<DOUBLESIZE; i++)
337         *--p = *q++ ;
338
339     return(0);
340 }
341
342
343 #endif    /* sparc */
344
345