gmx_bool fflib_fexist(const char *file) { char *file_fullpath; file_fullpath = low_gmxlibfn(file,TRUE,FALSE); if (file_fullpath == NULL) { return FALSE; } else { sfree(file_fullpath); return TRUE; } }
FILE *low_libopen(const char *file,gmx_bool bFatal) { FILE *ff; char *fn; fn=low_gmxlibfn(file,TRUE,bFatal); if (fn==NULL) { ff=NULL; } else { if (debug) fprintf(debug,"Opening library file %s\n",fn); ff=fopen(fn,"r"); } sfree(fn); return ff; }
char *gmxlibfn(const char *file) { return low_gmxlibfn(file, TRUE, TRUE); }
/* Open the file to be processed. The handle variable holds internal info for the cpp emulator. Return integer status */ int cpp_open_file(const char *filenm, gmx_cpp_t *handle, char **cppopts) { gmx_cpp_t cpp; char *buf, *pdum; char *ptr, *ptr2; int i; unsigned int i1; /* First process options, they might be necessary for opening files (especially include statements). */ i = 0; if (cppopts) { while (cppopts[i]) { if (strstr(cppopts[i], "-I") == cppopts[i]) { add_include(cppopts[i]+2); } if (strstr(cppopts[i], "-D") == cppopts[i]) { /* If the option contains a =, split it into name and value. */ ptr = strchr(cppopts[i], '='); if (ptr) { buf = gmx_strndup(cppopts[i] + 2, ptr - cppopts[i] - 2); add_define(buf, ptr + 1); sfree(buf); } else { add_define(cppopts[i] + 2, NULL); } } i++; } } if (debug) { fprintf(debug, "GMXCPP: added %d command line arguments\n", i); } snew(cpp, 1); *handle = cpp; cpp->fn = NULL; /* Find the file. First check whether it is in the current directory. */ if (gmx_fexist(filenm)) { cpp->fn = gmx_strdup(filenm); } else { /* If not, check all the paths given with -I. */ for (i = 0; i < nincl; ++i) { snew(buf, strlen(incl[i]) + strlen(filenm) + 2); sprintf(buf, "%s/%s", incl[i], filenm); if (gmx_fexist(buf)) { cpp->fn = buf; break; } sfree(buf); } /* If still not found, check the Gromacs library search path. */ if (!cpp->fn) { cpp->fn = low_gmxlibfn(filenm, FALSE, FALSE); } } if (!cpp->fn) { gmx_fatal(FARGS, "Topology include file \"%s\" not found", filenm); } if (NULL != debug) { fprintf(debug, "GMXCPP: cpp file open %s\n", cpp->fn); } /* If the file name has a path component, we need to change to that * directory. Note that we - just as C - always use UNIX path separators * internally in include file names. */ ptr = strrchr(cpp->fn, '/'); ptr2 = strrchr(cpp->fn, DIR_SEPARATOR); if (ptr == NULL || (ptr2 != NULL && ptr2 > ptr)) { ptr = ptr2; } if (ptr == NULL) { cpp->path = NULL; cpp->cwd = NULL; } else { cpp->path = cpp->fn; *ptr = '\0'; cpp->fn = gmx_strdup(ptr+1); snew(cpp->cwd, STRLEN); gmx_getcwd(cpp->cwd, STRLEN); if (NULL != debug) { fprintf(debug, "GMXCPP: cwd %s\n", cpp->cwd); } gmx_chdir(cpp->path); if (NULL != debug) { fprintf(debug, "GMXCPP: chdir to %s\n", cpp->path); } } cpp->line_len = 0; cpp->line = NULL; cpp->line_nr = 0; cpp->nifdef = 0; cpp->ifdefs = NULL; cpp->child = NULL; cpp->parent = NULL; if (cpp->fp == NULL) { if (NULL != debug) { fprintf(debug, "GMXCPP: opening file %s\n", cpp->fn); } cpp->fp = fopen(cpp->fn, "r"); } if (cpp->fp == NULL) { switch (errno) { case EINVAL: default: return eCPP_UNKNOWN; } } return eCPP_OK; }
void choose_ff(const char *ffsel, char *forcefield, int ff_maxlen, char *ffdir, int ffdir_maxlen) { int nff; char **ffdirs,**ffs,**ffs_dir,*ptr; int i,j,sel,cwdsel,nfound; char buf[STRLEN],**desc; FILE *fp; char *pret; nff = fflib_search_file_in_dirend(fflib_forcefield_itp(), fflib_forcefield_dir_ext(), &ffdirs); if (nff == 0) { gmx_fatal(FARGS,"No force fields found (files with name '%s' in subdirectories ending on '%s')", fflib_forcefield_itp(),fflib_forcefield_dir_ext()); } /* Replace with unix path separators */ if(DIR_SEPARATOR!='/') { for(i=0;i<nff;i++) { while( (ptr=strchr(ffdirs[i],DIR_SEPARATOR))!=NULL ) { *ptr='/'; } } } /* Store the force field names in ffs */ snew(ffs,nff); snew(ffs_dir,nff); for(i=0; i<nff; i++) { /* Remove the path from the ffdir name - use our unix standard here! */ ptr = strrchr(ffdirs[i],'/'); if (ptr == NULL) { ffs[i] = strdup(ffdirs[i]); ffs_dir[i] = low_gmxlibfn(ffdirs[i],FALSE,FALSE); if (ffs_dir[i] == NULL) { gmx_fatal(FARGS,"Can no longer find file '%s'",ffdirs[i]); } } else { ffs[i] = strdup(ptr+1); ffs_dir[i] = strdup(ffdirs[i]); } ffs_dir[i][strlen(ffs_dir[i])-strlen(ffs[i])-1] = '\0'; /* Remove the extension from the ffdir name */ ffs[i][strlen(ffs[i])-strlen(fflib_forcefield_dir_ext())] = '\0'; } if (ffsel != NULL) { sel = -1; cwdsel = -1; nfound = 0; for(i=0; i<nff; i++) { if ( strcmp(ffs[i],ffsel)==0 ) { /* Matching ff name */ sel = i; nfound++; if( strncmp(ffs_dir[i],".",1)==0 ) { cwdsel = i; } } } if(cwdsel != -1) { sel = cwdsel; } if(nfound>1) { if(cwdsel!=-1) { fprintf(stderr, "Force field '%s' occurs in %d places. pdb2gmx is using the one in the\n" "current directory. Use interactive selection (not the -ff option) if\n" "you would prefer a different one.\n",ffsel,nfound); } else { gmx_fatal(FARGS, "Force field '%s' occurs in %d places, but not in the current directory.\n" "Run without the -ff switch and select the force field interactively.",ffsel,nfound); } } else if (nfound==0) { gmx_fatal(FARGS,"Could not find force field '%s' in current directory, install tree or GMXDATA path.",ffsel); } } else if (nff > 1) { snew(desc,nff); for(i=0; (i<nff); i++) { sprintf(buf,"%s%c%s%s%c%s", ffs_dir[i],DIR_SEPARATOR, ffs[i],fflib_forcefield_dir_ext(),DIR_SEPARATOR, fflib_forcefield_doc()); if (gmx_fexist(buf)) { /* We don't use fflib_open, because we don't want printf's */ fp = ffopen(buf,"r"); snew(desc[i],STRLEN); get_a_line(fp,desc[i],STRLEN); ffclose(fp); } else { desc[i] = strdup(ffs[i]); } } /* Order force fields from the same dir alphabetically * and put deprecated force fields at the end. */ for(i=0; (i<nff); i++) { for(j=i+1; (j<nff); j++) { if (strcmp(ffs_dir[i],ffs_dir[j]) == 0 && ((desc[i][0] == '[' && desc[j][0] != '[') || ((desc[i][0] == '[' || desc[j][0] != '[') && gmx_strcasecmp(desc[i],desc[j]) > 0))) { swap_strings(ffdirs,i,j); swap_strings(ffs ,i,j); swap_strings(desc ,i,j); } } } printf("\nSelect the Force Field:\n"); for(i=0; (i<nff); i++) { if (i == 0 || strcmp(ffs_dir[i-1],ffs_dir[i]) != 0) { if( strcmp(ffs_dir[i],".")==0 ) { printf("From current directory:\n"); } else { printf("From '%s':\n",ffs_dir[i]); } } printf("%2d: %s\n",i+1,desc[i]); sfree(desc[i]); } sfree(desc); do { pret = fgets(buf,STRLEN,stdin); if (pret != NULL) { sscanf(buf,"%d",&sel); sel--; } } while ( pret==NULL || (sel < 0) || (sel >= nff)); } else { sel = 0; } if (strlen(ffs[sel]) >= (size_t)ff_maxlen) { gmx_fatal(FARGS,"Length of force field name (%d) >= maxlen (%d)", strlen(ffs[sel]),ff_maxlen); } strcpy(forcefield,ffs[sel]); if (strlen(ffdirs[sel]) >= (size_t)ffdir_maxlen) { gmx_fatal(FARGS,"Length of force field dir (%d) >= maxlen (%d)", strlen(ffdirs[sel]),ffdir_maxlen); } strcpy(ffdir,ffdirs[sel]); for(i=0; (i<nff); i++) { sfree(ffdirs[i]); sfree(ffs[i]); sfree(ffs_dir[i]); } sfree(ffdirs); sfree(ffs); sfree(ffs_dir); }