Converted gmxpreprocess to compile as C++
[alexxy/gromacs.git] / src / gromacs / fileio / gmx_system_xdr.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef GMX_FILEIO_GMX_SYSTEM_XDR_H
39 #define GMX_FILEIO_GMX_SYSTEM_XDR_H
40
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45
46 /*
47  * This header file is ONLY used on windows systems, since these do
48  * not include the XDR routines present on a unix machine. It will
49  * most probably work on other platforms too, but make sure you
50  * test that the xtc files produced are ok before using it.
51  *
52  * This header file contains Gromacs versions of the definitions for
53  * Sun External Data Representation (XDR) headers and routines.
54  *
55  * On most UNIX systems this is already present as part of your
56  * system libraries, but since we want to make Gromacs portable to
57  * platforms like Microsoft Windows we have created a private version
58  * of the necessary routines and distribute them with the Gromacs source.
59  *
60  * Although the rest of Gromacs is LGPL, you can copy and use the XDR
61  * routines in any way you want as long as you obey Sun's license:
62  *
63  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
64  * unrestricted use provided that this legend is included on all tape
65  * media and as a part of the software program in whole or part.  Users
66  * may copy or modify Sun RPC without charge, but are not authorized
67  * to license or distribute it to anyone else except as part of a product or
68  * program developed by the user.
69  *
70  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
71  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
72  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
73  *
74  * Sun RPC is provided with no support and without any obligation on the
75  * part of Sun Microsystems, Inc. to assist in its use, correction,
76  * modification or enhancement.
77  *
78  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
79  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
80  * OR ANY PART THEREOF.
81  *
82  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
83  * or profits or other special, indirect and consequential damages, even if
84  * Sun has been advised of the possibility of such damages.
85  *
86  * Sun Microsystems, Inc.
87  * 2550 Garcia Avenue
88  * Mountain View, California  94043
89  */
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94
95 /*
96  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
97  * stream.  XDR_DECODE causes the type to be extracted from the stream.
98  * XDR_FREE can be used to release the space allocated by an
99  * XDR_DECODE request.
100  */
101
102 /* We already have a boolean type in Gromacs, but the XDR library
103  * one has a slightly different name (the calls should be identical).
104  */
105 typedef int bool_t;
106
107 /*
108  * Aninteger type that is 32 bits wide. Check if int,
109  * long or short is 32 bits and die if none of them is :-)
110  */
111 #if (INT_MAX == 2147483647)
112 typedef int xdr_int32_t;
113 typedef unsigned int xdr_uint32_t;
114 #elif (LONG_MAX == 2147483647L)
115 typedef long xdr_int32_t;
116 typedef unsigned long xdr_uint32_t;
117 #elif (SHRT_MAX == 2147483647)
118 typedef short xdr_int32_t;
119 typedef unsigned short xdr_uint32_t;
120 #else
121 #  error ERROR: No 32 bit wide integer type found!
122 #endif
123
124 enum xdr_op {
125     XDR_ENCODE = 0,
126     XDR_DECODE = 1,
127     XDR_FREE   = 2
128 };
129
130 #ifndef FALSE
131 #      define  FALSE   (0)
132 #endif
133 #ifndef TRUE
134 #      define  TRUE    (1)
135 #endif
136
137
138 #define BYTES_PER_XDR_UNIT  (4)
139 /* Macro to round up to units of 4. */
140 #define XDR_RNDUP(x)  (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
141
142
143 /*
144  * The XDR handle.
145  * Contains operation which is being applied to the stream,
146  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
147  * and two private fields for the use of the particular implementation.
148  */
149 typedef struct XDR XDR;
150 struct XDR
151 {
152     enum xdr_op x_op;       /* operation; fast additional param */
153     struct xdr_ops
154     {
155         bool_t       (*x_getbytes) (XDR *__xdrs, char *__addr, unsigned int __len);
156         /* get some bytes from " */
157         bool_t       (*x_putbytes) (XDR *__xdrs, char *__addr, unsigned int __len);
158         /* put some bytes to " */
159         unsigned int (*x_getpostn) (XDR *__xdrs);
160         /* returns bytes off from beginning */
161         bool_t       (*x_setpostn) (XDR *__xdrs, unsigned int __pos);
162         /* lets you reposition the stream */
163         xdr_int32_t *(*x_inline) (XDR *__xdrs, int __len);
164         /* buf quick ptr to buffered data */
165         void         (*x_destroy) (XDR *__xdrs);
166         /* free privates of this xdr_stream */
167         bool_t       (*x_getint32) (XDR *__xdrs, xdr_int32_t *__ip);
168         /* get a int from underlying stream */
169         bool_t       (*x_putint32) (XDR *__xdrs, xdr_int32_t *__ip);
170         /* put a int to " */
171         bool_t       (*x_getuint32) (XDR *__xdrs, xdr_uint32_t *__ip);
172         /* get a unsigned int from underlying stream */
173         bool_t       (*x_putuint32) (XDR *__xdrs, xdr_uint32_t *__ip);
174         /* put a int to " */
175     }
176     *x_ops;
177     char *x_public;     /* users' data */
178     char *x_private;    /* pointer to private data */
179     char *x_base;       /* private used for position info */
180     int   x_handy;      /* extra private word */
181 };
182
183 /*
184  * A xdrproc_t exists for each data type which is to be encoded or decoded.
185  *
186  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
187  * The opaque pointer generally points to a structure of the data type
188  * to be decoded.  If this pointer is 0, then the type routines should
189  * allocate dynamic storage of the appropriate size and return it.
190  */
191
192 typedef bool_t (*xdrproc_t) (XDR *, void *, ...);
193
194 /*
195  * Operations defined on a XDR handle
196  *
197  * XDR          *xdrs;
198  * xdr_int32_t  *int32p;
199  * long         *longp;
200  * char         *addr;
201  * unsigned int  len;
202  * unsigned int  pos;
203  */
204
205
206 #define xdr_getint32(xdrs, int32p)                      \
207     (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
208
209 #define xdr_putint32(xdrs, int32p)                      \
210     (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
211
212 #define xdr_getuint32(xdrs, uint32p)                      \
213     (*(xdrs)->x_ops->x_getuint32)(xdrs, uint32p)
214
215 #define xdr_putuint32(xdrs, uint32p)                      \
216     (*(xdrs)->x_ops->x_putuint32)(xdrs, uint32p)
217
218 #define xdr_getbytes(xdrs, addr, len)           \
219     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
220
221 #define xdr_putbytes(xdrs, addr, len)           \
222     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
223
224 #define xdr_getpos(xdrs)                \
225     (*(xdrs)->x_ops->x_getpostn)(xdrs)
226
227 #define xdr_setpos(xdrs, pos)               \
228     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
229
230 #define xdr_inline(xdrs, len)               \
231     (*(xdrs)->x_ops->x_inline)(xdrs, len)
232
233 #define xdr_destroy(xdrs)                   \
234     do {                            \
235         if ((xdrs)->x_ops->x_destroy) {           \
236             (*(xdrs)->x_ops->x_destroy)(xdrs); }  \
237     } while (0)
238
239
240 bool_t xdr_int (XDR *__xdrs, int *__ip);
241 bool_t xdr_u_int (XDR *__xdrs, unsigned int *__ip);
242 bool_t xdr_short (XDR *__xdrs, short *__ip);
243 bool_t xdr_u_short (XDR *__xdrs, unsigned short *__ip);
244 bool_t xdr_bool (XDR *__xdrs, int *__bp);
245 bool_t xdr_opaque (XDR *__xdrs, char *__cp, unsigned int __cnt);
246 bool_t xdr_string (XDR *__xdrs, char **__cpp, unsigned int __maxsize);
247 bool_t xdr_char (XDR *__xdrs, char *__cp);
248 bool_t xdr_u_char (XDR *__xdrs, unsigned char *__cp);
249 bool_t xdr_vector (XDR *__xdrs, char *__basep, unsigned int __nelem,
250                    unsigned int __elemsize, xdrproc_t __xdr_elem);
251 bool_t xdr_float (XDR *__xdrs, float *__fp);
252 bool_t xdr_double (XDR *__xdrs, double *__dp);
253 void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop);
254
255 /* free memory buffers for xdr */
256 void xdr_free (xdrproc_t __proc, char *__objp);
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif /* GMX_FILEIO_GMX_SYSTEM_XDR_H */