static void close_js_read(void *v) { jshandle *js = (jshandle *)v; fio_fclose(js->fd); #if JSMAJORVERSION > 1 if (js->bondfrom) free(js->bondfrom); if (js->bondto) free(js->bondto); if (js->bondorders) free(js->bondorders); /* free angle data */ if (js->angles != NULL) free(js->angles); if (js->dihedrals != NULL) free(js->dihedrals); if (js->impropers != NULL) free(js->impropers); if (js->cterms) free(js->cterms); #endif free(js); }
static void close_js_write(void *v) { jshandle *js = (jshandle *)v; /* update the trajectory header information */ fio_fseek(js->fd, JSNFRAMESOFFSET, FIO_SEEK_SET); fio_write_int32(js->fd, js->nframes); fio_fseek(js->fd, 0, FIO_SEEK_END); fio_fclose(js->fd); #if JSMAJORVERSION > 1 if (js->bondfrom) free(js->bondfrom); if (js->bondto) free(js->bondto); if (js->bondorders) free(js->bondorders); if (js->angles) free(js->angles); if (js->dihedrals) free(js->dihedrals); if (js->impropers) free(js->impropers); if (js->cterms) free(js->cterms); #endif free(js); }
static void close_file_write(void *v) { dcdhandle *dcd = (dcdhandle *)v; fio_fclose(dcd->fd); free(dcd->x); free(dcd->y); free(dcd->z); free(dcd); }
static void close_file_read(void *v) { dcdhandle *dcd = (dcdhandle *)v; close_dcd_read(dcd->freeind, dcd->fixedcoords); fio_fclose(dcd->fd); free(dcd->x); free(dcd->y); free(dcd->z); free(dcd); }
static void *open_dcd_write(const char *path, const char *filetype, int natoms) { dcdhandle *dcd; fio_fd fd; int rc; int istart, nsavc; double delta; int with_unitcell; int charmm; if (fio_open(path, FIO_WRITE, &fd) < 0) { printf("dcdplugin) Could not open file '%s' for writing\n", path); return NULL; } dcd = (dcdhandle *)malloc(sizeof(dcdhandle)); memset(dcd, 0, sizeof(dcdhandle)); dcd->fd = fd; istart = 0; /* starting timestep of DCD file */ nsavc = 1; /* number of timesteps between written DCD frames */ delta = 1.0; /* length of a timestep */ if (getenv("VMDDCDWRITEXPLORFORMAT") != NULL) { with_unitcell = 0; /* no unit cell info */ charmm = DCD_IS_XPLOR; /* X-PLOR format */ printf("dcdplugin) WARNING: Writing DCD file in X-PLOR format, \n"); printf("dcdplugin) WARNING: unit cell information will be lost!\n"); } else { with_unitcell = 1; /* contains unit cell infor (Charmm format) */ charmm = DCD_IS_CHARMM; /* charmm-formatted DCD file */ if (with_unitcell) charmm |= DCD_HAS_EXTRA_BLOCK; } rc = write_dcdheader(dcd->fd, "Created by DCD plugin", natoms, istart, nsavc, delta, with_unitcell, charmm); if (rc < 0) { print_dcderror("write_dcdheader", rc); fio_fclose(dcd->fd); free(dcd); return NULL; } dcd->natoms = natoms; dcd->nsets = 0; dcd->istart = istart; dcd->nsavc = nsavc; dcd->with_unitcell = with_unitcell; dcd->charmm = charmm; dcd->x = (float *)malloc(natoms * sizeof(float)); dcd->y = (float *)malloc(natoms * sizeof(float)); dcd->z = (float *)malloc(natoms * sizeof(float)); return dcd; }
static void *open_dcd_read(const char *path, const char *filetype, int *natoms) { dcdhandle *dcd; fio_fd fd; int rc; struct stat stbuf; if (!path) return NULL; /* See if the file exists, and get its size */ memset(&stbuf, 0, sizeof(struct stat)); if (stat(path, &stbuf)) { printf("dcdplugin) Could not access file '%s'.\n", path); return NULL; } if (fio_open(path, FIO_READ, &fd) < 0) { printf("dcdplugin) Could not open file '%s' for reading.\n", path); return NULL; } dcd = (dcdhandle *)malloc(sizeof(dcdhandle)); memset(dcd, 0, sizeof(dcdhandle)); dcd->fd = fd; if ((rc = read_dcdheader(dcd->fd, &dcd->natoms, &dcd->nsets, &dcd->istart, &dcd->nsavc, &dcd->delta, &dcd->nfixed, &dcd->freeind, &dcd->fixedcoords, &dcd->reverse, &dcd->charmm))) { print_dcderror("read_dcdheader", rc); fio_fclose(dcd->fd); free(dcd); return NULL; } /* * Check that the file is big enough to really hold the number of sets * it claims to have. Then we'll use nsets to keep track of where EOF * should be. */ { fio_size_t ndims, firstframesize, framesize, extrablocksize; fio_size_t trjsize, filesize, curpos; int newnsets; extrablocksize = dcd->charmm & DCD_HAS_EXTRA_BLOCK ? 48 + 8 : 0; ndims = dcd->charmm & DCD_HAS_4DIMS ? 4 : 3; firstframesize = (dcd->natoms+2) * ndims * sizeof(float) + extrablocksize; framesize = (dcd->natoms-dcd->nfixed+2) * ndims * sizeof(float) + extrablocksize; /* * It's safe to use ftell, even though ftell returns a long, because the * header size is < 4GB. */ curpos = fio_ftell(dcd->fd); /* save current offset (end of header) */ #if defined(_MSC_VER) && defined(FASTIO_NATIVEWIN32) /* the stat() call is not 64-bit savvy on Windows */ /* so we have to use the fastio fseek/ftell routines for this */ /* until we add a portable filesize routine for this purpose */ fio_fseek(dcd->fd, 0, FIO_SEEK_END); /* seek to end of file */ filesize = fio_ftell(dcd->fd); fio_fseek(dcd->fd, curpos, FIO_SEEK_SET); /* return to end of header */ #else filesize = stbuf.st_size; /* this works ok on Unix machines */ #endif trjsize = filesize - curpos - firstframesize; if (trjsize < 0) { printf("dcdplugin) file '%s' appears to contain no timesteps.\n", path); fio_fclose(dcd->fd); free(dcd); return NULL; } newnsets = trjsize / framesize + 1; if (dcd->nsets > 0 && newnsets != dcd->nsets) { printf("dcdplugin) Warning: DCD header claims %d frames, file size indicates there are actually %d frames\n", dcd->nsets, newnsets); } dcd->nsets = newnsets; dcd->setsread = 0; } dcd->first = 1; dcd->x = (float *)malloc(dcd->natoms * sizeof(float)); dcd->y = (float *)malloc(dcd->natoms * sizeof(float)); dcd->z = (float *)malloc(dcd->natoms * sizeof(float)); if (!dcd->x || !dcd->y || !dcd->z) { printf("dcdplugin) Unable to allocate space for %d atoms.\n", dcd->natoms); if (dcd->x) free(dcd->x); if (dcd->y) free(dcd->y); if (dcd->z) free(dcd->z); fio_fclose(dcd->fd); free(dcd); return NULL; } *natoms = dcd->natoms; return dcd; }
static void *open_js_read(const char *path, const char *filetype, int *natoms) { jshandle *js; int jsmagicnumber, jsendianism, jsmajorversion, jsminorversion; struct stat stbuf; char strbuf[1024]; int rc = 0; if (!path) return NULL; /* See if the file exists, and get its size */ memset(&stbuf, 0, sizeof(struct stat)); if (stat(path, &stbuf)) { printf("jsplugin) Could not access file '%s'.\n", path); return NULL; } js = (jshandle *)malloc(sizeof(jshandle)); memset(js, 0, sizeof(jshandle)); if (fio_open(path, FIO_READ, &js->fd) < 0) { printf("jsplugin) Could not open file '%s' for reading.\n", path); free(js); return NULL; } /* emit header information */ fio_fread(strbuf, strlen(JSHEADERSTRING), 1, js->fd); strbuf[strlen(JSHEADERSTRING)] = '\0'; if (strcmp(strbuf, JSHEADERSTRING)) { printf("jsplugin) Bad trajectory header!\n"); printf("jsplugin) Read string: %s\n", strbuf); return NULL; } fio_read_int32(js->fd, &jsmagicnumber); fio_read_int32(js->fd, &jsendianism); fio_read_int32(js->fd, &jsmajorversion); fio_read_int32(js->fd, &jsminorversion); fio_read_int32(js->fd, &js->natoms); fio_read_int32(js->fd, &js->nframes); if ((jsmagicnumber != JSMAGICNUMBER) || (jsendianism != JSENDIANISM)) { printf("jsplugin) opposite endianism file, enabling byte swapping\n"); js->reverseendian = 1; swap4_aligned(&jsmagicnumber, 1); swap4_aligned(&jsendianism, 1); swap4_aligned(&jsmajorversion, 1); swap4_aligned(&jsminorversion, 1); swap4_aligned(&js->natoms, 1); swap4_aligned(&js->nframes, 1); } else { printf("jsplugin) native endianism file\n"); } if ((jsmagicnumber != JSMAGICNUMBER) || (jsendianism != JSENDIANISM)) { printf("jsplugin) read_jsreader returned %d\n", rc); fio_fclose(js->fd); free(js); return NULL; } if (jsmajorversion != JSMAJORVERSION) { printf("jsplugin) major version mismatch\n"); printf("jsplugin) file version: %d\n", jsmajorversion); printf("jsplugin) plugin version: %d\n", JSMAJORVERSION); fio_fclose(js->fd); free(js); return NULL; } *natoms = js->natoms; return js; }
/** close file IO stream **********************************************/ void fio_fclose_( int32_t *fid ) { int32_t ierr; ierr = fio_fclose( *fid ); }