Don't use POSIX fnmatch() for pattern matching.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / gmx_system_xdr.c
blob37495fcca46221e216698a4e4893e0a9adcb0ea2
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #ifdef GMX_INTERNAL_XDR
42 #include <stdlib.h>
43 #include <limits.h>
44 #include <string.h>
46 #include "gmx_system_xdr.h"
49 /* NB - THIS FILE IS ONLY USED ON MICROSOFT WINDOWS, since that
50 * system doesn't provide any standard XDR system libraries. It will
51 * most probably work on other platforms too, but make sure you
52 * test that the xtc files produced are ok before using it.
54 * This header file contains Gromacs versions of the definitions for
55 * Sun External Data Representation (XDR) headers and routines.
57 * On most UNIX systems this is already present as part of your
58 * system libraries, but since we want to make Gromacs portable to
59 * platforms like Microsoft Windows we have created a private version
60 * of the necessary routines and distribute them with the Gromacs source.
62 * Although the rest of Gromacs is GPL, you can copy and use the XDR
63 * routines in any way you want as long as you obey Sun's license:
65 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
66 * unrestricted use provided that this legend is included on all tape
67 * media and as a part of the software program in whole or part. Users
68 * may copy or modify Sun RPC without charge, but are not authorized
69 * to license or distribute it to anyone else except as part of a product or
70 * program developed by the user.
72 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
73 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
74 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
76 * Sun RPC is provided with no support and without any obligation on the
77 * part of Sun Microsystems, Inc. to assist in its use, correction,
78 * modification or enhancement.
80 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
81 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
82 * OR ANY PART THEREOF.
84 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
85 * or profits or other special, indirect and consequential damages, even if
86 * Sun has been advised of the possibility of such damages.
88 * Sun Microsystems, Inc.
89 * 2550 Garcia Avenue
90 * Mountain View, California 94043
91 */
96 * for unit alignment
98 static char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
100 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
102 xdr_uint32_t y;
103 int i;
104 char *px=(char *)&x;
105 char *py=(char *)&y;
107 for(i=0;i<4;i++)
108 py[i]=px[3-i];
110 return y;
113 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
115 short s=0x0F00;
116 if( *((char *)&s)==(char)0x0F) {
117 /* bigendian, do nothing */
118 return x;
119 } else {
120 /* smallendian,swap bytes */
121 return xdr_swapbytes(x);
125 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
127 short s=0x0F00;
128 if( *((char *)&s)==(char)0x0F) {
129 /* bigendian, do nothing */
130 return x;
131 } else {
132 /* smallendian, swap bytes */
133 return xdr_swapbytes(x);
139 * Free a data structure using XDR
140 * Not a filter, but a convenient utility nonetheless
142 void
143 xdr_free (xdrproc_t proc, char *objp)
145 XDR x;
147 x.x_op = XDR_FREE;
148 (*proc) (&x, objp);
152 * XDR nothing
154 bool_t
155 xdr_void (void)
157 return TRUE;
161 * XDR integers
163 bool_t
164 xdr_int (XDR *xdrs, int *ip)
166 xdr_int32_t l;
168 switch (xdrs->x_op)
170 case XDR_ENCODE:
171 l = (xdr_int32_t) (*ip);
172 return xdr_putint32 (xdrs, &l);
174 case XDR_DECODE:
175 if (!xdr_getint32 (xdrs, &l))
177 return FALSE;
179 *ip = (int) l;
181 case XDR_FREE:
182 return TRUE;
184 return FALSE;
189 * XDR unsigned integers
191 bool_t
192 xdr_u_int (XDR *xdrs, unsigned int *up)
194 xdr_uint32_t l;
196 switch (xdrs->x_op)
198 case XDR_ENCODE:
199 l = (xdr_uint32_t) (*up);
200 return xdr_putuint32 (xdrs, &l);
202 case XDR_DECODE:
203 if (!xdr_getuint32 (xdrs, &l))
205 return FALSE;
207 *up = (unsigned int) l;
209 case XDR_FREE:
210 return TRUE;
212 return FALSE;
219 * XDR short integers
221 bool_t
222 xdr_short (XDR *xdrs, short *sp)
224 xdr_int32_t l;
226 switch (xdrs->x_op)
228 case XDR_ENCODE:
229 l = (xdr_int32_t) *sp;
230 return xdr_putint32 (xdrs, &l);
232 case XDR_DECODE:
233 if (!xdr_getint32 (xdrs, &l))
235 return FALSE;
237 *sp = (short) l;
238 return TRUE;
240 case XDR_FREE:
241 return TRUE;
243 return FALSE;
248 * XDR unsigned short integers
250 bool_t
251 xdr_u_short (XDR *xdrs, unsigned short *usp)
253 xdr_uint32_t l;
255 switch (xdrs->x_op)
257 case XDR_ENCODE:
258 l = (xdr_uint32_t) *usp;
259 return xdr_putuint32 (xdrs, &l);
261 case XDR_DECODE:
262 if (!xdr_getuint32 (xdrs, &l))
264 return FALSE;
266 *usp = (unsigned short) l;
267 return TRUE;
269 case XDR_FREE:
270 return TRUE;
272 return FALSE;
277 * XDR a char
279 bool_t
280 xdr_char (XDR *xdrs, char *cp)
282 int i;
284 i = (*cp);
285 if (!xdr_int (xdrs, &i))
287 return FALSE;
289 *cp = i;
290 return TRUE;
294 * XDR an unsigned char
296 bool_t
297 xdr_u_char (XDR *xdrs, unsigned char *cp)
299 unsigned int u;
301 u = (*cp);
302 if (!xdr_u_int (xdrs, &u))
304 return FALSE;
306 *cp = u;
307 return TRUE;
311 * XDR booleans
313 bool_t
314 xdr_bool (XDR *xdrs, int *bp)
316 #define XDR_FALSE ((xdr_int32_t) 0)
317 #define XDR_TRUE ((xdr_int32_t) 1)
319 xdr_int32_t lb;
321 switch (xdrs->x_op)
323 case XDR_ENCODE:
324 lb = *bp ? XDR_TRUE : XDR_FALSE;
325 return xdr_putint32 (xdrs, &lb);
327 case XDR_DECODE:
328 if (!xdr_getint32 (xdrs, &lb))
330 return FALSE;
332 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
333 return TRUE;
335 case XDR_FREE:
336 return TRUE;
338 return FALSE;
339 #undef XDR_FALSE
340 #undef XDR_TRUE
346 * XDR opaque data
347 * Allows the specification of a fixed size sequence of opaque bytes.
348 * cp points to the opaque object and cnt gives the byte length.
350 bool_t
351 xdr_opaque (XDR *xdrs, char *cp, unsigned int cnt)
353 unsigned int rndup;
354 static char crud[BYTES_PER_XDR_UNIT];
357 * if no data we are done
359 if (cnt == 0)
360 return TRUE;
363 * round byte count to full xdr units
365 rndup = cnt % BYTES_PER_XDR_UNIT;
366 if (rndup > 0)
367 rndup = BYTES_PER_XDR_UNIT - rndup;
369 switch (xdrs->x_op)
371 case XDR_DECODE:
372 if (!xdr_getbytes (xdrs, cp, cnt))
374 return FALSE;
376 if (rndup == 0)
377 return TRUE;
378 return xdr_getbytes (xdrs, (char *)crud, rndup);
380 case XDR_ENCODE:
381 if (!xdr_putbytes (xdrs, cp, cnt))
383 return FALSE;
385 if (rndup == 0)
386 return TRUE;
387 return xdr_putbytes (xdrs, xdr_zero, rndup);
389 case XDR_FREE:
390 return TRUE;
392 return FALSE;
397 * XDR null terminated ASCII strings
398 * xdr_string deals with "C strings" - arrays of bytes that are
399 * terminated by a NULL character. The parameter cpp references a
400 * pointer to storage; If the pointer is null, then the necessary
401 * storage is allocated. The last parameter is the max allowed length
402 * of the string as specified by a protocol.
404 bool_t
405 xdr_string (xdrs, cpp, maxsize)
406 XDR *xdrs;
407 char **cpp;
408 unsigned int maxsize;
410 char *sp = *cpp; /* sp is the actual string pointer */
411 unsigned int size = 0;
412 unsigned int nodesize = 0;
415 * first deal with the length since xdr strings are counted-strings
417 switch (xdrs->x_op)
419 case XDR_FREE:
420 if (sp == NULL)
422 return TRUE; /* already free */
424 /* fall through... */
425 case XDR_ENCODE:
426 if (sp == NULL)
427 return FALSE;
428 size = strlen (sp);
429 break;
430 case XDR_DECODE:
431 break;
434 if (!xdr_u_int (xdrs, &size))
436 return FALSE;
438 if (size > maxsize)
440 return FALSE;
442 nodesize = size + 1;
445 * now deal with the actual bytes
447 switch (xdrs->x_op)
449 case XDR_DECODE:
450 if (nodesize == 0)
452 return TRUE;
454 if (sp == NULL)
455 *cpp = sp = (char *) malloc (nodesize);
456 if (sp == NULL)
458 (void) fputs ("xdr_string: out of memory\n", stderr);
459 return FALSE;
461 sp[size] = 0;
462 /* fall into ... */
464 case XDR_ENCODE:
465 return xdr_opaque (xdrs, sp, size);
467 case XDR_FREE:
468 free (sp);
469 *cpp = NULL;
470 return TRUE;
472 return FALSE;
477 /* Floating-point stuff */
479 bool_t
480 xdr_float(xdrs, fp)
481 XDR *xdrs;
482 float *fp;
484 xdr_int32_t tmp;
486 switch (xdrs->x_op) {
488 case XDR_ENCODE:
489 tmp = *(xdr_int32_t *)fp;
490 return (xdr_putint32(xdrs, &tmp));
492 break;
494 case XDR_DECODE:
495 if (xdr_getint32(xdrs, &tmp)) {
496 *(xdr_int32_t *)fp = tmp;
497 return (TRUE);
500 break;
502 case XDR_FREE:
503 return (TRUE);
505 return (FALSE);
509 bool_t
510 xdr_double(xdrs, dp)
511 XDR *xdrs;
512 double *dp;
515 /* Windows and some other systems dont define double-precision
516 * word order in the header files, so unfortunately we have
517 * to calculate it!
519 static int LSW=-1; /* Least significant fp word */
520 int *ip;
521 xdr_int32_t tmp[2];
523 if(LSW<0) {
524 double x=0.987654321; /* Just a number */
526 /* Possible representations in IEEE double precision:
527 * (S=small endian, B=big endian)
529 * Byte order, Word order, Hex
530 * S S b8 56 0e 3c dd 9a ef 3f
531 * B S 3c 0e 56 b8 3f ef 9a dd
532 * S B dd 9a ef 3f b8 56 0e 3c
533 * B B 3f ef 9a dd 3c 0e 56 b8
536 unsigned char ix = *((char *)&x);
538 if(ix==0xdd || ix==0x3f)
539 LSW=1; /* Big endian word order */
540 else if(ix==0xb8 || ix==0x3c)
541 LSW=0; /* Small endian word order */
542 else { /* Catch strange errors */
543 printf("Error when detecting floating-point word order.\n"
544 "Do you have a non-IEEE system?\n"
545 "If possible, use the XDR libraries provided with your system,\n"
546 "instead of the Gromacs fallback XDR source.\n");
547 exit(0);
551 switch (xdrs->x_op) {
553 case XDR_ENCODE:
554 ip = (int *)dp;
555 tmp[0] = ip[!LSW];
556 tmp[1] = ip[LSW];
557 return (xdr_putint32(xdrs, tmp) &&
558 xdr_putint32(xdrs, tmp+1));
560 break;
562 case XDR_DECODE:
563 ip = (int *)dp;
564 if (xdr_getint32(xdrs, tmp+!LSW) &&
565 xdr_getint32(xdrs, tmp+LSW)) {
566 ip[0] = tmp[0];
567 ip[1] = tmp[1];
568 return (TRUE);
571 break;
573 case XDR_FREE:
574 return (TRUE);
576 return (FALSE);
580 /* Array routines */
583 * xdr_vector():
585 * XDR a fixed length array. Unlike variable-length arrays,
586 * the storage of fixed length arrays is static and unfreeable.
587 * > basep: base of the array
588 * > size: size of the array
589 * > elemsize: size of each element
590 * > xdr_elem: routine to XDR each element
592 bool_t
593 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
594 XDR *xdrs;
595 char *basep;
596 unsigned int nelem;
597 unsigned int elemsize;
598 xdrproc_t xdr_elem;
600 #define LASTUNSIGNED ((unsigned int)0-1)
601 unsigned int i;
602 char *elptr;
604 elptr = basep;
605 for (i = 0; i < nelem; i++)
607 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
609 return FALSE;
611 elptr += elemsize;
613 return TRUE;
614 #undef LASTUNSIGNED
619 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
620 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
621 static unsigned int xdrstdio_getpos (XDR *);
622 static bool_t xdrstdio_setpos (XDR *, unsigned int);
623 static xdr_int32_t *xdrstdio_inline (XDR *, int);
624 static void xdrstdio_destroy (XDR *);
625 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
626 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
627 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
628 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
631 * Ops vector for stdio type XDR
633 static const struct xdr_ops xdrstdio_ops =
635 xdrstdio_getbytes, /* deserialize counted bytes */
636 xdrstdio_putbytes, /* serialize counted bytes */
637 xdrstdio_getpos, /* get offset in the stream */
638 xdrstdio_setpos, /* set offset in the stream */
639 xdrstdio_inline, /* prime stream for inline macros */
640 xdrstdio_destroy, /* destroy stream */
641 xdrstdio_getint32, /* deserialize a int */
642 xdrstdio_putint32, /* serialize a int */
643 xdrstdio_getuint32, /* deserialize a int */
644 xdrstdio_putuint32 /* serialize a int */
648 * Initialize a stdio xdr stream.
649 * Sets the xdr stream handle xdrs for use on the stream file.
650 * Operation flag is set to op.
652 void
653 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
655 xdrs->x_op = op;
656 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
657 is not `const'. */
658 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
659 xdrs->x_private = (char *) file;
660 xdrs->x_handy = 0;
661 xdrs->x_base = 0;
665 * Destroy a stdio xdr stream.
666 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
668 static void
669 xdrstdio_destroy (XDR *xdrs)
671 (void) fflush ((FILE *) xdrs->x_private);
672 /* xx should we close the file ?? */
676 static bool_t
677 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
679 if ((len != 0) && (fread (addr, (int) len, 1,
680 (FILE *) xdrs->x_private) != 1))
681 return FALSE;
682 return TRUE;
685 static bool_t
686 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
688 if ((len != 0) && (fwrite (addr, (int) len, 1,
689 (FILE *) xdrs->x_private) != 1))
690 return FALSE;
691 return TRUE;
694 static unsigned int
695 xdrstdio_getpos (XDR *xdrs)
697 return (unsigned int) ftell ((FILE *) xdrs->x_private);
700 static bool_t
701 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
703 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
706 static xdr_int32_t *
707 xdrstdio_inline (XDR *xdrs, int len)
710 * Must do some work to implement this: must insure
711 * enough data in the underlying stdio buffer,
712 * that the buffer is aligned so that we can indirect through a
713 * long *, and stuff this pointer in xdrs->x_buf. Doing
714 * a fread or fwrite to a scratch buffer would defeat
715 * most of the gains to be had here and require storage
716 * management on this buffer, so we don't do this.
718 return NULL;
721 static bool_t
722 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
724 xdr_int32_t mycopy;
726 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
727 return FALSE;
728 *ip = xdr_ntohl (mycopy);
729 return TRUE;
732 static bool_t
733 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
735 xdr_int32_t mycopy = xdr_htonl (*ip);
737 ip = &mycopy;
738 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
739 return FALSE;
740 return TRUE;
743 static bool_t
744 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
746 xdr_uint32_t mycopy;
748 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
749 return FALSE;
750 *ip = xdr_ntohl (mycopy);
751 return TRUE;
754 static bool_t
755 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
757 xdr_uint32_t mycopy = xdr_htonl (*ip);
759 ip = &mycopy;
760 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
761 return FALSE;
762 return TRUE;
765 #else
767 gmx_system_xdr_empty;
768 #endif /* GMX_SYSTEM_XDR */