#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/dbFind.c $ %D% SwRI" #include #include #include "dbf.h" /****************** ** >FILE: dbFind.c ******************/ char *c2; int ans, KEYLEN; static int intcompare(i, j) char *i, *j; { return((ans=memcmp(i, (c2=j), KEYLEN))); } /********************************************************* ** Function: int dbFind(int dbf, int ndx, char *arg, boolean mode) ** ** int ndx index handle ** char *arg character string to find ** boolean mode search flag ** ** Description: ** This routine finds the closest or exact entry which matches the arg ** key for the database associated with the database structure D. If mode ** is set then the return value reflects the status of the find, either ** exact, greater or less match. ** ** Return Values: ** ***************** *****************/ dbFindRet_t dbFind(SDDAS_INT dbf, SDDAS_INT ndx, char *arg, SDDAS_BOOL mode) { register char *fnd; SDDAS_INT rec, rec_size; dbfRecord_t *D; ndxRecord_t *N; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAIL; if ((N = GetOldNdxHandle(dbf, ndx)) == NULL) return FAIL; KEYLEN = (N->KeyLen >= strlen(arg)) ? strlen(arg) : N->KeyLen; if (D->NumRecs == 0) return NO_MATCH ; rec_size = N->KeyLen + DB_ALIGN(N->KeyLen) + sizeof(SDDAS_INT); fnd = (char *)bsearch(arg, N->Index, D->NumRecs, rec_size, intcompare); if (fnd != NULL) { rec = (SDDAS_INT) ((((char *)fnd) - ((char *) N->Index)) / rec_size) + 1; if (GoTo(dbf, ItoR(N, rec)) != SUCCESS) { return FIND_ERROR ; } N->IndexRec = rec; return EXACT_MATCH ; } if (mode == sFalse) return NO_MATCH; rec = (SDDAS_INT) ((((char *)c2) - ((char *) N->Index)) / rec_size) + 1; if (GoTo(dbf, ItoR(N, rec)) != SUCCESS) { return FIND_ERROR ; } N->IndexRec = rec; if (ans < 0) { return MATCH_LESS; } return MATCH_GREATER; }