void gmx_fio_checktype(t_fileio *fio) { if (in_ftpset(fio->iFTP, asize(ftpXDR), ftpXDR)) { return; } else if (in_ftpset(fio->iFTP, asize(ftpASC), ftpASC)) { return; } else if (in_ftpset(fio->iFTP, asize(ftpBIN), ftpBIN)) { return; } #ifdef HAVE_XMl else if (in_ftpset(fio->iFTP, asize(ftpXML), ftpXML)) { return; } #endif else { gmx_fatal(FARGS, "Can not read/write topologies to file type %s", ftp2ext(fio->iFTP)); } }
/* set the reader/writer functions based on the file type */ static void gmx_fio_set_iotype(t_fileio *fio) { if (in_ftpset(fio->iFTP, asize(ftpXDR), ftpXDR)) { #ifdef USE_XDR fio->iotp = &xdr_iotype; #else gmx_fatal(FARGS, "Sorry, no XDR"); #endif } else if (in_ftpset(fio->iFTP, asize(ftpASC), ftpASC)) { fio->iotp = &asc_iotype; } else if (in_ftpset(fio->iFTP, asize(ftpBIN), ftpBIN)) { fio->iotp = &bin_iotype; } #ifdef HAVE_XMl else if (in_ftpset(fio->iFTP, asize(ftpXML), ftpXML)) { fio->iotp = &dummy_iotype; } #endif else { fio->iotp = &dummy_iotype; } }
static int gmx_fio_close_locked(t_fileio *fio) { int rc = 0; if (!fio->bOpen) { gmx_fatal(FARGS, "File %s closed twice!\n", fio->fn); } if (in_ftpset(fio->iFTP, asize(ftpXDR), ftpXDR)) { xdr_destroy(fio->xdr); sfree(fio->xdr); } /* Don't close stdin and stdout! */ if (!fio->bStdio && fio->fp != NULL) { rc = ffclose(fio->fp); /* fclose returns 0 if happy */ } fio->bOpen = FALSE; return rc; }
/* close only fp but keep FIO entry. */ int gmx_fio_fp_close(t_fileio *fio) { int rc = 0; gmx_fio_lock(fio); if (!in_ftpset(fio->iFTP, asize(ftpXDR), ftpXDR) && !fio->bStdio) { rc = ffclose(fio->fp); /* fclose returns 0 if happy */ fio->fp = NULL; } gmx_fio_unlock(fio); return rc; }
void gmx_fio_select(int fio) { gmx_fio_check(fio); #ifdef DEBUG fprintf(stderr,"Select fio called with type %d for file %s\n", FIO[fio].iFTP,FIO[fio].fn); #endif if (in_ftpset(FIO[fio].iFTP,asize(ftpXDR),ftpXDR)) { #ifdef USE_XDR do_read = do_xdr; do_write = do_xdr; #else gmx_fatal(FARGS,"Sorry, no XDR"); #endif } else if (in_ftpset(FIO[fio].iFTP,asize(ftpASC),ftpASC)) { do_read = do_ascread; do_write = do_ascwrite; } else if (in_ftpset(FIO[fio].iFTP,asize(ftpBIN),ftpBIN)) { do_read = do_binread; do_write = do_binwrite; } #ifdef HAVE_XMl else if (in_ftpset(FIO[fio].iFTP,asize(ftpXML),ftpXML)) { do_read = do_dummy; do_write = do_dummy; } #endif else gmx_fatal(FARGS,"Can not read/write topologies to file type %s", ftp2ext(curfio->iFTP)); curfio = &(FIO[fio]); }
int gmx_fio_close(int fio) { int rc = 0; gmx_fio_check(fio); if (in_ftpset(FIO[fio].iFTP,asize(ftpXDR),ftpXDR)) { rc = !xdrclose(FIO[fio].xdr); /* xdrclose returns 1 if happy, negate it */ sfree(FIO[fio].xdr); } else { /* Don't close stdin and stdout! */ if (!FIO[fio].bStdio) rc = fclose(FIO[fio].fp); /* fclose returns 0 if happy */ } sfree(FIO[fio].fn); FIO[fio].bOpen = FALSE; do_read = do_dummy; do_write = do_dummy; return rc; }
/***************************************************************** * * EXPORTED SECTION * *****************************************************************/ t_fileio *gmx_fio_open(const char *fn, const char *mode) { t_fileio *fio = NULL; int i; char newmode[5]; gmx_bool bRead, bReadWrite; int xdrid; if (fn2ftp(fn) == efTPA) { strcpy(newmode, mode); } else { /* sanitize the mode string */ if (strncmp(mode, "r+", 2) == 0) { strcpy(newmode, "r+"); } else if (mode[0] == 'r') { strcpy(newmode, "r"); } else if (strncmp(mode, "w+", 2) == 0) { strcpy(newmode, "w+"); } else if (mode[0] == 'w') { strcpy(newmode, "w"); } else if (strncmp(mode, "a+", 2) == 0) { strcpy(newmode, "a+"); } else if (mode[0] == 'a') { strcpy(newmode, "a"); } else { gmx_fatal(FARGS, "DEATH HORROR in gmx_fio_open, mode is '%s'", mode); } } /* Check if it should be opened as a binary file */ if (strncmp(ftp2ftype(fn2ftp(fn)), "ASCII", 5)) { /* Not ascii, add b to file mode */ if ((strchr(newmode, 'b') == NULL) && (strchr(newmode, 'B') == NULL)) { strcat(newmode, "b"); } } snew(fio, 1); #ifdef GMX_THREAD_MPI tMPI_Lock_init(&(fio->mtx)); #endif bRead = (newmode[0] == 'r' && newmode[1] != '+'); bReadWrite = (newmode[1] == '+'); fio->fp = NULL; fio->xdr = NULL; if (fn) { fio->iFTP = fn2ftp(fn); fio->fn = strdup(fn); fio->bStdio = FALSE; /* If this file type is in the list of XDR files, open it like that */ if (in_ftpset(fio->iFTP, asize(ftpXDR), ftpXDR)) { /* First check whether we have to make a backup, * only for writing, not for read or append. */ if (newmode[0] == 'w') { #ifndef GMX_FAHCORE /* only make backups for normal gromacs */ make_backup(fn); #endif } else { /* Check whether file exists */ if (!gmx_fexist(fn)) { gmx_open(fn); } } /* Open the file */ fio->fp = ffopen(fn, newmode); /* determine the XDR direction */ if (newmode[0] == 'w' || newmode[0] == 'a') { fio->xdrmode = XDR_ENCODE; } else { fio->xdrmode = XDR_DECODE; } snew(fio->xdr, 1); xdrstdio_create(fio->xdr, fio->fp, fio->xdrmode); } else { /* If it is not, open it as a regular file */ fio->fp = ffopen(fn, newmode); } /* for appending seek to end of file to make sure ftell gives correct position * important for checkpointing */ if (newmode[0] == 'a') { gmx_fseek(fio->fp, 0, SEEK_END); } } else { /* Use stdin/stdout for I/O */ fio->iFTP = efTPA; fio->fp = bRead ? stdin : stdout; fio->fn = strdup("STDIO"); fio->bStdio = TRUE; } fio->bRead = bRead; fio->bReadWrite = bReadWrite; fio->bDouble = (sizeof(real) == sizeof(double)); fio->bDebug = FALSE; fio->bOpen = TRUE; fio->bLargerThan_off_t = FALSE; /* set the reader/writer functions */ gmx_fio_set_iotype(fio); /* and now insert this file into the list of open files. */ gmx_fio_insert(fio); return fio; }
/***************************************************************** * * EXPORTED SECTION * *****************************************************************/ int gmx_fio_open(const char *fn,const char *mode) { t_fileio *fio=NULL; int i,nfio=0; char *bf,newmode[5]; bool bRead; int xdrid; if (fn2ftp(fn)==efTPA) { strcpy(newmode,mode); } else { if (mode[0]=='r') { strcpy(newmode,"r"); } else if (mode[0]=='w') { strcpy(newmode,"w"); } else if (mode[0]=='a') { strcpy(newmode,"a"); } else { gmx_fatal(FARGS,"DEATH HORROR in gmx_fio_open, mode is '%s'",mode); } } /* Check if it should be opened as a binary file */ if (strncmp(ftp2ftype(fn2ftp(fn)),"ASCII",5)) { /* Not ascii, add b to file mode */ if ((strchr(newmode,'b')==NULL) && (strchr(newmode,'B')==NULL)) { strcat(newmode,"b"); } } /* Determine whether we have to make a new one */ for(i=0; (i<nFIO); i++) { if (!FIO[i].bOpen) { fio = &(FIO[i]); nfio = i; break; } } if (i == nFIO) { nFIO++; srenew(FIO,nFIO); fio = &(FIO[nFIO-1]); nfio = nFIO-1; } bRead = (newmode[0]=='r'); fio->fp = NULL; fio->xdr = NULL; if (fn) { fio->iFTP = fn2ftp(fn); fio->fn = strdup(fn); fio->bStdio = FALSE; /* If this file type is in the list of XDR files, open it like that */ if (in_ftpset(fio->iFTP,asize(ftpXDR),ftpXDR)) { /* First check whether we have to make a backup, * only for writing, not for read or append. */ if (newmode[0]=='w') { #ifndef GMX_FAHCORE /* only make backups for normal gromacs */ if (gmx_fexist(fn)) { bf=(char *)backup_fn(fn); if (rename(fn,bf) == 0) { fprintf(stderr,"\nBack Off! I just backed up %s to %s\n",fn,bf); } else { fprintf(stderr,"Sorry, I couldn't backup %s to %s\n",fn,bf); } } #endif } else { /* Check whether file exists */ if (!gmx_fexist(fn)) { gmx_open(fn); } } snew(fio->xdr,1); xdrid = xdropen(fio->xdr,fn,newmode); if (xdrid == 0) { if(newmode[0]=='r') gmx_fatal(FARGS,"Cannot open file %s for reading\nCheck permissions if it exists.",fn); else gmx_fatal(FARGS,"Cannot open file %s for writing.\nCheck your permissions, disk space and/or quota.",fn); } fio->fp = xdr_get_fp(xdrid); } else { /* If it is not, open it as a regular file */ fio->fp = ffopen(fn,newmode); } } else { /* Use stdin/stdout for I/O */ fio->iFTP = efTPA; fio->fp = bRead ? stdin : stdout; fio->fn = strdup("STDIO"); fio->bStdio = TRUE; } fio->bRead = bRead; fio->bDouble= (sizeof(real) == sizeof(double)); fio->bDebug = FALSE; fio->bOpen = TRUE; return nfio; }