MOAB: Mesh Oriented datABase  (version 5.4.1)
dump_sets.c
Go to the documentation of this file.
00001 #include "mhdf.h"
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <H5Tpublic.h>
00005 
00006 #define CHECK                                                                                                 \
00007     do                                                                                                        \
00008     {                                                                                                         \
00009         if( mhdf_isError( &status ) )                                                                         \
00010         {                                                                                                     \
00011             fprintf( stderr, "mhdf error at %s:%d : \"%s\"\n", __FILE__, __LINE__, mhdf_message( &status ) ); \
00012             exit( 2 );                                                                                        \
00013         }                                                                                                     \
00014     } while( 0 )
00015 
00016 void dump_sets( mhdf_FileHandle file );
00017 void dump_set_contents( long first, long num_sets, const int* flags, const long* end_indices, const long* contents );
00018 void dump_set_list( long id, const long* array, long count );
00019 void dump_set_ranges( long id, const long* array, long count );
00020 
00021 int WIDTH;
00022 
00023 int main( int argc, char* argv[] )
00024 {
00025     mhdf_Status status;
00026     mhdf_FileHandle file;
00027     unsigned long max_id;
00028 
00029     if( argc != 2 )
00030     {
00031         fprintf( stderr, "Usage: %s <input_file>\n", argv[0] );
00032         return 1;
00033     }
00034 
00035     file = mhdf_openFile( argv[1], 0, &max_id, -1, &status );
00036     CHECK;
00037 
00038     WIDTH = 1;
00039     while( max_id >= 10 )
00040     {
00041         max_id /= 10;
00042         ++WIDTH;
00043     }
00044     dump_sets( file );
00045     mhdf_closeFile( file, &status );
00046     CHECK;
00047     return 0;
00048 }
00049 
00050 void dump_sets( mhdf_FileHandle file )
00051 {
00052     long *end_indices, *data, *new_data;
00053     int* flags;
00054     long num_sets, first, num_data;
00055     hid_t meta, handle;
00056     mhdf_Status status;
00057 
00058     meta = mhdf_openSetMeta( file, &num_sets, &first, &status );
00059     CHECK;
00060     end_indices = malloc( num_sets * sizeof( long ) );
00061 
00062     printf( "\nSet IDs: %ld - %ld\n", first, first + num_sets - 1 );
00063 
00064     puts( "\nSets:\n" );
00065     flags = malloc( num_sets * sizeof( int ) );
00066     mhdf_readSetFlags( meta, 0, num_sets, H5T_NATIVE_INT, flags, &status );
00067     CHECK;
00068     mhdf_readSetContentEndIndices( meta, 0, num_sets, H5T_NATIVE_LONG, end_indices, &status );
00069     CHECK;
00070     handle = mhdf_openSetData( file, &num_data, &status );
00071     CHECK;
00072     data = malloc( num_data * sizeof( long ) );
00073     mhdf_readSetData( handle, 0, num_data, H5T_NATIVE_LONG, data, &status );
00074     CHECK;
00075     mhdf_closeData( file, handle, &status );
00076     CHECK;
00077     dump_set_contents( first, num_sets, flags, end_indices, data );
00078     free( flags );
00079 
00080     puts( "\nSet Children:\n" );
00081     mhdf_readSetChildEndIndices( meta, 0, num_sets, H5T_NATIVE_LONG, end_indices, &status );
00082     CHECK;
00083     handle = mhdf_openSetChildren( file, &num_data, &status );
00084     CHECK;
00085     new_data = realloc( data, num_data * sizeof( long ) );
00086     if( !new_data )
00087     {
00088         fprintf( stderr, "dump_sets.c::dump_sets(): reallocation of data failed\n" );
00089         free( data );
00090         free( end_indices );
00091         mhdf_closeData( file, handle, &status );
00092         CHECK;
00093         mhdf_closeData( file, meta, &status );
00094         CHECK;
00095         return;
00096     }
00097     else
00098         data = new_data;
00099     mhdf_readSetParentsChildren( handle, 0, num_data, H5T_NATIVE_LONG, data, &status );
00100     CHECK;
00101     mhdf_closeData( file, handle, &status );
00102     CHECK;
00103     dump_set_contents( first, num_sets, NULL, end_indices, data );
00104 
00105     puts( "\nSet Parents:\n" );
00106     mhdf_readSetParentEndIndices( meta, 0, num_sets, H5T_NATIVE_LONG, end_indices, &status );
00107     CHECK;
00108     handle = mhdf_openSetParents( file, &num_data, &status );
00109     CHECK;
00110     new_data = realloc( data, num_data * sizeof( long ) );
00111     if( !new_data )
00112     {
00113         fprintf( stderr, "dump_sets.c::dump_sets(): reallocation of data failed\n" );
00114         free( data );
00115         free( end_indices );
00116         mhdf_closeData( file, handle, &status );
00117         CHECK;
00118         mhdf_closeData( file, meta, &status );
00119         CHECK;
00120         return;
00121     }
00122     else
00123         data = new_data;
00124     mhdf_readSetParentsChildren( handle, 0, num_data, H5T_NATIVE_LONG, data, &status );
00125     CHECK;
00126     mhdf_closeData( file, handle, &status );
00127     CHECK;
00128     dump_set_contents( first, num_sets, NULL, end_indices, data );
00129 
00130     free( end_indices );
00131     free( data );
00132     mhdf_closeData( file, meta, &status );
00133     CHECK;
00134 }
00135 
00136 void dump_set_contents( long first, long num_sets, const int* flags, const long* end_indices, const long* contents )
00137 {
00138     long prev = -1;
00139     long i, end;
00140 
00141     for( i = 0; i < num_sets; ++i )
00142     {
00143         end = end_indices[i];
00144         if( end < prev )
00145         {
00146             fprintf( stderr, "Invalid end index for set %ld (ID %ld): %ld following %ld\n", i, first + i, end, prev );
00147             exit( 2 );
00148         }
00149         if( flags && ( flags[i] & mhdf_SET_RANGE_BIT ) )
00150         {
00151             dump_set_ranges( i + first, contents + prev + 1, end - prev );
00152         }
00153         else
00154         {
00155             dump_set_list( i + first, contents + prev + 1, end - prev );
00156         }
00157         prev = end;
00158     }
00159 }
00160 
00161 void dump_set_list( long id, const long* array, long count )
00162 {
00163     long i;
00164 
00165     if( !count ) return; /* skip empty sets */
00166 
00167     printf( "%*ld: %ld", WIDTH, id, array[0] );
00168     for( i = 1; i < count; ++i )
00169         printf( ", %ld", array[i] );
00170     printf( " (%ld)\n", count );
00171 }
00172 
00173 void dump_set_ranges( long id, const long* array, long count )
00174 {
00175     long i, n = 0;
00176 
00177     if( !count ) return; /* skip empty sets */
00178     printf( "%*ld:", WIDTH, id );
00179     if( count % 2 )
00180     {
00181         puts( " <INVALID DATA LENGTH FOR RANGED LIST>\n" );
00182         return;
00183     }
00184 
00185     for( i = 0; i < count; i += 2 )
00186     {
00187         if( array[i + 1] > 1 )
00188             printf( " %ld-%ld", array[i], array[i] + array[i + 1] - 1 );
00189         else
00190             printf( " %ld", array[i] );
00191         if( i + 2 < count ) putchar( ',' );
00192         n += array[i + 1];
00193     }
00194     printf( " (%ld)\n", n );
00195 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines