LIB_EXPORT uint32_t CC AgrepFindBest( const AgrepParams *self, int32_t threshold, const char *buf, int32_t len, AgrepMatch *match ) { if( self != NULL && buf != NULL && match != NULL ) { AgrepCallArgs args; args.self = self; args.threshold = threshold; args.buf = buf; args.buflen = len; args.cb = AgrepFindBestCallback; args.cbinfo = match; match->score = -1; AgrepFindAll(&args); if (match->score != -1) { return 1; } } return 0; }
LIB_EXPORT int32_t CC agrep ( AgrepFlags alg, char const* pattern, FILE* fp, int32_t print, int32_t printcontext, int32_t k, int32_t findall, int32_t simulate ) { AgrepParams *self = NULL; int32_t count; size_t len; AgrepFlags mode = alg; AgrepMatch match; agrepinfo info; char szBuf[BUF_SIZE]; AgrepCallArgs args; int32_t linenum = 0; char const* na = "ACGTN"; size_t const nPatternSize = strlen(pattern); size_t const nBufSize = BUF_SIZE; size_t const nMaxTextLen = nBufSize-nPatternSize-1; size_t nExtendBufSize; int32_t i; AgrepMake(&self, mode, pattern); count = 0; while (!feof(fp) && fgets(szBuf, nMaxTextLen, fp)) { ++linenum; len = strlen(szBuf); if (szBuf[len-1] == '\n') { szBuf[len-1] = '\0'; --len; } /* Make szBuf somewhat circular to be able to find pattern cut in between */ nExtendBufSize = nPatternSize <= len ? nPatternSize : len; memmove(szBuf+len, szBuf, nExtendBufSize - 1); szBuf[len + nExtendBufSize - 1] = '\0'; if (simulate) { for (i = 0; szBuf[i]; ++i) { if (NULL != index(na, szBuf[i])) { szBuf[i] = index(na, szBuf[i]) - na; } } } if (findall) { AgrepBestMatch best_match = {9999, 0, 0}; /* TODO: it's not C89*/ info.buf = szBuf; info.linenum = linenum; info.original_length = len; info.pBestMatch = &best_match; args.self = self; args.threshold = k; args.buf = szBuf; args.buflen = len + nPatternSize; args.cb = simulate ? agrep_2na_callback : agrep_callback_find_best; args.cbinfo = (void *)&info; AgrepFindAll(&args); if (!simulate) /* output best match */ { printf("line=%d, pos=%d, len=%d, end=%d, score=%d: %.*s%s%.*s\n", linenum, best_match.indexStart, best_match.nMatchLength, (best_match.indexStart + best_match.nMatchLength) % (int32_t)len, best_match.nScore, (int)(min(best_match.nMatchLength, (int32_t)len - best_match.indexStart)), szBuf + best_match.indexStart, (best_match.indexStart + best_match.nMatchLength) > (int32_t)len ? "|" : "", (int)((best_match.indexStart + best_match.nMatchLength) > (int32_t)len ? best_match.nMatchLength - ((int32_t)len - best_match.indexStart) : 0), szBuf); } } else if (AgrepFindFirst(self, k, szBuf, len + nPatternSize, &match)) { ++count; if (print) { printf("%s\n", szBuf); } if (printcontext) { printf("%.*s\n", match.length, szBuf + match.position); } } } return count; }