MOAB: Mesh Oriented datABase  (version 5.2.1)
mhdf_public_test.cpp
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include "moab/mhdf_public.h"
00006 
00007 static int print_file_summary( MHDF_FileDesc* data );
00008 
00009 int main( int argc, char* argv[] )
00010 {
00011     int result;
00012     MHDF_FileHandle file;
00013     MHDF_Status status;
00014     unsigned long max_id;
00015     MHDF_FileDesc* data;
00016 
00017     if( argc != 2 )
00018     {
00019         fprintf( stderr, "Usage: %s <filename>\n", argv[0] );
00020         return 0;
00021     }
00022 
00023     file = mhdf_openFile( argv[1], 0, &max_id, -1, &status );
00024     if( mhdf_isError( &status ) )
00025     {
00026         fprintf( stderr, "%s: %s\n", argv[1], mhdf_message( &status ) );
00027         return 1;
00028     }
00029 
00030     data = mhdf_getFileSummary( file, H5T_NATIVE_ULONG, &status, 1 );
00031     if( mhdf_isError( &status ) )
00032     {
00033         fprintf( stderr, "%s: %s\n", argv[1], mhdf_message( &status ) );
00034         return 1;
00035     }
00036 
00037     mhdf_closeFile( file, &status );
00038 
00039     printf( "%s:\n", argv[1] );
00040     result = print_file_summary( data );
00041     free( data );
00042     return result;
00043 }
00044 
00045 static void print_ent_desc( const char* name, const char* subname, MHDF_EntDesc* data, const char* vals_label,
00046                             const char* extra_label, MHDF_FileDesc* all )
00047 {
00048     int i, len = 10;
00049 
00050     if( vals_label && (int)strlen( vals_label ) > len ) len = strlen( vals_label );
00051     if( extra_label && (int)strlen( extra_label ) > len ) len = strlen( extra_label );
00052 
00053     if( subname )
00054         printf( "    %s (%s):\n", name, subname );
00055     else
00056         printf( "    %s:\n", name );
00057 
00058     if( vals_label ) printf( "      %-*s: %d\n", len, vals_label, data->vals_per_ent );
00059 
00060     printf( "      %-*s: %ld [%ld - %ld]\n", len, "entities", data->count, data->start_id,
00061             data->start_id + data->count - 1 );
00062 
00063     if( extra_label ) printf( "      %-*s\n", len, extra_label );
00064 
00065     if( !data->num_dense_tags ) return;
00066 
00067     printf( "      %-*s: \"%s\"", len, "dense tags", all->tags[data->dense_tag_indices[0]].name );
00068     for( i = 1; i < data->num_dense_tags; ++i )
00069         printf( ", \"%s\"", all->tags[data->dense_tag_indices[i]].name );
00070     printf( "\n" );
00071 }
00072 
00073 static void print_elem_desc( MHDF_ElemDesc* data, MHDF_FileDesc* all )
00074 {
00075     const char* adj = data->have_adj ? "adjacencies" : "no adjencies";
00076     print_ent_desc( data->handle, data->type, &data->desc, "nodes per element", adj, all );
00077 }
00078 
00079 static const char* tag_type_name( MHDF_TagDataType type )
00080 {
00081     static const char opaque[]  = "opaque";
00082     static const char integer[] = "integer";
00083     static const char real[]    = "real";
00084     static const char bits[]    = "bit field";
00085     static const char boolean[] = "boolean";
00086     static const char id[]      = "entity id";
00087     static const char unknown[] = "(UNKNOWN TYPE ID)";
00088     switch( type )
00089     {
00090         case mhdf_OPAQUE:
00091             return opaque;
00092         case mhdf_INTEGER:
00093             return integer;
00094         case mhdf_FLOAT:
00095             return real;
00096         case mhdf_BITFIELD:
00097             return bits;
00098         case mhdf_BOOLEAN:
00099             return boolean;
00100         case mhdf_ENTITY_ID:
00101             return id;
00102     }
00103     return unknown;
00104 }
00105 
00106 static const char* string_tag_value( const void* value, MHDF_TagDataType type, int size )
00107 {
00108     static char buffer[1024];
00109     const char* data = (const char*)value;
00110     char* offset     = buffer;
00111     int print, i;
00112     const int* intptr          = (const int*)value;
00113     const double* dblptr       = (const double*)value;
00114     const unsigned long* idptr = (const unsigned long*)value;
00115 
00116     if( size <= 0 )
00117     {
00118         *buffer = '\0';
00119         return buffer;
00120     }
00121 
00122     switch( type )
00123     {
00124         case mhdf_OPAQUE:
00125             print = 1;
00126             for( i = 0; i < size; ++i )
00127                 if( !isprint( data[i] ) ) print = 0;
00128             if( print )
00129             {
00130                 offset[0] = '"';
00131                 memcpy( offset + 1, data, size );
00132                 offset[size + 1] = '"';
00133                 offset[size + 2] = '\0';
00134                 offset += size + 2;
00135             }
00136             else
00137             {
00138                 strcpy( offset, "0x" );
00139                 offset += 2;
00140                 for( i = 0; i < size; ++i )
00141                     offset += sprintf( offset, "%02x", (unsigned int)data[i] );
00142             }
00143             break;
00144         case mhdf_INTEGER:
00145             if( size == 1 ) { offset += sprintf( offset, "%d", intptr[0] ); }
00146             else
00147             {
00148                 offset += sprintf( offset, "{%d", intptr[0] );
00149                 for( i = 1; i < size; ++i )
00150                     offset += sprintf( offset, ",%d", intptr[i] );
00151                 offset += sprintf( offset, "}" );
00152             }
00153             break;
00154         case mhdf_FLOAT:
00155             if( size == 1 ) { offset += sprintf( offset, "%g", dblptr[0] ); }
00156             else
00157             {
00158                 offset += sprintf( offset, "{%g", dblptr[0] );
00159                 for( i = 1; i < size; ++i )
00160                     offset += sprintf( offset, ",%g", dblptr[i] );
00161                 offset += sprintf( offset, "}" );
00162             }
00163             break;
00164         case mhdf_BITFIELD:
00165             if( size > 8 )
00166                 offset += sprintf( offset, "(more than 8 bits)" );
00167             else
00168             {
00169                 for( i = size - 1; i >= 0; --i )
00170                     *( offset++ ) = (char)( *data & ( 1 << i ) ? '1' : '0' );
00171                 *offset = '\0';
00172             }
00173             break;
00174         case mhdf_BOOLEAN:
00175             if( size == 1 ) { offset += sprintf( offset, "%s", data[0] ? "true" : "false" ); }
00176             else
00177             {
00178                 offset += sprintf( offset, "{%s", data[0] ? "true" : "false" );
00179                 for( i = 1; i < size; ++i )
00180                     offset += sprintf( offset, ",%s", data[i] ? "true" : "false" );
00181                 offset += sprintf( offset, "}" );
00182             }
00183             break;
00184         case mhdf_ENTITY_ID:
00185             if( size == 1 ) { offset += sprintf( offset, "%lu", idptr[0] ); }
00186             else
00187             {
00188                 offset += sprintf( offset, "{%lu", idptr[0] );
00189                 for( i = 1; i < size; ++i )
00190                     offset += sprintf( offset, ",%lu", idptr[i] );
00191                 offset += sprintf( offset, "}" );
00192             }
00193             break;
00194         default:
00195             strcpy( buffer, "(unknown data type)" );
00196             break;
00197     }
00198 
00199     return buffer;
00200 }
00201 
00202 static const char* ent_desc_name( MHDF_FileDesc* all, int idx )
00203 {
00204     static const char nodes[]   = "Nodes";
00205     static const char sets[]    = "Sets";
00206     static const char invalid[] = "<INVALID INDEX!>";
00207     if( idx == -2 ) return sets;
00208     if( idx == -1 ) return nodes;
00209     if( idx >= all->num_elem_desc || idx < -2 ) return invalid;
00210     return all->elems[idx].handle;
00211 }
00212 
00213 static void print_tag_desc( MHDF_TagDesc* data, MHDF_FileDesc* all )
00214 {
00215     int i, width = 8;
00216 
00217     printf( "    \"%s\":\n", data->name );
00218     printf( "      %-*s: %s\n", width, "type", tag_type_name( data->type ) );
00219     if( data->size < 0 )
00220         printf( "      %-*s: (variable)\n", width, "size" );
00221     else
00222         printf( "      %-*s: %d (%d bytes)\n", width, "size", data->size, data->bytes );
00223     printf( "      %-*s: %x\n", width, "flags", data->storage );
00224     if( data->default_value )
00225         printf( "      %-*s: %s\n", width, "default",
00226                 string_tag_value( data->default_value, data->type, data->default_value_size ) );
00227     if( data->global_value )
00228         printf( "      %-*s: %s\n", width, "mesh val",
00229                 string_tag_value( data->global_value, data->type, data->global_value_size ) );
00230     if( data->have_sparse )
00231     {
00232         printf( "      %-*s: (sparse)", width, "tables" );
00233         for( i = 0; i < data->num_dense_indices; ++i )
00234             printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
00235     }
00236     else if( data->num_dense_indices )
00237     {
00238         printf( "      %-*s: %s", width, "tables", ent_desc_name( all, data->dense_elem_indices[0] ) );
00239         for( i = 1; i < data->num_dense_indices; ++i )
00240             printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
00241     }
00242     else
00243     {
00244         printf( "      %-*s: (none)", width, "tables" );
00245     }
00246     printf( "\n" );
00247 }
00248 
00249 static int print_file_summary( MHDF_FileDesc* data )
00250 {
00251     int i;
00252 
00253     printf( "  Entities:\n" );
00254     print_ent_desc( "Nodes", NULL, &data->nodes, "dimension", NULL, data );
00255     for( i = 0; i < data->num_elem_desc; ++i )
00256         print_elem_desc( data->elems + i, data );
00257     print_ent_desc( "Sets", NULL, &data->sets, NULL, NULL, data );
00258 
00259     printf( "  Tags:\n" );
00260     for( i = 0; i < data->num_tag_desc; ++i )
00261         print_tag_desc( data->tags + i, data );
00262 
00263     printf( "   Number partitions: %d\n", data->numEntSets[0] );
00264     for( i = 0; i < data->numEntSets[0]; i++ )
00265         printf( " set id %d value %d \n", data->defTagsEntSets[0][i], data->defTagsVals[0][i] );
00266     printf( "\n   Number material sets: %d\n", data->numEntSets[1] );
00267     for( i = 0; i < data->numEntSets[1]; i++ )
00268         printf( " set id %d value %d \n", data->defTagsEntSets[1][i], data->defTagsVals[1][i] );
00269     printf( "\n   Number neumann sets: %d\n", data->numEntSets[2] );
00270     for( i = 0; i < data->numEntSets[2]; i++ )
00271         printf( " set id %d value %d \n", data->defTagsEntSets[2][i], data->defTagsVals[2][i] );
00272     printf( "\n   Number dirichlet sets: %d\n", data->numEntSets[3] );
00273     for( i = 0; i < data->numEntSets[3]; i++ )
00274         printf( " set id %d value %d \n", data->defTagsEntSets[3][i], data->defTagsVals[3][i] );
00275     printf( "\n   Number geometry sets: %d\n", data->numEntSets[4] );
00276     for( i = 0; i < data->numEntSets[4]; i++ )
00277         printf( " set id %d value %d \n", data->defTagsEntSets[4][i], data->defTagsVals[4][i] );
00278 
00279     return 0;
00280 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines