int getcmap(FILE *in,char *fn,t_mapping **map) { int i,n; char line[STRLEN]; char code[STRLEN],desc[STRLEN]; double r,g,b; t_mapping *m; if (fgets2(line,STRLEN-1,in) == NULL) gmx_fatal(FARGS,"Not enough lines in colormap file %s" "(just wanted to read number of entries)",fn); sscanf(line,"%d",&n); snew(m,n); for(i=0; (i<n); i++) { if (fgets2(line,STRLEN-1,in) == NULL) gmx_fatal(FARGS,"Not enough lines in colormap file %s" "(should be %d, found only %d)",fn,n+1,i); sscanf(line,"%s%s%lf%lf%lf",code,desc,&r,&g,&b); m[i].code.c1=code[0]; m[i].code.c2=0; m[i].desc=strdup(desc); m[i].rgb.r=r; m[i].rgb.g=g; m[i].rgb.b=b; } *map=m; return n; }
static void get_coordnum_fp (FILE *in,char *title, int *natoms) { char line[STRLEN+1]; fgets2 (title,STRLEN,in); fgets2 (line,STRLEN,in); if (sscanf (line,"%d",natoms) != 1) { gmx_fatal(FARGS,"gro file does not have the number of atoms on the second line"); } }
void cr_c(char *fn) { FILE *in,*out; char ofn[1024],line[MAXS+1],cwd[1024]; char *p,*fn_; gmx_bool bH; sprintf(ofn,"%s.bak",fn); fprintf(stderr,"Processing %s (backed up to %s)\n", fn,ofn); if (rename(fn,ofn) != 0) { perror(ofn); exit(1); } in=ffopen(ofn,"r"); out=ffopen(fn,"w"); /* Skip over empty lines in the beginning only */ do { if (fgets2(line,MAXS,in)) rtrim(line); } while ((strlen(line) == 0) && (!feof(in))); /* Now we are at end of file, or we have a non-empty string */ if (strlen(line) != 0) { if (strstr(line,"/*") != NULL) { /* File does start with comment, so delete it and add new */ while ((strstr(line,"*/") == NULL) && (!feof(in))) fgets2(line,MAXS,in); } fn_=strdup(fn); p=strchr(fn_,'.'); if (p) p[0]='_'; bH=FALSE; do { fgets2(line,MAXS,in); if ( (strstr(line,fn_) != NULL) && (strstr(line,"#define") != NULL) ) bH=TRUE; } while ( ( (strstr(line,fn_) != NULL) || (strlen(line)==0) ) && (!feof(in) ) ); getcwd(cwd,STRLEN); head(out,fn_,bH,"/*"," *"," */"); do { fprintf(out,"%s\n",line); } while (!feof(in) && fgets2(line,MAXS,in)); } ffclose(in); ffclose(out); }
static t_pdbfile *read_pdbf(const char *fn) { t_pdbfile *pdbf; double e; FILE *fp; snew(pdbf, 1); t_topology top; read_tps_conf(fn, &top, &pdbf->ePBC, &pdbf->x, NULL, pdbf->box, FALSE); pdbf->atoms = top.atoms; fp = gmx_ffopen(fn, "r"); char buf[256], *ptr; while ((ptr = fgets2(buf, 255, fp)) != NULL) { if (std::strstr(buf, "Intermolecular") != NULL) { ptr = std::strchr(buf, '='); sscanf(ptr+1, "%lf", &e); pdbf->edocked = e; } else if (std::strstr(buf, "Estimated Free") != NULL) { ptr = std::strchr(buf, '='); sscanf(ptr+1, "%lf", &e); pdbf->efree = e; } } gmx_ffclose(fp); return pdbf; }
static t_pdbfile *read_pdbf(char *fn) { t_pdbfile *pdbf; double e; char buf[256],*ptr; int natoms; FILE *fp; snew(pdbf,1); get_stx_coordnum (fn,&natoms); init_t_atoms(&(pdbf->atoms),natoms,FALSE); snew(pdbf->x,natoms); read_stx_conf(fn,buf,&pdbf->atoms,pdbf->x,NULL,&pdbf->ePBC,pdbf->box); fp = fopen(fn,"r"); do { ptr = fgets2(buf,255,fp); if (ptr) { if (strstr(buf,"Intermolecular") != NULL) { ptr = strchr(buf,'='); sscanf(ptr+1,"%lf",&e); pdbf->edocked = e; } else if (strstr(buf,"Estimated Free") != NULL) { ptr = strchr(buf,'='); sscanf(ptr+1,"%lf",&e); pdbf->efree = e; } } } while (ptr != NULL); fclose(fp); return pdbf; }
static void command_handler(GtkWidget *window, int fd, GdkInputCondition condition) { char *buf = NULL; int size = 0; int len; if ( window == NULL ) return; len = fgets2(stdin, &buf, &size); if ( buf == NULL ) return; if ( len > 0 ) { //char *args = buf + 1; switch ( buf[0] ) { case 'R': /* Raise */ gtk_window_present(GTK_WINDOW(window)); break; default: /* Ignore other commands */ break; } } free(buf); }
int fget_lines(FILE *in,char ***strings) { char **ptr; char buf[256]; int i,nstr; char *pret; pret = fgets(buf,255,in); if ( pret==NULL || sscanf(buf,"%d",&nstr) != 1) { gmx_warning("File is empty"); ffclose(in); return 0; } snew(ptr,nstr); for(i=0; (i<nstr); i++) { fgets2(buf,255,in); ptr[i] = gmx_strdup(buf); } (*strings) = ptr; return nstr; }
int get_file(const char *db,char ***strings) { FILE *in; char **ptr=NULL; char buf[STRLEN]; int i,nstr,maxi; in=libopen(db); i=maxi=0; while (fgets2(buf,STRLEN-1,in)) { if (i>=maxi) { maxi+=50; srenew(ptr,maxi); } ptr[i] = strdup(buf); i++; } nstr=i; ffclose(in); srenew(ptr,nstr); *strings=ptr; return nstr; }
static char *next_item(FILE *fp) { /* This routine reads strings from the file fp, strips comment * and buffers. If there are multiple strings on a line, they will * be stored here, and indices in the line buffer (buf) will be * stored in bufindex. This way we can uncomment on the fly, * without too much double work. Each string is first read through * fscanf in this routine, and then through sscanf in do_read. * No unnecessary string copying is done. */ #define MAXBUF 20 static char buf[STRLEN]; static int bufindex[MAXBUF]; int i,j0; char ccc; if (nbuf) { j0 = bufindex[0]; for(i=1; (i<nbuf); i++) bufindex[i-1] = bufindex[i]; nbuf--; return buf+j0; } else { /* First read until we find something that is not comment */ if (fgets2(buf,STRLEN-1,fp) == NULL) gmx_file("End of file"); i = 0; do { /* Skip over leading spaces */ while ((buf[i] != '\0') && (buf[i] != ';') && isspace(buf[i])) i++; /* Store start of something non-space */ j0 = i; /* Look for next spaces */ while ((buf[i] != '\0') && (buf[i] != ';') && !isspace(buf[i])) i++; /* Store the last character in the string */ ccc = buf[i]; /* If the string is non-empty, add it to the list */ if (i > j0) { buf[i] = '\0'; bufindex[nbuf++] = j0; /* We increment i here; otherwise the next test for buf[i] would be * '\0', since we test the main loop for ccc anyway, we cant go SEGV */ i++; } } while ((ccc != '\0') && (ccc != ';')); return next_item(fp); } }
static void rd_nm2type_file(const char *fn,int *nnm,t_nm2type **nmp) { FILE *fp; gmx_bool bCont; char libfilename[128]; char format[128],f1[128]; char buf[1024],elem[16],type[16],nbbuf[16],**newbuf; int i,nb,nnnm,line=1; double qq,mm,*blen; t_nm2type *nm2t=NULL; fp = fflib_open(fn); if (NULL == fp) gmx_fatal(FARGS,"Can not find %s in library directory",fn); nnnm = *nnm; nm2t = *nmp; do { /* Read a line from the file */ bCont = (fgets2(buf,1023,fp) != NULL); if (bCont) { /* Remove comment */ strip_comment(buf); if (sscanf(buf,"%s%s%lf%lf%d",elem,type,&qq,&mm,&nb) == 5) { /* If we can read the first four, there probably is more */ srenew(nm2t,nnnm+1); snew(nm2t[nnnm].blen,nb); if (nb > 0) { snew(newbuf,nb); strcpy(format,"%*s%*s%*s%*s%*s"); for(i=0; (i<nb); i++) { /* Complicated format statement */ strcpy(f1,format); strcat(f1,"%s%lf"); if (sscanf(buf,f1,nbbuf,&(nm2t[nnnm].blen[i])) != 2) gmx_fatal(FARGS,"Error on line %d of %s",line,libfilename); newbuf[i] = strdup(nbbuf); strcat(format,"%*s%*s"); } } else newbuf = NULL; nm2t[nnnm].elem = strdup(elem); nm2t[nnnm].type = strdup(type); nm2t[nnnm].q = qq; nm2t[nnnm].m = mm; nm2t[nnnm].nbonds = nb; nm2t[nnnm].bond = newbuf; nnnm++; } line++; } } while(bCont); ffclose(fp); *nnm = nnnm; *nmp = nm2t; }
static void rd_ener_nms(FILE *in,int *nre,char ***nm) { char line[256]; int i; fgets2(line,255,in); if (sscanf(line,"%d",nre) == 0) { *nre=0; return; } snew((*nm),*nre); for(i=0; (i< (*nre)); i++) { fgets2(line,255,in); trim(line); (*nm)[i]=strdup(line); } }
void ComplexParametersCategory(char *llog, statistics stats, FILE **fOut, FILE **fcfg) { int i, j; char lkk1[LONGSTRINGSIZE], lkk2[LONGSTRINGSIZE]; fgets2(lkk, LONGSTRINGSIZE, *fcfg); while ((lkk[0] != '#') && (lkk[0] != '\0') && (!feof(*fcfg))) { i=strpos2(lkk, ":OPT", 1); if (lkk[0]=='*') /*if the line is comment out, simply ignore it*/ i=0; if (i) { /* line with OPT */ j=1; ReadSubKey(lkk1, lkk, &j, '#', '#', 5); /* lkk1=symbol */ lkk2[0]='\0'; i=0; j=1; while ( (strcmp (lkk1, lkk2)) && (j<strlen(llog) ) ) { i++; ReadSubKey(lkk2, llog, &j, ':', ':', 0); StripSpaces(lkk2); } if (j==strlen(llog)) { printf("auxfunc_log.c - ComplexParametersCategory -- Wrong config opened.\n"); exit(EXIT_FAILURE); } ReadSubKey(lkk2, llog, &j, ':', ':', 0); /*the measured value for the previously text*/ i++; /*the correct position of the value*/ j=1; ReadSubKey(lkk1, lkk, &j, ':', ':', 0); strsub(lkk1, lkk, 1, j); sprintf(lkk2, "%s:%E:%E", lkk2, stats.min[i], stats.max[i]); strcat(lkk1, lkk2); ReadSubKey(lkk2, lkk, &j, ':', ':', 0); ReadSubKey(lkk2, lkk, &j, ':', ':', 0); ReadSubKey(lkk2, lkk, &j, ':', ':', 0); strsub(lkk2, lkk, j, (int)strlen(lkk)); strcat(lkk1, lkk2); fprintf(*fOut, "%s\n", lkk1); } else { /* line with --- */ fprintf(*fOut, "%s\n", lkk); } fgets2(lkk, LONGSTRINGSIZE, *fcfg); } }
int main() { const char *p; unsigned int lines = 0; while ((p=fgets2(stdin))) lines++; printf("lines=%d\n", lines); return 0; }
static char *report_xslt_recover(char *out_name) { int complete = 0; FILE *f; /* Open input file */ f = fopen(out_name, "r"); if ( f == NULL ) { fprintf(stderr, "%s: Error opening Test Output file: %s\n", out_name, strerror(errno)); return NULL; } /* Check for file completion */ if ( fseek(f, -20, SEEK_END) ) { fprintf(stderr, "%s: Error seeking Test Output file: %s\n", out_name, strerror(errno)); } else { char *buf = NULL; int size = 0; /* Look for last XML element */ while ( !complete && !feof(f) ) { int len = fgets2(f, &buf, &size); if ( len < 0 ) break; if ( (len > 0) && (buf != NULL) ) { if ( strcmp(strskip_spaces(buf), "</RESULT>") == 0 ) complete = 1; } } if ( buf != NULL ) free(buf); } /* Close input file */ fclose(f); if ( complete ) return out_name; /* Setup name of recovered file */ report_xslt_filename = (char *) realloc(report_xslt_filename, strlen(out_name)+16); sprintf(report_xslt_filename, "%s.tmp%d", out_name, getpid()); if ( filecopy(out_name, report_xslt_filename) ) return NULL; if ( xml_recover(report_xslt_filename) ) return NULL; return report_xslt_filename; }
void HelpNow(t_dlg *dlg, t_dlgitem *dlgitem) { char buf[80]; bool bCont = true; int i, nlines = 0; char **lines = NULL; if (!dlgitem->help) { NoHelp(dlg); return; } std::printf("%s\n", dlgitem->help); do { fgets2(buf, 79, stdin); #ifdef DEBUG std::fprintf(dlg->x11->console, "buffer: '%s'\n", buf); dlg->x11->Flush(dlg->x11); #endif if (gmx_strcasecmp(buf, "nok") == 0) { /* An error occurred */ if (lines) { for (i = 0; (i < nlines); i++) { sfree(lines[i]); } sfree(lines); } NoHelp(dlg); return; } else { bCont = (gmx_strcasecmp(buf, "ok") != 0); if (bCont) { srenew(lines, ++nlines); lines[nlines-1] = gmx_strdup(buf); } } } while (bCont); MessageBox(dlg->x11, dlg->wDad, "Help", nlines, lines, MB_OK | MB_ICONINFORMATION | MB_APPLMODAL, NULL, NULL); for (i = 0; (i < nlines); i++) { sfree(lines[i]); } sfree(lines); }
static int read_g96_vel(char line[], FILE *fp, const char *infile, t_trxframe *fr) { gmx_bool bEnd; int nwanted, natoms = -1, shift; double db1, db2, db3; nwanted = fr->natoms; if (fr->v && fr->bV) { if (strcmp(line, "VELOCITYRED") == 0) { shift = 0; } else { shift = CHAR_SHIFT; } natoms = 0; bEnd = FALSE; while (!bEnd && fgets2(line, STRLEN, fp)) { bEnd = (strncmp(line, "END", 3) == 0); if (!bEnd && (line[0] != '#')) { if (sscanf(line+shift, "%15lf%15lf%15lf", &db1, &db2, &db3) != 3) { gmx_fatal(FARGS, "Did not find 3 velocities for atom %d in %s", natoms+1, infile); } if ((nwanted != -1) && (natoms >= nwanted)) { gmx_fatal(FARGS, "Found more velocities (%d) in %s than expected %d\n", natoms, infile, nwanted); } if (fr->v) { fr->v[natoms][0] = db1; fr->v[natoms][1] = db2; fr->v[natoms][2] = db3; } natoms++; } } if ((nwanted != -1) && (natoms != nwanted)) { fprintf(stderr, "Warning: found less velocities (%d) in %s than expected %d\n", natoms, infile, nwanted); } } return natoms; }
void cr_tex(char *fn) { FILE *in,*out; char ofn[1024],line[MAXS+1]; char *p; sprintf(ofn,"%s.bak",fn); fprintf(stderr,"Processing (as Tex) %s (backed up to %s)\n", fn,ofn); if (rename(fn,ofn) != 0) { perror(ofn); exit(1); } in=ffopen(ofn,"r"); out=ffopen(fn,"w"); /* Skip over empty lines in the beginning only */ do if (fgets2(line,MAXS,in)) rtrim(line); while ((strlen(line) == 0) && (!feof(in))); /* Now we are at end of file, or we have a non-empty string */ if (strlen(line) != 0) { while ((strstr(line,"%") != NULL) && (!feof(in))) /* File does start with comment, so delete it and add new */ fgets2(line,MAXS,in); head(out,"",FALSE,"%","%","%"); /* Skip over empty lines */ while ( (strlen(line) == 0) && !feof(in) ) if (fgets2(line,MAXS,in)) rtrim(line); do fprintf(out,"%s\n",line); while (!feof(in) && fgets2(line,MAXS,in)); } ffclose(in); ffclose(out); }
gpp_atomtype_t read_atype(const char *ffdir, t_symtab *tab) { int nfile, f; char **file; FILE *in; char buf[STRLEN], name[STRLEN]; double m; int nratt = 0; gpp_atomtype_t at; t_atom *a; t_param *nb; nfile = fflib_search_file_end(ffdir, ".atp", TRUE, &file); at = init_atomtype(); snew(a, 1); snew(nb, 1); for (f = 0; f < nfile; f++) { in = fflib_open(file[f]); while (!feof(in)) { /* Skip blank or comment-only lines */ do { if (fgets2(buf, STRLEN, in) != NULL) { strip_comment(buf); trim(buf); } } while (!feof(in) && strlen(buf) == 0); if (sscanf(buf, "%s%lf", name, &m) == 2) { a->m = m; add_atomtype(at, tab, a, name, nb, 0, 0.0, 0.0, 0.0, 0, 0.0, 0.0 ); fprintf(stderr, "\rAtomtype %d", ++nratt); fflush(stderr); } else { fprintf(stderr, "\nInvalid format: %s\n", buf); } } gmx_ffclose(in); sfree(file[f]); } fprintf(stderr, "\n"); sfree(file); return at; }
int read_h_db(char *fn,t_hackblock **ah) { FILE *in; char hfn[STRLEN], line[STRLEN], buf[STRLEN]; int i, n, nab, nah; t_hackblock *aah; sprintf(hfn,"%s.hdb",fn); in=libopen(hfn); if (debug) fprintf(debug,"Hydrogen Database (%s):\n",hfn); nah=0; aah=NULL; while (fgets2(line,STRLEN-1,in)) { if (sscanf(line,"%s%n",buf,&n) != 1) { fprintf(stderr,"Error in hdb file: nah = %d\nline = '%s'\n", nah,line); break; } if (debug) fprintf(debug,"%s",buf); srenew(aah,nah+1); clear_t_hackblock(&aah[nah]); aah[nah].name=strdup(buf); if (sscanf(line+n,"%d",&nab) == 1) { if (debug) fprintf(debug," %d\n",nab); snew(aah[nah].hack,nab); aah[nah].nhack = nab; for(i=0; (i<nab); i++) { if (feof(in)) gmx_fatal(FARGS, "Expected %d lines of hydrogens, found only %d " "while reading Hydrogen Database %s residue %s", nab, i-1, aah[nah].name, hfn); if(NULL==fgets(buf, STRLEN, in)) { gmx_fatal(FARGS,"Error reading from file %s",fn); } read_ab(buf,hfn,&(aah[nah].hack[i])); } } nah++; } fclose(in); /* Sort the list (necessary to be able to use bsearch */ qsort(aah,nah,(size_t)sizeof(**ah),compaddh); if (debug) dump_h_db(fn,nah,aah); *ah=aah; return nah; }
void get_pdb_coordnum(FILE *in,int *natoms) { char line[STRLEN]; *natoms=0; while (fgets2(line,STRLEN,in)) { if ( ( bTER && (strncmp(line,"TER",3) == 0)) || (!bTER && (strncmp(line,"ENDMDL",6) == 0)) ) break; if ((strncmp(line,"ATOM ",6) == 0) || (strncmp(line,"HETATM",6) == 0)) (*natoms)++; } }
static void read_col(t_x11 *x11,t_sc *sc,char *rgb) { FILE *fp; char buf[STRLEN],name[STRLEN],*dummy; int i,j; int r,g,b; XColor xc,exact; t_col *cols; if ((fp=fopen(rgb,"r"))==NULL) { perror(rgb); exit(1); } do { dummy=NULL; if (fscanf(fp,"%d%d%d",&r,&g,&b)==3) { dummy=fgets2(buf,STRLEN-1,fp); if (dummy) { trim(buf); /* Filter out colours with names of two words */ sscanf(buf,"%s",name); /* Filter out duplicate colours (grey and gray) */ if (strstr(name,"gray")==0) { if (XAllocNamedColor(x11->disp,x11->cmap,name,&xc,&exact)) { srenew(sc->col,++sc->ncol); sc->col[sc->ncol-1].xc=xc; #ifdef DEBUG printf("color %3d: %s\n",sc->ncol,buf); #endif InitWin(&(sc->col[sc->ncol-1].wd),0,0,1,1,0,name); } } } } } while (dummy); fclose(fp); if (sc->ncol) qsort(sc->col,sc->ncol,sizeof(sc->col[0]),col_comp); /* Now filter out doubles */ cols=&(sc->col[0]); for(i=1,j=0; (i<sc->ncol); i++) { if ((cols[i].xc.red != cols[j].xc.red) || (cols[i].xc.green != cols[j].xc.green) || (cols[i].xc.blue != cols[j].xc.blue)) { j++; cols[j]=cols[i]; } } sc->ncol=j; }
void get_pdb_coordnum(FILE *in, int *natoms) { char line[STRLEN]; *natoms = 0; while (fgets2(line, STRLEN, in)) { if (std::strncmp(line, "ENDMDL", 6) == 0) { break; } if ((std::strncmp(line, "ATOM ", 6) == 0) || (std::strncmp(line, "HETATM", 6) == 0)) { (*natoms)++; } } }
static void search_history_load(viewer_t *viewer) { FILE *f; char *buf = NULL; int size = 0; /* Get history file name */ if ( search_history_file(viewer) == NULL ) return; /* Open history file name for read */ if ( (f = fopen(viewer->search_history, "r")) == NULL ) { fprintf(stderr, NAME ": WARNING: Cannot open (r) search history %s: %s\n", viewer->search_history, strerror(errno)); return; } /* Clear history list */ search_history_clear(viewer); /* Get history list from file */ while ( ! feof(f) ) { int len; len = fgets2(f, &buf, &size); if ( len > 0 ) { if ( viewer->search_list == NULL ) gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(viewer->search_combo)->entry), buf); viewer->search_list = g_list_append(viewer->search_list, strdup(buf)); } } fclose(f); /* Free read buffer */ if ( buf != NULL ) free(buf); /* Update search entry combo */ gtk_combo_set_popdown_strings(GTK_COMBO(viewer->search_combo), viewer->search_list) ; }
void cat(FILE *out,char *fn,real t) { FILE *in; char *ptr,buf[256]; int anr,rnr; char anm[24],rnm[24]; double f1,f2,f3,f4,f5,f6; in=gmx_ffopen(fn,"r"); while ((ptr=fgets2(buf,255,in)) != NULL) { sscanf(buf,"%d%d%s%s%lf%lf%lf%lf%lf%lf", &anr,&rnr,rnm,anm,&f1,&f2,&f3,&f4,&f5,&f6); fprintf(out,"%8g %10g %10g %10g %10g %10g %10g %s%d-%s%d\n", t,f6,f1,f2,f3,f4,f5,rnm,rnr,anm,anr); } /*if ((int)strlen(buf) > 0) fprintf(out,"%s\n",buf);*/ fflush(out); gmx_ffclose(in); }
static void command_handler(viewer_t *viewer, int fd, GdkInputCondition condition) { char *buf = NULL; int size = 0; int len; if ( viewer == NULL ) return; len = fgets2(stdin, &buf, &size); if ( buf == NULL ) return; if ( len > 0 ) { char *args = buf + 1; switch ( buf[0] ) { case 'L': /* Load [<log_file>] */ viewer_load(viewer, args); break; case 'F': /* Follow [<mode>] */ viewer_follow(viewer, atoi(args)); break; case 'C': /* Clear log */ viewer_clear(viewer); break; case 'R': /* Raise [<case>] */ gtk_window_present(GTK_WINDOW(viewer->window)); if ( *args != '\0' ) list_moveto_case(viewer->list, args); break; default: /* Ignore other commands */ break; } } free(buf); }
int read_pdbfile(FILE *in,char *title,int *model_nr, t_atoms *atoms,rvec x[],int *ePBC,matrix box,bool bChange, gmx_conect conect) { gmx_conect_t *gc = (gmx_conect_t *)conect; static t_symtab symtab; static bool bFirst=TRUE; bool bCOMPND; bool bConnWarn = FALSE; char line[STRLEN+1]; int line_type; char *c,*d; int natom; bool bStop=FALSE; if (ePBC) { /* Only assume pbc when there is a CRYST1 entry */ *ePBC = epbcNONE; } if (box != NULL) clear_mat(box); if (bFirst) { open_symtab(&symtab); bFirst=FALSE; } bCOMPND=FALSE; title[0]='\0'; natom=0; while (!bStop && (fgets2(line,STRLEN,in) != NULL)) { line_type = line2type(line); switch(line_type) { case epdbATOM: case epdbHETATM: natom = read_atom(&symtab,line,line_type,natom,atoms,x,bChange); break; case epdbANISOU: if (atoms->pdbinfo) read_anisou(line,natom,atoms); break; case epdbCRYST1: read_cryst1(line,ePBC,box); break; case epdbTITLE: case epdbHEADER: if (strlen(line) > 6) { c=line+6; /* skip HEADER or TITLE and spaces */ while (c && (c[0]!=' ')) c++; while (c && (c[0]==' ')) c++; /* truncate after title */ d=strstr(c," "); if (d) { d[0]='\0'; } if (strlen(c)>0) strcpy(title,c); } break; case epdbCOMPND: if ((!strstr(line,": ")) || (strstr(line+6,"MOLECULE:"))) { if ( !(c=strstr(line+6,"MOLECULE:")) ) c=line; /* skip 'MOLECULE:' and spaces */ while (c && (c[0]!=' ')) c++; while (c && (c[0]==' ')) c++; /* truncate after title */ d=strstr(c," "); if (d) { while ( (d[-1]==';') && d>c) d--; d[0]='\0'; } if (strlen(c) > 0) { if (bCOMPND) { strcat(title,"; "); strcat(title,c); } else strcpy(title,c); } bCOMPND=TRUE; } break; case epdbTER: if (bTER) bStop=TRUE; break; case epdbMODEL: if(model_nr) sscanf(line,"%*s%d",model_nr); break; case epdbENDMDL: bStop=TRUE; break; case epdbCONECT: if (gc) add_conection(gc,line); else if (!bConnWarn) { fprintf(stderr,"WARNING: all CONECT records are ignored\n"); bConnWarn = TRUE; } break; default: break; } } return natom; }
t_inpfile *read_inpfile(const char *fn, int *ninp, char **cppopts, warninp_t wi) { FILE *in; char buf[STRLEN], lbuf[STRLEN], rbuf[STRLEN], warn_buf[STRLEN]; char *ptr, *cptr; t_inpfile *inp = NULL; int nin, lc, i, j, k; /* setting cppopts from command-line options would be cooler */ gmx_bool allow_override = FALSE; if (debug) { fprintf(debug, "Reading MDP file %s\n", fn); } in = ffopen(fn, "r"); nin = lc = 0; do { ptr = fgets2(buf, STRLEN-1, in); lc++; set_warning_line(wi, fn, lc); if (ptr) { /* Strip comment */ if ((cptr = strchr(buf, COMMENTSIGN)) != NULL) { *cptr = '\0'; } /* Strip spaces */ trim(buf); for (j = 0; (buf[j] != '=') && (buf[j] != '\0'); j++) { ; } if (buf[j] == '\0') { if (j > 0) { if (debug) { fprintf(debug, "No = on line %d in file %s, ignored\n", lc, fn); } } } else { for (i = 0; (i < j); i++) { lbuf[i] = buf[i]; } lbuf[i] = '\0'; trim(lbuf); if (lbuf[0] == '\0') { if (debug) { fprintf(debug, "Empty left hand side on line %d in file %s, ignored\n", lc, fn); } } else { for (i = j+1, k = 0; (buf[i] != '\0'); i++, k++) { rbuf[k] = buf[i]; } rbuf[k] = '\0'; trim(rbuf); if (rbuf[0] == '\0') { if (debug) { fprintf(debug, "Empty right hand side on line %d in file %s, ignored\n", lc, fn); } } else { /* Now finally something sensible */ int found_index; /* first check whether we hit the 'multiple_entries' option */ if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptName], lbuf) == 0) { /* we now check whether to allow overrides from here or not */ if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptNo], rbuf) == 0) { allow_override = FALSE; } else if (gmx_strcasecmp_min(eMultentOpt_names[eMultentOptLast], rbuf) == 0) { allow_override = TRUE; } else { sprintf(warn_buf, "Parameter \"%s\" should either be %s or %s\n", lbuf, eMultentOpt_names[eMultentOptNo], eMultentOpt_names[eMultentOptLast]); warning_error(wi, warn_buf); } } else { /* it is a regular option; check for duplicates */ found_index = search_einp(nin, inp, lbuf); if (found_index == -1) { /* add a new item */ srenew(inp, ++nin); inp[nin-1].inp_count = 1; inp[nin-1].count = 0; inp[nin-1].bObsolete = FALSE; inp[nin-1].bSet = FALSE; inp[nin-1].name = strdup(lbuf); inp[nin-1].value = strdup(rbuf); } else { if (!allow_override) { sprintf(warn_buf, "Parameter \"%s\" doubly defined (and multiple assignments not allowed)\n", lbuf); warning_error(wi, warn_buf); } else { /* override */ sfree(inp[found_index].value); inp[found_index].value = strdup(rbuf); sprintf(warn_buf, "Overriding existing parameter \"%s\" with value \"%s\"\n", lbuf, rbuf); warning_note(wi, warn_buf); } } } } } } } } while (ptr); ffclose(in); if (debug) { fprintf(debug, "Done reading MDP file, there were %d entries in there\n", nin); } *ninp = nin; return inp; }
int main(int argc, char **argv) { const char *b, *e, *p; const char *output_file = NULL; int f, tot, last, linenum, err, parse_err; struct timer *t = NULL, *t2; struct eb32_node *n; int val, test; int array[5]; int filter_acc_delay = 0, filter_acc_count = 0; int filter_time_resp = 0; int skip_fields = 1; argc--; argv++; while (argc > 0) { if (*argv[0] != '-') break; if (strcmp(argv[0], "-ad") == 0) { if (argc < 2) die("missing option for -ad"); argc--; argv++; filter |= FILT_ACC_DELAY; filter_acc_delay = atol(*argv); } else if (strcmp(argv[0], "-ac") == 0) { if (argc < 2) die("missing option for -ac"); argc--; argv++; filter |= FILT_ACC_COUNT; filter_acc_count = atol(*argv); } else if (strcmp(argv[0], "-rt") == 0) { if (argc < 2) die("missing option for -rt"); argc--; argv++; filter |= FILT_TIME_RESP; filter_time_resp = atol(*argv); } else if (strcmp(argv[0], "-RT") == 0) { if (argc < 2) die("missing option for -RT"); argc--; argv++; filter |= FILT_TIME_RESP | FILT_INVERT_TIME_RESP; filter_time_resp = atol(*argv); } else if (strcmp(argv[0], "-s") == 0) { if (argc < 2) die("missing option for -s"); argc--; argv++; skip_fields = atol(*argv); } else if (strcmp(argv[0], "-e") == 0) filter |= FILT_ERRORS_ONLY; else if (strcmp(argv[0], "-E") == 0) filter |= FILT_ERRORS_ONLY | FILT_INVERT_ERRORS; else if (strcmp(argv[0], "-c") == 0) filter |= FILT_COUNT_ONLY; else if (strcmp(argv[0], "-q") == 0) filter |= FILT_QUIET; else if (strcmp(argv[0], "-v") == 0) filter_invert = !filter_invert; else if (strcmp(argv[0], "-gt") == 0) filter |= FILT_GRAPH_TIMERS; else if (strcmp(argv[0], "-pct") == 0) filter |= FILT_PERCENTILE; else if (strcmp(argv[0], "-o") == 0) { if (output_file) die("Fatal: output file name already specified.\n"); if (argc < 2) die("Fatal: missing output file name.\n"); output_file = argv[1]; } argc--; argv++; } if (!filter) die("No action specified.\n"); if (filter & FILT_ACC_COUNT && !filter_acc_count) filter_acc_count=1; if (filter & FILT_ACC_DELAY && !filter_acc_delay) filter_acc_delay = 1; linenum = 0; tot = 0; parse_err = 0; while ((line = fgets2(stdin)) != NULL) { linenum++; test = 1; if (filter & FILT_TIME_RESP) { int tps; /* only report lines with response times larger than filter_time_resp */ b = field_start(line, TIME_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } e = field_stop(b + 1); /* we have field TIME_FIELD in [b]..[e-1] */ p = b; err = 0; for (f = 0; f < 4 && *p; f++) { tps = str2ic(p); if (tps < 0) { tps = -1; err = 1; } SKIP_CHAR(p, '/'); } if (f < 4) { parse_err++; continue; } test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP); } if (filter & FILT_ERRORS_ONLY) { /* only report erroneous status codes */ b = field_start(line, STATUS_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } if (*b == '-') { test &= !!(filter & FILT_INVERT_ERRORS); } else { val = strl2ui(b, 3); test &= (val >= 500 && val <= 599) ^ !!(filter & FILT_INVERT_ERRORS); } } if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { b = field_start(line, ACCEPT_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } tot++; val = convert_date(b); //printf("date=%s => %d\n", b, val); if (val < 0) { parse_err++; continue; } t2 = insert_value(&timers[0], &t, val); t2->count++; continue; } if (filter & (FILT_GRAPH_TIMERS|FILT_PERCENTILE)) { int f; b = field_start(line, TIME_FIELD + skip_fields); if (!*b) { truncated_line(linenum, line); continue; } e = field_stop(b + 1); /* we have field TIME_FIELD in [b]..[e-1] */ p = b; err = 0; for (f = 0; f < 5 && *p; f++) { array[f] = str2ic(p); if (array[f] < 0) { array[f] = -1; err = 1; } SKIP_CHAR(p, '/'); } if (f < 5) { parse_err++; continue; } /* if we find at least one negative time, we count one error * with a time equal to the total session time. This will * emphasize quantum timing effects associated to known * timeouts. Note that on some buggy machines, it is possible * that the total time is negative, hence the reason to reset * it. */ if (filter & FILT_GRAPH_TIMERS) { if (err) { if (array[4] < 0) array[4] = -1; t2 = insert_timer(&timers[0], &t, array[4]); // total time t2->count++; } else { int v; t2 = insert_timer(&timers[1], &t, array[0]); t2->count++; // req t2 = insert_timer(&timers[2], &t, array[2]); t2->count++; // conn t2 = insert_timer(&timers[3], &t, array[3]); t2->count++; // resp v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time if (v < 0 && !(filter & FILT_QUIET)) fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", line, array[0], array[1], array[2], array[3], array[4], v); t2 = insert_timer(&timers[4], &t, v); t2->count++; tot++; } } else { /* percentile */ if (err) { if (array[4] < 0) array[4] = -1; t2 = insert_value(&timers[0], &t, array[4]); // total time t2->count++; } else { int v; t2 = insert_value(&timers[1], &t, array[0]); t2->count++; // req t2 = insert_value(&timers[2], &t, array[2]); t2->count++; // conn t2 = insert_value(&timers[3], &t, array[3]); t2->count++; // resp v = array[4] - array[0] - array[1] - array[2] - array[3]; // data time if (v < 0 && !(filter & FILT_QUIET)) fprintf(stderr, "ERR: %s (%d %d %d %d %d => %d)\n", line, array[0], array[1], array[2], array[3], array[4], v); t2 = insert_value(&timers[4], &t, v); t2->count++; tot++; } } continue; } test ^= filter_invert; if (!test) continue; /* all other cases mean we just want to count lines */ tot++; if (!(filter & FILT_COUNT_ONLY)) puts(line); } if (t) free(t); if (filter & FILT_COUNT_ONLY) { printf("%d\n", tot); exit(0); } if (filter & FILT_ERRORS_ONLY) exit(0); if (filter & (FILT_ACC_COUNT|FILT_ACC_DELAY)) { /* sort and count all timers. Output will look like this : * <accept_date> <delta_ms from previous one> <nb entries> */ n = eb32_first(&timers[0]); if (n) last = n->key; while (n) { unsigned int d, h, m, s, ms; t = container_of(n, struct timer, node); h = n->key; d = h - last; last = h; if (d >= filter_acc_delay && t->count >= filter_acc_count) { ms = h % 1000; h = h / 1000; s = h % 60; h = h / 60; m = h % 60; h = h / 60; tot++; printf("%02d:%02d:%02d.%03d %d %d %d\n", h, m, s, ms, last, d, t->count); } n = eb32_next(n); } }
static void read_h_db_file(const char *hfn,int *nahptr,t_hackblock **ah) { FILE *in; char filebase[STRLEN],line[STRLEN], buf[STRLEN]; int i, n, nab, nah; t_hackblock *aah; if (debug) fprintf(debug,"Hydrogen Database (%s):\n",hfn); fflib_filename_base(hfn,filebase,STRLEN); /* Currently filebase is read and set, but not used. * hdb entries from any hdb file and be applied to rtp entries * in any rtp file. */ in = fflib_open(hfn); nah = *nahptr; aah = *ah; while (fgets2(line,STRLEN-1,in)) { if (sscanf(line,"%s%n",buf,&n) != 1) { fprintf(stderr,"Error in hdb file: nah = %d\nline = '%s'\n", nah,line); break; } if (debug) fprintf(debug,"%s",buf); srenew(aah,nah+1); clear_t_hackblock(&aah[nah]); aah[nah].name = strdup(buf); aah[nah].filebase = strdup(filebase); if (sscanf(line+n,"%d",&nab) == 1) { if (debug) fprintf(debug," %d\n",nab); snew(aah[nah].hack,nab); aah[nah].nhack = nab; for(i=0; (i<nab); i++) { if (feof(in)) gmx_fatal(FARGS, "Expected %d lines of hydrogens, found only %d " "while reading Hydrogen Database %s residue %s", nab, i-1, aah[nah].name, hfn); if(NULL==fgets(buf, STRLEN, in)) { gmx_fatal(FARGS,"Error reading from file %s",hfn); } read_ab(buf,hfn,&(aah[nah].hack[i])); } } nah++; } ffclose(in); /* Sort the list (necessary to be able to use bsearch */ qsort(aah,nah,(size_t)sizeof(**ah),compaddh); /* if (debug) dump_h_db(hfn,nah,aah); */ *nahptr = nah; *ah = aah; }
/* Return one whole line from the file into buf which holds at most n characters, for subsequent processing. Returns integer status. This routine also does all the "intelligent" work like processing cpp directives and so on. Note that often the routine is called recursively and no cpp directives are printed. */ int cpp_read_line(gmx_cpp_t *handlep, int n, char buf[]) { gmx_cpp_t handle = (gmx_cpp_t)*handlep; int i, nn, len, status; const char *ptr, *ptr2; char *name; char *dname, *dval; gmx_bool bEOF; if (!handle) { return eCPP_INVALID_HANDLE; } if (!handle->fp) { return eCPP_FILE_NOT_OPEN; } bEOF = feof(handle->fp); if (!bEOF) { /* Read the actual line now. */ if (fgets2(buf, n-1, handle->fp) == NULL) { /* Recheck EOF, since we could have been at the end before * the fgets2 call, but we need to read past the end to know. */ bEOF = feof(handle->fp); if (!bEOF) { /* Something strange happened, fgets returned NULL, * but we are not at EOF. */ return eCPP_UNKNOWN; } } } if (bEOF) { if (handle->parent == NULL) { return eCPP_EOF; } cpp_close_file(handlep); *handlep = handle->parent; handle->child = NULL; return cpp_read_line(handlep, n, buf); } else { if (n > handle->line_len) { handle->line_len = n; srenew(handle->line, n); } strcpy(handle->line, buf); handle->line_nr++; } /* Now we've read a line! */ if (debug) { fprintf(debug, "%s : %4d : %s\n", handle->fn, handle->line_nr, buf); } /* Process directives if this line contains one */ if (find_directive(buf, &dname, &dval)) { status = process_directive(handlep, dname, dval); if (status != eCPP_OK) { return status; } /* Don't print lines with directives, go on to the next */ return cpp_read_line(handlep, n, buf); } /* Check whether we're not ifdeffed out. The order of this statement is important. It has to come after #ifdef, #else and #endif, but anything else should be ignored. */ if (is_ifdeffed_out(handle)) { return cpp_read_line(handlep, n, buf); } /* Check whether we have any defines that need to be replaced. Note that we have to use a best fit algorithm, rather than first come first go. We do this by sorting the defines on length first, and then on alphabetical order. */ for (i = 0; (i < ndef); i++) { if (defs[i].def) { nn = 0; ptr = buf; while ((ptr = strstrw(ptr, defs[i].name)) != NULL) { nn++; ptr += strlen(defs[i].name); } if (nn > 0) { len = strlen(buf) + nn*max(4, 4+strlen(defs[i].def)-strlen(defs[i].name)); snew(name, len); ptr = buf; while ((ptr2 = strstrw(ptr, defs[i].name)) != NULL) { strncat(name, ptr, (int)(ptr2-ptr)); strcat(name, defs[i].def); ptr = ptr2 + strlen(defs[i].name); } strcat(name, ptr); strcpy(buf, name); sfree(name); } } } return eCPP_OK; }