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