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