int read_ter_db(char *FF,char ter,t_hackblock **tbptr,gpp_atomtype_t atype) { FILE *in; char inf[STRLEN],header[STRLEN],buf[STRLEN],line[STRLEN]; t_hackblock *tb; int i,j,n,ni,kwnr,nb,maxnb,nh; sprintf(inf,"%s-%c.tdb",FF,ter); in=libopen(inf); if (debug) fprintf(debug,"Opened %s\n",inf); tb=NULL; nb=-1; maxnb=0; kwnr=NOTSET; get_a_line(in,line,STRLEN); while (!feof(in)) { if (get_header(line,header)) { /* this is a new block, or a new keyword */ kwnr=find_kw(header); if (kwnr == NOTSET) { nb++; /* here starts a new block */ if ( nb >= maxnb ) { maxnb+=100; srenew(tb,maxnb); } clear_t_hackblock(&tb[nb]); tb[nb].name=strdup(header); } } else { if (nb < 0) gmx_fatal(FARGS,"reading termini database: " "directive expected before line:\n%s\n" "This might be a file in an old format.",line); /* this is not a header, so it must be data */ if (kwnr >= ebtsNR) { /* this is a hack: add/rename/delete atoms */ /* make space for hacks */ if (tb[nb].nhack >= tb[nb].maxhack) { tb[nb].maxhack+=10; srenew(tb[nb].hack, tb[nb].maxhack); } nh=tb[nb].nhack; clear_t_hack(&(tb[nb].hack[nh])); for(i=0; i<4; i++) tb[nb].hack[nh].a[i]=NULL; tb[nb].nhack++; /* get data */ n=0; if ( kwnr==ekwRepl || kwnr==ekwDel ) { if (sscanf(line, "%s%n", buf, &n) != 1) gmx_fatal(FARGS,"Reading Termini Database: " "expected atom name on line\n%s",line); tb[nb].hack[nh].oname = strdup(buf); /* we only replace or delete one atom at a time */ tb[nb].hack[nh].nr = 1; } else if ( kwnr==ekwAdd ) { read_ab(line, inf, &(tb[nb].hack[nh])); get_a_line(in, line, STRLEN); } else gmx_fatal(FARGS,"unimplemented keyword number %d (%s:%d)", kwnr,__FILE__,__LINE__); if ( kwnr==ekwRepl || kwnr==ekwAdd ) { snew(tb[nb].hack[nh].atom, 1); read_atom(line+n, tb[nb].hack[nh].atom, atype, &tb[nb].hack[nh].cgnr); if (!tb[nb].hack[nh].nname) { if (tb[nb].hack[nh].oname) tb[nb].hack[nh].nname = strdup(tb[nb].hack[nh].oname); else gmx_fatal(FARGS,"Don't know which name the new atom should have"); } } } else if (kwnr >= 0 && kwnr < ebtsNR) { /* this is bonded data: bonds, angles, dihedrals or impropers */ srenew(tb[nb].rb[kwnr].b,tb[nb].rb[kwnr].nb+1); n=0; for(j=0; j<btsNiatoms[kwnr]; j++) { if ( sscanf(line+n, "%s%n", buf, &ni) == 1 ) tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = strdup(buf); else gmx_fatal(FARGS,"Reading Termini Database: expected %d atom names (found %d) on line\n%s", btsNiatoms[kwnr], j-1, line); n+=ni; } for( ; j<MAXATOMLIST; j++) tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = NULL; strcpy(buf, ""); sscanf(line+n, "%s", buf); tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].s = strdup(buf); tb[nb].rb[kwnr].nb++; } else gmx_fatal(FARGS,"Reading Termini Database: Expecting a header at line\n" "%s",line); } get_a_line(in,line,STRLEN); } nb++; srenew(tb,nb); fclose(in); if (debug) print_ter_db(FF,ter,nb,tb,atype); *tbptr=tb; return nb; }
static void read_ter_db_file(const char *fn, int *ntbptr, t_hackblock **tbptr, gpp_atomtype_t atype) { char filebase[STRLEN], *ptr; FILE *in; char header[STRLEN], buf[STRLEN], line[STRLEN]; t_hackblock *tb; int i, j, n, ni, kwnr, nb, maxnb, nh; fflib_filename_base(fn, filebase, STRLEN); /* Remove the C/N termini extension */ ptr = strrchr(filebase, '.'); if (ptr != nullptr) { ptr[0] = '\0'; } in = fflib_open(fn); tb = *tbptr; nb = *ntbptr - 1; maxnb = 0; kwnr = NOTSET; get_a_line(in, line, STRLEN); while (!feof(in)) { if (get_header(line, header)) { /* this is a new block, or a new keyword */ kwnr = find_kw(header); if (kwnr == NOTSET) { nb++; /* here starts a new block */ if (nb >= maxnb) { maxnb = nb + 100; srenew(tb, maxnb); } clear_t_hackblock(&tb[nb]); tb[nb].name = gmx_strdup(header); tb[nb].filebase = gmx_strdup(filebase); } } else { if (nb < 0) { gmx_fatal(FARGS, "reading termini database: " "directive expected before line:\n%s\n" "This might be a file in an old format.", line); } /* this is not a header, so it must be data */ if (kwnr >= ebtsNR) { /* this is a hack: add/rename/delete atoms */ /* make space for hacks */ if (tb[nb].nhack >= tb[nb].maxhack) { tb[nb].maxhack += 10; srenew(tb[nb].hack, tb[nb].maxhack); } nh = tb[nb].nhack; clear_t_hack(&(tb[nb].hack[nh])); for (i = 0; i < 4; i++) { tb[nb].hack[nh].a[i] = nullptr; } tb[nb].nhack++; /* get data */ n = 0; if (kwnr == ekwRepl || kwnr == ekwDel) { if (sscanf(line, "%s%n", buf, &n) != 1) { gmx_fatal(FARGS, "Reading Termini Database '%s': " "expected atom name on line\n%s", fn, line); } tb[nb].hack[nh].oname = gmx_strdup(buf); /* we only replace or delete one atom at a time */ tb[nb].hack[nh].nr = 1; } else if (kwnr == ekwAdd) { read_ab(line, fn, &(tb[nb].hack[nh])); get_a_line(in, line, STRLEN); } else { gmx_fatal(FARGS, "unimplemented keyword number %d (%s:%d)", kwnr, __FILE__, __LINE__); } if (kwnr == ekwRepl || kwnr == ekwAdd) { snew(tb[nb].hack[nh].atom, 1); read_atom(line+n, kwnr == ekwAdd, &tb[nb].hack[nh].nname, tb[nb].hack[nh].atom, atype, &tb[nb].hack[nh].cgnr); if (tb[nb].hack[nh].nname == nullptr) { if (tb[nb].hack[nh].oname != nullptr) { tb[nb].hack[nh].nname = gmx_strdup(tb[nb].hack[nh].oname); } else { gmx_fatal(FARGS, "Reading Termini Database '%s': don't know which name the new atom should have on line\n%s", fn, line); } } } } else if (kwnr >= 0 && kwnr < ebtsNR) { /* this is bonded data: bonds, angles, dihedrals or impropers */ srenew(tb[nb].rb[kwnr].b, tb[nb].rb[kwnr].nb+1); n = 0; for (j = 0; j < btsNiatoms[kwnr]; j++) { if (sscanf(line+n, "%s%n", buf, &ni) == 1) { tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = gmx_strdup(buf); } else { gmx_fatal(FARGS, "Reading Termini Database '%s': expected %d atom names (found %d) on line\n%s", fn, btsNiatoms[kwnr], j-1, line); } n += ni; } for (; j < MAXATOMLIST; j++) { tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].a[j] = nullptr; } strcpy(buf, ""); sscanf(line+n, "%s", buf); tb[nb].rb[kwnr].b[tb[nb].rb[kwnr].nb].s = gmx_strdup(buf); tb[nb].rb[kwnr].nb++; } else { gmx_fatal(FARGS, "Reading Termini Database: Expecting a header at line\n" "%s", line); } } get_a_line(in, line, STRLEN); } nb++; srenew(tb, nb); gmx_ffclose(in); *ntbptr = nb; *tbptr = tb; }