26 #include <valgrind/memcheck.h>
28 #define VALGRIND_CHECK_MEM_IS_DEFINED( A, B )
29 #define VALGRIND_MAKE_MEM_UNDEFINED( A, B )
35 result = malloc(
size );
36 if( !result )
mhdf_setFail( status,
"Allocation of %d bytes failed.\n", (
int)
size );
43 result = realloc( ptr,
size );
44 if( !result )
mhdf_setFail( status,
"Allocation of %d bytes failed.\n", (
int)
size );
53 if( 0 == strcmp( name,
"." ) )
55 if( path_len >= 4 ) sprintf( path,
"\\%02X", (
int)*name );
59 for( iter = (
unsigned char*)name; *iter; ++iter )
61 if( iscntrl( *iter ) || *iter ==
'/' || *iter ==
'\\' || *iter > 127 )
68 for( iter = (
unsigned char*)name; *iter; ++iter )
70 if( iscntrl( *iter ) || *iter ==
'/' || *iter ==
'\\' || *iter > 127 )
72 sprintf( path,
"\\%02X", (
int)( *iter ) );
90 return toupper( c ) - (
'A' - 10 );
98 for( iter = path; *iter; ++iter, ++name )
104 if( !isxdigit( c1 ) || !isxdigit( c2 ) )
return 0;
125 if( !
buffer )
return NULL;
136 plen = strlen( prefix );
139 if( !
buffer )
return NULL;
141 memcpy(
buffer, prefix, plen );
152 if( NULL == path )
return -1;
154 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
155 result = H5Gopen2( file_ptr->
hdf_handle, path, H5P_DEFAULT );
157 result = H5Gopen( file_ptr->
hdf_handle, path );
160 if( result < 0 )
mhdf_setFail( status,
"Failed to open element group: \"%s\"", elem_handle );
166 hid_t dspace_id, attr_id;
169 dspace_id = H5Screate( H5S_SCALAR );
172 mhdf_setFail( status,
"Internal error calling H5Screate_simple." );
176 #if defined( H5Acreate_vers ) && H5Acreate_vers > 1
177 attr_id = H5Acreate2(
object, name, type, dspace_id, H5P_DEFAULT, H5P_DEFAULT );
179 attr_id = H5Acreate(
object, name, type, dspace_id, H5P_DEFAULT );
181 H5Sclose( dspace_id );
184 mhdf_setFail( status,
"Failed to create \"%s\" attrib.", name );
189 rval = H5Awrite( attr_id, type, value );
193 mhdf_setFail( status,
"Failed to write \"%s\" attrib.", name );
202 hid_t attr_id, type_id;
205 attr_id = H5Aopen_name(
object, name );
208 mhdf_setFail( status,
"Failed to create \"%s\" attrib.", name );
218 type_id = H5Aget_type( attr_id );
226 rval = H5Aread( attr_id, type_id, value );
228 if( type < 1 ) H5Tclose( type_id );
231 mhdf_setFail( status,
"Failed to read \"%s\" attrib.", name );
238 #if defined( H5Aiterate_vers ) && H5Aiterate_vers > 1
239 static herr_t
find_attr_by_name( hid_t handle,
const char* name,
const H5A_info_t* info,
void* mydata )
253 return !strcmp( name, (
const char*)mydata );
259 #if defined( H5Aiterate_vers ) && H5Aiterate_vers > 1
261 rval = H5Aiterate2(
object, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, &idx, &
find_attr_by_name, (
void*)attrib_name );
262 *index_out = (
unsigned int)idx;
267 if( rval < 0 )
mhdf_setFail( status,
"Internal error calling H5Aiterate." );
277 return !strcmp( name, (
const char*)mydata );
284 if( rval < 0 )
mhdf_setFail( status,
"Internal error in H5Giterate." );
297 hid_t slab_id, mem_id;
298 hsize_t offsets[2], counts[2] = { 1, 1 };
302 const hsize_t one = 1;
305 if( offset < 0 || count < 0 )
308 "Invalid input for %s: "
309 "offset = %ld, count = %ld\n",
310 read ?
"read" :
"write", offset, count );
314 slab_id = H5Dget_space( data_id );
317 mhdf_setFail( status,
"Internal error calling H5Dget_space." );
321 dims = H5Sget_simple_extent_ndims( slab_id );
322 if( dims < 1 || dims > 2 )
325 mhdf_setFail( status,
"Internal error: unexpected dataset rank: %d.", dims );
329 dims = H5Sget_simple_extent_dims( slab_id, counts, NULL );
333 mhdf_setFail( status,
"Internal error calling H5Sget_simple_extend_dims." );
337 if( (
unsigned long)( offset + count ) > counts[0] )
340 mhdf_setFail( status,
"Requested %s of rows %ld to %ld of a %ld row table.\n", read ?
"read" :
"write", offset,
341 offset + count - 1, (
long)counts[dims - 1] );
345 counts[0] = (hsize_t)count;
346 offsets[0] = (hsize_t)offset;
349 rval = H5Sselect_hyperslab( slab_id, H5S_SELECT_SET, offsets, NULL, counts, NULL );
351 rval = H5Sselect_none( slab_id );
355 mhdf_setFail( status,
"Internal error calling H5Sselect_hyperslab." );
360 mem_id = H5Screate_simple( dims, counts, NULL );
366 mem_id = H5Screate_simple( 1, &one, NULL );
367 if( mem_id && 0 > H5Sselect_none( mem_id ) )
378 mhdf_setFail( status,
"Internal error calling H5Screate_simple." );
383 rval = H5Dread( data_id, type, mem_id, slab_id, io_prop, array );
387 rval = H5Dwrite( data_id, type, mem_id, slab_id, io_prop, array );
393 mhdf_setFail( status,
"Internal error calling H5D%s.", read ?
"read" :
"write" );
411 hid_t slab_id, mem_id;
412 hsize_t offsets[2], counts[2];
416 const hsize_t one = 1;
419 if( column < 0 || offset < 0 || count < 0 )
422 "Invalid input for %s: "
423 "column = %d, offset = %ld, count = %ld\n",
424 read ?
"read" :
"write", column, offset, count );
428 slab_id = H5Dget_space( data_id );
431 mhdf_setFail( status,
"Internal error calling H5Dget_space." );
435 dims = H5Sget_simple_extent_ndims( slab_id );
436 if( dims < 1 || dims > 2 )
439 mhdf_setFail( status,
"Internal error: unexpected dataset rank: %d.", dims );
443 dims = H5Sget_simple_extent_dims( slab_id, counts, NULL );
447 mhdf_setFail( status,
"Internal error calling H5Sget_simple_extend_dims." );
451 if( (
unsigned long)( offset + count ) > counts[0] || (
unsigned long)column > counts[1] )
454 mhdf_setFail( status,
"Requested %s of (%ld,%d)->(%ld,%ld) of (%ld, %ld) table.\n", read ?
"read" :
"write",
455 offset, column, offset + count - 1, column, (
long)counts[0], (
long)counts[1] );
459 counts[0] = (hsize_t)count;
460 offsets[0] = (hsize_t)offset;
464 rval = H5Sselect_hyperslab( slab_id, H5S_SELECT_SET, offsets, NULL, counts, NULL );
466 rval = H5Sselect_none( slab_id );
470 mhdf_setFail( status,
"Internal error calling H5Sselect_hyperslab." );
475 mem_id = H5Screate_simple( dims, counts, NULL );
481 mem_id = H5Screate_simple( 1, &one, NULL );
482 if( mem_id && 0 > H5Sselect_none( mem_id ) )
493 mhdf_setFail( status,
"Internal error calling H5Screate_simple." );
498 rval = H5Dread( data_id, type, mem_id, slab_id, io_prop, array );
502 rval = H5Dwrite( data_id, type, mem_id, slab_id, io_prop, array );
509 mhdf_setFail( status,
"Internal error calling H5D%s.", read ?
"read" :
"write" );
525 return mhdf_readwrite( data_id, 0, offset, count, type_id, (
void*)array, prop, status );
536 return mhdf_readwrite( data_id, 1, offset, count, type_id, array, prop, status );
576 hid_t space_id, table_id;
578 space_id = H5Screate_simple( rank, dims, NULL );
581 mhdf_setFail( status,
"Internal error calling H5Screate_simple." );
585 #if defined( H5Dcreate_vers ) && H5Dcreate_vers > 1
586 table_id = H5Dcreate2( group_id, path, type, space_id, H5P_DEFAULT, create_prop, H5P_DEFAULT );
588 table_id = H5Dcreate( group_id, path, type, space_id, create_prop );
590 H5Sclose( space_id );
593 mhdf_setFail( status,
"HDF5 DataSet creation failed." );
603 hid_t table_id, space_id;
607 #if defined( H5Dopen_vers ) && H5Dopen_vers > 1
608 table_id = H5Dopen2( group_id, path, H5P_DEFAULT );
610 table_id = H5Dopen( group_id, path );
614 mhdf_setFail( status,
"HDF5 DataSet creation failed." );
618 space_id = H5Dget_space( table_id );
621 mhdf_setFail( status,
"Internal error in H5Dget_space." );
622 H5Dclose( table_id );
626 rank = H5Sget_simple_extent_ndims( space_id );
627 if( rank != ( columns ? 1 : 2 ) )
629 mhdf_setFail( status,
"Incorrect DataSpace for DataSet." );
630 H5Sclose( space_id );
631 H5Dclose( table_id );
635 rank = H5Sget_simple_extent_dims( space_id, dims, NULL );
636 H5Sclose( space_id );
639 mhdf_setFail( status,
"Internal error calling H5Sget_simple_extent_dims." );
640 H5Dclose( table_id );
656 hid_t table_id, space_id;
658 #if defined( H5Dopen_vers ) && H5Dopen_vers > 1
659 table_id = H5Dopen2( group_id, path, H5P_DEFAULT );
661 table_id = H5Dopen( group_id, path );
665 mhdf_setFail( status,
"HDF5 DataSet creation failed." );
669 space_id = H5Dget_space( table_id );
672 mhdf_setFail( status,
"Internal error in H5Dget_space." );
673 H5Dclose( table_id );
677 if( H5Sget_simple_extent_ndims( space_id ) != rank )
679 mhdf_setFail( status,
"Incorrect DataSpace for DataSet." );
680 H5Sclose( space_id );
681 H5Dclose( table_id );
685 rank = H5Sget_simple_extent_dims( space_id, dims_out, NULL );
686 H5Sclose( space_id );
689 mhdf_setFail( status,
"Internal error calling H5Sget_simple_extent_dims." );
690 H5Dclose( table_id );
696 mhdf_setFail( status,
"File format error. Failed to retreive ID offset." );
697 H5Dclose( table_id );
709 #if defined( H5Dopen_vers ) && H5Dopen_vers > 1
710 table_id = H5Dopen2( group_id, path, H5P_DEFAULT );
712 table_id = H5Dopen( group_id, path );
716 mhdf_setFail( status,
"HDF5 DataSet creation failed." );
728 const int* left = (
const int*)ptr1;
729 const int* right = (
const int*)ptr2;
730 return *left < *right ? -1 : *left > *right ? 1 : 0;
739 int *copy_ptr = 0, *w_iter;
749 while( iter < end && *( iter++ ) == ++prev )
752 if( new_length > ( iter - ids ) ) need_copy = 1;
755 if( new_length > *
length )
return 0;
759 blen =
sizeof( int ) * *
length;
760 copy_ptr = (
int*)malloc( blen );
761 memcpy( copy_ptr, ids, blen );
775 while( iter < end && *( iter++ ) == ++prev )
777 *( w_iter++ ) = prev - count;
778 *( w_iter++ ) = count;
782 if( need_copy ) free( copy_ptr );
789 #if defined( H5Topen_vers ) && H5Topen_vers > 1
794 if( result < 0 )
mhdf_setFail( status,
"Element type enum does not exist in file. Invalid file." );
800 hid_t group_id, attr_id, space_id;
803 #if defined( H5Gopen_vers ) && H5Gopen_vers > 1
810 mhdf_setFail( status,
"Internal error -- file invalid." );
817 space_id = H5Screate( H5S_SCALAR );
818 #if defined( H5Acreate_vers ) && H5Acreate_vers > 1
819 attr_id = H5Acreate2( group_id,
MAX_ID_ATTRIB, H5T_NATIVE_ULONG, space_id, H5P_DEFAULT, H5P_DEFAULT );
821 attr_id = H5Acreate( group_id,
MAX_ID_ATTRIB, H5T_NATIVE_ULONG, space_id, H5P_DEFAULT );
823 H5Sclose( space_id );
825 H5Gclose( group_id );
832 rval = H5Awrite( attr_id, H5T_NATIVE_ULONG, &file_ptr->
max_id );
848 int nf, rval, i, count = 0;
850 nf = H5Fget_obj_ids( H5F_OBJ_ALL, H5F_OBJ_FILE,
sizeof( list ) /
sizeof( hid_t ), list );
851 if( nf <= 0 || nf > 64 )
return 0;
853 for( i = 0; i < nf; i++ )
855 rval = H5Fget_obj_count( list[i], H5F_OBJ_ALL );
856 if( rval > 0 ) count += rval;
877 fprintf( stderr,
"Unclosed handles at end of mhdf API call.\n" );
878 fprintf( stderr,
"Entered with %d, expected %d change, got %d.\n",
mhdf_api_handle_count, expected_diff,
880 fprintf( stderr,
"%s:%d\n", filename, linenumber );