t_hackblock *choose_ter(int nb, t_hackblock **tb, const char *title) { int i, sel, ret; printf("%s\n", title); for (i = 0; (i < nb); i++) { bool bIsZwitterion = (0 == gmx_wcmatch("*ZWITTERION*", (*tb[i]).name)); printf("%2d: %s%s\n", i, (*tb[i]).name, bIsZwitterion ? " (only use with zwitterions containing exactly one residue)" : ""); } do { ret = fscanf(stdin, "%d", &sel); } while ((ret != 1) || (sel < 0) || (sel >= nb)); return tb[sel]; }
/*! * See sel_updatefunc() for description of the parameters. * \p data should point to a \c t_methoddata_kwstr. * * Does a linear search to find which atoms match the strings in the * \c t_methoddata_kwstr structure for this selection. * Wildcards are allowed in the strings. * Matching atoms are stored in \p out->u.g. */ static int evaluate_keyword_str(t_topology *top, t_trxframe *fr, t_pbc *pbc, gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data) { t_methoddata_kwstr *d = (t_methoddata_kwstr *)data; int i, j; gmx_bool bFound; out->u.g->isize = 0; for (i = 0; i < g->isize; ++i) { bFound = FALSE; for (j = 0; j < d->n && !bFound; ++j) { if (d->m[j].bRegExp) { #ifdef USE_REGEX /* This branch should only be taken if regular expressions * are available, but the ifdef is still needed. */ if (!regexec(&d->m[j].u.r, d->v[i], 0, NULL, 0)) { bFound = TRUE; } #endif } else { if (gmx_wcmatch(d->m[j].u.s, d->v[i]) == 0) { bFound = TRUE; } } } if (bFound) { out->u.g->index[out->u.g->isize++] = g->index[i]; } } return 0; }
/*! * \param[in] pattern Pattern to match against. * \param[in] str String to match. * \returns 0 on match, GMX_NO_WCMATCH if there is no match. * * Matches \p str against \p pattern, which may contain * and ? wildcards. * All other characters are matched literally. * Currently, it is not possible to match literal * or ?. */ int gmx_wcmatch(const char *pattern, const char *str) { while (*pattern) { if (*pattern == '*') { /* Skip multiple wildcards in a sequence */ while (*pattern == '*' || *pattern == '?') { ++pattern; /* For ?, we need to check that there are characters left * in str. */ if (*pattern == '?') { if (*str == 0) { return GMX_NO_WCMATCH; } else { ++str; } } } /* If the pattern ends after the star, we have a match */ if (*pattern == 0) { return 0; } /* Match the rest against each possible suffix of str */ while (*str) { /* Only do the recursive call if the first character * matches. We don't have to worry about wildcards here, * since we have processed them above. */ if (*pattern == *str) { int rc; /* Match the suffix, and return if a match or an error */ rc = gmx_wcmatch(pattern, str); if (rc != GMX_NO_WCMATCH) { return rc; } } ++str; } /* If no suffix of str matches, we don't have a match */ return GMX_NO_WCMATCH; } else if ((*pattern == '?' && *str != 0) || *pattern == *str) { ++str; } else { return GMX_NO_WCMATCH; } ++pattern; } /* When the pattern runs out, we have a match if the string has ended. */ return (*str == 0) ? 0 : GMX_NO_WCMATCH; }