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