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