#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/OpenNdx.c $ %D% SwRI" #include #include #include #include #include #include #include "dbf.h" /******************* ** >FILE: OpenNdx.c *******************/ /*********************************************************** ** Function: int OpenNdx(int dbf, char *fname, int ndx) ** ** Description: ** This routine opens and reads the index array and sets all necessary ** variables in the database structure D. ** ** Return Values: ** ***************** *****************/ dbRet_t OpenNdx(SDDAS_INT dbf, char *fname, SDDAS_INT *ndx) { ndxRecord_t *N; char iHeader[96]; int fd, n; SDDAS_INT i, startpos; long nbytes; FieldRecord_t *fld; if ((N = GetNewNdxHandle(dbf, ndx)) == NULL) return FAILURE; /* | open the index file */ strcpy(N->IndexName, fname); if ((fd = open(N->IndexName, O_RDONLY | O_BINARY)) < 0) { sprintf(msg, "'%s'", N->IndexName); dbf_code = errno; return FAILURE; } /* | read the header info and set the next free index record, the maximum | number of indexes available and the number of index records. */ memset(iHeader, 0, sizeof(iHeader)); if ((n = read(fd, (char *)iHeader, sizeof(iHeader))) < 0) { sprintf(msg, "'%s'", N->IndexName); dbf_code = READ_ERROR; return FAILURE; } N->NextFreeIndexRec = *((SDDAS_INT *)&iHeader[sizeof (SDDAS_INT)]); N->MAXIndexRecs = *((SDDAS_INT *)&iHeader[sizeof (SDDAS_INT) * 2]); N->NumIndexFields = *((SDDAS_INT *)&iHeader[sizeof (SDDAS_INT) * 3]); /* | allocate the necesary memory for the index fields */ nbytes = N->NumIndexFields * sizeof(FieldRecord_t); if ((N->IndexFields = (FieldRecord_t *)calloc(N->NumIndexFields, sizeof(FieldRecord_t))) == NULL) { dbf_msg_clr; dbf_code = errno; return FAILURE; } /* | set the index fields */ nbytes = 0; N->KeyLen = 0; startpos = sizeof (SDDAS_INT) * 4; for (i=0; iNumIndexFields; i++) { memcpy((N->IndexFields+i)->Name, &iHeader[i*16 + startpos], FIELD_NAME_LEN); memcpy((char *)&(N->IndexFields+i)->Typ, &iHeader[i*16 + startpos + FIELD_NAME_LEN], 3); if ((fld = dbField(dbf, (N->IndexFields+i)->Name)) == NULL) { return FAILURE; } (N->IndexFields+i)->Parm = fld->Parm; N->KeyLen += (N->IndexFields+i)->Len; } /* | allocate the necessay memory for the index array */ nbytes += N->KeyLen + DB_ALIGN(N->KeyLen) + sizeof(SDDAS_INT); if ((N->Index = calloc(N->MAXIndexRecs, nbytes)) == NULL) { dbf_msg_clr; dbf_code = errno; return FAILURE; } /* | read the index array */ nbytes *= N->MAXIndexRecs; if ((n = read(fd, (char *)N->Index, nbytes)) < 0) { sprintf(msg, "'%s'", N->IndexName); dbf_code = errno; return FAILURE; } /* | the index file is no longer needed, close it */ (void)close(fd); N->iStatus = NotUpdated; dbf_msg_clr; dbf_code = 0; return SUCCESS; }