/*************************************************************************** *cr *cr (C) Copyright 1995-2002 The Board of Trustees of the *cr University of Illinois *cr All Rights Reserved *cr ***************************************************************************/ /*************************************************************************** * RCS INFORMATION: * * $RCSfile: crdplugin.c,v $ * $Author: johns $ $Locker: $ $State: Exp $ * $Revision: 1.7 $ $Date: 2002/05/31 19:16:55 $ * ***************************************************************************/ #include #include #include #include "molfile_plugin.h" typedef struct { FILE *file; int has_box; int numatoms; } crddata; static void *open_crd_read(const char *filename, const char *filetype, molfile_header_t *header) { FILE *fd; crddata *data; fd = fopen(filename, "rb"); if (!fd) return NULL; /* first line is title, so skip past it */ while (getc(fd) != 10); /* * CRD's don't store the number of atoms in the timestep, so we assume that * the application will determine this for us. Also don't know how many * frames there are. */ data = (crddata *)malloc(sizeof(crddata)); data->file = fd; header->numatoms = -1; header->numsteps = -1; /* filetype "crd" has no box; filetype "crdbox" does. */ data->has_box = strcmp(filetype, "crd"); return data; } /* * CRD files with box info are indistinguishable from regular CRD's. We * regard CRD's with box info as a different file format. * molfile_timestep_t doesn't have box info yet. Also, CRD's don't tell * how many atoms there are in each frame. We therefore rely on the * numatoms field in the molfile_timestep_t parameter. */ static int read_crd_timestep(void *mydata, molfile_timestep_t *ts) { crddata *crd = (crddata *)mydata; int i, j; float x, y, z; float a, b, c; for (i=0; inumatoms; i++) { j = fscanf(crd->file, "%f %f %f", &x, &y, &z); if (j == EOF) { return -1; } else if (j <= 0) { fprintf(stderr, "Problem reading CRD file\n"); return 1; } ts->coords[3*i] = x; ts->coords[3*i+1] = y; ts->coords[3*i+2] = z; if (crd->has_box) { /* Scan through box info without reading it. */ j = fscanf (crd->file, "%f %f %f %f %f %f", &a, &b, &c, &x, &y, &z); if (j == EOF) { printf("EOF in box\n"); return -1; } else if (j <= 0) { printf("Problem reading box part of CRD file, scanf returned %d\n",j); return -1; } } } return 0; } static void close_crd_read(void *mydata) { crddata *crd = (crddata *)mydata; fclose(crd->file); free(crd); } static void *open_crd_write(const char *path, const char *filetype, const molfile_header_t *header) { crddata *crd; FILE *fd; fd = fopen(path, "wb"); if (!fd) { fprintf(stderr, "Could not open file %s for writing\n", path); return NULL; } fprintf(fd, "TITLE : Created by VMD with %d atoms, %d steps\n", header->numatoms, header->numsteps); crd = (crddata *)malloc(sizeof(crddata)); crd->file = fd; crd->numatoms = header->numatoms; crd->has_box = strcmp(filetype, "crd"); return crd; } static int write_crd_timestep(void *v, const molfile_timestep_t *ts) { crddata *crd = (crddata *)v; int i; const int ndata = crd->numatoms * 3; for (i=0; ifile, "%8.3f", ts->coords[i]); if (i % 10 == 0) fprintf(crd->file, "\n"); } if (crd->has_box) { fprintf (crd->file, "\n0.000 0.000 0.000\n"); } return 0; } static void close_crd_write(void *v) { crddata *crd = (crddata *)v; fclose(crd->file); free(crd); } /* registration stuff */ static molfile_api crdapi = { "crd", open_crd_read, 0, read_crd_timestep, close_crd_read, open_crd_write, 0, write_crd_timestep, close_crd_write }; int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) { vmdplugin_t plugin = { MOLFILE_PLUGIN_TYPE, /* type */ "", /* name */ "Justin Gullingsrud", /* author */ 0, /* major version */ 1, /* minor version */ 1, /* is reentrant */ &crdapi }; plugin.name = "crd"; (*cb)(v, &plugin); plugin.name = "crdbox"; (*cb)(v, &plugin); return 0; }