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, 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; (src[i] != '\0') && (i < maxlen - 1); i++)
112 if ((src[i] == ' ') || (src[i] == '\t'))
119 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
122 static void decode_string(int maxlen, char dst[], const char src[])
126 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
141 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
145 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
146 const char *desc, const char *srcfile, int line)
152 char buf[GMX_FIO_BUFLEN];
153 unsigned char *ucptr;
156 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
162 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
163 gmx_fio_dbgstr(fio, desc, buf));
166 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
170 case eioGMX_LARGE_INT:
171 sprintf(strbuf, "%s%s%s", "%", gmx_large_int_fmt, "\n");
172 res = fprintf(fp, strbuf, *((gmx_large_int_t *) item),
173 gmx_fio_dbgstr(fio, desc, buf));
176 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
177 gmx_fio_dbgstr(fio, desc, buf));
180 ucptr = (unsigned char *) item;
181 for (i = 0; (i < nitem); i++)
182 res = fprintf(fp, "%4d", (int) ucptr[i]);
183 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
186 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
187 gmx_fio_dbgstr(fio, desc, buf));
191 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
192 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
195 for (i = 0; (i < nitem); i++)
197 ptr = ((rvec *) item)[i];
198 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
199 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
204 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
205 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
208 encode_string(256, strbuf, (char *) item);
209 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
212 gmx_fio_fe(fio, eio, desc, srcfile, line);
214 if ((res <= 0) && fio->bDebug)
216 "Error writing %s %s to file %s (source %s, line %d)\n",
217 eioNames[eio], desc, fio->fn, srcfile, line);
223 static char *next_item(FILE *fp, char *buf, int buflen)
226 gmx_bool in_comment = FALSE;
227 gmx_bool in_token = FALSE;
229 /* This routine reads strings from the file fp, strips comment
230 * and buffers. For thread-safety reasons, It reads through getc() */
234 gmx_file("End of file");
244 if (isspace(rd) || rd == ';')
246 buf[i++] = (char) rd;
257 buf[i++] = (char) (rd);
263 } while ((rd = getc(fp)) != EOF);
265 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
272 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
273 const char *desc, const char *srcfile, int line)
276 int i, m, res = 0, *iptr, ix;
280 unsigned char uc, *ucptr;
282 #define NEXT_ITEM_BUF_LEN 128
283 char ni_buf[NEXT_ITEM_BUF_LEN];
285 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
291 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
293 *((real *) item) = d;
296 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
300 case eioGMX_LARGE_INT:
301 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
302 gmx_large_int_pfmt, &s);
304 *((gmx_large_int_t *) item) = s;
307 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
309 *((unsigned char *) item) = uc;
312 ucptr = (unsigned char *) item;
313 for (i = 0; (i < nitem); i++)
315 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
321 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
323 *((unsigned short *) item) = i;
327 for (m = 0; (m < DIM); m++)
329 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
334 for (i = 0; (i < nitem); i++)
336 ptr = ((rvec *) item)[i];
337 for (m = 0; (m < DIM); m++)
339 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
348 for (m = 0; (m < DIM); m++)
350 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
356 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
359 decode_string(strlen(cptr) + 1, (char *) item, cptr);
360 /* res = sscanf(cptr,"%s",(char *)item);*/
365 gmx_fio_fe(fio, eio, desc, srcfile, line);
368 if ((res <= 0) && fio->bDebug)
370 "Error reading %s %s from file %s (source %s, line %d)\n",
371 eioNames[eio], desc, fio->fn, srcfile, line);