#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/DeleteRec.c $ %D% SwRI" #include #include #include "dbf.h" dbRet_t DeleteRec(SDDAS_INT dbf, SDDAS_INT ndx, SDDAS_INT rec) { dbfRecord_t *D; ndxRecord_t *N = 0; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; if (ndx != -1) { if ((N = GetOldNdxHandle(dbf, ndx)) == NULL) return FAILURE; if (GoTo(dbf, ItoR(N, rec)) != SUCCESS) return FAILURE; } else if (GoTo(dbf, rec) != SUCCESS) return FAILURE; if (D->CurRecord[0][0] == ' ') { D->CurRecord[0][0] = '*'; if (N) DeleteNdxRec(N, ItoR(N, rec)); return PutDbfRecord(dbf, rec); } return SUCCESS; } dbRet_t DeleteNdxRec(ndxRecord_t *N, SDDAS_INT rec) { int size, nbytes; char *ndx; size = N->KeyLen + DB_ALIGN(N->KeyLen) + sizeof(SDDAS_INT); ndx = N->Index + size * (rec-1); nbytes = (N->NextFreeIndexRec - N->IndexRec) * size; memcpy(ndx, ndx + size, nbytes); N->iStatus = Updated; return SUCCESS; } dbRet_t UnDeleteRec(SDDAS_INT dbf, SDDAS_INT ndx, SDDAS_INT rec) { dbfRecord_t *D; ndxRecord_t *N = 0; dbRet_t ret; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; if (ndx != -1) if ((N = GetOldNdxHandle(dbf, ndx)) == NULL) return FAILURE; if (GoTo(dbf, rec) != SUCCESS) return FAILURE; if (D->CurRecord[0][0] == '*') { D->CurRecord[0][0] = ' '; if ((ret=PutDbfRecord(dbf, rec)) == SUCCESS) if (N) ret = AddIndexRec(D, N); return ret; } return SUCCESS; } dbRet_t AddIndexRec(dbfRecord_t *D, ndxRecord_t *N) { int size, k, j; char *cptr; size = N->KeyLen + DB_ALIGN(N->KeyLen) + sizeof(SDDAS_INT); cptr = (char *)N->Index + (N->NextFreeIndexRec * size); for (k=0; kNumIndexFields; k++) { for (j=0; jIndexFields[k].Len; j++) { char c; c = (char )N->IndexFields[k].Parm[j]; *(cptr++) = c; } } cptr += DB_ALIGN(N->KeyLen); *((int *)cptr) = D->CurRec; N->NextFreeIndexRec++; if (N->NextFreeIndexRec == N->MAXIndexRecs) { N->MAXIndexRecs += 50; size = N->MAXIndexRecs * (N->KeyLen + DB_ALIGN(N->KeyLen) + sizeof(SDDAS_INT)); if ((N->Index = (char *)realloc(N->Index, size)) == NULL) { perror("...realloc: "); exit (2); } } N->iStatus = Updated; return SUCCESS; }