Example #1
0
static int
scan_for_max_id( FileHandle* file_ptr, mhdf_Status* status )
{
  hid_t group_id;
  herr_t rval;
  
    /* Check for new format, with max_id as attrib of root group */
#if defined(H5Gopen_vers) && H5Gopen_vers > 1  
  group_id = H5Gopen2( file_ptr->hdf_handle, ROOT_GROUP, H5P_DEFAULT );
#else
  group_id = H5Gopen( file_ptr->hdf_handle, ROOT_GROUP );
#endif
  if (group_id < 0)
  {
    mhdf_setFail( status, "Internal error - invalid file.");
    return 0;
  }
  if (mhdf_read_scalar_attrib( group_id, MAX_ID_ATTRIB,
                               H5T_NATIVE_ULONG, &file_ptr->max_id,
                               status ))
  {
    H5Gclose( group_id );
    return 1;
  }
  
    /* Didn't find it, scan the elements group */
  rval = H5Giterate( group_id, ELEMENT_GROUP_NAME, 0, &max_id_iter, &file_ptr->max_id );
  if (rval)
  {
    H5Gclose( group_id );
    mhdf_setFail( status, "Internal error -- invalid file." );
    return 0;
  }
  
    /* Check node table too */
  rval = get_max_id( group_id, NODE_GROUP_NAME, "coordinates", (unsigned long*)(&file_ptr->max_id) );
  if (rval)
  {
    H5Gclose( group_id );
    mhdf_setFail( status, "Internal error -- invalid file." );
    return 0;
  }
  
    /* Check set table, if it exists */
  rval = mhdf_is_in_group( group_id, SET_GROUP_NAME, status );
  if (rval < 1)
  {
    H5Gclose( group_id );
    return !rval;
  }
  rval = get_max_id( group_id, SET_GROUP_NAME, SET_META_NAME, (unsigned long*)(&file_ptr->max_id) );
  H5Gclose( group_id );
  if (rval)
  {
    mhdf_setFail( status, "Internal error -- invalid file." );
    return 0;
  }

  return 1;
}    
Example #2
0
void 
mhdf_getElemTypeName( mhdf_FileHandle file_handle,
                      const char* elem_handle,
                      char* buffer, size_t buf_len,
                      mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, type_id, attr_id;
  char bytes[16];
  herr_t rval;
  API_BEGIN;
 
  if (NULL == buffer || buf_len < 2)
  {
    mhdf_setFail( status, "invalid input" );
    return;
  }
  buffer[0] = '\0';
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
  
  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
  if (elem_id < 0) return;
  
  attr_id = H5Aopen_name( elem_id, ELEM_TYPE_ATTRIB );
  H5Gclose( elem_id );
  if (attr_id < 0)
  {
    mhdf_setFail( status, "Missing element type attribute.  Invalid file." );
    return;
  }
  
  type_id = H5Aget_type( attr_id );
  assert( type_id > 0 );
  
  rval = H5Aread( attr_id, type_id, bytes );
  H5Aclose( attr_id );
  if (rval < 0)
  {
    H5Tclose( type_id );
    mhdf_setFail( status, "Failed to read element type attribute.  Invalid file." );
    return;
  }
  
  rval = H5Tenum_nameof( type_id, bytes, buffer, buf_len );
  H5Tclose( type_id );
  if (rval < 0)
  {
    mhdf_setFail( status, "Invalid datatype for element type attribute.  Invalid file." );
    return;
  }
  
  mhdf_setOkay( status );  
  API_END;
  return ;
}
Example #3
0
void
mhdf_writeHistory( mhdf_FileHandle file_handle, 
                   const char** strings, 
                   int num_strings,
                   mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t data_id, type_id, space_id;
  hsize_t dim = (hsize_t)num_strings;
  herr_t rval;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
    
  type_id = H5Tcopy( H5T_C_S1 );
  if (type_id < 0 || H5Tset_size( type_id, H5T_VARIABLE ) < 0)
  {
    if (type_id >= 0) H5Tclose(type_id);
    mhdf_setFail( status, "Could not create variable length string type." );
    return;
  }
  
  space_id = H5Screate_simple( 1, &dim, NULL );
  if (space_id < 0)
  {
    H5Tclose( type_id );
    mhdf_setFail( status, "H5Screate_simple failed." );
    return;
  }
  
#if defined(H5Dcreate_vers) && H5Dcreate_vers > 1
  data_id = H5Dcreate2( file_ptr->hdf_handle, HISTORY_PATH, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
#else
  data_id = H5Dcreate( file_ptr->hdf_handle, HISTORY_PATH, type_id, space_id, H5P_DEFAULT );
#endif
  H5Sclose( space_id );
  if (data_id < 0)
  {
    H5Tclose( type_id );
    mhdf_setFail( status, "Failed to create \"%s\".", HISTORY_PATH );
    return;
  }
    
  rval = H5Dwrite( data_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, strings );
  H5Dclose( data_id );
  H5Tclose( type_id );
  if (rval < 0)
  {
    H5Gunlink( file_ptr->hdf_handle, HISTORY_PATH );
    mhdf_setFail( status, "Failed to write history data." );
    return;
  }
  
  mhdf_setOkay( status );
  API_END;
}
Example #4
0
void
mhdf_closeData( mhdf_FileHandle file, hid_t handle, mhdf_Status* status )
{
  FileHandle* file_ptr;
  herr_t rval = -1;
  
  file_ptr = (FileHandle*)(file);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
  
  switch ( H5Iget_type( handle ) )
  {
    case H5I_GROUP    :  rval = H5Gclose( handle );  break;
    case H5I_DATATYPE :  rval = H5Tclose( handle );  break;
    case H5I_DATASPACE:  rval = H5Sclose( handle );  break;
    case H5I_DATASET  :  rval = H5Dclose( handle );  break;
    default           :  rval = -1;
  }
  
  if (rval < 0)
  {
    mhdf_setFail( status, "H5Xclose failed.  Invalid handle?\n");
  }
  else
  {
    file_ptr->open_handle_count--;
    mhdf_setOkay( status );
  }
}
Example #5
0
void 
mhdf_closeFile( mhdf_FileHandle handle,
                mhdf_Status* status )
{
  FileHandle* file_ptr;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
/* 
  if (file_ptr->open_handle_count)
  {
    mhdf_setError( status, "Cannot close file with %d open data handles.", 
      file_ptr->open_handle_count );
    return;
  }
*/  
  
  /* Check for open handles.  HDF5 will not actually close the
     file until all handles are closed. */
  if (mhdf_checkOpenHandles( handle, status ))
    return;
 
  if (0 > H5Fclose( file_ptr->hdf_handle ))
  {
    mhdf_setFail( status, "H5FClose failed.  Invalid handle?" );
    return;
  } 
  
  memset( file_ptr, 0, sizeof(FileHandle) );
  free( file_ptr );
  mhdf_setOkay( status );
  API_END_H( -1 );
}
Example #6
0
int
mhdf_checkOpenHandles( mhdf_FileHandle handle,
                       mhdf_Status* status )
{
  FileHandle* file_ptr;
  int result;
  API_BEGIN;

  file_ptr = (FileHandle*)(handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;
  
  /* Check for open handles.  HDF5 will not actually close the
     file until all handles are closed. */
  result = H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_ALL );
  if (result != 1)
  {
    mhdf_setFail( status, "Cannot close file with open handles: "
                 "%d file, %d data, %d group, %d type, %d attr\n",
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_FILE ) - 1,
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_DATASET ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_GROUP ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_DATATYPE ),
                  H5Fget_obj_count( file_ptr->hdf_handle, H5F_OBJ_ATTR ) );
    return result - 1;
  }

  API_END_H( 0 );
  return 0;
}
Example #7
0
void
mhdf_openPolyConnectivity( mhdf_FileHandle file_handle,
                           const char* element_handle,
                           long* num_poly_out,
                           long* data_list_length_out,
                           long* first_poly_id_out,
                           hid_t handles_out[2],
                           mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, table_id, index_id;
  hsize_t row_count;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return ;
  
  if (!num_poly_out || !data_list_length_out || !first_poly_id_out)
  {
    mhdf_setFail( status, "Invalid argument." );
    return ;
  }
  
  elem_id = mhdf_elem_group_from_handle( file_ptr, element_handle, status );
  if (elem_id < 0) return ;
  
  index_id = mhdf_open_table( elem_id, POLY_INDEX_NAME,
                              1, &row_count, status );
  if (index_id < 0)
    { H5Gclose(elem_id); return ; }
  *num_poly_out = (int)row_count;
  
  table_id = mhdf_open_table( elem_id, CONNECTIVITY_NAME, 
                              1, &row_count, status );
  
  H5Gclose( elem_id );
  if (table_id < 0)
  {
    H5Dclose( index_id );
    return ;
  }
  *data_list_length_out = (long)row_count;
    
  if (!mhdf_read_scalar_attrib( table_id, START_ID_ATTRIB, H5T_NATIVE_LONG, 
                                first_poly_id_out, status ))
  {
    H5Dclose( table_id );
    H5Dclose( index_id );
    return ;
  }
  
  file_ptr->open_handle_count++;
  handles_out[0] = index_id;
  handles_out[1] = table_id;
  mhdf_setOkay( status );
  API_END_H(2);
}
Example #8
0
void
mhdf_getElemName( mhdf_FileHandle file_handle,
                  unsigned int type_index,
                  char* buffer,
                  size_t buf_size,
                  mhdf_Status* status )
{
  FileHandle* file_ptr;
  herr_t rval;
  hid_t enum_id;
  API_BEGIN;
  
  if (type_index > 255)
  {
    mhdf_setFail( status, "Type index out of bounds." );
    return;
  }
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;

  enum_id = get_elem_type_enum( file_ptr, status );
  if (enum_id < 0)
    return;
  
  rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super(enum_id), 1, &type_index, NULL, H5P_DEFAULT );
  if (rval < 0)
  {
    H5Tclose( enum_id );
    mhdf_setFail( status, "Internal error converting to enum type." );
    return;
  }
  
  rval = H5Tenum_nameof( enum_id, &type_index, buffer, buf_size );
  H5Tclose( enum_id );
  if (rval < 0)
    mhdf_setFail( status, "H5Tenum_nameof failed.  Invalid type index?" );
  else
    mhdf_setOkay( status );
    
  API_END;
}
Example #9
0
hid_t
mhdf_createAdjacency( mhdf_FileHandle file,
                      const char* elem_handle,
                      long adj_list_size,
                      mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, table_id;
  hsize_t dim = (hsize_t)adj_list_size;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;

  if (adj_list_size < 1)
  {
    mhdf_setFail( status, "Invalid argument.\n" );
    return -1;
  }
  
  if (elem_handle == mhdf_node_type_handle())
  {
    table_id = mhdf_create_table( file_ptr->hdf_handle,
                                  NODE_ADJCY_PATH,
                                  file_ptr->id_type,
                                  1, &dim,
                                  status );
  }
  else
  {
    elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
    if (elem_id < 0) return -1;
    
    table_id = mhdf_create_table( elem_id,
                                  ADJACENCY_NAME,
                                  file_ptr->id_type,
                                  1, &dim,
                                  status );
    H5Gclose( elem_id );
  }
  
  API_END_H(1);
  return table_id;
}
Example #10
0
hid_t
mhdf_openConnectivity( mhdf_FileHandle file_handle,
                       const char* elem_handle,
                       int* num_nodes_per_elem_out,
                       long* num_elements_out,
                       long* first_elem_id_out,
                       mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, table_id;
  hsize_t dims[2];
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;
  
  if (!num_nodes_per_elem_out || !num_elements_out || !first_elem_id_out)
  {
    mhdf_setFail( status, "Invalid argument." );
    return -1;
  }
  
  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
  if (elem_id < 0) return -1;
  
  table_id = mhdf_open_table2( elem_id, CONNECTIVITY_NAME, 
                               2, dims,
                               first_elem_id_out, status );
  
  H5Gclose( elem_id );
  if (table_id < 0)
    return -1;
  
  *num_elements_out = dims[0];
  *num_nodes_per_elem_out = dims[1];
  
  file_ptr->open_handle_count++;
  mhdf_setOkay( status );
  API_END_H(1);
  return table_id;
}
Example #11
0
static int
make_hdf_group( const char* path, hid_t file, size_t sz, mhdf_Status* status )
{
#if defined(H5Gcreate_vers) && H5Gcreate_vers > 1
  hid_t handle = H5Gcreate2( file, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
    /* empty statement to avoid compiler warning */
  if (sz) {}
#else
  hid_t handle = H5Gcreate( file, path, sz );
#endif
  if (handle < 0)
  {
    mhdf_setFail( status, "Failed to create \"%s\" group.", path );
    return 0;
  }
  else
  {
    H5Gclose( handle );
    return 1;
  }
}
Example #12
0
int
mhdf_haveAdjacency( mhdf_FileHandle file,
                    const char* elem_group,
                    mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id;
  int result;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;
  
  if (elem_group == mhdf_node_type_handle())
  {
#if defined(H5Gopen_vers) && H5Gopen_vers > 1  
    elem_id = H5Gopen( file_ptr->hdf_handle, NODE_GROUP, H5P_DEFAULT );
#else
    elem_id = H5Gopen( file_ptr->hdf_handle, NODE_GROUP );
#endif
    if (elem_id < 0)
    {
      mhdf_setFail( status, "H5Gopen( \"%s\" ) failed.\n", NODE_GROUP );
      return -1;
    }
  }
  else
  {
    elem_id = mhdf_elem_group_from_handle( file_ptr, elem_group, status );
    if (elem_id < 0)
      return -1;
  }
  
  result = mhdf_is_in_group( elem_id, ADJACENCY_NAME, status );
  H5Gclose( elem_id );
  mhdf_setOkay( status );
  API_END;
  return result;
}
Example #13
0
hid_t 
mhdf_createConnectivity( mhdf_FileHandle file_handle,
                         const char* elem_handle,
                         int nodes_per_elem,
                         long count,
                         long* first_id_out,
                         mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, table_id;
  hsize_t dims[2];
  long first_id;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return -1;
  
  if (nodes_per_elem <= 0 || count < 0 || !first_id_out)
  {
    mhdf_setFail( status, "Invalid argument." );
    return -1;
  }
  
  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
  if (elem_id < 0) return -1;
  
  
  dims[0] = (hsize_t)count;
  dims[1] = (hsize_t)nodes_per_elem;
  table_id = mhdf_create_table( elem_id, 
                                CONNECTIVITY_NAME,
                                file_ptr->id_type,
                                2, dims, 
                                status );
  H5Gclose( elem_id );
  if (table_id < 0)
    return -1;
  
  first_id = file_ptr->max_id + 1;
  if (!mhdf_create_scalar_attrib( table_id, 
                                 START_ID_ATTRIB, 
                                 H5T_NATIVE_LONG,
                                 &first_id,
                                 status ))
  {
    H5Dclose( table_id );
    return -1;
  }
  
  *first_id_out = first_id;
  file_ptr->max_id += count;
  if (!mhdf_write_max_id( file_ptr, status))
  {
    H5Dclose( table_id );
    return -1;
  }
  file_ptr->open_handle_count++;
  mhdf_setOkay( status );
 
  API_END_H(1);
  return table_id;
}
Example #14
0
void 
mhdf_createPolyConnectivity( mhdf_FileHandle file_handle,
                             const char* elem_type,
                             long num_poly,
                             long data_list_length,
                             long* first_id_out,
                             hid_t handles_out[2],
                             mhdf_Status* status )
{
  FileHandle* file_ptr;
  hid_t elem_id, index_id, conn_id;
  hsize_t dim;
  long first_id;
  API_BEGIN;
  
  file_ptr = (FileHandle*)(file_handle);
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
  
  if (num_poly <= 0 || data_list_length <= 0 || !first_id_out)
  {
    mhdf_setFail( status, "Invalid argument." );
    return;
  }
  
  if (data_list_length < 3*num_poly)
  {
    /* Could check agains 4*num_poly, but allow degenerate polys
       (1 for count plus 2 dim-1 defining entities, where > 2
        defining entities is a normal poly, 2 defining entities 
        is a degenerate poly and 1 defning entity is not valid.)
    */
    
    mhdf_setFail( status, "Invalid polygon data:  data length of %ld is "
                          "insufficient for %ld poly(gons/hedra).\n",
                          data_list_length, num_poly );
    return;
  }
  
  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_type, status );
  if (elem_id < 0) return;
  
  dim = (hsize_t)num_poly;
  index_id = mhdf_create_table( elem_id,
                                POLY_INDEX_NAME,
                                MHDF_INDEX_TYPE,
                                1, &dim,
                                status );
  if (index_id < 0)
    { H5Gclose(elem_id); return; }
  
  
  dim = (hsize_t)data_list_length;
  conn_id = mhdf_create_table( elem_id, 
                                CONNECTIVITY_NAME,
                                file_ptr->id_type,
                                1, &dim, 
                                status );
  H5Gclose( elem_id );
  if (conn_id < 0)
  {
    H5Dclose(index_id);
    return;
  }
  
  first_id = file_ptr->max_id + 1;
  if (!mhdf_create_scalar_attrib( conn_id, 
                                 START_ID_ATTRIB, 
                                 H5T_NATIVE_LONG,
                                 &first_id,
                                 status ))
  {
    H5Dclose(index_id);
    H5Dclose( conn_id );
    return;
  }
  
  *first_id_out = first_id;
  file_ptr->max_id += num_poly;
  if (!mhdf_write_max_id( file_ptr, status))
  {
    H5Dclose(index_id);
    H5Dclose( conn_id );
    return;
  }
  file_ptr->open_handle_count++;
  mhdf_setOkay( status );
  handles_out[0] = index_id;
  handles_out[1] = conn_id;
  API_END_H(2);
}
Example #15
0
mhdf_FileHandle
mhdf_openFileWithOpt( const char* filename, 
                      int writable, 
                      unsigned long* max_id_out,
                      hid_t id_type,
                      hid_t access_prop,
                      mhdf_Status* status )
{
  FileHandle* file_ptr;
  unsigned int flags;
  hid_t group_id;
  int check_is_hdf5 = 1;
#ifdef HDF5_PARALLEL
  herr_t err;
  MPI_Comm comm;
  MPI_Info info;
#endif
  API_BEGIN;
  
    /* Check if file is HDF5 */
  /* Don't do this because it can't handle MPI-IO driver code that
     passes options via prefixes on the file name. */
#ifdef HDF5_PARALLEL
  if (access_prop != H5P_DEFAULT) {
    err = H5Pget_fapl_mpio( access_prop, &comm, &info );
    if (err >= 0) {
      check_is_hdf5 = 0;
      /* MPI Documentation is inconsistent with regards to whether
         or not the above call dup's these, but my testing with 1.8.3
         indicates that at least for that version they are not.
      MPI_Comm_free(&comm);
      MPI_Info_free(&info); */
    }
  }
#endif
  if (check_is_hdf5 && H5Fis_hdf5( filename ) <= 0) {
    mhdf_setFail( status, "%s: File is not HDF5", filename );
    return NULL;
  }
  
    /* Create struct to hold working data */
  file_ptr = mhdf_alloc_FileHandle( 0, id_type, status );
  if (!file_ptr) {
    mhdf_setFail( status, "Memory allocation failed" );
    return NULL;
  }  

    /* Create the file */
  flags = writable ? H5F_ACC_RDWR : H5F_ACC_RDONLY;
  file_ptr->hdf_handle = H5Fopen( filename, flags, access_prop );
  if (file_ptr->hdf_handle < 0)
  {
    mhdf_setFail( status, "Failed to open file \"%s\"", filename );
    free( file_ptr );
    return NULL;
  }
  
    /* Check for TSTT data in file */
#if defined(H5Gopen_vers) && H5Gopen_vers > 1  
  group_id = H5Gopen2( file_ptr->hdf_handle, ROOT_GROUP, H5P_DEFAULT );
#else
  group_id = H5Gopen( file_ptr->hdf_handle, ROOT_GROUP );
#endif
  if (group_id < 0)
  {
    mhdf_setFail( status, "Invalid file \"%s\"\n", filename );
    H5Fclose( file_ptr->hdf_handle );
    free( file_ptr );
    return NULL;
  }
  H5Gclose( group_id );
  
    /* Get max id */
  if (!scan_for_max_id( file_ptr, status ))
  {
    H5Fclose( file_ptr->hdf_handle );
    mhdf_setFail( status, "Internal error reading file" );
    free( file_ptr );
    return NULL;
  }
  
  if (max_id_out)
    *max_id_out = file_ptr->max_id;
    
  mhdf_setOkay( status );
  API_END_H(1);
  return file_ptr;
}
Example #16
0
void
mhdf_addElement( mhdf_FileHandle file_handle, 
                 const char* name, 
                 unsigned int elem_type,
                 mhdf_Status* status )
{
  FileHandle* file_ptr = (FileHandle*)file_handle;
  hid_t group_id, tag_id, enum_id;
  char* path, *ptr;
  size_t name_len;
  herr_t rval;
  API_BEGIN;
  
  if (!mhdf_check_valid_file( file_ptr, status ))
    return;
  
  name_len = mhdf_name_to_path( name, NULL, 0 );
  name_len += strlen(ELEMENT_GROUP) + 1;
  path = (char*)mhdf_malloc( name_len, status );
  if (!path)
    return;
  
  strcpy( path, ELEMENT_GROUP );
  ptr = path + strlen(ELEMENT_GROUP);
  if (!mhdf_path_to_name( name, ptr ))
  {
    mhdf_setFail( status, "Invalid character string in internal file path: \"%s\"\n",
      name );
    return;
  }

#if defined(H5Gcreate_vers) && H5Gcreate_vers > 1
  group_id = H5Gcreate2( file_ptr->hdf_handle, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
#else
  group_id = H5Gcreate( file_ptr->hdf_handle, path, 3 );
#endif
  if (group_id < 0)
  {
    mhdf_setFail( status, "Creation of \"%s\" group failed.\n", path );
    free( path );
    return;
  }
  free( path );
  
#if defined(H5Gcreate_vers) && H5Gcreate_vers > 1
  tag_id = H5Gcreate2( group_id, DENSE_TAG_SUBGROUP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
#else
  tag_id = H5Gcreate( group_id, DENSE_TAG_SUBGROUP, 0 );
#endif
  if (tag_id < 0)
  {
    H5Gclose( group_id );
    mhdf_setFail( status, "Creation of tag subgroup failed.\n" );
    return;
  }
  H5Gclose( tag_id );
  
  enum_id = get_elem_type_enum( file_ptr, status );
  if (enum_id < 0)
  {
    H5Gclose( group_id );
    return;
  }
  
  rval = H5Tconvert( H5T_NATIVE_UINT, H5Tget_super(enum_id), 1, &elem_type, NULL, H5P_DEFAULT );
  if (rval < 0)
  {
    H5Gclose( group_id );
    H5Tclose( enum_id );
    mhdf_setFail( status, "Internal error converting to enum type." );
    return;
  }
  
  rval = mhdf_create_scalar_attrib( group_id, ELEM_TYPE_ATTRIB, enum_id,
                                 &elem_type, status );
  H5Tclose( enum_id );
  if (rval < 0)
  {
    H5Gclose( group_id );
    return;
  }
  
  H5Gclose( group_id );
  mhdf_setOkay( status );
  API_END;
}
Example #17
0
mhdf_FileHandle
mhdf_createFile( const char* filename, 
                 int overwrite, 
                 const char** elem_type_list,
                 size_t elem_list_len,
                 hid_t id_type,
                 mhdf_Status* status )
{
  FileHandle* file_ptr;
  unsigned int flags;
  unsigned char idx;
  size_t i;
  hid_t enum_id, group_id;
  int rval;
  API_BEGIN;
  
  if (elem_list_len > 255)
  {
    mhdf_setFail( status, "Element type list too long." );
    return NULL;
  }
  mhdf_setOkay( status );
  
    /* Create struct to hold working data */
  file_ptr = mhdf_alloc_FileHandle( 0, id_type, status );
  if (!file_ptr) return NULL;

    /* Create the file */
  flags = overwrite ? H5F_ACC_TRUNC : H5F_ACC_EXCL;
  file_ptr->hdf_handle = H5Fcreate( filename, flags, H5P_DEFAULT, H5P_DEFAULT );
  if (file_ptr->hdf_handle < 0)
  {
    mhdf_setFail( status, "Failed to create file \"%s\"", filename );
    free( file_ptr );
    return NULL;
  }
  
  
    /* Create file structure */
  if (!make_hdf_group(     ROOT_GROUP, file_ptr->hdf_handle, 6, status )
   || !make_hdf_group(      TAG_GROUP, file_ptr->hdf_handle, 0, status )
   || !make_hdf_group(  ELEMENT_GROUP, file_ptr->hdf_handle, 8, status )
   || !make_hdf_group(     NODE_GROUP, file_ptr->hdf_handle, 3, status )
   || !make_hdf_group(      SET_GROUP, file_ptr->hdf_handle, 5, status )
   || !make_hdf_group( NODE_TAG_GROUP, file_ptr->hdf_handle, 0, status )
   || !make_hdf_group(  SET_TAG_GROUP, file_ptr->hdf_handle, 0, status ))
  {
    H5Fclose( file_ptr->hdf_handle );
    free( file_ptr );
    return NULL;
  }
  
    /* Store the max ID as an attribite on the /tstt/ group */
#if defined(H5Gopen_vers) && H5Gopen_vers > 1  
  group_id = H5Gopen2( file_ptr->hdf_handle, ROOT_GROUP, H5P_DEFAULT );
#else
  group_id = H5Gopen( file_ptr->hdf_handle, ROOT_GROUP );
#endif
  rval = mhdf_create_scalar_attrib( group_id, 
                                    MAX_ID_ATTRIB, 
                                    H5T_NATIVE_ULONG, 
                                    &file_ptr->max_id,
                                    status );
  H5Gclose( group_id );
  if (!rval)
  {
    H5Fclose( file_ptr->hdf_handle );
    free( file_ptr );
    return NULL;
  }
  
    /* Create the type name list in file */
  enum_id = H5Tenum_create( H5T_NATIVE_UCHAR );
  if (enum_id < 0)
  {
    mhdf_setFail( status, "Failed to store elem type list." );
    H5Fclose( file_ptr->hdf_handle );
    free( file_ptr );
    return NULL;
  }
  for (i = 0; i < elem_list_len; ++i)
  {
    if (!elem_type_list[i] || !*elem_type_list[i])
      continue;
      
    idx = (unsigned char)i;
    if ( H5Tenum_insert( enum_id, elem_type_list[i], &idx ) < 0)
    {
      mhdf_setFail( status, "Failed to store elem type list." );
      H5Fclose( file_ptr->hdf_handle );
      free( file_ptr );
      return NULL;
    }
  }
#if defined(H5Tcommit_vers) && H5Tcommit_vers > 1
  if (H5Tcommit2( file_ptr->hdf_handle, TYPE_ENUM_PATH, enum_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ) < 0)  
#else
  if (H5Tcommit( file_ptr->hdf_handle, TYPE_ENUM_PATH, enum_id ) < 0)  
#endif
  {
    mhdf_setFail( status, "Failed to store elem type list." );
    H5Fclose( file_ptr->hdf_handle );
    free( file_ptr );
    return NULL;
  }
  H5Tclose( enum_id );
  
  API_END_H( 1 );
  return file_ptr;
}