1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
49 #include "gmx_fatal.h"
59 #include "thread_mpi.h"
62 #include "gmxfio_int.h"
65 /* This is the part that reads dummy and ascii files. */
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);
81 const t_iotype asc_iotype={do_ascread, do_ascwrite};
82 const t_iotype dummy_iotype={do_dummyread, do_dummywrite};
89 static gmx_bool do_dummyread(t_fileio *fio, void *item, int nitem, int eio,
90 const char *desc, const char *srcfile, int line)
92 gmx_fatal(FARGS, "File type not set!");
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)
99 gmx_fatal(FARGS, "File type not set!");
105 static void encode_string(int maxlen, char dst[], const char src[])
109 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
110 if ((src[i] == ' ') || (src[i] == '\t'))
117 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
120 static void decode_string(int maxlen, char dst[], const char src[])
124 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
139 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
143 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
144 const char *desc, const char *srcfile, int line)
150 char buf[GMX_FIO_BUFLEN];
151 unsigned char *ucptr;
154 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
160 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
161 gmx_fio_dbgstr(fio, desc, buf));
164 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
168 case eioGMX_LARGE_INT:
169 sprintf(strbuf, "%s%s%s", "%", gmx_large_int_fmt, "\n");
170 res = fprintf(fp, strbuf, *((gmx_large_int_t *) item),
171 gmx_fio_dbgstr(fio, desc, buf));
174 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
175 gmx_fio_dbgstr(fio, desc, buf));
178 ucptr = (unsigned char *) item;
179 for (i = 0; (i < nitem); i++)
180 res = fprintf(fp, "%4d", (int) ucptr[i]);
181 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
184 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
185 gmx_fio_dbgstr(fio, desc, buf));
189 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
190 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
193 for (i = 0; (i < nitem); i++)
195 ptr = ((rvec *) item)[i];
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));
202 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
203 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
206 encode_string(256, strbuf, (char *) item);
207 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
210 gmx_fio_fe(fio, eio, desc, srcfile, line);
212 if ((res <= 0) && fio->bDebug)
214 "Error writing %s %s to file %s (source %s, line %d)\n",
215 eioNames[eio], desc, fio->fn, srcfile, line);
221 static char *next_item(FILE *fp, char *buf, int buflen)
224 gmx_bool in_comment = FALSE;
225 gmx_bool in_token = FALSE;
227 /* This routine reads strings from the file fp, strips comment
228 * and buffers. For thread-safety reasons, It reads through getc() */
232 gmx_file("End of file");
242 if (isspace(rd) || rd == ';')
244 buf[i++] = (char) rd;
255 buf[i++] = (char) (rd);
261 } while ((rd = getc(fp)) != EOF);
263 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
270 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
271 const char *desc, const char *srcfile, int line)
274 int i, m, res = 0, *iptr, ix;
278 unsigned char uc, *ucptr;
280 #define NEXT_ITEM_BUF_LEN 128
281 char ni_buf[NEXT_ITEM_BUF_LEN];
283 gmx_fio_check_nitem(fio, eio, nitem, srcfile, line);
289 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
291 *((real *) item) = d;
294 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
298 case eioGMX_LARGE_INT:
299 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
300 gmx_large_int_pfmt, &s);
302 *((gmx_large_int_t *) item) = s;
305 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
307 *((unsigned char *) item) = uc;
310 ucptr = (unsigned char *) item;
311 for (i = 0; (i < nitem); i++)
313 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
319 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
321 *((unsigned short *) item) = i;
325 for (m = 0; (m < DIM); m++)
327 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
332 for (i = 0; (i < nitem); i++)
334 ptr = ((rvec *) item)[i];
335 for (m = 0; (m < DIM); m++)
337 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
346 for (m = 0; (m < DIM); m++)
348 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
354 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
357 decode_string(strlen(cptr) + 1, (char *) item, cptr);
358 /* res = sscanf(cptr,"%s",(char *)item);*/
363 gmx_fio_fe(fio, eio, desc, srcfile, line);
366 if ((res <= 0) && fio->bDebug)
368 "Error reading %s %s from file %s (source %s, line %d)\n",
369 eioNames[eio], desc, fio->fn, srcfile, line);