1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "moab/mhdf_public.h"
6 
7 static int print_file_summary( MHDF_FileDesc* data );
8 
main(int argc,char * argv[])9 int main( int argc, char* argv[] )
10 {
11   int result;
12   MHDF_FileHandle file;
13   MHDF_Status status;
14   unsigned long max_id;
15   MHDF_FileDesc* data;
16 
17   if (argc != 2) {
18     fprintf( stderr,"Usage: %s <filename>\n", argv[0] );
19     return 0;
20   }
21 
22   file = mhdf_openFile( argv[1], 0, &max_id, -1, &status );
23   if (mhdf_isError( &status )) {
24     fprintf( stderr,"%s: %s\n", argv[1], mhdf_message( &status ) );
25     return 1;
26   }
27 
28   data = mhdf_getFileSummary( file, H5T_NATIVE_ULONG, &status, 1 );
29   if (mhdf_isError( &status )) {
30     fprintf( stderr,"%s: %s\n", argv[1], mhdf_message( &status ) );
31     return 1;
32   }
33 
34   mhdf_closeFile( file, &status );
35 
36   printf( "%s:\n", argv[1] );
37   result = print_file_summary( data );
38   free( data );
39   return result;
40 }
41 
print_ent_desc(const char * name,const char * subname,MHDF_EntDesc * data,const char * vals_label,const char * extra_label,MHDF_FileDesc * all)42 static void print_ent_desc( const char* name,
43                             const char* subname,
44                             MHDF_EntDesc* data,
45                             const char* vals_label,
46                             const char* extra_label,
47                             MHDF_FileDesc* all )
48 {
49   int i, len = 10;
50 
51   if (vals_label && (int)strlen(vals_label) > len)
52     len = strlen(vals_label);
53   if (extra_label && (int)strlen(extra_label) > len)
54     len = strlen(extra_label);
55 
56   if (subname)
57     printf( "    %s (%s):\n", name, subname );
58   else
59     printf( "    %s:\n", name );
60 
61   if (vals_label)
62     printf( "      %-*s: %d\n", len, vals_label, data->vals_per_ent );
63 
64   printf( "      %-*s: %ld [%ld - %ld]\n", len, "entities", data->count, data->start_id, data->start_id + data->count - 1 );
65 
66   if (extra_label)
67     printf( "      %-*s\n", len, extra_label );
68 
69   if (!data->num_dense_tags)
70     return;
71 
72   printf( "      %-*s: \"%s\"", len, "dense tags", all->tags[data->dense_tag_indices[0]].name );
73   for (i = 1; i < data->num_dense_tags; ++i)
74     printf( ", \"%s\"", all->tags[data->dense_tag_indices[i]].name );
75   printf ("\n");
76 }
77 
print_elem_desc(MHDF_ElemDesc * data,MHDF_FileDesc * all)78 static void print_elem_desc( MHDF_ElemDesc* data, MHDF_FileDesc* all )
79 {
80   const char* adj = data->have_adj ? "adjacencies" : "no adjencies";
81   print_ent_desc( data->handle, data->type, &data->desc, "nodes per element", adj, all );
82 }
83 
tag_type_name(MHDF_TagDataType type)84 static const char* tag_type_name( MHDF_TagDataType type )
85 {
86   static const char opaque[] = "opaque";
87   static const char integer[] = "integer";
88   static const char real[] = "real";
89   static const char bits[] = "bit field";
90   static const char boolean[] = "boolean";
91   static const char id[] = "entity id";
92   static const char unknown[] = "(UNKNOWN TYPE ID)";
93   switch (type) {
94     case mhdf_OPAQUE:    return opaque;
95     case mhdf_INTEGER:   return integer;
96     case mhdf_FLOAT:     return real;
97     case mhdf_BITFIELD:  return bits;
98     case mhdf_BOOLEAN:   return boolean;
99     case mhdf_ENTITY_ID: return id;
100   }
101   return unknown;
102 }
103 
string_tag_value(const void * value,MHDF_TagDataType type,int size)104 static const char* string_tag_value( const void* value,
105                                      MHDF_TagDataType type,
106                                      int size )
107 {
108   static char buffer[1024];
109   const char* data = (const char*) value;
110   char* offset = buffer;
111   int print, i;
112   const int* intptr = (const int *) value;
113   const double* dblptr = (const double*) value;
114   const unsigned long* idptr = (const unsigned long *) value;
115 
116 
117   if (size <= 0) {
118     *buffer = '\0';
119     return buffer;
120   }
121 
122   switch (type) {
123     case mhdf_OPAQUE:
124       print = 1;
125       for (i = 0; i < size; ++i)
126         if (!isprint(data[i]))
127           print = 0;
128       if (print) {
129         offset[0] = '"';
130         memcpy( offset+1, data, size );
131         offset[size+1] = '"';
132         offset[size+2] = '\0';
133         offset += size+2;
134       }
135       else {
136         strcpy( offset, "0x" );
137         offset += 2;
138         for (i = 0; i < size; ++i)
139           offset += sprintf( offset, "%02x", (unsigned int)data[i] );
140       }
141       break;
142     case mhdf_INTEGER:
143       if (size == 1) {
144         offset += sprintf( offset, "%d", intptr[0] );
145       }
146       else {
147         offset += sprintf( offset, "{%d", intptr[0] );
148         for (i = 1; i < size; ++i)
149           offset += sprintf( offset, ",%d", intptr[i] );
150         offset += sprintf( offset, "}" );
151       }
152       break;
153     case mhdf_FLOAT:
154       if (size == 1) {
155         offset += sprintf( offset, "%g", dblptr[0] );
156       }
157       else {
158         offset += sprintf( offset, "{%g", dblptr[0] );
159         for (i = 1; i < size; ++i)
160           offset += sprintf( offset, ",%g", dblptr[i] );
161         offset += sprintf( offset, "}" );
162       }
163       break;
164     case mhdf_BITFIELD:
165       if (size > 8)
166         offset += sprintf( offset, "(more than 8 bits)" );
167       else {
168         for (i = size - 1; i >= 0; --i)
169           *(offset++) = (char)(*data & (1<<i) ? '1' : '0');
170         *offset = '\0';
171       }
172       break;
173     case mhdf_BOOLEAN:
174       if (size == 1) {
175         offset += sprintf( offset, "%s", data[0] ? "true" : "false" );
176       }
177       else {
178         offset += sprintf( offset, "{%s", data[0] ? "true" : "false" );
179         for (i = 1; i < size; ++i)
180           offset += sprintf( offset, ",%s", data[i] ? "true" : "false" );
181         offset += sprintf( offset, "}" );
182       }
183       break;
184     case mhdf_ENTITY_ID:
185       if (size == 1) {
186         offset += sprintf( offset, "%lu", idptr[0] );
187       }
188       else {
189         offset += sprintf( offset, "{%lu", idptr[0] );
190         for (i = 1; i < size; ++i)
191           offset += sprintf( offset, ",%lu", idptr[i] );
192         offset += sprintf( offset, "}" );
193       }
194       break;
195     default:
196       strcpy( buffer, "(unknown data type)" );
197       break;
198   }
199 
200   return buffer;
201 }
202 
ent_desc_name(MHDF_FileDesc * all,int idx)203 static const char* ent_desc_name( MHDF_FileDesc* all, int idx )
204 {
205   static const char nodes[] = "Nodes";
206   static const char sets[] = "Sets";
207   static const char invalid[] = "<INVALID INDEX!>";
208   if (idx == -2) return sets;
209   if (idx == -1) return nodes;
210   if (idx >= all->num_elem_desc || idx < -2) return invalid;
211   return all->elems[idx].handle;
212 }
213 
print_tag_desc(MHDF_TagDesc * data,MHDF_FileDesc * all)214 static void print_tag_desc( MHDF_TagDesc* data, MHDF_FileDesc* all )
215 {
216   int i, width = 8;
217 
218   printf( "    \"%s\":\n", data->name );
219   printf( "      %-*s: %s\n", width, "type", tag_type_name( data->type ) );
220   if (data->size < 0)
221     printf( "      %-*s: (variable)\n", width, "size" );
222   else
223     printf( "      %-*s: %d (%d bytes)\n", width, "size", data->size, data->bytes );
224   printf( "      %-*s: %x\n", width, "flags", data->storage );
225   if (data->default_value)
226     printf( "      %-*s: %s\n", width, "default",
227       string_tag_value( data->default_value, data->type, data->default_value_size ) );
228   if (data->global_value)
229     printf( "      %-*s: %s\n", width, "mesh val",
230       string_tag_value( data->global_value, data->type, data->global_value_size ) );
231   if (data->have_sparse) {
232     printf( "      %-*s: (sparse)", width, "tables" );
233     for (i = 0; i < data->num_dense_indices; ++i)
234       printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
235   }
236   else if (data->num_dense_indices) {
237     printf( "      %-*s: %s", width, "tables", ent_desc_name( all, data->dense_elem_indices[0] ) );
238     for (i = 1; i < data->num_dense_indices; ++i)
239       printf( ", %s", ent_desc_name( all, data->dense_elem_indices[i] ) );
240   }
241   else {
242     printf( "      %-*s: (none)", width, "tables" );
243   }
244   printf( "\n" );
245 }
246 
print_file_summary(MHDF_FileDesc * data)247 static int print_file_summary( MHDF_FileDesc* data )
248 {
249   int i;
250 
251   printf( "  Entities:\n" );
252   print_ent_desc( "Nodes", NULL, &data->nodes, "dimension", NULL, data );
253   for (i = 0; i < data->num_elem_desc; ++i)
254     print_elem_desc( data->elems + i, data );
255   print_ent_desc( "Sets", NULL, &data->sets, NULL, NULL, data );
256 
257   printf( "  Tags:\n" );
258   for (i = 0; i < data->num_tag_desc; ++i)
259     print_tag_desc( data->tags + i, data );
260 
261   printf( "   Number partitions: %d\n", data->numEntSets[0]);
262   for (i=0; i<data->numEntSets[0]; i++)
263     printf( " set id %d value %d \n", data->defTagsEntSets[0][i],data->defTagsVals[0][i]);
264   printf( "\n   Number material sets: %d\n", data->numEntSets[1]);
265   for (i=0; i<data->numEntSets[1]; i++)
266     printf( " set id %d value %d \n", data->defTagsEntSets[1][i],data->defTagsVals[1][i]);
267   printf( "\n   Number neumann sets: %d\n", data->numEntSets[2]);
268   for (i=0; i<data->numEntSets[2]; i++)
269     printf( " set id %d value %d \n", data->defTagsEntSets[2][i],data->defTagsVals[2][i]);
270   printf( "\n   Number dirichlet sets: %d\n", data->numEntSets[3]);
271   for (i=0; i<data->numEntSets[3]; i++)
272     printf( " set id %d value %d \n", data->defTagsEntSets[3][i],data->defTagsVals[3][i]);
273   printf( "\n   Number geometry sets: %d\n", data->numEntSets[4]);
274   for (i=0; i<data->numEntSets[4]; i++)
275     printf( " set id %d value %d \n", data->defTagsEntSets[4][i],data->defTagsVals[4][i]);
276 
277   return 0;
278 }
279