#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/CloseDbf.c $ %D% SwRI" #include #include #include #include #include #include #include "dbf.h" /******************** ** >FILE: CloseDbf.c ********************/ /********************************************** ** Function: int UpdateHeader(dbfrecord_t *D) ** ** dbfrecord_t *d handle pointer to database structure ** ** Description: ** reset the header information and write it out to the database file ** ** Return Values: ** * FAILURE - dbf_code set to actual error ** * SUCCESS - the routine completed without error ** ***************** *****************/ dbRet_t UpdateHeader(dbfRecord_t *D) { struct tm *mytm; struct stat buf; off_t offset; int r; r = (int) D->NumRecs; /* | get the date of the file for the header information */ (void)fstat(D->dFile, &buf); mytm = localtime(&buf.st_mtime); /* | be sure to write it out the date of the file in the correct database file | type format */ if (D->HeadProlog[0] == DB2File) { D->HeadProlog[5] = mytm->tm_year; /* Year */ D->HeadProlog[3] = mytm->tm_mon; /* Month of year */ D->HeadProlog[4] = mytm->tm_mday; /* Day of month */ D->HeadProlog[2] = r/256; r -= D->HeadProlog[5]*2560; D->HeadProlog[1] = r; } else { D->HeadProlog[1] = mytm->tm_year; /* Year */ D->HeadProlog[2] = mytm->tm_mon; /* Month of year */ D->HeadProlog[3] = mytm->tm_mday; /* Day of month */ D->HeadProlog[7] = (char) (r/16777216); r -= ((unsigned char)D->HeadProlog[7] * 16777216); D->HeadProlog[6] = (char) (r/65536); r -= ((unsigned char)D->HeadProlog[6] * 65536); D->HeadProlog[5] = (char) (r/256); r -= ((unsigned char)D->HeadProlog[5] * 256); D->HeadProlog[4] = (char)r; } /* | the header information goes at the beginning of the file, lseek to it. | If lseeking to the offset position does not retuen the offset, and error | has occured, return an lseek error. */ offset = 0; if (lseek(D->dFile, offset, SEEK_SET) < offset) { dbf_code = LSEEK_ERROR; sprintf(msg, "'%s'", D->FileName); return FAILURE; } /* | if there is no problem in seeking to the beginning of the file write | out the header information */ if (write(D->dFile, D->HeadProlog, 8) < 8) { dbf_code = WRITE_ERROR; sprintf(msg, "'%s'", D->FileName); return FAILURE; } /* | reset the status of the header info indicating that it matches the info | in the file */ D->hStatus = NotUpdated; return SUCCESS; } /************************************** ** Function: int CloseDbf(int handle) ** ** int handle database handle ** ** Description: ** This subroutine closes the database file associated with the database ** structure D. If the current record has been updated then it is written ** to the database file. Once the database file is closed, all the ** parameters in the database structure is reset, and any pointers are ** freed. ** ** Return Values: ** * FAILURE - dbf_code set to actual error ** * SUCCESS - the routine completed without error ** ***************** *****************/ dbRet_t CloseDbf(SDDAS_INT handle) { dbfRecord_t *D; if ((D = GetOldDbfHandle(handle)) == NULL) return FAILURE; /* | update the database file if needed */ if (D->rStatus == Updated) { if (PutDbfRecord(handle, D->CurRec) != SUCCESS) { return FAILURE; } } if (D->hStatus == Updated) { if (UpdateHeader(D) != SUCCESS) { return FAILURE; } } /* | close the database file and set the appropiate variables */ (void)close(D->dFile); D->dFile = -1; D->hStatus = NotOpen; /* | free any memory associated with the database structure */ free((char *)D->CurRecord); D->CurRecord = NULL; free((char *)D->Fields); D->Fields = NULL; if (D == dbfRecords) dbfRecords = D->next; else { dbfRecord_t *d; for (d = dbfRecords ; d->next != NULL; d = d->next) if (d->next == D) break; d->next = D->next; } free(D); return SUCCESS; }