2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
51 #include "gmx_fatal.h"
61 #include "thread_mpi.h"
64 #include "gmxfio_int.h"
67 /* This is the part that reads dummy and ascii files. */
72 /* file type functions */
73 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
74 const char *desc, const char *srcfile, int line);
75 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
76 const char *desc, const char *srcfile, int line);
77 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
78 const char *desc, const char *srcfile, int line);
79 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
80 const char *desc, const char *srcfile, int line);
83 const t_iotype asc_iotype = {do_ascread, do_ascwrite};
84 const t_iotype dummy_iotype = {do_dummyread, do_dummywrite};
91 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
92 const char *desc, const char *srcfile, int line)
94 gmx_fatal(FARGS, "File type not set!");
98 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
99 const char *desc, const char *srcfile, int line)
101 gmx_fatal(FARGS, "File type not set!");
107 static void encode_string(int maxlen, char dst[], const char src[])
111 for (i = 0; (i < maxlen - 1) && (src[i] != '\0'); i++)
113 if ((src[i] == ' ') || (src[i] == '\t'))
126 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
130 static void decode_string(int maxlen, char dst[], const char src[])
134 for (i = 0; (i < maxlen - 1) && (src[i] != '\0'); i++)
149 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
153 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
154 const char *desc, const char *srcfile, int line)
160 char buf[GMX_FIO_BUFLEN];
161 unsigned char *ucptr;
164 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
170 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
171 gmx_fio_dbgstr(fio, desc, buf));
174 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
178 case eioGMX_LARGE_INT:
179 sprintf(strbuf, "%s%s%s", "%", gmx_large_int_fmt, "\n");
180 res = fprintf(fp, strbuf, *((gmx_large_int_t *) item),
181 gmx_fio_dbgstr(fio, desc, buf));
184 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
185 gmx_fio_dbgstr(fio, desc, buf));
188 ucptr = (unsigned char *) item;
189 for (i = 0; (i < nitem); i++)
191 res = fprintf(fp, "%4d", (int) ucptr[i]);
193 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
196 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
197 gmx_fio_dbgstr(fio, desc, buf));
201 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
202 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
205 for (i = 0; (i < nitem); i++)
207 ptr = ((rvec *) item)[i];
208 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
209 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
214 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
215 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
218 encode_string(256, strbuf, (char *) item);
219 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
222 gmx_fio_fe(fio, eio, desc, srcfile, line);
224 if ((res <= 0) && fio->bDebug)
227 "Error writing %s %s to file %s (source %s, line %d)\n",
228 eioNames[eio], desc, fio->fn, srcfile, line);
235 static char *next_item(FILE *fp, char *buf, int buflen)
238 gmx_bool in_comment = FALSE;
239 gmx_bool in_token = FALSE;
241 /* This routine reads strings from the file fp, strips comment
242 * and buffers. For thread-safety reasons, It reads through getc() */
247 gmx_file("End of file");
260 if (isspace(rd) || rd == ';')
264 buf[i++] = (char) rd;
277 buf[i++] = (char) (rd);
286 while ((rd = getc(fp)) != EOF);
288 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
295 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
296 const char *desc, const char *srcfile, int line)
299 int i, m, res = 0, *iptr, ix;
303 unsigned char uc, *ucptr;
305 #define NEXT_ITEM_BUF_LEN 128
306 char ni_buf[NEXT_ITEM_BUF_LEN];
308 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
314 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
317 *((real *) item) = d;
321 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
327 case eioGMX_LARGE_INT:
328 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
329 gmx_large_int_pfmt, &s);
332 *((gmx_large_int_t *) item) = s;
336 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
339 *((unsigned char *) item) = uc;
343 ucptr = (unsigned char *) item;
344 for (i = 0; (i < nitem); i++)
346 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
354 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
357 *((unsigned short *) item) = i;
362 for (m = 0; (m < DIM); m++)
364 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
369 for (i = 0; (i < nitem); i++)
371 ptr = ((rvec *) item)[i];
372 for (m = 0; (m < DIM); m++)
374 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
385 for (m = 0; (m < DIM); m++)
387 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
395 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
398 decode_string(strlen(cptr) + 1, (char *) item, cptr);
399 /* res = sscanf(cptr,"%s",(char *)item);*/
404 gmx_fio_fe(fio, eio, desc, srcfile, line);
407 if ((res <= 0) && fio->bDebug)
410 "Error reading %s %s from file %s (source %s, line %d)\n",
411 eioNames[eio], desc, fio->fn, srcfile, line);