Merge branch 'master' into cde-next
[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
174     if (len <= 0)
175         return 0;
176
177     /* Load the entire string. */
178     memcpy((void *) dst, (const void *) src, len);
179     *(dst + len) = '\0';
180     return 0;
181 }
182
183 int
184 stchar2(char *src, char *dst, int len)
185 {
186     char       c;
187
188     if (len <= 0)
189         return 0;
190
191     /* Copy up to a NULL character. */
192     do {
193         if ((c = *src++) == '\0')
194             break;
195         *dst++ = c;
196     } while (--len > 0);
197
198     /* Pad with NULLs. */
199     if (len > 0)
200         memset(dst, 0, len);
201     return 0;
202 }
203
204 /* ldfloat() - Load a float number from a potentially  unaligned address */
205
206 float
207 ldfloat(char *p)
208 {
209     union {
210         float fval;
211         int   ival;
212     } uval;
213     unsigned int val;
214
215     val = *((unsigned char *)p++);
216     val = (val << 8) + *((unsigned char *)p++);
217     val = (val << 8) + *((unsigned char *)p++);
218     val = (val << 8) + *((unsigned char *)p++);
219
220     uval.ival = val;
221     return (uval.fval);
222 }
223
224 /* stfloat() - Store a float number at a potentially unaligned address */
225
226 /* f, Bug - it is passed as double */
227 int
228 stfloat(float f, char *p)
229 {
230     unsigned    val;
231     union {
232         float fval;
233         int   ival;
234     } uval;
235
236     uval.fval = f;                           /* This fixes compiler bug */
237     val = uval.ival;
238
239     p += LONGSIZE;
240     *--p = val & 255;
241     *--p = (val >> 8) & 255;
242     *--p = (val >> 16) & 255;
243     *--p = (val >> 24) & 255;
244
245     return(0);
246 }
247
248 #if sparc | mc68000    /* MRJ */
249
250 /* ldbld() - Load a double float number from a potentially unaligned address */
251
252 double
253 lddbl(char *p)
254 {
255     double val;
256
257     memcpy((void *)&val, (const void *) p, DOUBLESIZE);
258     return (val);
259 }
260
261 /* stdbl() - Store a double float number at a potentially unaligned address */
262
263 int
264 stdbl(double val, char *p)
265 {
266     memcpy ( p,(char *)&val, DOUBLESIZE);
267     return 0;
268 }
269
270 #else      /* 386i -- do it the long way round....  */
271
272 /* ldbld() - Load a double float number from a potentially unaligned address */
273
274 double
275 lddbl(char *p)
276 {
277     union {
278         double rval;
279         char   sval[DOUBLESIZE];
280     } x;
281
282     char  *q;
283     int  i;
284
285     q  =  x.sval;
286     p +=  DOUBLESIZE;
287
288     for (i=0; i<DOUBLESIZE; i++)
289         *q++ = *--p;
290     return (x.rval);
291 }
292
293 /* stdbl() - Store a double float number at a potentially unaligned address */
294
295 int
296 stdbl(double val, char *p)
297 {
298     union {
299         double rval;
300         char   sval[DOUBLESIZE];
301     } x;
302
303     char  *q;
304     int  i;
305
306     x.rval = val;
307     q  =  x.sval;
308     p +=  DOUBLESIZE;
309
310     for (i=0; i<DOUBLESIZE; i++)
311         *--p = *q++ ;
312
313     return(0);
314 }
315
316
317 #endif    /* sparc */