static void update_topol(const char *topinout, int p_num, int n_num, const char *p_name, const char *n_name, char *grpname) { FILE *fpin, *fpout; char buf[STRLEN], buf2[STRLEN], *temp, **mol_line = NULL; int line, i, nmol_line, sol_line, nsol_last; gmx_bool bMolecules; char temporary_filename[STRLEN]; printf("\nProcessing topology\n"); fpin = gmx_ffopen(topinout, "r"); std::strncpy(temporary_filename, "temp.topXXXXXX", STRLEN); fpout = gmx_fopen_temporary(temporary_filename); line = 0; bMolecules = FALSE; nmol_line = 0; sol_line = -1; nsol_last = -1; while (fgets(buf, STRLEN, fpin)) { line++; std::strcpy(buf2, buf); if ((temp = std::strchr(buf2, '\n')) != NULL) { temp[0] = '\0'; } ltrim(buf2); if (buf2[0] == '[') { buf2[0] = ' '; if ((temp = std::strchr(buf2, '\n')) != NULL) { temp[0] = '\0'; } rtrim(buf2); if (buf2[std::strlen(buf2)-1] == ']') { buf2[std::strlen(buf2)-1] = '\0'; ltrim(buf2); rtrim(buf2); bMolecules = (gmx_strcasecmp(buf2, "molecules") == 0); } fprintf(fpout, "%s", buf); } else if (!bMolecules) { fprintf(fpout, "%s", buf); } else { /* Check if this is a line with solvent molecules */ sscanf(buf, "%s", buf2); if (gmx_strcasecmp(buf2, grpname) == 0) { sol_line = nmol_line; sscanf(buf, "%*s %d", &nsol_last); } /* Store this molecules section line */ srenew(mol_line, nmol_line+1); mol_line[nmol_line] = gmx_strdup(buf); nmol_line++; } } gmx_ffclose(fpin); if (sol_line == -1) { gmx_ffclose(fpout); gmx_fatal(FARGS, "No line with moleculetype '%s' found the [ molecules ] section of file '%s'", grpname, topinout); } if (nsol_last < p_num+n_num) { gmx_ffclose(fpout); gmx_fatal(FARGS, "The last entry for moleculetype '%s' in the [ molecules ] section of file '%s' has less solvent molecules (%d) than were replaced (%d)", grpname, topinout, nsol_last, p_num+n_num); } /* Print all the molecule entries */ for (i = 0; i < nmol_line; i++) { if (i != sol_line) { fprintf(fpout, "%s", mol_line[i]); } else { printf("Replacing %d solute molecules in topology file (%s) " " by %d %s and %d %s ions.\n", p_num+n_num, topinout, p_num, p_name, n_num, n_name); nsol_last -= p_num + n_num; if (nsol_last > 0) { fprintf(fpout, "%-10s %d\n", grpname, nsol_last); } if (p_num > 0) { fprintf(fpout, "%-15s %d\n", p_name, p_num); } if (n_num > 0) { fprintf(fpout, "%-15s %d\n", n_name, n_num); } } } gmx_ffclose(fpout); make_backup(topinout); gmx_file_rename(temporary_filename, topinout); }
t_dlg *select_filter(t_x11 *x11, t_gmx *gmx) { static const char *title = "Group"; static const char *dummy = "\"FALSE\""; static const char *ok = "\"Ok\""; FILE *tmp; t_dlg *dlg; char tmpfile[STRLEN]; int i, j, k, len, tlen, ht, ncol, nrow, x0; len = std::strlen(title); for (i = 0; (i < (int)gmx->filter->grps->nr); i++) { len = std::max(len, static_cast<int>(std::strlen(gmx->filter->grpnames[i]))); } len += 2; ncol = 1+(gmx->filter->grps->nr / 15); nrow = gmx->filter->grps->nr/ncol; if (nrow*ncol < gmx->filter->grps->nr) { nrow++; } if (ncol > 1) { ht = 1+(nrow+1)*2+3; } else { ht = 1+(gmx->filter->grps->nr+1)*2+3; } std::strcpy(tmpfile, "filterXXXXXX"); tmp = gmx_fopen_temporary(tmpfile); #ifdef DEBUG std::fprintf(stderr, "file: %s\n", tmpfile); #endif tlen = 1+ncol*(1+len); std::fprintf(tmp, "grid %d %d {\n\n", tlen, ht); for (k = j = 0, x0 = 1; (j < ncol); j++, x0 += len+1) { std::fprintf(tmp, "group \"%s-%d\" %d 1 %d %d {\n", title, j+1, x0, len, ht-5); for (i = 0; (i < nrow) && (k < gmx->filter->grps->nr); i++, k++) { if (!gmx->filter->bDisable[k]) { std::fprintf(tmp, "checkbox \"%s\" \"%d\" %s %s %s\n", gmx->filter->grpnames[k], k, dummy, dummy, dummy); } else { std::fprintf(tmp, "statictext { \" %s\" } \"%d\" %s %s %s\n", gmx->filter->grpnames[k], k, dummy, dummy, dummy); } } std::fprintf(tmp, "}\n\n"); } std::fprintf(tmp, "simple 1 %d %d 2 {\n", ht-3, tlen-2); std::fprintf(tmp, "defbutton %s %s %s %s %s\n", ok, ok, dummy, dummy, dummy); std::fprintf(tmp, "}\n\n}\n"); gmx_ffclose(tmp); dlg = ReadDlg(x11, gmx->wd->self, title, tmpfile, 0, 0, true, false, FilterCB, gmx); std::remove(tmpfile); return dlg; }