#include "dbinc.h" /***************************************************************************** * * * COMMON OCEANOGRAPHIC DATA ACCESS SYSTEM (CODAS) * * * * WRITTEN BY: RAMON CABRERA, ERIC FIRING, and JULIE RANADA * * JOINT INSTITUTE FOR MARINE AND ATMOSPHERIC RESEARCH * * 1000 POPE ROAD MSB 312 * * HONOLULU, HI 96822 * * * * VERSION: 3.10 * * * * DATE: APRIL 1989, AUGUST 1995 * * * *****************************************************************************/ /* FILE: dbgetput.c SYSTEM FUNCTIONS USED BY DBGET, DBGET_F, DBPUT & DBPUT_F */ /*----------------------------------------------------------------------------- FUNCTION: get_access_param This function returns the number of bytes of the specified data type that are available for retrieval from a profile, and the offset to these data in the file. PARAMETERS: type = pointer to data type nb = pointer to number of bytes that can be retrieved from a profile ofs = pointer to offset to position to start retrieving from RETURNS: VOID */ void get_access_param(int *type, unsigned int *nb, long *ofs) { int ind; /* index into data directory */ if (db_ptr->data_list[*type].access_type == BLOCK_VAR_ACCESS_CODE) { *ofs = db_ptr->data_list[*type].access_0; *nb = min_val(db_ptr->data_list[*type].access_1, MAXUINT); /*jr*/ } else if (db_ptr->data_list[*type].access_type == PROFILE_VAR_ACCESS_CODE) { ind = db_ptr->data_list[*type].access_0; *nb = min_val(db_ptr->data_dir[ind].nbytes, MAXUINT); /*jr*/ *ofs = db_ptr->profile_dir_entry_ptr->ofs + db_ptr->data_dir[ind].ofs; } } /*----------------------------------------------------------------------------- FUNCTION: dbget_flags It checks the necessary database status flags prior to retrieving data using DBGET or DBGET_F, or rewriting data using DBPUT or DBPUT_F. RETURNS: 0 if okay error code otherwise */ int dbget_flags(void) { if ((!(db_ptr)) || first_database_call) return(DB_IS_NOT_OPEN); if (db_ptr->create_mode) return(OPEN_FOR_CREATE); if (!(db_ptr->block_is_open)) return(NO_BLOCK_IS_OPEN); return(0); } /*----------------------------------------------------------------------------- FUNCTION: load_block_strdef() It loads the block file structure definitions into memory. RETURNS: 0 if okay db_ptr->error_code otherwise */ int load_block_strdef(void) { unsigned int converted_nb, nentries; int nb; long file_ofs; char *dest_ptr; STRUCT_DEF_HDR_TYPE **link_address; int nelem, i, resize; STRUCT_DEF_ENTRY_TYPE *sde_ptr; DB_STRUCT_DEF_ENTRY_TYPE *dbsde_ptr; nentries = db_ptr->block_hdr.struct_def_nentries; if (nentries == 0) nentries++; else { nb = nentries * DB_STRUCT_DEF_ENTRY_SIZE; file_ofs = db_ptr->block_hdr.data_list_ofs + db_ptr->block_hdr.data_list_nentries * DATA_LIST_ENTRY_SIZE; } if ((db_ptr->block_strdef = (STRUCT_DEF_HDR_TYPE *) calloc(nentries, STRUCT_DEF_ENTRY_SIZE)) == NULL) { db_ptr->error_code = INSUFFICIENT_MEMORY; db_ptr->error_data = STRUCTURE_DEF; goto error_found; } if (nb) { if (DB_STRUCT_DEF_ENTRY_SIZE == STRUCT_DEF_ENTRY_SIZE) { resize = 0; dest_ptr = (char *) db_ptr->block_strdef; } else { resize = 1; dest_ptr = calloc(nentries, DB_STRUCT_DEF_ENTRY_SIZE); if (dest_ptr == NULL) { db_ptr->error_code = INSUFFICIENT_MEMORY; db_ptr->error_data = STRUCTURE_DEF; goto error_found; } } if ((db_ptr->error_code = read_data(db_ptr->fpblkdata, dest_ptr, file_ofs, nb)) != 0) { db_ptr->error_data = STRUCTURE_DEF; free(db_ptr->block_strdef); db_ptr->block_strdef_loaded = 0; goto error_found; } if (resize) { dbsde_ptr = (DB_STRUCT_DEF_ENTRY_TYPE *) dest_ptr; sde_ptr = (STRUCT_DEF_ENTRY_TYPE *) db_ptr->block_strdef; for (i=0; iblock_host != HOST_ENVIRONMENT) { dest_ptr = (char *) db_ptr->block_strdef; do { if ( (converted_nb = convert_struct(dest_ptr, dest_ptr, db_ptr->block_host, HOST_ENVIRONMENT, "struct_def_hdr", &(dbint_sd[0].struct_def_hdr))) == BADUINT) { db_ptr->error_code = CONVERT_ERROR; db_ptr->error_data = STRUCTURE_DEF; goto error_found; } nelem = ((STRUCT_DEF_HDR_TYPE *)(dest_ptr))->nelem; dest_ptr += converted_nb; nb -= converted_nb; for (i = 0; i < nelem; i++) { if ((converted_nb = convert_struct(dest_ptr, dest_ptr, db_ptr->block_host, HOST_ENVIRONMENT, "struct_def_elem", &(dbint_sd[0].struct_def_elem))) == BADUINT) { db_ptr->error_code = CONVERT_ERROR; db_ptr->error_data = STRUCTURE_DEF; goto error_found; } dest_ptr += converted_nb; nb -= converted_nb; } } while (nb > 0); } } if (db_ptr->prev_strdef == NULL) /* block_strdef heads strdefs */ db_ptr->struct_def = db_ptr->block_strdef; /* must set main strdef to block */ else /* connect block strdef to previous */ { link_address = (STRUCT_DEF_HDR_TYPE **) &( (db_ptr->prev_strdef)->LINK_PTR ); *link_address = db_ptr->block_strdef; } link_address = (STRUCT_DEF_HDR_TYPE **) &( (db_ptr->block_strdef + (nentries - 1))->LINK_PTR ); *link_address = db_ptr->next_strdef; /* connect block_strdef trailer to next strdef */ db_ptr->block_strdef_loaded = 1; db_ptr->formats_matched = 0; return(0); error_found: report_db_error("load_block_strdef"); return(db_ptr->error_code); }