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 * 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.
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.
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.
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.
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.
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.
50 #include "gmx_fatal.h"
59 #include "gmxfio_int.h"
62 /* This is the part that reads dummy and ascii files. */
67 /* file type functions */
68 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
69 const char *desc, const char *srcfile, int line);
70 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
71 const char *desc, const char *srcfile, int line);
72 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
73 const char *desc, const char *srcfile, int line);
74 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
75 const char *desc, const char *srcfile, int line);
78 const t_iotype asc_iotype = {do_ascread, do_ascwrite};
79 const t_iotype dummy_iotype = {do_dummyread, do_dummywrite};
86 static gmx_bool do_dummyread(t_fileio gmx_unused *fio, void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
87 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused line)
89 gmx_fatal(FARGS, "File type not set!");
93 static gmx_bool do_dummywrite(t_fileio gmx_unused *fio, const void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
94 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused line)
96 gmx_fatal(FARGS, "File type not set!");
102 static void encode_string(int maxlen, char dst[], const char src[])
106 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
108 if ((src[i] == ' ') || (src[i] == '\t'))
121 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
125 static void decode_string(int maxlen, char dst[], const char src[])
129 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
144 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
148 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
149 const char *desc, const char *srcfile, int line)
155 char buf[GMX_FIO_BUFLEN];
156 unsigned char *ucptr;
159 gmx_fio_check_nitem(eio, nitem, srcfile, line);
165 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
166 gmx_fio_dbgstr(fio, desc, buf));
169 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
174 sprintf(strbuf, "%s%s%s", "%", GMX_PRId64, "\n");
175 res = fprintf(fp, strbuf, *((gmx_int64_t *) item),
176 gmx_fio_dbgstr(fio, desc, buf));
179 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
180 gmx_fio_dbgstr(fio, desc, buf));
183 ucptr = (unsigned char *) item;
184 for (i = 0; (i < nitem); i++)
186 res = fprintf(fp, "%4d", (int) ucptr[i]);
188 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
191 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
192 gmx_fio_dbgstr(fio, desc, buf));
196 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
197 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
200 for (i = 0; (i < nitem); i++)
202 ptr = ((rvec *) item)[i];
203 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
204 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
209 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
210 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
213 encode_string(256, strbuf, (char *) item);
214 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
217 gmx_fio_fe(fio, eio, desc, srcfile, line);
219 if ((res <= 0) && fio->bDebug)
222 "Error writing %s %s to file %s (source %s, line %d)\n",
223 eioNames[eio], desc, fio->fn, srcfile, line);
230 static char *next_item(FILE *fp, char *buf, int buflen)
233 gmx_bool in_comment = FALSE;
234 gmx_bool in_token = FALSE;
236 /* This routine reads strings from the file fp, strips comment
237 * and buffers. For thread-safety reasons, It reads through getc() */
242 gmx_file("End of file");
255 if (isspace(rd) || rd == ';')
259 buf[i++] = (char) rd;
272 buf[i++] = (char) (rd);
281 while ((rd = getc(fp)) != EOF);
283 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
290 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
291 const char *desc, const char *srcfile, int line)
294 int i, m, res = 0, *iptr, ix;
298 unsigned char uc, *ucptr;
300 #define NEXT_ITEM_BUF_LEN 128
301 char ni_buf[NEXT_ITEM_BUF_LEN];
303 gmx_fio_check_nitem(eio, nitem, srcfile, line);
309 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
312 *((real *) item) = d;
316 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
323 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
327 *((gmx_int64_t *) item) = s;
331 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
334 *((unsigned char *) item) = uc;
338 ucptr = (unsigned char *) item;
339 for (i = 0; (i < nitem); i++)
341 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
349 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
352 *((unsigned short *) item) = i;
357 for (m = 0; (m < DIM); m++)
359 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
364 for (i = 0; (i < nitem); i++)
366 ptr = ((rvec *) item)[i];
367 for (m = 0; (m < DIM); m++)
369 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
380 for (m = 0; (m < DIM); m++)
382 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
390 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
393 decode_string(strlen(cptr) + 1, (char *) item, cptr);
394 /* res = sscanf(cptr,"%s",(char *)item);*/
399 gmx_fio_fe(fio, eio, desc, srcfile, line);
402 if ((res <= 0) && fio->bDebug)
405 "Error reading %s %s from file %s (source %s, line %d)\n",
406 eioNames[eio], desc, fio->fn, srcfile, line);