tt/mini_isam: remove all ancient sccsid blocks
[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 libraries 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 /*
29  * Copyright (c) 1988 by Sun Microsystems, Inc.
30  */
31
32 /*
33  * isdatconv.c
34  *
35  * Description:
36  *      Conversion function between machine dependent and the X/OPEN 
37  *      machine independent formats.
38  *
39  * Some pieces of code may not be very "structured", but they result in
40  * optimized code with the -O compiler option.
41  *
42  */
43
44 #include "isam_impl.h"
45
46 #define BLANK   ' '
47
48 /* conversion functions for sparc architecture */
49
50 /* ldlong() - Load a long integer from a potentially  unaligned address */
51
52 long 
53 ldlong(char *p)
54 {
55     int i;
56 #if LONG_BIT == 64
57     unsigned long val;
58 #else
59     unsigned int val;
60 #endif
61
62     val = 0;
63     for (i=0; i<LONGSIZE ; i++)  
64         val = (val << 8) + *((unsigned char *)p++);
65     
66     return ((long)val);
67 }
68
69 /* stlong() - Store a long integer at a potentially unaligned address */
70
71 int
72 stlong(long val, char *p)
73 {
74     int i;
75     p += LONGSIZE;
76
77     for (i=0; i<LONGSIZE ; i++)
78         *--p = (val >> 8*i) & 255;
79
80     return(0);
81 }
82
83 /* ldint() - Load a short integer from a potentially  unaligned address */
84
85 short
86 ldint(char *p)
87 {
88     unsigned int val;
89
90     val = *((unsigned char *)p++);
91     val = (val << 8) + *((unsigned char *)p++);
92
93     return ((short)val);
94 }
95
96
97 /* ldunshort - load a unshort integer : for 64K record length */
98
99 u_short
100 ldunshort(char *p)
101 {
102     unsigned int val;
103
104     val = *((unsigned char *)p++);
105     val = (val << 8) + *((unsigned char *)p++);
106
107     return ((u_short)val);
108 }
109
110 /* stint() - Store a short integer at a potentially unaligned address */
111
112 int
113 stint(short val, char *p)
114 {
115     p += SHORTSIZE;
116     *--p = val & 255;
117     *--p = (val >> 8) & 255;
118
119     return(0);
120 }
121
122 /* ldchar() - Load character field */
123
124 int
125 ldchar(char *src, int len, char *dst)
126 {
127     char        *p;
128
129     if (len <= 0)
130         return 0;
131
132     /* Load the entire string. */
133     memcpy((void *) dst, (const void *) src, len);
134
135     /* Remove trailing blanks. */
136     p = dst + len;
137     while (--p >= dst) {
138         if (*p != BLANK) {
139             break;
140         }
141     }
142
143     *++p = '\0';
144     return 0;
145 }
146
147 int
148 stchar(char *src, char *dst, int len)
149 {
150     char        c;
151
152     if (len <= 0)
153         return 0;
154
155     /* Copy up to NULL character. */
156     do {
157         if ((c = *src++) == '\0') 
158             break;
159         *dst++ = c;
160     } while (--len > 0);
161     
162     /* Pad with blanks. */
163     if (len > 0)
164         (void) memset((void *) dst, BLANK, len);
165     return 0;
166 }
167
168 /* ldchar2() - Load character field (C style, NULL padded) */
169  
170 int
171 ldchar2(char *src, int len, char *dst)
172 {
173     char       *p;
174  
175     if (len <= 0)
176         return 0;
177  
178     /* Load the entire string. */
179     memcpy((void *) dst, (const void *) src, len);
180     *(dst + len) = '\0';
181     return 0;
182 }
183  
184 int
185 stchar2(char *src, char *dst, int len)
186 {
187     char       c;
188  
189     if (len <= 0)
190         return 0;
191  
192     /* Copy up to a NULL character. */
193     do {
194         if ((c = *src++) == '\0')
195             break;
196         *dst++ = c;
197     } while (--len > 0);
198     
199     /* Pad with NULLs. */
200     if (len > 0)
201         memset(dst, 0, len);
202     return 0;
203 }
204
205 /* ldfloat() - Load a float number from a potentially  unaligned address */
206
207 float
208 ldfloat(char *p)
209 {
210     union {
211         float fval;
212         int   ival;
213     } uval;
214     unsigned int val;
215
216     val = *((unsigned char *)p++);
217     val = (val << 8) + *((unsigned char *)p++);
218     val = (val << 8) + *((unsigned char *)p++);
219     val = (val << 8) + *((unsigned char *)p++);
220
221     uval.ival = val;
222     return (uval.fval);
223 }
224
225 /* stfloat() - Store a float number at a potentially unaligned address */
226
227 /* f, Bug - it is passed as double */
228 int
229 stfloat(float f, char *p)
230 {
231     unsigned    val;
232     union {
233         float fval;
234         int   ival;
235     } uval;
236
237     uval.fval = f;                           /* This fixes compiler bug */
238     val = uval.ival;
239
240     p += LONGSIZE;
241     *--p = val & 255;
242     *--p = (val >> 8) & 255;
243     *--p = (val >> 16) & 255;
244     *--p = (val >> 24) & 255;
245
246     return(0);
247 }
248
249 #if sparc | mc68000    /* MRJ */
250
251 /* ldbld() - Load a double float number from a potentially unaligned address */
252
253 double
254 lddbl(char *p)
255 {
256     double val;
257
258     memcpy((void *)&val, (const void *) p, DOUBLESIZE);
259     return (val);
260 }
261
262 /* stdbl() - Store a double float number at a potentially unaligned address */
263
264 int
265 stdbl(double val, char *p)
266 {
267     memcpy ( p,(char *)&val, DOUBLESIZE);
268     return 0;
269 }
270
271 #else      /* 386i -- do it the long way round....  */
272
273 /* ldbld() - Load a double float number from a potentially unaligned address */
274
275 double
276 lddbl(char *p)
277 {
278     union {
279         double rval;
280         char   sval[DOUBLESIZE];
281     } x;
282
283     char  *q;
284     int  i;
285
286     q  =  x.sval;
287     p +=  DOUBLESIZE;
288    
289     for (i=0; i<DOUBLESIZE; i++)
290         *q++ = *--p;
291     return (x.rval);
292 }
293
294 /* stdbl() - Store a double float number at a potentially unaligned address */
295
296 int
297 stdbl(double val, char *p)
298 {
299     union {
300         double rval;
301         char   sval[DOUBLESIZE];
302     } x;
303
304     char  *q;
305     int  i;
306
307     x.rval = val;
308     q  =  x.sval;
309     p +=  DOUBLESIZE;
310    
311     for (i=0; i<DOUBLESIZE; i++)
312         *--p = *q++ ;
313
314     return(0);
315 }
316
317
318 #endif    /* sparc */
319
320