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,2014, 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.
51 #include "gmx_fatal.h"
53 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/cstringutil.h"
60 #include "gmxfio_int.h"
63 /* This is the part that reads dummy and ascii files. */
68 /* file type functions */
69 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
70 const char *desc, const char *srcfile, int line);
71 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
72 const char *desc, const char *srcfile, int line);
73 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
74 const char *desc, const char *srcfile, int line);
75 static gmx_bool do_dummywrite(t_fileio *fio, const void *item, int nitem, int eio,
76 const char *desc, const char *srcfile, int line);
79 const t_iotype asc_iotype = {do_ascread, do_ascwrite};
80 const t_iotype dummy_iotype = {do_dummyread, do_dummywrite};
87 static gmx_bool do_dummyread(t_fileio gmx_unused *fio, void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
88 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused line)
90 gmx_fatal(FARGS, "File type not set!");
94 static gmx_bool do_dummywrite(t_fileio gmx_unused *fio, const void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
95 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused line)
97 gmx_fatal(FARGS, "File type not set!");
103 static void encode_string(int maxlen, char dst[], const char src[])
107 for (i = 0; (i < maxlen - 1) && (src[i] != '\0'); i++)
109 if ((src[i] == ' ') || (src[i] == '\t'))
122 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
126 static void decode_string(int maxlen, char dst[], const char src[])
130 for (i = 0; (i < maxlen - 1) && (src[i] != '\0'); i++)
145 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
149 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
150 const char *desc, const char *srcfile, int line)
156 char buf[GMX_FIO_BUFLEN];
157 unsigned char *ucptr;
160 gmx_fio_check_nitem(eio, nitem, srcfile, line);
166 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
167 gmx_fio_dbgstr(fio, desc, buf));
170 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
175 sprintf(strbuf, "%s%s%s", "%", GMX_PRId64, "\n");
176 res = fprintf(fp, strbuf, *((gmx_int64_t *) item),
177 gmx_fio_dbgstr(fio, desc, buf));
180 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
181 gmx_fio_dbgstr(fio, desc, buf));
184 ucptr = (unsigned char *) item;
185 for (i = 0; (i < nitem); i++)
187 res = fprintf(fp, "%4d", (int) ucptr[i]);
189 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
192 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
193 gmx_fio_dbgstr(fio, desc, buf));
197 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
198 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
201 for (i = 0; (i < nitem); i++)
203 ptr = ((rvec *) item)[i];
204 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
205 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
210 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
211 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
214 encode_string(256, strbuf, (char *) item);
215 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
218 gmx_fio_fe(fio, eio, desc, srcfile, line);
220 if ((res <= 0) && fio->bDebug)
223 "Error writing %s %s to file %s (source %s, line %d)\n",
224 eioNames[eio], desc, fio->fn, srcfile, line);
231 static char *next_item(FILE *fp, char *buf, int buflen)
234 gmx_bool in_comment = FALSE;
235 gmx_bool in_token = FALSE;
237 /* This routine reads strings from the file fp, strips comment
238 * and buffers. For thread-safety reasons, It reads through getc() */
243 gmx_file("End of file");
256 if (isspace(rd) || rd == ';')
260 buf[i++] = (char) rd;
273 buf[i++] = (char) (rd);
282 while ((rd = getc(fp)) != EOF);
284 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
291 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
292 const char *desc, const char *srcfile, int line)
295 int i, m, res = 0, *iptr, ix;
299 unsigned char uc, *ucptr;
301 #define NEXT_ITEM_BUF_LEN 128
302 char ni_buf[NEXT_ITEM_BUF_LEN];
304 gmx_fio_check_nitem(eio, nitem, srcfile, line);
310 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
313 *((real *) item) = d;
317 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
324 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
328 *((gmx_int64_t *) item) = s;
332 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
335 *((unsigned char *) item) = uc;
339 ucptr = (unsigned char *) item;
340 for (i = 0; (i < nitem); i++)
342 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
350 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
353 *((unsigned short *) item) = i;
358 for (m = 0; (m < DIM); m++)
360 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
365 for (i = 0; (i < nitem); i++)
367 ptr = ((rvec *) item)[i];
368 for (m = 0; (m < DIM); m++)
370 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
381 for (m = 0; (m < DIM); m++)
383 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
391 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
394 decode_string(strlen(cptr) + 1, (char *) item, cptr);
395 /* res = sscanf(cptr,"%s",(char *)item);*/
400 gmx_fio_fe(fio, eio, desc, srcfile, line);
403 if ((res <= 0) && fio->bDebug)
406 "Error reading %s %s from file %s (source %s, line %d)\n",
407 eioNames[eio], desc, fio->fn, srcfile, line);