#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libdB/GetHandle.c $ %D% SwRI" #include #include #include #define _INIT_ #include "dbf.h" /************** ** GetHandle.c **************/ /****************************************************** ** Function: dbfRecord_t *GetOldDbfHandle(int handle) ** ** Description: ** return the pointer to the database structure for the given handle. ** ** Return Values: ** NULL - handle invalid, check dbf_code for explination ** Upon valid handle a pointer to the database structure is returned ** ***************** *****************/ dbfRecord_t * GetOldDbfHandle(SDDAS_INT handle) { dbfRecord_t *D; /* * find the currect handle * couldn't find this handle??, error! */ if ((D=dbfHandle(handle)) == NULL) return NULL; if (D->dFile == -1) { dbf_msg_clr; dbf_code = EMPTY_HANDLE; return NULL; } return D; } /******************************************************* ** Function: dbfRecord_t *GetNewDbfHandle(int *handle) ** ** Description: ** return the handle to a new database structure ** ** Return Values: ** * -1 - cannot get a new handle, check dbf_code for explination ** * Valid handle number (starting at 0) ** ***************** *****************/ dbfRecord_t * GetNewDbfHandle(SDDAS_INT *handle) { dbfRecord_t *D; if (dbfRecords == NULL) { /* * no database structures initializes as yet */ if ((dbfRecords = (dbfRecord_t *) calloc(1, sizeof(dbfRecord_t))) == NULL) { dbf_msg_clr; dbf_code = errno; return NULL; } D = dbfRecords; D->handle = 1; } else { /* * need a new handle, allocate a new one and append it to the end * of the list of current handles */ for (D = dbfRecords; D->next != NULL; D = D->next); /* last handle */ if ((D->next = (dbfRecord_t *) calloc(1, sizeof(dbfRecord_t))) == NULL) { dbf_code = errno; dbf_msg_clr; return NULL; } D->next->handle = D->handle + 1; D = D->next; } InitDbfHandle(D); *handle = D->handle; return D; } /********************************************************** ** Function: ndxRecord *GetOldNdxHandle(int dbf, int ndx) ** ** Description: ** return the pointer to the database structure for the given handle. ** ** Return Values: ** NULL - handle invalid, check dbf_code for explination ** Upon valid handle a pointer to the database structure is returned ** ***************** *****************/ ndxRecord_t * GetOldNdxHandle(SDDAS_INT dbf, SDDAS_INT ndx) { ndxRecord_t *N; /* * find the currect handle * couldn't find this handle??, error! */ if ((N=ndxHandle(dbf, ndx)) == NULL) return NULL; if (N->Index == NULL) { dbf_code = EMPTY_HANDLE; dbf_msg_clr; return NULL; } return N; } /**************************************************************** ** Function: ndxRecord_t *GetNewNdxHandle(int dbf, int *handle) ** ** Description: ** return the handle to a new index structure ** ** Return Values: ** * -1 - cannot get a new handle, check dbf_code for explination ** * Valid handle number (starting at 0) ** ***************** *****************/ ndxRecord_t * GetNewNdxHandle(SDDAS_INT dbf, SDDAS_INT *handle) { dbfRecord_t *D; ndxRecord_t *N; if ((D=dbfHandle(dbf)) == NULL) return NULL; if (D->ndxRecords == NULL) { /* * no index structures initializes as yet */ if ((D->ndxRecords = (ndxRecord_t *) calloc(1, sizeof(ndxRecord_t))) == NULL) { dbf_code = errno; dbf_msg_clr; return NULL; } N = D->ndxRecords; N->handle = 1; } else { /* * need a new handle, allocate a new one and append it to the end * of the list of current handles */ for (N = D->ndxRecords; N->next != NULL; N = N->next); /* last handle */ if ((N->next = (ndxRecord_t *) calloc(1, sizeof(ndxRecord_t))) == NULL) { dbf_code = errno; dbf_msg_clr; return NULL; } N->next->handle = N->handle + 1; N = N->next; } InitNdxHandle(N); *handle = N->handle; return N; } /******************************************* ** Function: InitDbfHandle(dbfRecord_t *d) ** ** Description: ** initialize a handle's structure to a known state ** ** Return Values: ** SUCCESS ** ***************** *****************/ dbRet_t InitDbfHandle(dbfRecord_t *D) { /* * initialize all the fields of the structure */ memset(D->FileName, 0, sizeof(D->FileName)); D->dFile = -1; memset(D->HeadProlog, 0, sizeof(HeaderProlog_t)); D->hStatus = NotOpen; D->WithMemo = (char) sFalse; memset(D->DateOfUpdate, 0, sizeof(D->DateOfUpdate)); D->NumRecs = 0; D->HeadLen = 0; D->RecLen = 0; D->NumFields = 0; D->Fields = NULL; D->CurRec = -1; D->rStatus = NotOpen; D->CurRecord = NULL; D->ndxRecords = NULL; D->next = NULL; return SUCCESS; } /******************************************* ** Function: InitNdxHandle(ndxRecord_t *N) ** ** Description: ** initialize a handle's structure to a known state ** ** Return Values: ** None ** ***************** *****************/ dbRet_t InitNdxHandle(ndxRecord_t *N) { /* * initialize all the fields of the structure */ memset(N->IndexName, 0, sizeof(N->IndexName)); N->IndexRec = -1; N->NextFreeIndexRec = 0; N->MAXIndexRecs = 0; N->iStatus = NotOpen; N->NumIndexFields = 0; N->IndexFields = NULL; N->KeyLen = 0; N->Index = NULL; N->next = NULL; return SUCCESS; } dbfRecord_t * dbfHandle(SDDAS_INT handle) { dbfRecord_t *D; /* * find the desired handle */ for (D = dbfRecords; D != NULL; D = D->next) if (D->handle == handle) break; if (D == NULL) { dbf_code = UNKNOWN_HANDLE; dbf_msg_clr; } return D; } ndxRecord_t * ndxHandle(SDDAS_INT dbf, SDDAS_INT ndx) { ndxRecord_t *N; dbfRecord_t *D; if ((D = dbfHandle(dbf)) == NULL) return NULL; /* * find the desired handle */ for (N = D->ndxRecords; N != NULL; N = N->next) if (N->handle == ndx) break; if (N == NULL) { dbf_msg_clr; dbf_code = UNKNOWN_HANDLE; } return N; }