1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "mhdf.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <H5Tpublic.h>

static int print_file_summary( struct mhdf_FileDesc* data );

int main( int argc, char* argv[] )
{
    int result;
    mhdf_FileHandle file;
    mhdf_Status status;
    unsigned long max_id;
    struct mhdf_FileDesc* data;

    if( argc != 2 )
    {
        fprintf( stderr, "Usage: %s <filename>\n", argv[0] );
        return 1;
    }

    file = mhdf_openFile( argv[1], 0, &max_id, -1, &status );
    if( mhdf_isError( &status ) )
    {
        fprintf( stderr, "%s: %s\n", argv[1], mhdf_message( &status ) );
        return 1;
    }

    data = mhdf_getFileSummary( file, H5T_NATIVE_ULONG, &status, 0 ); /* no extra set info here*/
    if( mhdf_isError( &status ) )
    {
        fprintf( stderr, "%s: %s\n", argv[1], mhdf_message( &status ) );
        return 1;
    }

    mhdf_closeFile( file, &status );

    printf( "%s:\n", argv[1] );
    result = print_file_summary( data );
    free( data );
    return result;
}

static void print_ent_desc( const char* name,
                            const char* subname,
                            struct mhdf_EntDesc* data,
                            const char* vals_label,
                            const char* extra_label,
                            struct mhdf_FileDesc* all )
{
    int i, len = 10;

    if( vals_label && (int)strlen( vals_label ) > len ) len = strlen( vals_label );
    if( extra_label && (int)strlen( extra_label ) > len ) len = strlen( extra_label );

    if( subname )
        printf( "    %s (%s):\n", name, subname );
    else
        printf( "    %s:\n", name );

    if( vals_label ) printf( "      %-*s: %d\n", len, vals_label, data->vals_per_ent );

    printf( "      %-*s: %ld [%ld - %ld]\n", len, "entities", data->count, data->start_id,
            data->start_id + data->count - 1 );

    if( extra_label ) printf( "      %-*s\n", len, extra_label );

    if( !data->num_dense_tags ) return;

    printf( "      %-*s: \"%s\"", len, "dense tags", all->tags[data->dense_tag_indices[0]].name );
    for( i = 1; i < data->num_dense_tags; ++i )
        printf( ", \"%s\"", all->tags[data->dense_tag_indices[i]].name );
    printf( "\n" );
}

static void print_elem_desc( struct mhdf_ElemDesc* data, struct mhdf_FileDesc* all )
{
    const char* adj = data->have_adj ? "adjacencies" : "no adjencies";
    print_ent_desc( data->handle, data->type, &data->desc, "nodes per element", adj, all );
}

static const char* tag_type_name( enum mhdf_TagDataType type )
{
    static const char opaque[]  = "opaque";
    static const char integer[] = "integer";
    static const char real[]    = "real";
    static const char bits[]    = "bit field";
    static const char boolean[] = "boolean";
    static const char id[]      = "entity id";
    static const char unknown[] = "(UNKNOWN TYPE ID)";
    switch( type )
    {
        case mhdf_OPAQUE:
            return opaque;
        case mhdf_INTEGER:
            return integer;
        case mhdf_FLOAT:
            return real;
        case mhdf_BITFIELD:
            return bits;
        case mhdf_BOOLEAN:
            return boolean;
        case mhdf_ENTITY_ID:
            return id;
    }
    return unknown;
}

static const char* string_tag_value( const void* value, enum mhdf_TagDataType type, int size )
{
    static char buffer[1024];
    const char* data = value;
    char* offset     = buffer;
    int print, i;
    const int* intptr          = value;
    const double* dblptr       = value;
    const unsigned long* idptr = value;

    if( size <= 0 )
    {
        *buffer = '\0';
        return buffer;
    }

    switch( type )
    {
        case mhdf_OPAQUE:
            print = 1;
            for( i = 0; i < size; ++i )
                if( !isprint( data[i] ) ) print = 0;
            if( print )
            {
                offset[0] = '"';
                memcpy( offset + 1, data, size );
                offset[size + 1] = '"';
                offset[size + 2] = '\0';
                offset += size + 2;<--- Variable 'offset' is assigned a value that is never used.
            }
            else
            {
                strcpy( offset, "0x" );
                offset += 2;
                for( i = 0; i < size; ++i )
                    offset += sprintf( offset, "%02x", (unsigned int)data[i] );
            }
            break;
        case mhdf_INTEGER:
            if( size == 1 )
            {
                offset += sprintf( offset, "%d", intptr[0] );
            }
            else
            {
                offset += sprintf( offset, "{%d", intptr[0] );
                for( i = 1; i < size; ++i )
                    offset += sprintf( offset, ",%d", intptr[i] );
                offset += sprintf( offset, "}" );
            }
            break;
        case mhdf_FLOAT:
            if( size == 1 )
            {
                offset += sprintf( offset, "%g", dblptr[0] );
            }
            else
            {
                offset += sprintf( offset, "{%g", dblptr[0] );
                for( i = 1; i < size; ++i )
                    offset += sprintf( offset, ",%g", dblptr[i] );
                offset += sprintf( offset, "}" );
            }
            break;
        case mhdf_BITFIELD:
            if( size > 8 )
                offset += sprintf( offset, "(more than 8 bits)" );
            else
            {
                for( i = size - 1; i >= 0; --i )
                    *( offset++ ) = (char)( *data & ( 1 << i ) ? '1' : '0' );
                *offset = '\0';
            }
            break;
        case mhdf_BOOLEAN:
            if( size == 1 )
            {
                offset += sprintf( offset, "%s", data[0] ? "true" : "false" );
            }
            else
            {
                offset += sprintf( offset, "{%s", data[0] ? "true" : "false" );
                for( i = 1; i < size; ++i )
                    offset += sprintf( offset, ",%s", data[i] ? "true" : "false" );
                offset += sprintf( offset, "}" );
            }
            break;
        case mhdf_ENTITY_ID:
            if( size == 1 )
            {
                offset += sprintf( offset, "%lu", idptr[0] );
            }
            else
            {
                offset += sprintf( offset, "{%lu", idptr[0] );
                for( i = 1; i < size; ++i )
                    offset += sprintf( offset, ",%lu", idptr[i] );
                offset += sprintf( offset, "}" );
            }
            break;
        default:
            strcpy( buffer, "(unknown data type)" );
            break;
    }

    return buffer;
}

static const char* ent_desc_name( struct mhdf_FileDesc* all, int idx )
{
    static const char nodes[]   = "Nodes";
    static const char sets[]    = "Sets";
    static const char invalid[] = "<INVALID INDEX!>";
    if( idx == -2 ) return sets;
    if( idx == -1 ) return nodes;
    if( idx >= all->num_elem_desc || idx < -2 ) return invalid;
    return all->elems[idx].handle;
}

static void print_tag_desc( struct mhdf_TagDesc* data, struct mhdf_FileDesc* all )
{
    int i, width = 8;

    printf( "    \"%s\":\n", data->name );
    printf( "      %-*s: %s\n", width, "type", tag_type_name( data->type ) );
    if( data->size < 0 )
        printf( "      %-*s: (variable)\n", width, "size" );
    else
        printf( "      %-*s: %d (%d bytes)\n", width, "size", data->size, data->bytes );
    printf( "      %-*s: %x\n", width, "flags", data->storage );
    if( data->default_value )
        printf( "      %-*s: %s\n", width, "default",
                string_tag_value( data->default_value, data->type, data->default_value_size ) );
    if( data->global_value )
        printf( "      %-*s: %s\n", width, "mesh val",
                string_tag_value( data->global_value, data->type, data->global_value_size ) );
    if( data->have_sparse )
    {
        printf( "      %-*s: (sparse)", width, "tables" );
        for( i = 0; i < data->num_dense_indices; ++i )
            printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
    }
    else if( data->num_dense_indices )
    {
        printf( "      %-*s: %s", width, "tables", ent_desc_name( all, data->dense_elem_indices[0] ) );
        for( i = 1; i < data->num_dense_indices; ++i )
            printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
    }
    else
    {
        printf( "      %-*s: (none)", width, "tables" );
    }
    printf( "\n" );
}

static int print_file_summary( struct mhdf_FileDesc* data )
{
    int i;

    printf( "  Entities:\n" );
    print_ent_desc( "Nodes", NULL, &data->nodes, "dimension", NULL, data );
    for( i = 0; i < data->num_elem_desc; ++i )
        print_elem_desc( data->elems + i, data );
    print_ent_desc( "Sets", NULL, &data->sets, NULL, NULL, data );

    printf( "  Tags:\n" );
    for( i = 0; i < data->num_tag_desc; ++i )
        print_tag_desc( data->tags + i, data );

    return 0;
}