Revised wording in pdb2gmx.c, hopefully clearer now.
[gromacs/rigid-bodies.git] / src / gmxlib / gmx_system_xdr.c
blobe66a59970ce22c7d931c4a8f4e5028b482ab50ec
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 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 * For thread safety, we calculate it every time: locking this would
520 * be more expensive.
522 /*static int LSW=-1;*/ /* Least significant fp word */
523 int LSW=-1; /* Least significant fp word */
526 int *ip;
527 xdr_int32_t tmp[2];
529 if(LSW<0) {
530 double x=0.987654321; /* Just a number */
532 /* Possible representations in IEEE double precision:
533 * (S=small endian, B=big endian)
535 * Byte order, Word order, Hex
536 * S S b8 56 0e 3c dd 9a ef 3f
537 * B S 3c 0e 56 b8 3f ef 9a dd
538 * S B dd 9a ef 3f b8 56 0e 3c
539 * B B 3f ef 9a dd 3c 0e 56 b8
542 unsigned char ix = *((char *)&x);
544 if(ix==0xdd || ix==0x3f)
545 LSW=1; /* Big endian word order */
546 else if(ix==0xb8 || ix==0x3c)
547 LSW=0; /* Small endian word order */
548 else { /* Catch strange errors */
549 printf("Error when detecting floating-point word order.\n"
550 "Do you have a non-IEEE system?\n"
551 "If possible, use the XDR libraries provided with your system,\n"
552 "instead of the Gromacs fallback XDR source.\n");
553 exit(0);
557 switch (xdrs->x_op) {
559 case XDR_ENCODE:
560 ip = (int *)dp;
561 tmp[0] = ip[!LSW];
562 tmp[1] = ip[LSW];
563 return (xdr_putint32(xdrs, tmp) &&
564 xdr_putint32(xdrs, tmp+1));
566 break;
568 case XDR_DECODE:
569 ip = (int *)dp;
570 if (xdr_getint32(xdrs, tmp+!LSW) &&
571 xdr_getint32(xdrs, tmp+LSW)) {
572 ip[0] = tmp[0];
573 ip[1] = tmp[1];
574 return (TRUE);
577 break;
579 case XDR_FREE:
580 return (TRUE);
582 return (FALSE);
586 /* Array routines */
589 * xdr_vector():
591 * XDR a fixed length array. Unlike variable-length arrays,
592 * the storage of fixed length arrays is static and unfreeable.
593 * > basep: base of the array
594 * > size: size of the array
595 * > elemsize: size of each element
596 * > xdr_elem: routine to XDR each element
598 bool_t
599 xdr_vector (xdrs, basep, nelem, elemsize, xdr_elem)
600 XDR *xdrs;
601 char *basep;
602 unsigned int nelem;
603 unsigned int elemsize;
604 xdrproc_t xdr_elem;
606 #define LASTUNSIGNED ((unsigned int)0-1)
607 unsigned int i;
608 char *elptr;
610 elptr = basep;
611 for (i = 0; i < nelem; i++)
613 if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED))
615 return FALSE;
617 elptr += elemsize;
619 return TRUE;
620 #undef LASTUNSIGNED
625 static bool_t xdrstdio_getbytes (XDR *, char *, unsigned int);
626 static bool_t xdrstdio_putbytes (XDR *, char *, unsigned int);
627 static unsigned int xdrstdio_getpos (XDR *);
628 static bool_t xdrstdio_setpos (XDR *, unsigned int);
629 static xdr_int32_t *xdrstdio_inline (XDR *, int);
630 static void xdrstdio_destroy (XDR *);
631 static bool_t xdrstdio_getint32 (XDR *, xdr_int32_t *);
632 static bool_t xdrstdio_putint32 (XDR *, xdr_int32_t *);
633 static bool_t xdrstdio_getuint32 (XDR *, xdr_uint32_t *);
634 static bool_t xdrstdio_putuint32 (XDR *, xdr_uint32_t *);
637 * Ops vector for stdio type XDR
639 static const struct xdr_ops xdrstdio_ops =
641 xdrstdio_getbytes, /* deserialize counted bytes */
642 xdrstdio_putbytes, /* serialize counted bytes */
643 xdrstdio_getpos, /* get offset in the stream */
644 xdrstdio_setpos, /* set offset in the stream */
645 xdrstdio_inline, /* prime stream for inline macros */
646 xdrstdio_destroy, /* destroy stream */
647 xdrstdio_getint32, /* deserialize a int */
648 xdrstdio_putint32, /* serialize a int */
649 xdrstdio_getuint32, /* deserialize a int */
650 xdrstdio_putuint32 /* serialize a int */
654 * Initialize a stdio xdr stream.
655 * Sets the xdr stream handle xdrs for use on the stream file.
656 * Operation flag is set to op.
658 void
659 xdrstdio_create (XDR *xdrs, FILE *file, enum xdr_op op)
661 xdrs->x_op = op;
662 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
663 is not `const'. */
664 xdrs->x_ops = (struct xdr_ops *) &xdrstdio_ops;
665 xdrs->x_private = (char *) file;
666 xdrs->x_handy = 0;
667 xdrs->x_base = 0;
671 * Destroy a stdio xdr stream.
672 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
674 static void
675 xdrstdio_destroy (XDR *xdrs)
677 (void) fflush ((FILE *) xdrs->x_private);
678 /* xx should we close the file ?? */
682 static bool_t
683 xdrstdio_getbytes (XDR *xdrs, char *addr, unsigned int len)
685 if ((len != 0) && (fread (addr, (int) len, 1,
686 (FILE *) xdrs->x_private) != 1))
687 return FALSE;
688 return TRUE;
691 static bool_t
692 xdrstdio_putbytes (XDR *xdrs, char *addr, unsigned int len)
694 if ((len != 0) && (fwrite (addr, (int) len, 1,
695 (FILE *) xdrs->x_private) != 1))
696 return FALSE;
697 return TRUE;
700 static unsigned int
701 xdrstdio_getpos (XDR *xdrs)
703 return (unsigned int) ftell ((FILE *) xdrs->x_private);
706 static bool_t
707 xdrstdio_setpos (XDR *xdrs, unsigned int pos)
709 return fseek ((FILE *) xdrs->x_private, (xdr_int32_t) pos, 0) < 0 ? FALSE : TRUE;
712 static xdr_int32_t *
713 xdrstdio_inline (XDR *xdrs, int len)
716 * Must do some work to implement this: must insure
717 * enough data in the underlying stdio buffer,
718 * that the buffer is aligned so that we can indirect through a
719 * long *, and stuff this pointer in xdrs->x_buf. Doing
720 * a fread or fwrite to a scratch buffer would defeat
721 * most of the gains to be had here and require storage
722 * management on this buffer, so we don't do this.
724 return NULL;
727 static bool_t
728 xdrstdio_getint32 (XDR *xdrs, xdr_int32_t *ip)
730 xdr_int32_t mycopy;
732 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
733 return FALSE;
734 *ip = xdr_ntohl (mycopy);
735 return TRUE;
738 static bool_t
739 xdrstdio_putint32 (XDR *xdrs, xdr_int32_t *ip)
741 xdr_int32_t mycopy = xdr_htonl (*ip);
743 ip = &mycopy;
744 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
745 return FALSE;
746 return TRUE;
749 static bool_t
750 xdrstdio_getuint32 (XDR *xdrs, xdr_uint32_t *ip)
752 xdr_uint32_t mycopy;
754 if (fread ((char *) &mycopy, 4, 1, (FILE *) xdrs->x_private) != 1)
755 return FALSE;
756 *ip = xdr_ntohl (mycopy);
757 return TRUE;
760 static bool_t
761 xdrstdio_putuint32 (XDR *xdrs, xdr_uint32_t *ip)
763 xdr_uint32_t mycopy = xdr_htonl (*ip);
765 ip = &mycopy;
766 if (fwrite ((char *) ip, 4, 1, (FILE *) xdrs->x_private) != 1)
767 return FALSE;
768 return TRUE;
771 #else
773 gmx_system_xdr_empty;
774 #endif /* GMX_SYSTEM_XDR */