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 gmx_unused *fio, void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
90 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused line)
92 gmx_fatal(FARGS, "File type not set!");
96 static gmx_bool do_dummywrite(t_fileio gmx_unused *fio, const void gmx_unused *item, int gmx_unused nitem, int gmx_unused eio,
97 const char gmx_unused *desc, const char gmx_unused *srcfile, int gmx_unused 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++)
111 if ((src[i] == ' ') || (src[i] == '\t'))
124 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
128 static void decode_string(int maxlen, char dst[], const char src[])
132 for (i = 0; (src[i] != '\0') && (i < maxlen - 1); i++)
147 fprintf(stderr, "String '%s' truncated to '%s'\n", src, dst);
151 static gmx_bool do_ascwrite(t_fileio *fio, const void *item, int nitem, int eio,
152 const char *desc, const char *srcfile, int line)
158 char buf[GMX_FIO_BUFLEN];
159 unsigned char *ucptr;
162 gmx_fio_check_nitem(eio, nitem, srcfile, line);
168 res = fprintf(fp, "%18.10e%s\n", *((real *) item),
169 gmx_fio_dbgstr(fio, desc, buf));
172 res = fprintf(fp, "%18d%s\n", *((int *) item), gmx_fio_dbgstr(fio,
176 case eioGMX_LARGE_INT:
177 sprintf(strbuf, "%s%s%s", "%", gmx_large_int_fmt, "\n");
178 res = fprintf(fp, strbuf, *((gmx_large_int_t *) item),
179 gmx_fio_dbgstr(fio, desc, buf));
182 res = fprintf(fp, "%4d%s\n", *((unsigned char *) item),
183 gmx_fio_dbgstr(fio, desc, buf));
186 ucptr = (unsigned char *) item;
187 for (i = 0; (i < nitem); i++)
189 res = fprintf(fp, "%4d", (int) ucptr[i]);
191 fprintf(fio->fp, "%s\n", gmx_fio_dbgstr(fio, desc, buf));
194 res = fprintf(fp, "%18d%s\n", *((unsigned short *) item),
195 gmx_fio_dbgstr(fio, desc, buf));
199 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
200 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
203 for (i = 0; (i < nitem); i++)
205 ptr = ((rvec *) item)[i];
206 res = fprintf(fp, "%18.10e%18.10e%18.10e%s\n", ptr[XX],
207 ptr[YY], ptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
212 res = fprintf(fp, "%18d%18d%18d%s\n", iptr[XX], iptr[YY],
213 iptr[ZZ], gmx_fio_dbgstr(fio, desc, buf));
216 encode_string(256, strbuf, (char *) item);
217 res = fprintf(fp, "%-18s%s\n", strbuf, gmx_fio_dbgstr(fio, desc, buf));
220 gmx_fio_fe(fio, eio, desc, srcfile, line);
222 if ((res <= 0) && fio->bDebug)
225 "Error writing %s %s to file %s (source %s, line %d)\n",
226 eioNames[eio], desc, fio->fn, srcfile, line);
233 static char *next_item(FILE *fp, char *buf, int buflen)
236 gmx_bool in_comment = FALSE;
237 gmx_bool in_token = FALSE;
239 /* This routine reads strings from the file fp, strips comment
240 * and buffers. For thread-safety reasons, It reads through getc() */
245 gmx_file("End of file");
258 if (isspace(rd) || rd == ';')
262 buf[i++] = (char) rd;
275 buf[i++] = (char) (rd);
284 while ((rd = getc(fp)) != EOF);
286 fprintf(stderr, "WARNING, ftpASC file type not tested!\n");
293 static gmx_bool do_ascread(t_fileio *fio, void *item, int nitem, int eio,
294 const char *desc, const char *srcfile, int line)
297 int i, m, res = 0, *iptr, ix;
301 unsigned char uc, *ucptr;
303 #define NEXT_ITEM_BUF_LEN 128
304 char ni_buf[NEXT_ITEM_BUF_LEN];
306 gmx_fio_check_nitem(eio, nitem, srcfile, line);
312 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf", &d);
315 *((real *) item) = d;
319 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
325 case eioGMX_LARGE_INT:
326 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN),
327 gmx_large_int_pfmt, &s);
330 *((gmx_large_int_t *) item) = s;
334 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%c", &uc);
337 *((unsigned char *) item) = uc;
341 ucptr = (unsigned char *) item;
342 for (i = 0; (i < nitem); i++)
344 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &ix);
352 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d", &i);
355 *((unsigned short *) item) = i;
360 for (m = 0; (m < DIM); m++)
362 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n", &x);
367 for (i = 0; (i < nitem); i++)
369 ptr = ((rvec *) item)[i];
370 for (m = 0; (m < DIM); m++)
372 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%lf\n",
383 for (m = 0; (m < DIM); m++)
385 res = sscanf(next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN), "%d\n", &ix);
393 cptr = next_item(fp, ni_buf, NEXT_ITEM_BUF_LEN);
396 decode_string(strlen(cptr) + 1, (char *) item, cptr);
397 /* res = sscanf(cptr,"%s",(char *)item);*/
402 gmx_fio_fe(fio, eio, desc, srcfile, line);
405 if ((res <= 0) && fio->bDebug)
408 "Error reading %s %s from file %s (source %s, line %d)\n",
409 eioNames[eio], desc, fio->fn, srcfile, line);