#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/GetDbfRecord.c $ %D% SwRI" #include #include #include #include #include #include "dbf.h" /************************ ** >FILE: GetDbfRecord.c ************************/ /****************************************************** ** Function: int GetDbfRecord(int dbf, int RecNum) ** ** int dbf handle to database structure ** int RecNum record number to read ** ** Description: ** This routine reads the RecNm record from the database associated with ** the database structure D. THe RecNum is checked for a valid record ** number and returns an error if RecNum is not within a valid record number ** range. Before reading the RecNum record, a check is made to make sure ** the current reocrd has not been updated, if it has been updated then the ** record is written to the file. ** ** Return Values: ** * FAILURE - dbf_code set to actual error ** * SUCCESS - the routine completed without error ** ***************** *****************/ dbRet_t GetDbfRecord(SDDAS_INT dbf, SDDAS_INT RecNum) { dbfRecord_t *D; int nbytes; off_t offset; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; /* | check for a valid record number */ if (RecNum > D->NumRecs) { sprintf(msg, "'%s'", D->FileName); dbf_code = REC_TOO_HIGH; return FAILURE; } else if (RecNum < 1) { sprintf(msg, "'%s'", D->FileName); dbf_code = REC_TOO_LOW; return FAILURE; } /* | update the current record if needed */ if (D->rStatus == Updated) { if (PutDbfRecord (dbf, D->CurRec) != SUCCESS) { return FAILURE; } } /* | position to the record to read */ offset = D->HeadLen + (RecNum-1) * D->RecLen; if ((nbytes=lseek(D->dFile, offset, SEEK_SET)) < offset) { sprintf(msg, "'%s'", D->FileName); if (nbytes == -1) dbf_code = errno; else dbf_code = LSEEK_ERROR; return FAILURE; } if ((nbytes=read(D->dFile, (char *)D->CurRecord, D->RecLen)) < D->RecLen) { sprintf(msg, "'%s'", D->FileName); if (nbytes == -1) dbf_code = errno; else dbf_code = READ_ERROR; return FAILURE; } /* | update the current record number */ D->CurRec = RecNum; dbf_msg_clr; dbf_code = 0; return SUCCESS; } /****************************************************** ** Function: int PutDbfRecord(int dbf, int RecNum) ** ** int dbf handle to database structure ** ** Description: ** The PutDbfRecord routine writes the current record to the database ** file at RecNum. THe record number is checked for a valid record number, ** if the RecNum number is greater than the number of records in the ** database, RecNum is changed to be the new last record number. If however ** RecNum is less than 1 then an error is returned. The record status is ** updated and if this is a new record then the header status is changed to ** 'Updated' so the header will be updated upon close. ** ** Return Values: ** ***************** *****************/ dbRet_t PutDbfRecord(SDDAS_INT dbf, SDDAS_INT RecNum) { dbfRecord_t *D; char new_rec; int nbytes; off_t offset; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; new_rec = 0; /* | check the record numbwe for a valid number */ if (RecNum > D->NumRecs) { RecNum = D->NumRecs+1; D->NumRecs = RecNum; new_rec = 1; D->CurRecord[0][0] = ' '; } offset = D->HeadLen + (RecNum-1) * D->RecLen; if ((nbytes=lseek(D->dFile, offset, SEEK_SET)) < offset) { sprintf(msg, "'%s'", D->FileName); if (nbytes == -1) dbf_code = errno; else dbf_code = LSEEK_ERROR; return FAILURE; } if ((nbytes=write(D->dFile, (char *)D->CurRecord, D->RecLen)) < D->RecLen) { sprintf(msg, "'%s'", D->FileName); perror ("write"); dbf_code = errno; return FAILURE; } D->rStatus = NotUpdated; if (new_rec) D->hStatus = Updated; D->CurRec = RecNum; dbf_msg_clr; dbf_code = 0; return SUCCESS; } /*************************************** ** Function: int AppendDbf(int dbf) ** ** int dbf handle to database structure ** ** Description: ** This routine simply calls PutDbfRecord with a new last record number ** to append the current record in memory to the database. ** ** Return Values: ** ** * return value from PutDbfRecord() ** ***************** *****************/ dbRet_t AppendDbf(SDDAS_INT dbf) { dbfRecord_t *D; if ((D = GetOldDbfHandle(dbf)) == NULL) return FAILURE; return(PutDbfRecord(dbf, (int)(D->NumRecs+1))); }