Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / gmxlib / gmxfio_asc.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  *
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  *
31  * For more info, check our website at http://www.gromacs.org
32  *
33  * And Hey:
34  * GROningen Mixture of Alchemy and Childrens' Stories
35  */
36
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #ifdef HAVE_IO_H
46 #include <io.h>
47 #endif
48
49 #include "gmx_fatal.h"
50 #include "macros.h"
51 #include "smalloc.h"
52 #include "futil.h"
53 #include "filenm.h"
54 #include "string2.h"
55 #include "gmxfio.h"
56 #include "md5.h"
57
58 #ifdef GMX_THREAD_MPI
59 #include "thread_mpi.h"
60 #endif
61
62 #include "gmxfio_int.h"
63
64
65 /* This is the part that reads dummy and ascii files.  */
66
67
68
69
70 /* file type functions */
71 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
72                            const char *desc, const char *srcfile, int line);
73 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
74                             const char *desc, const char *srcfile, int line);
75 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
76                              const char *desc, const char *srcfile, int line);
77 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
78                               const char *desc, const char *srcfile, int line);
79
80
81 const t_iotype asc_iotype = {do_ascread, do_ascwrite};
82 const t_iotype dummy_iotype = {do_dummyread, do_dummywrite};
83
84
85
86
87
88
89 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
90                              const char *desc, const char *srcfile, int line)
91 {
92     gmx_fatal(FARGS, "File type not set!");
93     return FALSE;
94 }
95
96 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
97                               const char *desc, const char *srcfile, int line)
98 {
99     gmx_fatal(FARGS, "File type not set!");
100     return FALSE;
101 }
102
103
104
105 static void encode_string(int maxlen, char dst[], const char src[])
106 {
107     int i;
108
109     for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
110     {
111         if ((src[i] == ' ') || (src[i] == '\t'))
112         {
113             dst[i] = '_';
114         }
115         else
116         {
117             dst[i] = src[i];
118         }
119     }
120     dst[i] = '\0';
121
122     if (i == maxlen)
123     {
124         fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
125     }
126 }
127
128 static void decode_string(int maxlen, char dst[], const char src[])
129 {
130     int i;
131
132     for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
133     {
134         if (src[i] == '_')
135         {
136             dst[i] = ' ';
137         }
138         else
139         {
140             dst[i] = src[i];
141         }
142     }
143     dst[i] = '\0';
144
145     if (i == maxlen)
146     {
147         fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
148     }
149 }
150
151 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
152                             const char *desc, const char *srcfile, int line)
153 {
154     int            i;
155     int            res = 0, *iptr;
156     real          *ptr;
157     char           strbuf[256];
158     char           buf[GMX_FIO_BUFLEN];
159     unsigned char *ucptr;
160     FILE          *fp = fio->fp;
161
162     gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
163     switch (eio)
164     {
165         case eioREAL:
166         case eioFLOAT:
167         case eioDOUBLE:
168             res = fprintf(fp, "%18.10e%s\n", *((real *) item),
169                           gmx_fio_dbgstr(fio, desc, buf));
170             break;
171         case eioINT:
172             res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
173                                                                           desc,
174                                                                           buf));
175             break;
176         case eioGMX_LARGE_INT:
177             sprintf(strbuf, "%s%s%s", "%", gmx_large_int_fmt, "\n");
178             res = fprintf(fp, strbuf, *((gmx_large_int_t *) item),
179                           gmx_fio_dbgstr(fio, desc, buf));
180             break;
181         case eioUCHAR:
182             res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
183                           gmx_fio_dbgstr(fio, desc, buf));
184             break;
185         case eioNUCHAR:
186             ucptr = (unsigned char *) item;
187             for (i = 0; (i < nitem); i++)
188             {
189                 res = fprintf(fp, "%4d", (int) ucptr[i]);
190             }
191             fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
192             break;
193         case eioUSHORT:
194             res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
195                           gmx_fio_dbgstr(fio, desc, buf));
196             break;
197         case eioRVEC:
198             ptr = (real *) item;
199             res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
200                           ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
201             break;
202         case eioNRVEC:
203             for (i = 0; (i < nitem); i++)
204             {
205                 ptr = ((rvec *) item)[i];
206                 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
207                               ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
208             }
209             break;
210         case eioIVEC:
211             iptr = (int *) item;
212             res  = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
213                            iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
214             break;
215         case eioSTRING:
216             encode_string(256, strbuf, (char *) item);
217             res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
218             break;
219         default:
220             gmx_fio_fe(fio, eio, desc, srcfile, line);
221     }
222     if ((res <= 0) && fio->bDebug)
223     {
224         fprintf(stderr,
225                 "Error writing %s %s to file %s (source %s, line %d)\n",
226                 eioNames[eio], desc, fio->fn, srcfile, line);
227     }
228
229     return (res > 0);
230 }
231
232
233 static char *next_item(FILE *fp, char *buf, int buflen)
234 {
235     int      rd;
236     gmx_bool in_comment = FALSE;
237     gmx_bool in_token   = FALSE;
238     int      i          = 0;
239     /* This routine reads strings from the file fp, strips comment
240      * and buffers. For thread-safety reasons, It reads through getc()  */
241
242     rd = getc(fp);
243     if (rd == EOF)
244     {
245         gmx_file("End of file");
246     }
247     do
248     {
249         if (in_comment)
250         {
251             if (rd == '\n')
252             {
253                 in_comment = FALSE;
254             }
255         }
256         else if (in_token)
257         {
258             if (isspace(rd) || rd == ';')
259             {
260                 break;
261             }
262             buf[i++] = (char) rd;
263         }
264         else
265         {
266             if (!isspace(rd))
267             {
268                 if (rd == ';')
269                 {
270                     in_comment = TRUE;
271                 }
272                 else
273                 {
274                     in_token = TRUE;
275                     buf[i++] = (char) (rd);
276                 }
277             }
278         }
279         if (i >= buflen - 2)
280         {
281             break;
282         }
283     }
284     while ((rd = getc(fp)) != EOF);
285
286     fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
287
288     buf[i] = 0;
289
290     return buf;
291 }
292
293 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
294                            const char *desc, const char *srcfile, int line)
295 {
296     FILE           *fp = fio->fp;
297     int             i, m, res = 0, *iptr, ix;
298     gmx_large_int_t s;
299     double          d, x;
300     real           *ptr;
301     unsigned char   uc, *ucptr;
302     char           *cptr;
303 #define NEXT_ITEM_BUF_LEN 128
304     char            ni_buf[NEXT_ITEM_BUF_LEN];
305
306     gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
307     switch (eio)
308     {
309         case eioREAL:
310         case eioFLOAT:
311         case eioDOUBLE:
312             res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
313             if (item)
314             {
315                 *((real *) item) = d;
316             }
317             break;
318         case eioINT:
319             res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
320             if (item)
321             {
322                 *((int *) item) = i;
323             }
324             break;
325         case eioGMX_LARGE_INT:
326             res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
327                          gmx_large_int_pfmt, &s);
328             if (item)
329             {
330                 *((gmx_large_int_t *) item) = s;
331             }
332             break;
333         case eioUCHAR:
334             res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
335             if (item)
336             {
337                 *((unsigned char *) item) = uc;
338             }
339             break;
340         case eioNUCHAR:
341             ucptr = (unsigned char *) item;
342             for (i = 0; (i < nitem); i++)
343             {
344                 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
345                 if (item)
346                 {
347                     ucptr[i] = ix;
348                 }
349             }
350             break;
351         case eioUSHORT:
352             res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
353             if (item)
354             {
355                 *((unsigned short *) item) = i;
356             }
357             break;
358         case eioRVEC:
359             ptr = (real *) item;
360             for (m = 0; (m < DIM); m++)
361             {
362                 res    = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
363                 ptr[m] = x;
364             }
365             break;
366         case eioNRVEC:
367             for (i = 0; (i < nitem); i++)
368             {
369                 ptr = ((rvec *) item)[i];
370                 for (m = 0; (m < DIM); m++)
371                 {
372                     res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
373                                  &x);
374                     if (item)
375                     {
376                         ptr[m] = x;
377                     }
378                 }
379             }
380             break;
381         case eioIVEC:
382             iptr = (int *) item;
383             for (m = 0; (m < DIM); m++)
384             {
385                 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
386                 if (item)
387                 {
388                     iptr[m] = ix;
389                 }
390             }
391             break;
392         case eioSTRING:
393             cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
394             if (item)
395             {
396                 decode_string(strlen(cptr) + 1, (char *) item, cptr);
397                 /* res = sscanf(cptr,"%s",(char *)item);*/
398                 res = 1;
399             }
400             break;
401         default:
402             gmx_fio_fe(fio, eio, desc, srcfile, line);
403     }
404
405     if ((res <= 0) && fio->bDebug)
406     {
407         fprintf(stderr,
408                 "Error reading %s %s from file %s (source %s, line %d)\n",
409                 eioNames[eio], desc, fio->fn, srcfile, line);
410     }
411     return (res > 0);
412 }