summaryrefslogtreecommitdiffstats
path: root/generic/tkGeometry.c
blob: 5c862e5f43a80eaa90897f3ef6768369d978df4e (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
 * tkGeometry.c --
 *
 *	This file contains generic Tk code for geometry management (stuff
 *	that's used by all geometry managers).
 *
 * Copyright (c) 1990-1994 The Regents of the University of California.
 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tkGeometry.c,v 1.8 2007/01/03 05:06:26 nijtmans Exp $
 */

#include "tkPort.h"
#include "tkInt.h"

/*
 * Data structures of the following type are used by Tk_MaintainGeometry. For
 * each slave managed by Tk_MaintainGeometry, there is one of these structures
 * associated with its master.
 */

typedef struct MaintainSlave {
    Tk_Window slave;		/* The slave window being positioned. */
    Tk_Window master;		/* The master that determines slave's
				 * position; it must be a descendant of
				 * slave's parent. */
    int x, y;			/* Desired position of slave relative to
				 * master. */
    int width, height;		/* Desired dimensions of slave. */
    struct MaintainSlave *nextPtr;
				/* Next in list of Maintains associated with
				 * master. */
} MaintainSlave;

/*
 * For each window that has been specified as a master to Tk_MaintainGeometry,
 * there is a structure of the following type:
 */

typedef struct MaintainMaster {
    Tk_Window ancestor;		/* The lowest ancestor of this window for
				 * which we have *not* created a
				 * StructureNotify handler. May be the same as
				 * the window itself. */
    int checkScheduled;		/* Non-zero means that there is already a call
				 * to MaintainCheckProc scheduled as an idle
				 * handler. */
    MaintainSlave *slavePtr;	/* First in list of all slaves associated with
				 * this master. */
} MaintainMaster;

/*
 * Prototypes for static procedures in this file:
 */

static void		MaintainCheckProc(ClientData clientData);
static void		MaintainMasterProc(ClientData clientData,
			    XEvent *eventPtr);
static void		MaintainSlaveProc(ClientData clientData,
			    XEvent *eventPtr);

/*
 *--------------------------------------------------------------
 *
 * Tk_ManageGeometry --
 *
 *	Arrange for a particular procedure to manage the geometry of a given
 *	slave window.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Proc becomes the new geometry manager for tkwin, replacing any
 *	previous geometry manager. The geometry manager will be notified (by
 *	calling procedures in *mgrPtr) when interesting things happen in the
 *	future. If there was an existing geometry manager for tkwin different
 *	from the new one, it is notified by calling its lostSlaveProc.
 *
 *--------------------------------------------------------------
 */

void
Tk_ManageGeometry(tkwin, mgrPtr, clientData)
    Tk_Window tkwin;		/* Window whose geometry is to be managed by
				 * proc. */
    CONST Tk_GeomMgr *mgrPtr;	/* Static structure describing the geometry
				 * manager. This structure must never go
				 * away. */
    ClientData clientData;	/* Arbitrary one-word argument to pass to
				 * geometry manager procedures. */
{
    register TkWindow *winPtr = (TkWindow *) tkwin;

    if ((winPtr->geomMgrPtr != NULL) && (mgrPtr != NULL)
	    && ((winPtr->geomMgrPtr != mgrPtr)
		|| (winPtr->geomData != clientData))
	    && (winPtr->geomMgrPtr->lostSlaveProc != NULL)) {
	(*winPtr->geomMgrPtr->lostSlaveProc)(winPtr->geomData, tkwin);
    }

    winPtr->geomMgrPtr = mgrPtr;
    winPtr->geomData = clientData;
}

/*
 *--------------------------------------------------------------
 *
 * Tk_GeometryRequest --
 *
 *	This procedure is invoked by widget code to indicate its preferences
 *	about the size of a window it manages. In general, widget code should
 *	call this procedure rather than Tk_ResizeWindow.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The geometry manager for tkwin (if any) is invoked to handle the
 *	request. If possible, it will reconfigure tkwin and/or other windows
 *	to satisfy the request. The caller gets no indication of success or
 *	failure, but it will get X events if the window size was actually
 *	changed.
 *
 *--------------------------------------------------------------
 */

void
Tk_GeometryRequest(tkwin, reqWidth, reqHeight)
    Tk_Window tkwin;		/* Window that geometry information pertains
				 * to. */
    int reqWidth, reqHeight;	/* Minimum desired dimensions for window, in
				 * pixels. */
{
    register TkWindow *winPtr = (TkWindow *) tkwin;

    /*
     * X gets very upset if a window requests a width or height of zero, so
     * rounds requested sizes up to at least 1.
     */

    if (reqWidth <= 0) {
	reqWidth = 1;
    }
    if (reqHeight <= 0) {
	reqHeight = 1;
    }
    if ((reqWidth == winPtr->reqWidth) && (reqHeight == winPtr->reqHeight)) {
	return;
    }
    winPtr->reqWidth = reqWidth;
    winPtr->reqHeight = reqHeight;
    if ((winPtr->geomMgrPtr != NULL)
	    && (winPtr->geomMgrPtr->requestProc != NULL)) {
	(*winPtr->geomMgrPtr->requestProc)(winPtr->geomData, tkwin);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_SetInternalBorderEx --
 *
 *	Notify relevant geometry managers that a window has an internal border
 *	of a given width and that child windows should not be placed on that
 *	border.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The border widths are recorded for the window, and all geometry
 *	managers of all children are notified so that can re-layout, if
 *	necessary.
 *
 *----------------------------------------------------------------------
 */

void
Tk_SetInternalBorderEx(tkwin, left, right, top, bottom)
    Tk_Window tkwin;		/* Window that will have internal border. */
    int left, right;		/* Width of internal border, in pixels. */
    int top, bottom;
{
    register TkWindow *winPtr = (TkWindow *) tkwin;
    register int changed = 0;

    if (left < 0) {
	left = 0;
    }
    if (left != winPtr->internalBorderLeft) {
	winPtr->internalBorderLeft = left;
	changed = 1;
    }

    if (right < 0) {
	right = 0;
    }
    if (right != winPtr->internalBorderRight) {
	winPtr->internalBorderRight = right;
	changed = 1;
    }

    if (top < 0) {
	top = 0;
    }
    if (top != winPtr->internalBorderTop) {
	winPtr->internalBorderTop = top;
	changed = 1;
    }

    if (bottom < 0) {
	bottom = 0;
    }
    if (bottom != winPtr->internalBorderBottom) {
	winPtr->internalBorderBottom = bottom;
	changed = 1;
    }

    /*
     * All the slaves for which this is the master window must now be
     * repositioned to take account of the new internal border width. To
     * signal all the geometry managers to do this, just resize the window to
     * its current size. The ConfigureNotify event will cause geometry
     * managers to recompute everything.
     */

    if (changed) {
	Tk_ResizeWindow(tkwin, Tk_Width(tkwin), Tk_Height(tkwin));
    }
}
/*
 *----------------------------------------------------------------------
 *
 * Tk_SetInternalBorder --
 *
 *	Notify relevant geometry managers that a window has an internal border
 *	of a given width and that child windows should not be placed on that
 *	border.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The border width is recorded for the window, and all geometry managers
 *	of all children are notified so that can re-layout, if necessary.
 *
 *----------------------------------------------------------------------
 */

void
Tk_SetInternalBorder(tkwin, width)
    Tk_Window tkwin;		/* Window that will have internal border. */
    int width;			/* Width of internal border, in pixels. */
{
    Tk_SetInternalBorderEx(tkwin, width, width, width, width);
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_SetMinimumRequestSize --
 *
 *	Notify relevant geometry managers that a window has a minimum request
 *	size.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The minimum request size is recorded for the window, and a new size is
 *	requested for the window, if necessary.
 *
 *----------------------------------------------------------------------
 */

void
Tk_SetMinimumRequestSize(tkwin, minWidth, minHeight)
    Tk_Window tkwin;		/* Window that will have internal border. */
    int minWidth, minHeight;	/* Minimum requested size, in pixels. */
{
    register TkWindow *winPtr = (TkWindow *) tkwin;

    if ((winPtr->minReqWidth == minWidth) &&
	    (winPtr->minReqHeight == minHeight)) {
	return;
    }

    winPtr->minReqWidth = minWidth;
    winPtr->minReqHeight = minHeight;

    /*
     * The changed min size may cause geometry managers to get a different
     * result, so make them recompute. To signal all the geometry managers to
     * do this, just resize the window to its current size. The
     * ConfigureNotify event will cause geometry managers to recompute
     * everything.
     */

    Tk_ResizeWindow(tkwin, Tk_Width(tkwin), Tk_Height(tkwin));
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_MaintainGeometry --
 *
 *	This procedure is invoked by geometry managers to handle slaves whose
 *	master's are not their parents. It translates the desired geometry for
 *	the slave into the coordinate system of the parent and respositions
 *	the slave if it isn't already at the right place. Furthermore, it sets
 *	up event handlers so that if the master (or any of its ancestors up to
 *	the slave's parent) is mapped, unmapped, or moved, then the slave will
 *	be adjusted to match.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Event handlers are created and state is allocated to keep track of
 *	slave. Note: if slave was already managed for master by
 *	Tk_MaintainGeometry, then the previous information is replaced with
 *	the new information. The caller must eventually call
 *	Tk_UnmaintainGeometry to eliminate the correspondence (or, the state
 *	is automatically freed when either window is destroyed).
 *
 *----------------------------------------------------------------------
 */

void
Tk_MaintainGeometry(slave, master, x, y, width, height)
    Tk_Window slave;		/* Slave for geometry management. */
    Tk_Window master;		/* Master for slave; must be a descendant of
				 * slave's parent. */
    int x, y;			/* Desired position of slave within master. */
    int width, height;		/* Desired dimensions for slave. */
{
    Tcl_HashEntry *hPtr;
    MaintainMaster *masterPtr;
    register MaintainSlave *slavePtr;
    int isNew, map;
    Tk_Window ancestor, parent;
    TkDisplay *dispPtr = ((TkWindow *) master)->dispPtr;

    if (master == Tk_Parent(slave)) {
	/*
	 * If the slave is a direct descendant of the master, don't bother
	 * setting up the extra infrastructure for management, just make a
	 * call to Tk_MoveResizeWindow; the parent/child relationship will
	 * take care of the rest.
	 */

	Tk_MoveResizeWindow(slave, x, y, width, height);

	/*
	 * Map the slave if the master is already mapped; otherwise, wait
	 * until the master is mapped later (in which case mapping the slave
	 * is taken care of elsewhere).
	 */

	if (Tk_IsMapped(master)) {
	    Tk_MapWindow(slave);
	}
	return;
    }

    if (!dispPtr->geomInit) {
	dispPtr->geomInit = 1;
	Tcl_InitHashTable(&dispPtr->maintainHashTable, TCL_ONE_WORD_KEYS);
    }

    /*
     * See if there is already a MaintainMaster structure for the master; if
     * not, then create one.
     */

    parent = Tk_Parent(slave);
    hPtr = Tcl_CreateHashEntry(&dispPtr->maintainHashTable,
	    (char *) master, &isNew);
    if (!isNew) {
	masterPtr = (MaintainMaster *) Tcl_GetHashValue(hPtr);
    } else {
	masterPtr = (MaintainMaster *) ckalloc(sizeof(MaintainMaster));
	masterPtr->ancestor = master;
	masterPtr->checkScheduled = 0;
	masterPtr->slavePtr = NULL;
	Tcl_SetHashValue(hPtr, masterPtr);
    }

    /*
     * Create a MaintainSlave structure for the slave if there isn't already
     * one.
     */

    for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
	    slavePtr = slavePtr->nextPtr) {
	if (slavePtr->slave == slave) {
	    goto gotSlave;
	}
    }
    slavePtr = (MaintainSlave *) ckalloc(sizeof(MaintainSlave));
    slavePtr->slave = slave;
    slavePtr->master = master;
    slavePtr->nextPtr = masterPtr->slavePtr;
    masterPtr->slavePtr = slavePtr;
    Tk_CreateEventHandler(slave, StructureNotifyMask, MaintainSlaveProc,
	    (ClientData) slavePtr);

    /*
     * Make sure that there are event handlers registered for all the windows
     * between master and slave's parent (including master but not slave's
     * parent). There may already be handlers for master and some of its
     * ancestors (masterPtr->ancestor tells how many).
     */

    for (ancestor = master; ancestor != parent;
	    ancestor = Tk_Parent(ancestor)) {
	if (ancestor == masterPtr->ancestor) {
	    Tk_CreateEventHandler(ancestor, StructureNotifyMask,
		    MaintainMasterProc, (ClientData) masterPtr);
	    masterPtr->ancestor = Tk_Parent(ancestor);
	}
    }

    /*
     * Fill in up-to-date information in the structure, then update the window
     * if it's not currently in the right place or state.
     */

  gotSlave:
    slavePtr->x = x;
    slavePtr->y = y;
    slavePtr->width = width;
    slavePtr->height = height;
    map = 1;
    for (ancestor = slavePtr->master; ; ancestor = Tk_Parent(ancestor)) {
	if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {
	    map = 0;
	}
	if (ancestor == parent) {
	    if ((x != Tk_X(slavePtr->slave))
		    || (y != Tk_Y(slavePtr->slave))
		    || (width != Tk_Width(slavePtr->slave))
		    || (height != Tk_Height(slavePtr->slave))) {
		Tk_MoveResizeWindow(slavePtr->slave, x, y, width, height);
	    }
	    if (map) {
		Tk_MapWindow(slavePtr->slave);
	    } else {
		Tk_UnmapWindow(slavePtr->slave);
	    }
	    break;
	}
	x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;
	y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_UnmaintainGeometry --
 *
 *	This procedure cancels a previous Tk_MaintainGeometry call, so that
 *	the relationship between slave and master is no longer maintained.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The slave is unmapped and state is released, so that slave won't track
 *	master any more. If we weren't previously managing slave relative to
 *	master, then this procedure has no effect.
 *
 *----------------------------------------------------------------------
 */

void
Tk_UnmaintainGeometry(slave, master)
    Tk_Window slave;		/* Slave for geometry management. */
    Tk_Window master;		/* Master for slave; must be a descendant of
				 * slave's parent. */
{
    Tcl_HashEntry *hPtr;
    MaintainMaster *masterPtr;
    register MaintainSlave *slavePtr, *prevPtr;
    Tk_Window ancestor;
    TkDisplay *dispPtr = ((TkWindow *) slave)->dispPtr;

    if (master == Tk_Parent(slave)) {
	/*
	 * If the slave is a direct descendant of the master,
	 * Tk_MaintainGeometry will not have set up any of the extra
	 * infrastructure. Don't even bother to look for it, just return.
	 */
	return;
    }

    if (!dispPtr->geomInit) {
	dispPtr->geomInit = 1;
	Tcl_InitHashTable(&dispPtr->maintainHashTable, TCL_ONE_WORD_KEYS);
    }

    if (!(((TkWindow *) slave)->flags & TK_ALREADY_DEAD)) {
	Tk_UnmapWindow(slave);
    }
    hPtr = Tcl_FindHashEntry(&dispPtr->maintainHashTable, (char *) master);
    if (hPtr == NULL) {
	return;
    }
    masterPtr = (MaintainMaster *) Tcl_GetHashValue(hPtr);
    slavePtr = masterPtr->slavePtr;
    if (slavePtr->slave == slave) {
	masterPtr->slavePtr = slavePtr->nextPtr;
    } else {
	for (prevPtr = slavePtr, slavePtr = slavePtr->nextPtr; ;
		prevPtr = slavePtr, slavePtr = slavePtr->nextPtr) {
	    if (slavePtr == NULL) {
		return;
	    }
	    if (slavePtr->slave == slave) {
		prevPtr->nextPtr = slavePtr->nextPtr;
		break;
	    }
	}
    }
    Tk_DeleteEventHandler(slavePtr->slave, StructureNotifyMask,
	    MaintainSlaveProc, (ClientData) slavePtr);
    ckfree((char *) slavePtr);
    if (masterPtr->slavePtr == NULL) {
	if (masterPtr->ancestor != NULL) {
	    for (ancestor = master; ; ancestor = Tk_Parent(ancestor)) {
		Tk_DeleteEventHandler(ancestor, StructureNotifyMask,
			MaintainMasterProc, (ClientData) masterPtr);
		if (ancestor == masterPtr->ancestor) {
		    break;
		}
	    }
	}
	if (masterPtr->checkScheduled) {
	    Tcl_CancelIdleCall(MaintainCheckProc, (ClientData) masterPtr);
	}
	Tcl_DeleteHashEntry(hPtr);
	ckfree((char *) masterPtr);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * MaintainMasterProc --
 *
 *	This procedure is invoked by the Tk event dispatcher in response to
 *	StructureNotify events on the master or one of its ancestors, on
 *	behalf of Tk_MaintainGeometry.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	It schedules a call to MaintainCheckProc, which will eventually caused
 *	the postions and mapped states to be recalculated for all the
 *	maintained slaves of the master. Or, if the master window is being
 *	deleted then state is cleaned up.
 *
 *----------------------------------------------------------------------
 */

static void
MaintainMasterProc(clientData, eventPtr)
    ClientData clientData;	/* Pointer to MaintainMaster structure for the
				 * master window. */
    XEvent *eventPtr;		/* Describes what just happened. */
{
    MaintainMaster *masterPtr = (MaintainMaster *) clientData;
    MaintainSlave *slavePtr;
    int done;

    if ((eventPtr->type == ConfigureNotify)
	    || (eventPtr->type == MapNotify)
	    || (eventPtr->type == UnmapNotify)) {
	if (!masterPtr->checkScheduled) {
	    masterPtr->checkScheduled = 1;
	    Tcl_DoWhenIdle(MaintainCheckProc, (ClientData) masterPtr);
	}
    } else if (eventPtr->type == DestroyNotify) {
	/*
	 * Delete all of the state associated with this master, but be careful
	 * not to use masterPtr after the last slave is deleted, since its
	 * memory will have been freed.
	 */

	done = 0;
	do {
	    slavePtr = masterPtr->slavePtr;
	    if (slavePtr->nextPtr == NULL) {
		done = 1;
	    }
	    Tk_UnmaintainGeometry(slavePtr->slave, slavePtr->master);
	} while (!done);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * MaintainSlaveProc --
 *
 *	This procedure is invoked by the Tk event dispatcher in response to
 *	StructureNotify events on a slave being managed by
 *	Tk_MaintainGeometry.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If the event is a DestroyNotify event then the Maintain state and
 *	event handlers for this slave are deleted.
 *
 *----------------------------------------------------------------------
 */

static void
MaintainSlaveProc(clientData, eventPtr)
    ClientData clientData;	/* Pointer to MaintainSlave structure for
				 * master-slave pair. */
    XEvent *eventPtr;		/* Describes what just happened. */
{
    MaintainSlave *slavePtr = (MaintainSlave *) clientData;

    if (eventPtr->type == DestroyNotify) {
	Tk_UnmaintainGeometry(slavePtr->slave, slavePtr->master);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * MaintainCheckProc --
 *
 *	This procedure is invoked by the Tk event dispatcher as an idle
 *	handler, when a master or one of its ancestors has been reconfigured,
 *	mapped, or unmapped. Its job is to scan all of the slaves for the
 *	master and reposition them, map them, or unmap them as needed to
 *	maintain their geometry relative to the master.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Slaves can get repositioned, mapped, or unmapped.
 *
 *----------------------------------------------------------------------
 */

static void
MaintainCheckProc(clientData)
    ClientData clientData;	/* Pointer to MaintainMaster structure for the
				 * master window. */
{
    MaintainMaster *masterPtr = (MaintainMaster *) clientData;
    MaintainSlave *slavePtr;
    Tk_Window ancestor, parent;
    int x, y, map;

    masterPtr->checkScheduled = 0;
    for (slavePtr = masterPtr->slavePtr; slavePtr != NULL;
	    slavePtr = slavePtr->nextPtr) {
	parent = Tk_Parent(slavePtr->slave);
	x = slavePtr->x;
	y = slavePtr->y;
	map = 1;
	for (ancestor = slavePtr->master; ; ancestor = Tk_Parent(ancestor)) {
	    if (!Tk_IsMapped(ancestor) && (ancestor != parent)) {
		map = 0;
	    }
	    if (ancestor == parent) {
		if ((x != Tk_X(slavePtr->slave))
			|| (y != Tk_Y(slavePtr->slave))) {
		    Tk_MoveWindow(slavePtr->slave, x, y);
		}
		if (map) {
		    Tk_MapWindow(slavePtr->slave);
		} else {
		    Tk_UnmapWindow(slavePtr->slave);
		}
		break;
	    }
	    x += Tk_X(ancestor) + Tk_Changes(ancestor)->border_width;
	    y += Tk_Y(ancestor) + Tk_Changes(ancestor)->border_width;
	}
    }
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */
2742' href='#n2742'>2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
/*
 * Copyright (C) 1998-2001 National Center for Supercomputing Applications
 *                         All rights reserved.
 *
 */
#include <stdio.h>
#include <stdlib.h>

#include "h5dump.h"
#include "H5private.h"
#include "h5tools.h"
#include "h5tools_utils.h"

/* module-scoped variables */
static const char  *progname = "h5dump";

static int          d_status = EXIT_SUCCESS;
static int          unamedtype = 0;    /* shared data type with no name */
static int          prefix_len = 1024;
static table_t     *group_table = NULL, *dset_table = NULL, *type_table = NULL;
static char        *prefix;

static const dump_header *dump_header_format;

/* things to display or which are set via command line parameters */
static int          display_all = TRUE;
static int          display_bb = FALSE;
static int          display_oid = FALSE;
static int          display_data = TRUE;
static int          usingdasho = FALSE;

/**
 **  Added for XML  **
 **/

/* fill_ref_path_table is called to inialize the object reference paths. */
static herr_t    fill_ref_path_table(hid_t, const char *, void UNUSED *);

/* module-scoped variables for XML option */
#define DEFAULT_DTD     "http://hdf.ncsa.uiuc.edu/DTDs/HDF5-File.dtd"

static int              doxml = 0;
static const char      *xml_dtd_uri = NULL;
static hid_t            thefile = -1;
/** end XML **/

/* internal functions */
static void      dump_oid(hid_t oid);
static void      print_enum(hid_t type);
static herr_t    dump_all(hid_t group, const char *name, void *op_data);
static char     *lookup_ref_path(hobj_ref_t *);
static void      check_compression(hid_t);
static struct ref_path_table_entry_t *ref_path_table_lookup(const char *);

/* external functions */
extern int       print_data(hid_t, hid_t, int);

static h5dump_t         dataformat = {
    0,				/*raw */

    "",				/*fmt_raw */
    "%d",			/*fmt_int */
    "%u",			/*fmt_uint */
    "%d",			/*fmt_schar */
    "%u",			/*fmt_uchar */
    "%d",			/*fmt_short */
    "%u",			/*fmt_ushort */
    "%ld",			/*fmt_long */
    "%lu",			/*fmt_ulong */
    NULL,			/*fmt_llong */
    NULL,			/*fmt_ullong */
    "%g",			/*fmt_double */
    "%g",			/*fmt_float */

    0,				/*ascii */
    0,				/*str_locale */
    0,				/*str_repeat */

    "[ ",			/*arr_pre */
    ", ",			/*arr_sep */
    " ]",			/*arr_suf */
    1,				/*arr_linebreak */

    "",				/*cmpd_name */
    ",\n",			/*cmpd_sep */
    "{\n",			/*cmpd_pre */
    "}",			/*cmpd_suf */
    "\n",			/*cmpd_end */

    ",",			/*vlen_sep */
    "(",			/*vlen_pre */
    ")",			/*vlen_suf */
    "",				/*vlen_end */

    "%s",			/*elmt_fmt */
    ",",			/*elmt_suf1 */
    " ",			/*elmt_suf2 */

    "",				/*idx_n_fmt */
    "",				/*idx_sep */
    "",				/*idx_fmt */

    80,				/*line_ncols *//*standard default columns */
    0,				/*line_per_line */
    "",				/*line_pre */
    "%s",			/*line_1st */
    "%s",			/*line_cont */
    "",				/*line_suf */
    "",				/*line_sep */
    1,				/*line_multi_new */
    "   ",			/*line_indent */

    1,				/*skip_first */

    1,				/*obj_hidefileno */
    " %lu:%lu",			/*obj_format */

    1,				/*dset_hidefileno */
    "DATASET %lu:%lu ",		/*dset_format */
    "%s",			/*dset_blockformat_pre */
    "%s",			/*dset_ptformat_pre */
    "%s",			/*dset_ptformat */
};

/**
 **  Added for XML  **
 **/
/*   
 *  Alternative formating for data dumped to XML
 *  In general, the numbers are the same, but separators
 *  except spaces are not used.
 *
 *  Some of these are not used, as some kinds of data are
 *  dumped in completely new subroutines.
 *
 *  Some of this formatting may yet need to change.
 *
 *  This table only affects XML output.
 */
static h5dump_t         xml_dataformat = {
    0,				/*raw */

    "",				/*fmt_raw */
    "%d",			/*fmt_int */
    "%u",			/*fmt_uint */
    "%d",			/*fmt_schar */
    "%u",			/*fmt_uchar */
    "%d",			/*fmt_short */
    "%u",			/*fmt_ushort */
    "%ld",			/*fmt_long */
    "%lu",			/*fmt_ulong */
    NULL,			/*fmt_llong */
    NULL,			/*fmt_ullong */
    "%g",			/*fmt_double */
    "%g",			/*fmt_float */

    0,				/*ascii */
    0,				/*str_locale */
    0,				/*str_repeat */

    " ",			/*arr_pre */
    " ",			/*arr_sep */
    " ",			/*arr_suf */
    1,				/*arr_linebreak */

    "",				/*cmpd_name */
    " ",			/*cmpd_sep */
    "",				/*cmpd_pre */
    "",				/*cmpd_suf */
    "",				/*cmpd_end */

    " ",			/*vlen_sep */
    " ",			/*vlen_pre */
    " ",			/*vlen_suf */
    "",				/*vlen_end */

    "%s",			/*elmt_fmt */
    " ",			/*elmt_suf1 */
    "",				/*elmt_suf2 */

    "",				/*idx_n_fmt */
    "",				/*idx_sep */
    "",				/*idx_fmt */

    80,				/*line_ncols *//*standard default columns */
    0,				/*line_per_line */
    "",				/*line_pre */
    "%s",			/*line_1st */
    "%s",			/*line_cont */
    "",				/*line_suf */
    "",				/*line_sep */
    1,				/*line_multi_new */
    "   ",			/*line_indent */

    1,				/*skip_first */

    1,				/*obj_hidefileno */
    " %lu:%lu",			/*obj_format */

    1,				/*dset_hidefileno */
    "DATASET %lu:%lu ",		/*dset_format */
    "%s",			/*dset_blockformat_pre */
    "%s",			/*dset_ptformat_pre */
    "%s",			/*dset_ptformat */
};

/** XML **/

static const dump_header standardformat = {
    "standardformat",		/*name */
    "HDF5",			/*fileebgin */
    "",				/*fileend */
    BOOT_BLOCK,			/*bootblockbegin */
    "",				/*bootblockend */
    GROUPNAME,			/*groupbegin */
    "",				/*groupend */
    DATASET,			/*datasetbegin */
    "",				/*datasetend */
    ATTRIBUTE,			/*attributebegin */
    "",				/*attributeend */
    DATATYPE,			/*datatypebegin */
    "",				/*datatypeend */
    DATASPACE,			/*dataspacebegin */
    "",				/*dataspaceend */
    DATA,			/*databegin */
    "",				/*dataend */
    SOFTLINK,			/*softlinkbegin */
    "",				/*softlinkend */
    SUBSET,			/*subsettingbegin */
    "",				/*subsettingend */
    START,			/*startbegin */
    "",				/*startend */
    STRIDE,			/*stridebegin */
    "",				/*strideend */
    COUNT,			/*countbegin */
    "",				/*countend */
    BLOCK,			/*blockbegin */
    "",				/*blockend */

    "{",			/*fileblockbegin */
    "}",			/*fileblockend */
    "{",			/*bootblockblockbegin */
    "}",			/*bootblockblockend */
    "{",			/*groupblockbegin */
    "}",			/*groupblockend */
    "{",			/*datasetblockbegin */
    "}",			/*datasetblockend */
    "{",			/*attributeblockbegin */
    "}",			/*attributeblockend */
    "",				/*datatypeblockbegin */
    "",				/*datatypeblockend */
    "",				/*dataspaceblockbegin */
    "",				/*dataspaceblockend */
    "{",			/*datablockbegin */
    "}",			/*datablockend */
    "{",			/*softlinkblockbegin */
    "}",			/*softlinkblockend */
    "{",			/*strblockbegin */
    "}",			/*strblockend */
    "{",			/*enumblockbegin */
    "}",			/*enumblockend */
    "{",			/*structblockbegin */
    "}",			/*structblockend */
    "{",			/*vlenblockbegin */
    "}",			/*vlenblockend */
    "{",                        /*subsettingblockbegin */
    "}",                        /*subsettingblockend */
    "(",                        /*startblockbegin */
    ");",                       /*startblockend */
    "(",                        /*strideblockbegin */
    ");",                       /*strideblockend */
    "(",                        /*countblockbegin */
    ");",                       /*countblockend */
    "(",                        /*blockblockbegin */
    ");",                       /*blockblockend */

    "",				/*dataspacedescriptionbegin */
    "",				/*dataspacedescriptionend */
    "(",			/*dataspacedimbegin */
    ")",			/*dataspacedimend */
};

/** 
 ** Added for XML **
 **/
/* The 'header' formats for XML -- mostly null
 *
 * XML output has values embedded in the 'headers', so
 * all the XML headers are done on a case by case basis.
 */
static const dump_header xmlformat = {
    "xml",			/*name */
    "",				/*filebegin */
    "</HDF5-File>",		/*fileend */
    "",				/*bootblockbegin */
    "",				/*bootblockend */
    "",				/*groupbegin */
    "</Group>",			/*groupend */
    "",				/*datasetbegin */
    "</Dataset>",		/*datasetend */
    "",				/*attributebegin */
    "</Attribute>",		/*attributeend */
    "<DataType>",		/*datatypeend */
    "</DataType>",		/*datatypeend */
    "<Dataspace>",		/*dataspacebegin */
    "</Dataspace>",		/*dataspaceend */
    "<Data>",			/*databegin */
    "</Data>",			/*dataend */
    "",				/*softlinkbegin */
    "",				/*softlinkend */
    "", 			/*subsettingbegin */
    "",				/*subsettingend */
    "", 			/*startbegin */
    "",				/*startend */
    "", 			/*stridebegin */
    "",				/*strideend */
    "", 			/*countbegin */
    "",				/*countend */
    "", 			/*blockbegin */
    "",				/*blockend */

    "",				/*fileblockbegin */
    "",				/*fileblockend */
    "",				/*bootblockblockbegin */
    "",				/*bootblockblockend */
    "",				/*groupblockbegin */
    "",				/*groupblockend */
    "",				/*datasetblockbegin */
    "",				/*datasetblockend */
    "",				/*attributeblockbegin */
    "",				/*attributeblockend */
    "",				/*datatypeblockbegin */
    "",				/*datatypeblockend */
    "",				/*dataspaceblockbegin */
    "",				/*dataspaceblockend */
    "",				/*datablockbegin */
    "",				/*datablockend */
    "",				/*softlinkblockbegin */
    "",				/*softlinkblockend */
    "",				/*strblockbegin */
    "",				/*strblockend */
    "",				/*enumblockbegin */
    "",				/*enumblockend */
    "",				/*structblockbegin */
    "",				/*structblockend */
    "",				/*vlenblockbegin */
    "",				/*vlenblockend */
    "",                         /*subsettingblockbegin */
    "",                         /*subsettingblockend */
    "",                         /*startblockbegin */
    "",                         /*startblockend */
    "",                         /*strideblockbegin */
    "",                         /*strideblockend */
    "",                         /*countblockbegin */
    "",                         /*countblockend */
    "",                         /*blockblockbegin */
    "",                         /*blockblockend */

    "",				/*dataspacedescriptionbegin */
    "",				/*dataspacedescriptionend */
    "",				/*dataspacedimbegin */
    "",				/*dataspacedimend */
};

/** XML **/

/** 
 ** Added for XML **
 **/
/* internal functions used by XML option */
static void             xml_print_datatype(hid_t);
static void             xml_print_enum(hid_t);
static int              xml_print_refs(hid_t, int);
static int              xml_print_strs(hid_t, int);
static hobj_ref_t      *ref_path_table_put(hid_t, const char *);
static char            *xml_escape_the_string(const char *, int);
static char            *xml_escape_the_name(const char *);

/* a structure for handling the order command-line parameters come in */
struct handler_t {
    void (*func)(hid_t, char *, void *);
    char *obj;
    struct subset_t *subset_info;
};

/*
 * Command-line options: The user can specify short or long-named
 * parameters. The long-named ones can be partially spelled. When
 * adding more, make sure that they don't clash with each other.
 */
#if 0
    /* binary: not implemented yet */
static const char *s_opts = "hbBHvVa:d:g:l:t:w:xD:o:s:S:c:k:";
#else
static const char *s_opts = "hBHvVa:d:g:l:t:w:xD:o:s:S:c:k:";
#endif  /* 0 */
static struct long_options l_opts[] = {
    { "help", no_arg, 'h' },
    { "hel", no_arg, 'h' },
#if 0
    /* binary: not implemented yet */
    { "binary", no_arg, 'b' },
    { "binar", no_arg, 'b' },
    { "bina", no_arg, 'b' },
    { "bin", no_arg, 'b' },
    { "bi", no_arg, 'b' },
#endif  /* 0 */
    { "boot-block", no_arg, 'B' },
    { "boot-bloc", no_arg, 'B' },
    { "boot-blo", no_arg, 'B' },
    { "boot-bl", no_arg, 'B' },
    { "boot-b", no_arg, 'B' },
    { "boot", no_arg, 'B' },
    { "boo", no_arg, 'B' },
    { "bo", no_arg, 'B' },
    { "header", no_arg, 'H' },
    { "heade", no_arg, 'H' },
    { "head", no_arg, 'H' },
    { "hea", no_arg, 'H' },
    { "object-ids", no_arg, 'i' },
    { "object-id", no_arg, 'i' },
    { "object-i", no_arg, 'i' },
    { "object", no_arg, 'i' },
    { "objec", no_arg, 'i' },
    { "obje", no_arg, 'i' },
    { "obj", no_arg, 'i' },
    { "ob", no_arg, 'i' },
    { "version", no_arg, 'V' },
    { "versio", no_arg, 'V' },
    { "versi", no_arg, 'V' },
    { "vers", no_arg, 'V' },
    { "ver", no_arg, 'V' },
    { "ve", no_arg, 'V' },
    { "attribute", require_arg, 'a' },
    { "attribut", require_arg, 'a' },
    { "attribu", require_arg, 'a' },
    { "attrib", require_arg, 'a' },
    { "attri", require_arg, 'a' },
    { "attr", require_arg, 'a' },
    { "att", require_arg, 'a' },
    { "at", require_arg, 'a' },
    { "block", require_arg, 'k' },
    { "bloc", require_arg, 'k' },
    { "blo", require_arg, 'k' },
    { "bl", require_arg, 'k' },
    { "count", require_arg, 'c' },
    { "coun", require_arg, 'c' },
    { "cou", require_arg, 'c' },
    { "co", require_arg, 'c' },
    { "dataset", require_arg, 'd' },
    { "datase", require_arg, 'd' },
    { "datas", require_arg, 'd' },
    { "datatype", require_arg, 't' },
    { "datatyp", require_arg, 't' },
    { "dataty", require_arg, 't' },
    { "datat", require_arg, 't' },
    { "group", require_arg, 'g' },
    { "grou", require_arg, 'g' },
    { "gro", require_arg, 'g' },
    { "gr", require_arg, 'g' },
    { "output", require_arg, 'o' },
    { "outpu", require_arg, 'o' },
    { "outp", require_arg, 'o' },
    { "out", require_arg, 'o' },
    { "ou", require_arg, 'o' },
    { "soft-link", require_arg, 'l' },
    { "soft-lin", require_arg, 'l' },
    { "soft-li", require_arg, 'l' },
    { "soft-l", require_arg, 'l' },
    { "soft", require_arg, 'l' },
    { "sof", require_arg, 'l' },
    { "so", require_arg, 'l' },
    { "start", require_arg, 's' },
    { "star", require_arg, 's' },
    { "sta", require_arg, 's' },
    { "stride", require_arg, 'S' },
    { "strid", require_arg, 'S' },
    { "stri", require_arg, 'S' },
    { "str", require_arg, 'S' },
    { "width", require_arg, 'w' },
    { "widt", require_arg, 'w' },
    { "wid", require_arg, 'w' },
    { "wi", require_arg, 'w' },
    { "xml", no_arg, 'x' },
    { "xm", no_arg, 'x' },
    { "xml-dtd", require_arg, 'D' },
    { "xml-dt", require_arg, 'D' },
    { "xml-d", require_arg, 'D' },
    { NULL, 0, '\0' }
};

/**
 **  Change for XML  **
 **
 **  The 'dump_xxx' functions have two versions, standard and XML.
 **
 **    They are called indirectly through the 'dump_function_table'.
 **    e.g., dump_group(...) becomes dump_functions->dump_group(...);
 **
 **    The standard functions are unchanged except for the way
 **    they are called
 **/

/* The dump functions of the dump_function_table */

/* standard format:  no change */
static void             dump_group(hid_t, const char *);
static void             dump_named_datatype(hid_t, const char *);
static void             dump_dataset(hid_t, const char *, struct subset_t *);
static void             dump_dataspace(hid_t space);
static void             dump_datatype(hid_t type);
static herr_t           dump_attr(hid_t, const char *, void UNUSED *);
static void             dump_data(hid_t, int, struct subset_t *);

/* XML format:   same interface, alternative output */

static void             xml_dump_group(hid_t, const char *);
static void             xml_dump_named_datatype(hid_t, const char *);
static void             xml_dump_dataset(hid_t, const char *, struct subset_t UNUSED *);
static void             xml_dump_dataspace(hid_t space);
static void             xml_dump_datatype(hid_t type);
static herr_t           xml_dump_attr(hid_t, const char *, void UNUSED *);
static void             xml_dump_data(hid_t, int, struct subset_t UNUSED *);

/** 
 ** Added for XML **
 **
 **  This is the global dispatch table for the dump functions.
 **/
/* the table of dump functions */
typedef struct dump_functions_t {
    void                (*dump_group_function) (hid_t, const char *);
    void                (*dump_named_datatype_function) (hid_t, const char *);
    void                (*dump_dataset_function) (hid_t, const char *, struct subset_t *);
    void                (*dump_dataspace_function) (hid_t);
    void                (*dump_datatype_function) (hid_t);
    herr_t              (*dump_attribute_function) (hid_t, const char *, void *);
    void                (*dump_data_function) (hid_t, int, struct subset_t *);
} dump_functions;

/* Standard DDL output */
static const dump_functions ddl_function_table = {
    dump_group,
    dump_named_datatype,
    dump_dataset,
    dump_dataspace,
    dump_datatype,
    dump_attr,
    dump_data
};

/* XML output */
static const dump_functions xml_function_table = {
    xml_dump_group,
    xml_dump_named_datatype,
    xml_dump_dataset,
    xml_dump_dataspace,
    xml_dump_datatype,
    xml_dump_attr,
    xml_dump_data
};

/*
 * The global table is set to either ddl_function_table or 
 * xml_function_table in the initialization.
 */
static const dump_functions *dump_function_table;

/*-------------------------------------------------------------------------
 * Function:    usage
 *
 * Purpose:     Print the usage message about dumper
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
usage(const char *prog)
{
    fflush(stdout);
    fprintf(stdout, "\
usage: %s [OPTIONS] file\n\
   OPTIONS\n\
      -h, --help          Print a usage message and exit\n\
      -B, --bootblock     Print the content of the boot block\n\
      -H, --header        Print the header only; no data is displayed\n\
      -i, --object-ids    Print the object ids\n\
      -V, --version       Print version number and exit\n\
      -a P, --attribute=P Print the specified attribute\n\
      -d P, --dataset=P   Print the specified dataset\n\
      -g P, --group=P     Print the specified group and all members\n\
      -l P, --soft-link=P Print the value(s) of the specified soft link\n\
      -o F, --output=F    Output raw data into file F\n\
      -t P, --datatype=P  Print the specified named data type\n\
      -w N, --width=N     Set the number of columns of output\n\
      -x, --xml           Output in XML\n\
      -D U, --xml-dtd=U   Use the DTD at U\n\
\n\
 Subsetting is available by using the following options with a dataset\n\
 attribute. Subsetting is done by selecting a hyperslab from the data.\n\
 Thus, the options mirror those for performing a hyperslab selection.\n\
 The START and COUNT parameters are mandatory if you do subsetting.\n\
 The STRIDE and BLOCK parameters are optional and will default to 1 in\n\
 each dimension.\n\
\n\
      -s L, --start=L     Offset of start of subsetting selection\n\
      -S L, --stride=L    Hyperslab stride\n\
      -c L, --count=L     Number of blocks to include in selection\n\
      -k L, --block=L     Size of block in hyperslab\n\
\n\
  P - is the full path from the root group to the object.\n\
  F - is a filename.\n\
  N - is an integer greater than 1.\n\
  L - is a list of integers the number of which are equal to the\n\
        number of dimensions in the dataspace being queried\n\
  U - is a URI reference (as defined in [IETF RFC 2396],\n\
        updated by [IETF RFC 2732])\n\
\n\
  Examples:\n\
\n\
  1) Attribute foo of the group /bar_none in file quux.h5\n\
\n\
     	h5dump -a /bar_none/foo quux.h5\n\
\n\
  2) Selecting a subset from dataset /foo in file quux.h5\n\
\n\
      h5dump -d /foo -s \"0,1\" -S \"1,1\" -c \"2,3\" -k \"2,2\" quux.h5\n\
\n", prog);
}

/*-------------------------------------------------------------------------
 * Function:    print_datatype
 *
 * Purpose:     print the data type.
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
print_datatype(hid_t type)
{
    char       *fname;
    hid_t       nmembers, mtype, str_type;
    int         i, j, ndims, perm[H5DUMP_MAX_RANK];
    size_t      size;
    hsize_t     dims[H5DUMP_MAX_RANK];
    H5T_str_t   str_pad;
    H5T_cset_t  cset;
    H5G_stat_t  statbuf;
    hid_t       super;

    switch (H5Tget_class(type)) {
    case H5T_INTEGER:
	if (H5Tequal(type, H5T_STD_I8BE)) {
	    printf("H5T_STD_I8BE");
	} else if (H5Tequal(type, H5T_STD_I8LE)) {
	    printf("H5T_STD_I8LE");
	} else if (H5Tequal(type, H5T_STD_I16BE)) {
	    printf("H5T_STD_I16BE");
	} else if (H5Tequal(type, H5T_STD_I16LE)) {
	    printf("H5T_STD_I16LE");
	} else if (H5Tequal(type, H5T_STD_I32BE)) {
	    printf("H5T_STD_I32BE");
	} else if (H5Tequal(type, H5T_STD_I32LE)) {
	    printf("H5T_STD_I32LE");
	} else if (H5Tequal(type, H5T_STD_I64BE)) {
	    printf("H5T_STD_I64BE");
	} else if (H5Tequal(type, H5T_STD_I64LE)) {
	    printf("H5T_STD_I64LE");
	} else if (H5Tequal(type, H5T_STD_U8BE)) {
	    printf("H5T_STD_U8BE");
	} else if (H5Tequal(type, H5T_STD_U8LE)) {
	    printf("H5T_STD_U8LE");
	} else if (H5Tequal(type, H5T_STD_U16BE)) {
	    printf("H5T_STD_U16BE");
	} else if (H5Tequal(type, H5T_STD_U16LE)) {
	    printf("H5T_STD_U16LE");
	} else if (H5Tequal(type, H5T_STD_U32BE)) {
	    printf("H5T_STD_U32BE");
	} else if (H5Tequal(type, H5T_STD_U32LE)) {
	    printf("H5T_STD_U32LE");
	} else if (H5Tequal(type, H5T_STD_U64BE)) {
	    printf("H5T_STD_U64BE");
	} else if (H5Tequal(type, H5T_STD_U64LE)) {
	    printf("H5T_STD_U64LE");
	} else if (H5Tequal(type, H5T_NATIVE_SCHAR)) {
	    printf("H5T_NATIVE_SCHAR");
	} else if (H5Tequal(type, H5T_NATIVE_UCHAR)) {
	    printf("H5T_NATIVE_UCHAR");
	} else if (H5Tequal(type, H5T_NATIVE_SHORT)) {
	    printf("H5T_NATIVE_SHORT");
	} else if (H5Tequal(type, H5T_NATIVE_USHORT)) {
	    printf("H5T_NATIVE_USHORT");
	} else if (H5Tequal(type, H5T_NATIVE_INT)) {
	    printf("H5T_NATIVE_INT");
	} else if (H5Tequal(type, H5T_NATIVE_UINT)) {
	    printf("H5T_NATIVE_UINT");
	} else if (H5Tequal(type, H5T_NATIVE_LONG)) {
	    printf("H5T_NATIVE_LONG");
	} else if (H5Tequal(type, H5T_NATIVE_ULONG)) {
	    printf("H5T_NATIVE_ULONG");
	} else if (H5Tequal(type, H5T_NATIVE_LLONG)) {
	    printf("H5T_NATIVE_LLONG");
	} else if (H5Tequal(type, H5T_NATIVE_ULLONG)) {
	    printf("H5T_NATIVE_ULLONG");
	} else {
	    printf("undefined integer");
	    d_status = EXIT_FAILURE;
	}
	break;

    case H5T_FLOAT:
	if (H5Tequal(type, H5T_IEEE_F32BE)) {
	    printf("H5T_IEEE_F32BE");
	} else if (H5Tequal(type, H5T_IEEE_F32LE)) {
	    printf("H5T_IEEE_F32LE");
	} else if (H5Tequal(type, H5T_IEEE_F64BE)) {
	    printf("H5T_IEEE_F64BE");
	} else if (H5Tequal(type, H5T_IEEE_F64LE)) {
	    printf("H5T_IEEE_F64LE");
	} else if (H5Tequal(type, H5T_NATIVE_FLOAT)) {
	    printf("H5T_NATIVE_FLOAT");
	} else if (H5Tequal(type, H5T_NATIVE_DOUBLE)) {
	    printf("H5T_NATIVE_DOUBLE");
	} else if (H5Tequal(type, H5T_NATIVE_LDOUBLE)) {
	    printf("H5T_NATIVE_LDOUBLE");
	} else {
	    printf("undefined float");
	    d_status = EXIT_FAILURE;
	}
	break;

    case H5T_TIME:
	printf("H5T_TIME: not yet implemented");
	break;

    case H5T_STRING:
	size = H5Tget_size(type);
	str_pad = H5Tget_strpad(type);
	cset = H5Tget_cset(type);

	printf("H5T_STRING %s\n", dump_header_format->strblockbegin);
	indent += COL;

	indentation(indent + COL);
	printf("%s %d;\n", STRSIZE, (int) size);

	indentation(indent + COL);
	printf("%s ", STRPAD);
	if (str_pad == H5T_STR_NULLTERM)
	    printf("H5T_STR_NULLTERM;\n");
	else if (str_pad == H5T_STR_NULLPAD)
	    printf("H5T_STR_NULLPAD;\n");
	else if (str_pad == H5T_STR_SPACEPAD)
	    printf("H5T_STR_SPACEPAD;\n");
	else
	    printf("H5T_STR_ERROR;\n");

	indentation(indent + COL);
	printf("%s ", CSET);

	if (cset == H5T_CSET_ASCII)
	    printf("H5T_CSET_ASCII;\n");
	else
	    printf("unknown_cset;\n");

	str_type = H5Tcopy(H5T_C_S1);
	H5Tset_cset(str_type, cset);
	H5Tset_size(str_type, size);
	H5Tset_strpad(str_type, str_pad);

	indentation(indent + COL);
	printf("%s ", CTYPE);

	if (H5Tequal(type, str_type)) {
	    printf("H5T_C_S1;\n");
	    H5Tclose(str_type);
	} else {
	    H5Tclose(str_type);
	    str_type = H5Tcopy(H5T_FORTRAN_S1);
	    H5Tset_cset(str_type, cset);
	    H5Tset_size(str_type, size);
	    H5Tset_strpad(str_type, str_pad);

	    if (H5Tequal(type, str_type)) {
		printf("H5T_FORTRAN_S1;\n");
	    } else {
		printf("unknown_one_character_type;\n ");
		d_status = EXIT_FAILURE;
	    }

	    H5Tclose(str_type);
	}

	indent -= COL;
	indentation(indent + COL);
	printf("%s", dump_header_format->strblockend);
	break;

    case H5T_BITFIELD:
	if (H5Tequal(type, H5T_STD_B8BE)) {
	    printf("H5T_STD_B8BE");
	} else if (H5Tequal(type, H5T_STD_B8LE)) {
	    printf("H5T_STD_B8LE");
	} else if (H5Tequal(type, H5T_STD_B16BE)) {
	    printf("H5T_STD_B16BE");
	} else if (H5Tequal(type, H5T_STD_B16LE)) {
	    printf("H5T_STD_B16LE");
	} else if (H5Tequal(type, H5T_STD_B32BE)) {
	    printf("H5T_STD_B32BE");
	} else if (H5Tequal(type, H5T_STD_B32LE)) {
	    printf("H5T_STD_B32LE");
	} else if (H5Tequal(type, H5T_STD_B64BE)) {
	    printf("H5T_STD_B64BE");
	} else if (H5Tequal(type, H5T_STD_B64LE)) {
	    printf("H5T_STD_B64LE");
	} else {
	    printf("undefined bitfield");
	    d_status = EXIT_FAILURE;
	}
	break;

    case H5T_OPAQUE:
	printf("\n");
	indentation(indent + COL);
	printf("H5T_OPAQUE;\n");
	indentation(indent + COL);
	printf("OPAQUE_TAG \"%s\";\n", H5Tget_tag(type));
	indentation(indent);
	break;

    case H5T_COMPOUND:
	if (H5Tcommitted(type) > 0) {
	    H5Gget_objinfo(type, ".", TRUE, &statbuf);
	    i = search_obj(type_table, statbuf.objno);

	    if (i >= 0) {
		if (!type_table->objs[i].recorded)
		    printf("\"/#%lu:%lu\"\n", type_table->objs[i].objno[0],
			   type_table->objs[i].objno[1]);
		else
		    printf("\"%s\"", type_table->objs[i].objname);
	    } else {
                error_msg(progname, "unknown committed type.\n");
		d_status = EXIT_FAILURE;
	    }
	} else {
	    nmembers = H5Tget_nmembers(type);
	    printf("H5T_COMPOUND %s\n", dump_header_format->structblockbegin);

	    for (i = 0; i < nmembers; i++) {
		fname = H5Tget_member_name(type, i);
		mtype = H5Tget_member_type(type, i);

		indentation(indent + COL);

		if (H5Tget_class(mtype) == H5T_COMPOUND)
		    indent += COL;

		print_datatype(mtype);

		if (H5Tget_class(mtype) == H5T_COMPOUND)
		    indent -= COL;

		printf(" \"%s\";\n", fname);
		free(fname);
	    }

	    indentation(indent);
	    printf("%s", dump_header_format->structblockend);
	}

	break;

    case H5T_REFERENCE:
	printf("H5T_REFERENCE");
	break;

    case H5T_ENUM:
	printf("H5T_ENUM %s\n", dump_header_format->enumblockbegin);
	indent += COL;
	indentation(indent + COL);
	super = H5Tget_super(type);
	print_datatype(super);
	printf(";\n");
	print_enum(type);
	indent -= COL;
	indentation(indent + COL);
	printf("%s", dump_header_format->enumblockend);
	break;

    case H5T_VLEN:
	printf("H5T_VLEN %s ", dump_header_format->vlenblockbegin);
	super = H5Tget_super(type);
	print_datatype(super);
	H5Tclose(super);

	/* Print closing */
	printf("%s", dump_header_format->vlenblockend);
	break;

    case H5T_ARRAY:
	/* Get array base type */
	super = H5Tget_super(type);

	/* Print lead-in */
	printf("H5T_ARRAY { ");

	/* Get array information */
	ndims = H5Tget_array_ndims(type);
	H5Tget_array_dims(type, dims, perm);

	/* Print array dimensions */
	for (j = 0; j < ndims; j++)
	    printf("[%d]", (int) dims[j]);

	printf(" ");

	/* Print base type */
	print_datatype(super);

	/* Close array base type */
	H5Tclose(super);

	/* Print closing */
	printf(" }");

	break;

    default:
	printf("unknown data type");
	d_status = EXIT_FAILURE;
	break;
    }
}

/*-------------------------------------------------------------------------
 * Function:    dump_bb
 *
 * Purpose:     Dump the boot block
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_bb(void)
{
    printf("%s %s boot block not yet implemented %s\n",
	   BOOT_BLOCK, BEGIN, END);
}

/*-------------------------------------------------------------------------
 * Function:    dump_datatype
 *
 * Purpose:     Dump the data type. Data type can be HDF5 predefined
 *              atomic data type or committed/transient data type.
 *
 * Return:      void 
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_datatype(hid_t type)
{
    indent += COL;

    indentation(indent);
    printf("%s %s ", dump_header_format->datatypebegin,
           dump_header_format->datatypeblockbegin);

    print_datatype(type);

    if (H5Tget_class(type) == H5T_COMPOUND || H5Tget_class(type) == H5T_STRING)
        indentation(indent);

    printf(" %s %s\n", dump_header_format->datatypeblockend,
           dump_header_format->datatypeend);
    indent -= COL;
}

/*-------------------------------------------------------------------------
 * Function:    dump_dataspace
 *
 * Purpose:     Dump the data space. Data space can be named data space,
 *              array, or others.
 *
 * Return:      void    
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_dataspace(hid_t space)
{
    hsize_t   size[H5DUMP_MAX_RANK];
    hsize_t   maxsize[H5DUMP_MAX_RANK];
    int       ndims = H5Sget_simple_extent_dims(space, size, maxsize);
    int       i;

    indentation(indent + COL);
    printf("%s ", dump_header_format->dataspacebegin);

    if (H5Sis_simple(space)) {
	if (ndims == 0) {
	    /* scalar dataspace */
	    HDfprintf(stdout, "%s %s ",
		      dump_header_format->dataspacedescriptionbegin, SCALAR);
	} else {
	    /* simple dataspace */
	    HDfprintf(stdout, "%s %s { %s %Hu",
		      dump_header_format->dataspacedescriptionbegin, SIMPLE,
		      dump_header_format->dataspacedimbegin, size[0]);

	    for (i = 1; i < ndims; i++)
		HDfprintf(stdout, ", %Hu", size[i]);

	    printf(" %s / ", dump_header_format->dataspacedimend);

	    if (maxsize[0] == H5S_UNLIMITED)
		HDfprintf(stdout, "%s %s",
			  dump_header_format->dataspacedimbegin,
			  "H5S_UNLIMITED");
	    else
		HDfprintf(stdout, "%s %Hu",
                          dump_header_format->dataspacedimbegin, maxsize[0]);

	    for (i = 1; i < ndims; i++)
		if (maxsize[i] == H5S_UNLIMITED)
		    HDfprintf(stdout, ", %s", "H5S_UNLIMITED");
		else
		    HDfprintf(stdout, ", %Hu", maxsize[i]);

	    printf(" %s }", dump_header_format->dataspacedimend);
	}
    } else {
	printf("%s not yet implemented %s\n", BEGIN, END);
    }

    end_obj(dump_header_format->dataspaceend,
	    dump_header_format->dataspaceblockend);
}

/*-------------------------------------------------------------------------
 * Function:    dump_attr
 *
 * Purpose:     dump the attribute
 *
 * Return:      Success:        SUCCEED
 *
 *              Failure:        FAIL
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
dump_attr(hid_t attr, const char *attr_name, void UNUSED *op_data)
{
    hid_t       attr_id, type, space;
    herr_t      ret = SUCCEED;

    indentation(indent);
    begin_obj(dump_header_format->attributebegin, attr_name,
	      dump_header_format->attributeblockbegin);

    if ((attr_id = H5Aopen_name(attr, attr_name)) < 0) {
	indentation(indent + COL);
        error_msg(progname, "unable to open attribute \"%s\"\n", attr_name);
	indentation(indent);
	end_obj(dump_header_format->attributeend,
		dump_header_format->attributeblockend);
	d_status = EXIT_FAILURE;
	ret = FAIL;
    } else {
	type = H5Aget_type(attr_id);
	space = H5Aget_space(attr_id);
	dump_datatype(type);
	dump_dataspace(space);

	if (display_oid)
	    dump_oid(attr_id);

	if (display_data)
	    dump_data(attr_id, ATTRIBUTE_DATA, NULL);

	H5Tclose(type);
	H5Sclose(space);
	H5Aclose(attr_id);
	indentation(indent);
	end_obj(dump_header_format->attributeend,
		dump_header_format->attributeblockend);
    }

    return ret;
}

/*-------------------------------------------------------------------------
 * Function:    dump_selected_attr
 *
 * Purpose:     dump the selected attribute
 *
 * Return:      Success:        SUCCEED
 *
 *              Failure:        FAIL
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
dump_selected_attr(hid_t loc_id, const char *name)
{
    int j;
    char *obj_name;
    const char *attr_name;
    hid_t  oid, attr_id, type, space;
    H5G_stat_t statbuf;

    j = (int)strlen(name) - 1;
    obj_name = malloc((size_t)j + 2);

    /* find the last / */
    while (name[j] != '/' && j >= 0)
	j--;

    /* object name */
    if (j == -1) {
	strcpy(obj_name, "/");
    } else {
        strncpy(obj_name, name, (size_t)j + 1);
        obj_name[j + 1] = '\0';
    }

    attr_name = name + j + 1;
    begin_obj(dump_header_format->attributebegin, name,
	      dump_header_format->attributeblockbegin);
    H5Gget_objinfo(loc_id, obj_name, FALSE, &statbuf);

    switch (statbuf.type) {
    case H5G_GROUP:
	if ((oid = H5Gopen(loc_id, obj_name)) < 0) {
	    indentation(COL);
            error_msg(progname, "unable to open group \"%s\"\n", obj_name);
	    end_obj(dump_header_format->attributeend,
		    dump_header_format->attributeblockend);
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;

    case H5G_DATASET:
	if ((oid = H5Dopen(loc_id, obj_name)) < 0) {
	    indentation(COL);
            error_msg(progname, "unable to open dataset \"%s\"\n", obj_name);
	    end_obj(dump_header_format->attributeend,
		    dump_header_format->attributeblockend);
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;

    case H5G_TYPE:
	if ((oid = H5Topen(loc_id, obj_name)) < 0) {
	    indentation(COL);
            error_msg(progname, "unable to open datatype \"%s\"\n", obj_name);
	    end_obj(dump_header_format->attributeend,
		    dump_header_format->attributeblockend);
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;

    default:
	indentation(COL);
        error_msg(progname, "unable to open unknown \"%s\"\n", obj_name);
	end_obj(dump_header_format->attributeend,
		dump_header_format->attributeblockend);
	d_status = EXIT_FAILURE;
	return FAIL;
    }

    if ((attr_id = H5Aopen_name(oid, attr_name)) >= 0) {
	type = H5Aget_type(attr_id);
	space = H5Aget_space(attr_id);
	dump_datatype(type);
	dump_dataspace(space);

	if (display_oid)
	    dump_oid(attr_id);

	if (display_data)
	    dump_data(attr_id, ATTRIBUTE_DATA, NULL);

	H5Tclose(type);
	H5Sclose(space);
	H5Aclose(attr_id);
	end_obj(dump_header_format->attributeend,
		dump_header_format->attributeblockend);
    } else {
	indentation(COL);
        error_msg(progname, "unable to open attribute \"%s\"\n", obj_name);
	end_obj(dump_header_format->attributeend,
		dump_header_format->attributeblockend);
	d_status = EXIT_FAILURE;
    }

    switch (statbuf.type) {
    case H5G_GROUP:
	if (H5Gclose(oid) < 0) {
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;

    case H5G_DATASET:
	if (H5Dclose(oid) < 0) {
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;

    case H5G_TYPE:
	if (H5Tclose(oid) < 0) {
	    d_status = EXIT_FAILURE;
	    return FAIL;
	}
	break;
    default:
	d_status = EXIT_FAILURE;
	return FAIL;
    }

    free(obj_name);
    return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    dump_all
 *
 * Purpose:     Dump everything in the specified object
 *
 * Return:      Success:        SUCCEED
 *
 *              Failure:        FAIL
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *          RMcG, November 2000
 *          Added XML support. Also, optionally checks the op_data
 *          argument.
 *
 *-------------------------------------------------------------------------
 */
static herr_t
dump_all(hid_t group, const char *name, void * op_data)
{
    hid_t       obj;
    char       *buf, *tmp = NULL;
    H5G_stat_t  statbuf;
    int         i;
    herr_t      ret = SUCCEED;

    H5Gget_objinfo(group, name, FALSE, &statbuf);

    if (*(int *)op_data != H5G_UNKNOWN && statbuf.type != *(int *) op_data)
        goto done;

    tmp = malloc(strlen(prefix) + strlen(name) + 2);
    strcpy(tmp, prefix);

    switch (statbuf.type) {
    case H5G_LINK:
	indentation(indent);
	buf = malloc(statbuf.linklen);

	if (!doxml) {
	    begin_obj(dump_header_format->softlinkbegin, name,
		      dump_header_format->softlinkblockbegin);
	    indentation(indent + COL);
	}

	if (H5Gget_linkval(group, name, statbuf.linklen, buf) < 0) {
            error_msg(progname, "unable to get link value\n");
	    d_status = EXIT_FAILURE;
            ret = FAIL;
	} else {
	    /* print the value of a soft link */
	    if (!doxml) {
		/* Standard DDL: no modification */
		printf("LINKTARGET \"%s\"\n", buf);
	    } else {
		/* XML */
                char *t_name = xml_escape_the_name(name);
                char *t_buf = xml_escape_the_name(buf);
                char *t_prefix = xml_escape_the_name(prefix);
                char *tmp2, *t_tmp2, *t_tmp;

                tmp2 = malloc(strlen(prefix) + statbuf.linklen + 1);
                strcpy(tmp2, prefix);

                if (buf && buf[0] == '/')
                    strcat(tmp2, buf);
                else
                    strcat(strcat(tmp2, "/"), buf);

                t_tmp = xml_escape_the_name(strcat(strcat(tmp, "/"), name));
                t_tmp2 = xml_escape_the_name(tmp2);

		printf("<SoftLink LinkName=\"%s\" Target=\"%s\" "
                       "TargetObj=\"%s\" OBJ-XID=\"%s\" Source=\"%s\"/>\n",
		       t_name, t_buf, t_tmp2, t_tmp,
		       (strcmp(prefix, "") ? t_prefix : "root"));

                free(t_name);
                free(t_buf);
                free(t_prefix);
                free(t_tmp);
                free(t_tmp2);
                free(tmp2);
	    }
	}

	if (!doxml) {
	    indentation(indent);
	    end_obj(dump_header_format->softlinkend,
		    dump_header_format->softlinkblockend);
	}

	free(buf);
	break;

    case H5G_GROUP:
	if ((obj = H5Gopen(group, name)) < 0) {
            error_msg(progname, "unable to dump group \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
            ret = FAIL;
	} else {
	    strcat(strcat(prefix, "/"), name);
	    dump_function_table->dump_group_function(obj, name);
	    strcpy(prefix, tmp);
	    H5Gclose(obj);
	}

	break;

    case H5G_DATASET:
	if ((obj = H5Dopen(group, name)) >= 0) {
	    /* hard link */
	    H5Gget_objinfo(obj, ".", TRUE, &statbuf);

	    if (statbuf.nlink > 1) {
		i = search_obj(dset_table, statbuf.objno);

		if (i < 0) {
		    indentation(indent);
		    begin_obj(dump_header_format->datasetbegin, name,
			      dump_header_format->datasetblockbegin);
		    indentation(indent + COL);
                    error_msg(progname,
                              "internal error (file %s:line %d)\n",
                              __FILE__, __LINE__);
		    indentation(indent);
		    end_obj(dump_header_format->datasetend,
			    dump_header_format->datasetblockend);
		    d_status = EXIT_FAILURE;
                    ret = FAIL;
		    H5Dclose(obj);
		    goto done;
		} else if (dset_table->objs[i].displayed) {
		    indentation(indent);

		    if (!doxml) {
			begin_obj(dump_header_format->datasetbegin, name,
				  dump_header_format->datasetblockbegin);
			indentation(indent + COL);
			printf("%s \"%s\"\n", HARDLINK,
			       dset_table->objs[i].objname);
			indentation(indent);
			end_obj(dump_header_format->datasetend,
				dump_header_format->datasetblockend);
		    } else {
			/* the XML version */
                        char *t_name = xml_escape_the_name(name);
                        char *t_tmp = xml_escape_the_name(strcat(strcat(tmp, "/"), name));
                        char *t_prefix = xml_escape_the_name(prefix);
                        char *t_objname = xml_escape_the_name(dset_table->objs[i].objname);

			printf("<Dataset Name=\"%s\" OBJ-XID=\"%s\" Parents=\"%s\">\n",
			       t_name, t_tmp,
			       (strcmp(prefix, "") ? t_prefix : "root"));

			indentation(indent + COL);
			printf("<DatasetPtr OBJ-XID=\"%s\"/>\n", t_objname);
			indentation(indent);
			printf("%s\n", dump_header_format->datasetend);

                        free(t_name);
                        free(t_tmp);
                        free(t_prefix);
                        free(t_objname);
		    }

		    H5Dclose(obj);
		    goto done;
		} else {
		    dset_table->objs[i].displayed = 1;
		    strcat(tmp, "/");
		    strcat(tmp, name);
		    strcpy(dset_table->objs[i].objname, tmp);
		}
	    }

	    dump_function_table->dump_dataset_function(obj, name, NULL);
	    H5Dclose(obj);
	} else {
            error_msg(progname, "unable to dump dataset \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
            ret = FAIL;
	}

	break;

    case H5G_TYPE:
	if ((obj = H5Topen(group, name)) < 0) {
            error_msg(progname, "unable to dump data type \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
            ret = FAIL;
	} else {
	    dump_function_table->dump_named_datatype_function(obj, name);
	    H5Tclose(obj);
	}

	break;

    default:
        error_msg(progname, "unknown object \"%s\"\n", name);
	d_status = EXIT_FAILURE;
	ret = FAIL;
    }

done:
    free(tmp);
    return ret;
}

/*-------------------------------------------------------------------------
 * Function:    dump_named_datatype
 *
 * Purpose:     Dump named data type
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications: Comments: not yet implemented.
 *
 *-------------------------------------------------------------------------
 */
static void
dump_named_datatype(hid_t type, const char *name)
{
    indentation(indent);
    printf("%s \"%s\" %s", dump_header_format->datatypebegin, name,
	   dump_header_format->datatypeblockbegin);

    if (H5Tget_class(type) == H5T_COMPOUND) {
	hid_t temp_type = H5Tcopy(type);

	print_datatype(temp_type);
	H5Tclose(temp_type);
    } else {
	indentation(indent + COL);
	print_datatype(type);
	printf(";\n");
    }

    indentation(indent);
    end_obj(dump_header_format->datatypeend,
	    dump_header_format->datatypeblockend);
}

/*-------------------------------------------------------------------------
 * Function:    dump_group
 *
 * Purpose:     Dump everything within the specified group
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *      Call to dump_all -- add parameter to select everything.
 *
 *-------------------------------------------------------------------------
 */
static void
dump_group(hid_t gid, const char *name)
{
    H5G_stat_t  statbuf;
    hid_t       dset, type;
    char        typename[1024], *tmp;
    int         i, xtype = H5G_UNKNOWN; /* dump all */

    tmp = malloc(strlen(prefix) + strlen(name) + 2);
    strcpy(tmp, prefix);
    indentation(indent);
    begin_obj(dump_header_format->groupbegin, name,
	      dump_header_format->groupblockbegin);
    indent += COL;

    if (display_oid)
	dump_oid(gid);

    if (!strcmp(name, "/") && unamedtype)
	/* dump unamed type in root group */
	for (i = 0; i < type_table->nobjs; i++)
	    if (!type_table->objs[i].recorded) {
		dset = H5Dopen(gid, type_table->objs[i].objname);
		type = H5Dget_type(dset);
		sprintf(typename, "#%lu:%lu",
			type_table->objs[i].objno[0],
			type_table->objs[i].objno[1]);
		dump_named_datatype(type, typename);
		H5Tclose(type);
		H5Dclose(dset);
	    }

    H5Gget_objinfo(gid, ".", TRUE, &statbuf);

    if (statbuf.nlink > 1) { 
        i = search_obj(group_table, statbuf.objno);

	if (i < 0) {
	    indentation(indent);
            error_msg(progname, "internal error (file %s:line %d)\n",
                      __FILE__, __LINE__);
	    d_status = EXIT_FAILURE;
	} else if (group_table->objs[i].displayed) {
	    indentation(indent);
	    printf("%s \"%s\"\n", HARDLINK, group_table->objs[i].objname);
	} else {
	    strcpy(group_table->objs[i].objname, prefix);
	    group_table->objs[i].displayed = 1;
	    H5Aiterate(gid, NULL, dump_attr, NULL);
	    H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	}
    } else {
	H5Aiterate(gid, NULL, dump_attr, NULL);
	H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
    }

    indent -= COL;
    indentation(indent);
    end_obj(dump_header_format->groupend, dump_header_format->groupblockend);
    free(tmp);
}

/*-------------------------------------------------------------------------
 * Function:    dump_dataset
 *
 * Purpose:     Dump the specified data set
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_dataset(hid_t did, const char *name, struct subset_t *sset)
{
    hid_t   type, space;

    indentation(indent);
    begin_obj(dump_header_format->datasetbegin, name,
	      dump_header_format->datasetblockbegin);
    type = H5Dget_type(did);
    space = H5Dget_space(did);
    dump_datatype(type);
    dump_dataspace(space);

    if (display_oid)
	dump_oid(did);

    if (display_data)
	switch (H5Tget_class(type)) {
	case H5T_TIME:
	    indentation(indent + COL);
	    printf("DATA{ not yet implemented.}\n");
	    break;

	case H5T_INTEGER:
	case H5T_FLOAT:
	case H5T_STRING:
	case H5T_BITFIELD:
	case H5T_OPAQUE:
	case H5T_COMPOUND:
	case H5T_REFERENCE:
	case H5T_ENUM:
	case H5T_VLEN:
	case H5T_ARRAY:
	    dump_data(did, DATASET_DATA, sset);
	    break;

	default:
	    break;
	}

    indent += COL;
    H5Aiterate(did, NULL, dump_attr, NULL);
    indent -= COL;
    H5Tclose(type);
    H5Sclose(space);
    indentation(indent);
    end_obj(dump_header_format->datasetend,
	    dump_header_format->datasetblockend);
}

/*-------------------------------------------------------------------------
 * Function:    dump_tables
 *
 * Purpose:     display the contents of tables for debugging purposes
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_tables(void)
{
#ifdef H5DUMP_DEBUG
    register int i;

    printf("group_table: # of entries = %d\n", group_table->nobjs);

    for (i = 0; i < group_table->nobjs; i++)
	printf("%lu %lu %s %d %d\n", group_table->objs[i].objno[0],
	       group_table->objs[i].objno[1],
	       group_table->objs[i].objname,
	       group_table->objs[i].displayed, group_table->objs[i].recorded);

    printf("\ndset_table: # of entries = %d\n", dset_table->nobjs);

    for (i = 0; i < dset_table->nobjs; i++)
	printf("%lu %lu %s %d %d\n", dset_table->objs[i].objno[0],
	       dset_table->objs[i].objno[1],
	       dset_table->objs[i].objname,
	       dset_table->objs[i].displayed, dset_table->objs[i].recorded);

    printf("\ntype_table: # of entries = %d\n", type_table->nobjs);

    for (i = 0; i < type_table->nobjs; i++)
	printf("%lu %lu %s %d %d\n", type_table->objs[i].objno[0],
	       type_table->objs[i].objno[1],
	       type_table->objs[i].objname,
	       type_table->objs[i].displayed, type_table->objs[i].recorded);
#else
    return;
#endif  /* H5DUMP_DEBUG */
}

/*-------------------------------------------------------------------------
 * Function:    dump_dims
 *
 * Purpose:     Dump the dimensions handed to it in a comma separated list
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 27. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_dims(hsize_t *s, int dims)
{
    register int i;

    for (i = 0; i < dims; i++) {
        printf("%u", (unsigned int)s[i]);

        if (i + 1 != dims)
            printf(", ");
    }
}

/*-------------------------------------------------------------------------
 * Function:    dump_subsetting_header
 *
 * Purpose:     Dump the subsetting header like specified in the DDL.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 27. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_subsetting_header(struct subset_t *sset, int dims)
{
    indentation(indent);
    printf("%s %s\n", dump_header_format->subsettingbegin,
           dump_header_format->subsettingblockbegin);

    indent += COL;
    indentation(indent);
    printf("%s %s ", dump_header_format->startbegin,
           dump_header_format->startblockbegin);
    dump_dims((hsize_t *)sset->start, dims);
    printf("%s %s\n", dump_header_format->startend,
           dump_header_format->startblockend);

    indentation(indent);
    printf("%s %s ", dump_header_format->stridebegin,
           dump_header_format->strideblockbegin);
    dump_dims(sset->stride, dims);
    printf("%s %s\n", dump_header_format->strideend,
           dump_header_format->strideblockend);

    indentation(indent);
    printf("%s %s ", dump_header_format->countbegin,
           dump_header_format->countblockbegin);

    if (sset->count)
        dump_dims(sset->count, dims);
    else
        printf("DEFAULT");

    printf("%s %s\n", dump_header_format->countend,
           dump_header_format->countblockend);

    indentation(indent);
    printf("%s %s ", dump_header_format->blockbegin,
           dump_header_format->blockblockbegin);

    if (sset->block)
        dump_dims(sset->block, dims);
    else
        printf("DEFAULT");

    printf("%s %s\n", dump_header_format->blockend,
           dump_header_format->blockblockend);
}

/*-------------------------------------------------------------------------
 * Function:    dump_data
 *
 * Purpose:     Dump attribute or dataset data
 *
 * Return:      void
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_data(hid_t obj_id, int obj_data, struct subset_t *sset)
{
    h5dump_t               *outputformat = &dataformat;
    int                     status = -1;
    void                   *buf;
    hid_t                   space, type, p_type;
    int                     ndims, i;
    hsize_t                 size[64], nelmts = 1, alloc_size;
    int                     depth;
    int                     stdindent = COL;	/* should be 3 */

    outputformat->line_ncols = nCols;
    indent += COL;

    /*
     * the depth will tell us how far we need to indent extra.  we use to just
     * use indent but with the merging of the tools lib we have to do
     * something different for the lib funtions... the normal indentation is 6
     * so when we don't need any extra indentation, depth will be 0.
     */
    depth = indent / stdindent + 1;

    if (sset && obj_data == DATASET_DATA) {
        hid_t f_space = H5Dget_space(obj_id);

        dump_subsetting_header(sset, H5Sget_simple_extent_ndims(f_space));
        H5Sclose(f_space);

        /* recalculate the depth of the data */
        depth = indent / stdindent + 1;
    }

    indentation(indent);
    begin_obj(dump_header_format->databegin, (const char *)NULL,
	      dump_header_format->datablockbegin);

    /* Print all the values. */
    if (obj_data == DATASET_DATA) {
	status = h5tools_dump_dset(stdout, outputformat, obj_id, -1, sset, depth);
    } else {
        /* need to call h5tools_dump_mem for the attribute data */    
        type = H5Aget_type(obj_id);
        p_type = h5tools_fixtype(type);
        space = H5Aget_space(obj_id);
        ndims = H5Sget_simple_extent_dims(space, size, NULL);

        for (i = 0; i < ndims; i++)
            nelmts *= size[i];

        alloc_size = nelmts * MAX(H5Tget_size(type), H5Tget_size(p_type));
        assert(alloc_size == (hsize_t)((size_t)alloc_size)); /*check for overflow*/
        buf = malloc((size_t)alloc_size);
        assert(buf);

        if (H5Aread(obj_id, p_type, buf) >= 0)
            status = h5tools_dump_mem(stdout, outputformat, obj_id, p_type,
                                      space, buf, depth);

        free(buf);
        H5Tclose(p_type); 
        H5Sclose(space);
        H5Tclose(type);
    }

    if (status == FAIL) {
        indentation(indent + COL);
        error_msg(progname, "unable to print data\n");
        d_status = EXIT_FAILURE;
    }

    indentation(indent);
    end_obj(dump_header_format->dataend, dump_header_format->datablockend);
    indent -= COL;

    if (sset && obj_data == DATASET_DATA) {
        indentation(indent);
        end_obj(dump_header_format->subsettingend,
                dump_header_format->subsettingblockend);
        indent -= COL;
    }
}

/*-------------------------------------------------------------------------
 * Function:    dump_oid
 *
 * Purpose:     Prints the object ids
 *
 * Return:      void
 *
 * Programmer:  Patrick Lu
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
dump_oid(hid_t oid)
{
    indentation(indent + COL);
    printf("%s %s %d %s\n", OBJID, BEGIN, oid, END);
}

/*-------------------------------------------------------------------------
 * Function:    set_output_file
 *
 * Purpose:     Open fname as the output file for dataset raw data.
 *		Set rawdatastream as its file stream.
 *
 * Return:      0 -- succeeded
 *		negative -- failed
 *
 * Programmer:  Albert Cheng, 2000/09/30
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
set_output_file(const char *fname)
{
    FILE    *f;	/* temporary holding place for the stream pointer
                 * so that rawdatastream is changed only when succeeded */

    if ((f = fopen(fname, "w")) != NULL) {
	rawdatastream = f;
	return 0;
    }

    return -1;
}

/*-------------------------------------------------------------------------
 * Function:    handle_attributes
 *
 * Purpose:     Handle the attributes from the command.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 9. January 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
handle_attributes(hid_t fid, char *attr, void UNUSED *data)
{
    dump_selected_attr(fid, attr);
}

/*-------------------------------------------------------------------------
 * Function:    parse_hsize_list
 *
 * Purpose:     Parse a list of comma or space separated integers and return
 *              them in a list. The string being passed into this function
 *              should be at the start of the list you want to parse. You are
 *              responsible for freeing the array returned from here.
 *
 *              Lists in the so-called "terse" syntax are separated by
 *              semicolons (;). The lists themselves can be separated by
 *              either commas (,) or white spaces.
 *
 * Return:      Success:    hsize_t array. NULL is a valid return type if
 *                          there aren't any elements in the array.
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 6. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hsize_t *
parse_hsize_list(const char *h_list)
{
    hsize_t        *p_list;
    const char     *ptr;
    unsigned int    size_count = 0, i = 0, last_digit = 0;

    if (!h_list || !*h_list || *h_list == ';')
        return NULL;

    /* count how many integers do we have */
    for (ptr = h_list; ptr && *ptr && *ptr != ';' && *ptr != ']'; ptr++)
        if (isdigit(*ptr)) {
            if (!last_digit)
                /* the last read character wasn't a digit */
                size_count++;

            last_digit = 1;
        } else {
            last_digit = 0;
        }

    if (size_count == 0)
        /* there aren't any integers to read */
        return NULL;

    /* allocate an array for the integers in the list */
    p_list = calloc(size_count, sizeof(hsize_t));

    for (ptr = h_list; i < size_count && ptr && *ptr && *ptr != ';' && *ptr != ']'; ptr++)
        if (isdigit(*ptr)) {
            /* we should have an integer now */
            p_list[i++] = (hsize_t)atoi(ptr);

            while (isdigit(*ptr))
                /* scroll to end of integer */
                ptr++;
        }

    return p_list;
}

/*-------------------------------------------------------------------------
 * Function:    parse_subset_params
 *
 * Purpose:     Parse the so-called "terse" syntax for specifying subsetting
 *              parameters.
 *
 * Return:      Success:    struct subset_t object
 *              Failure:    NULL
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 6. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static struct subset_t *
parse_subset_params(char *dset)
{
    struct subset_t *s = NULL;
    register char   *brace;

    if ((brace = strrchr(dset, '[')) != NULL) {
        char *slash = strrchr(dset, '/');

        /* sanity check to make sure the [ isn't part of the dataset name */
        if (brace > slash) {
            *brace++ = '\0';

            s = calloc(1, sizeof(struct subset_t));
            s->start = (hssize_t *)parse_hsize_list(brace);

            while (*brace && *brace != ';')
                brace++;

            if (*brace)
                brace++;

            s->stride = parse_hsize_list(brace);

            while (*brace && *brace != ';')
                brace++;

            if (*brace)
                brace++;

            s->count = parse_hsize_list(brace);

            while (*brace && *brace != ';')
                brace++;

            if (*brace)
                brace++;

            s->block = parse_hsize_list(brace);
        }
    }

    return s;
}

/*-------------------------------------------------------------------------
 * Function:    handle_datasets
 *
 * Purpose:     Handle the datasets from the command.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 9. January 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
handle_datasets(hid_t fid, char *dset, void *data)
{
    H5G_stat_t       statbuf;
    hid_t            dsetid;
    struct subset_t *sset = (struct subset_t *)data;

    if ((dsetid = H5Dopen(fid, dset)) < 0) {
        begin_obj(dump_header_format->datasetbegin, dset,
                  dump_header_format->datasetblockbegin); 
        indentation(COL);
        error_msg(progname, "unable to open dataset \"%s\"\n", dset);
        end_obj(dump_header_format->datasetend,
                dump_header_format->datasetblockend);
        d_status = EXIT_FAILURE;
        return;
    }

    if (sset) {
        if (!sset->start || !sset->stride || !sset->count || !sset->block) {
            /* they didn't specify a ``stride'' or ``block''. default to 1 in all
             * dimensions */
            hid_t sid = H5Dget_space(dsetid);
            unsigned int ndims = H5Sget_simple_extent_ndims(sid);

            if (!sset->start)
                /* default to (0, 0, ...) for the start coord */
                sset->start = calloc(ndims, sizeof(hsize_t));

            if (!sset->stride) {
                unsigned int i;

                sset->stride = calloc(ndims, sizeof(hsize_t));

                for (i = 0; i < ndims; i++)
                    sset->stride[i] = 1;
            }

            if (!sset->count) {
                hsize_t dims[H5S_MAX_RANK];
                herr_t status = H5Sget_simple_extent_dims(sid, dims, NULL);
                unsigned int i;

                if (status == FAIL) {
                    error_msg(progname, "unable to get dataset dimensions\n");
                    d_status = EXIT_FAILURE;
                    H5Sclose(sid);
                    return;
                }

                sset->count = calloc(ndims, sizeof(hsize_t));

                for (i = 0; i < ndims; i++)
                    sset->count[i] = dims[i] - sset->start[i];
            }

            if (!sset->block) {
                unsigned int i;

                sset->block = calloc(ndims, sizeof(hsize_t));

                for (i = 0; i < ndims; i++)
                    sset->block[i] = 1;
            }

            H5Sclose(sid);
        }
    }

    H5Gget_objinfo(dsetid, ".", TRUE, &statbuf);

    if (statbuf.nlink > 1) {
        int idx = search_obj(dset_table, statbuf.objno);

        if (idx >= 0) {
            if (dset_table->objs[idx].displayed) {
                begin_obj(dump_header_format->datasetbegin, dset,
                          dump_header_format->datasetblockbegin);
                indentation(indent + COL);
                printf("%s \"%s\"\n", HARDLINK,
                       dset_table->objs[idx].objname);
                indentation(indent);
                end_obj(dump_header_format->datasetend,
                        dump_header_format->datasetblockend);
            } else {
                strcpy(dset_table->objs[idx].objname, dset);
                dset_table->objs[idx].displayed = 1;
                dump_dataset(dsetid, dset, sset);
            }
        } else {
            d_status = EXIT_FAILURE;
        }
    } else {
        dump_dataset(dsetid, dset, sset);
    }

    if (H5Dclose(dsetid) < 1)
        d_status = EXIT_FAILURE;
}

/*-------------------------------------------------------------------------
 * Function:    handle_groups
 *
 * Purpose:     Handle the groups from the command.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 9. January 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
handle_groups(hid_t fid, char *group, void UNUSED *data)
{
    H5G_stat_t  statbuf;
    hid_t       gid;

    if ((gid = H5Gopen(fid, group)) < 0) {
        begin_obj(dump_header_format->groupbegin, group,
                  dump_header_format->groupblockbegin); 
        indentation(COL);
        error_msg(progname, "unable to open group \"%s\"\n", group);
        end_obj(dump_header_format->groupend,
                dump_header_format->groupblockend);
        d_status = EXIT_FAILURE;
    } else {
        H5Gget_objinfo(gid, ".", TRUE, &statbuf);
        strcpy(prefix, group);
        dump_group(gid, group);

        if (H5Gclose(gid) < 0)
            d_status = EXIT_FAILURE;
    }
}

/*-------------------------------------------------------------------------
 * Function:    handle_links
 *
 * Purpose:     Handle the links from the command.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 9. January 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
handle_links(hid_t fid, char *links, void UNUSED *data)
{
    H5G_stat_t  statbuf;

    if (H5Gget_objinfo(fid, links, FALSE, &statbuf) < 0) {
        begin_obj(dump_header_format->softlinkbegin, links,
                  dump_header_format->softlinkblockbegin);
        indentation(COL);
        error_msg(progname, "unable to get obj info from \"%s\"\n", links);
        end_obj(dump_header_format->softlinkend,
                dump_header_format->softlinkblockend);
        d_status = EXIT_FAILURE;
    } else if (statbuf.type == H5G_LINK) {
        char *buf = malloc(statbuf.linklen);

        begin_obj(dump_header_format->softlinkbegin, links,
                  dump_header_format->softlinkblockbegin);
        indentation(COL);

        if (H5Gget_linkval(fid, links, statbuf.linklen, buf) >= 0) {
            printf("LINKTARGET \"%s\"\n", buf);
        } else {
            error_msg(progname, "h5dump error: unable to get link value for \"%s\"\n",
                      links);
            d_status = EXIT_FAILURE;
        }

        end_obj(dump_header_format->softlinkend,
                dump_header_format->softlinkblockend);
        free(buf);
    } else {
        begin_obj(dump_header_format->softlinkbegin, links,
                  dump_header_format->softlinkblockbegin);
        indentation(COL);
        error_msg(progname, "\"%s\" is not a link\n", links);
        end_obj(dump_header_format->softlinkend,
                dump_header_format->softlinkblockend);
        d_status = EXIT_FAILURE;
    }
}

/*-------------------------------------------------------------------------
 * Function:    handle_datatypes
 *
 * Purpose:     Handle the datatypes from the command.
 *
 * Return:      void
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 9. January 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
handle_datatypes(hid_t fid, char *type, void UNUSED *data)
{
    hid_t       typeid;

    if ((typeid = H5Topen(fid, type)) < 0) {
        /* check if type is unamed data type */
        int idx = 0;

        while (idx < type_table->nobjs ) {
            char name[128], name1[128];

            if (!type_table->objs[idx].recorded) {
                /* unamed data type */
                sprintf(name, "#%lu:%lu\n",
                        type_table->objs[idx].objno[0], 
                        type_table->objs[idx].objno[1]);
                sprintf(name1, "/#%lu:%lu\n",
                        type_table->objs[idx].objno[0], 
                        type_table->objs[idx].objno[1]);

            if (!strncmp(name, type, strlen(type)) || 
                !strncmp(name1, type, strlen(type)))
                break;
            } 

            idx++;
        }

        if (idx ==  type_table->nobjs) {
            /* unknown type */
            begin_obj(dump_header_format->datatypebegin, type,
                      dump_header_format->datatypeblockbegin); 
            indentation(COL);
            error_msg(progname, "unable to open datatype \"%s\"\n", type);
            end_obj(dump_header_format->datatypeend,
                    dump_header_format->datatypeblockend);
            d_status = EXIT_FAILURE;
        } else {
            hid_t dsetid = H5Dopen(fid, type_table->objs[idx].objname);
            typeid = H5Dget_type(dsetid);
            dump_named_datatype(typeid, type);
            H5Tclose(typeid);
            H5Dclose(dsetid);
        }
    } else {
        dump_named_datatype(typeid, type);

        if (H5Tclose(typeid) < 0)
            d_status = EXIT_FAILURE;
    }
}


/*-------------------------------------------------------------------------
 * Function:    parse_command_line
 *
 * Purpose:     Parse the command line for the h5dumper.
 *
 * Return:      Success:    A pointer to an array of handler_t structures.
 *                          These contain all the information needed to dump
 *                          the necessary object.
 *
 *              Failure:    Exits program with EXIT_FAILURE value.
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 20. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static struct handler_t *
parse_command_line(int argc, const char *argv[])
{
    struct handler_t   *hand, *last_dset = NULL;
    int                 i, opt, last_was_dset = FALSE;

    if (argc < 2) {
        usage(progname);
        exit(EXIT_FAILURE);
    }

    /* this will be plenty big enough to hold the info */
    hand = calloc((size_t)argc, sizeof(struct handler_t));

    /* parse command line options */
    while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF) {
parse_start:
        switch ((char)opt) {
#if 0
        case 'b':
            /* binary output */
            break;
#endif  /* 0 */
        case 'B':
            display_bb = TRUE;
            last_was_dset = FALSE;
            break;
        case 'H':
            display_data = FALSE;
            last_was_dset = FALSE;
            break;
        case 'v':
            display_oid = TRUE;
            last_was_dset = FALSE;
            break;
        case 'V':
            print_version(progname);
            exit(EXIT_SUCCESS);
            break;
        case 'w':
            nCols = atoi(opt_arg);
            last_was_dset = FALSE;
            break;
        case 'a':
            display_all = 0;

            for (i = 0; i < argc; i++)
                if (!hand[i].func) {
                    hand[i].func = handle_attributes;
                    hand[i].obj = strdup(opt_arg);
                    break;
                }

            last_was_dset = FALSE;
            break;
        case 'd':
            display_all = 0;

            for (i = 0; i < argc; i++)
                if (!hand[i].func) {
                    hand[i].func = handle_datasets;
                    hand[i].obj = strdup(opt_arg);
                    hand[i].subset_info = parse_subset_params(hand[i].obj);
                    last_dset = hand;
                    break;
                }

            last_was_dset = TRUE;
            break;
        case 'g':
            display_all = 0;

            for (i = 0; i < argc; i++)
                if (!hand[i].func) {
                    hand[i].func = handle_groups;
                    hand[i].obj = strdup(opt_arg);
                    break;
                }

            last_was_dset = FALSE;
            break;
        case 'l':
            display_all = 0;

            for (i = 0; i < argc; i++)
                if (!hand[i].func) {
                    hand[i].func = handle_links;
                    hand[i].obj = strdup(opt_arg);
                    break;
                }

            last_was_dset = FALSE;
            break;
        case 't':
            display_all = 0;

            for (i = 0; i < argc; i++)
                if (!hand[i].func) {
                    hand[i].func = handle_datatypes;
                    hand[i].obj = strdup(opt_arg);
                    break;
                }

            last_was_dset = FALSE;
            break;
        case 'o':
            if (set_output_file(opt_arg) < 0){
                /* failed to set output file */
                usage(progname);
                exit(EXIT_FAILURE);
            }

            usingdasho = TRUE;
            last_was_dset = FALSE;
            break;

        /** begin XML parameters **/
        case 'x':
            /* select XML output */
            doxml = TRUE;
            dump_header_format = &xmlformat;
            dump_function_table = &xml_function_table;
            break;
        case 'D':
            /* specify alternative XML DTD */
            xml_dtd_uri = opt_arg;
            break;
        /** end XML parameters **/

        /** begin subsetting parameters **/
        case 's':
        case 'S':
        case 'c':
        case 'k': {
            struct subset_t *s;

            if (!last_was_dset) {
                error_msg(progname,
                          "option `-%c' can only be used after --dataset option\n",
                          opt);
                exit(EXIT_FAILURE);
            }

            if (last_dset->subset_info) {
                /*
                 * This overrides the "terse" syntax if they actually mixed
                 * the two.
                 */
                s = last_dset->subset_info;
            } else {
                last_dset->subset_info = s = calloc(1, sizeof(struct subset_t));
            }

            /*
             * slightly convoluted, but...we are only interested in options
             * for subsetting: "--start", "--stride", "--count", and "--block"
             * which can come in any order. If we run out of parameters (EOF)
             * or run into one which isn't a subsetting parameter (NOT s, S,
             * c, or K), then we exit the do-while look, set the subset_info
             * to the structure we've been filling. If we've reached the end
             * of the options, we exit the parsing (goto parse_end) otherwise,
             * since we've "read" the next option, we need to parse it. So we
             * jump to the beginning of the switch statement (goto parse_start).
             */
            do {
                switch ((char)opt) {
                case 's': free(s->start); s->start = (hssize_t *)parse_hsize_list(opt_arg); break;
                case 'S': free(s->stride); s->stride = parse_hsize_list(opt_arg); break;
                case 'c': free(s->count); s->count = parse_hsize_list(opt_arg); break;
                case 'k': free(s->block); s->block = parse_hsize_list(opt_arg); break;
                default: goto end_collect;
                }
            } while ((opt = get_option(argc, argv, s_opts, l_opts)) != EOF);

end_collect:
            last_was_dset = FALSE;

            if (opt != EOF)
                goto parse_start;
            else
                goto parse_end;
        }
        /** end subsetting parameters **/

        case 'h':
            usage(progname);
            exit(EXIT_SUCCESS);
        case '?':
        default:
            usage(progname);
            exit(EXIT_FAILURE);
        }
    }

parse_end:
    return hand;
}


/*-------------------------------------------------------------------------
 * Function:    free_handler
 *
 * Purpose:     Convenience function to free the handler_t structures. Needs a
 *              length variable (LEN) to know how many in the array it needs
 *              to free
 *
 * Return:      Nothing
 *
 * Programmer:  Bill Wendling
 *              Tuesday, 20. February 2001
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
free_handler(struct handler_t *hand, int len)
{
    register int i;

    for (i = 0; i < len; i++) {
        free(hand[i].obj);

        if (hand[i].subset_info) {
            free(hand[i].subset_info->start);
            free(hand[i].subset_info->stride);
            free(hand[i].subset_info->count);
            free(hand[i].subset_info->block);
            free(hand[i].subset_info);
        }
    }

    free(hand);
}

/*-------------------------------------------------------------------------
 * Function:    main
 *
 * Purpose:     HDF5 dumper
 *
 * Return:      Success:    0
 *              Failure:    1
 *
 * Programmer:  Ruey-Hsia Li
 *
 * Modifications:
 *        Albert Cheng
 *        30. September 2000
 *        Add the -o option--output file for datasets raw data
 *
 *        REMcG
 *        November 2000
 *        Changes to support XML.
 *
 *        Bill Wendling
 *        Wednesday, 10. January 2001
 *        Modified the way command line parameters are interpreted. They go
 *        through one function call now (get_option).
 *
 *        Bill Wendling
 *        Tuesday, 20. February 2001
 *        Moved command line parsing to separate function. Made various
 *        "display_*" flags global.
 *
 *-------------------------------------------------------------------------
 */
int
main(int argc, const char *argv[])
{
    hid_t               fid, gid;
    const char         *fname = NULL;
    void               *edata;
    hid_t               (*func)(void*);
    find_objs_t         info;
    struct handler_t   *hand;
    int                 i;

    if (argc < 2) {
        usage(progname);
        exit(EXIT_FAILURE);
    }

    dump_header_format = &standardformat;
    dump_function_table = &ddl_function_table;

    /* Disable error reporting */
    H5Eget_auto(&func, &edata);
    H5Eset_auto(NULL, NULL);

    /* Initialize h5tools lib */
    h5tools_init();
    hand = parse_command_line(argc, argv);

    /* Check for conflicting options */
    if (doxml) {
	if (!display_all) {
            error_msg(progname, "option \"%s\" not available for XML\n",
		      "to display selected objects");
	    exit(EXIT_FAILURE);
	} else if (display_bb) {
            error_msg(progname, "option \"%s\" not available for XML\n",
		      "--boot-block");
	    exit(EXIT_FAILURE);
	} else if (!display_data) {
            error_msg(progname, "option \"%s\" not available for XML\n",
		      "--header");
	    exit(EXIT_FAILURE);
	} else if (display_oid == 1) {
            error_msg(progname, "option \"%s\" not available for XML\n",
		      "--object-ids");
	    exit(EXIT_FAILURE);
	} else if (usingdasho) {
            error_msg(progname, "option \"%s\" not available for XML\n",
		      "--output");
	    exit(EXIT_FAILURE);
	}
    } else {
        if (xml_dtd_uri) {
            warn_msg(progname, "option \"%s\" only applies with XML: %s\n",
                     "--xml-dtd", xml_dtd_uri);
        }
    }

    if (argv[opt_ind][0] == '\\')
	fname = &argv[opt_ind][1];
    else
	fname = argv[opt_ind];

    fid = h5tools_fopen(fname, NULL, 0);

    if (fid < 0) {
        error_msg(progname, "unable to open file \"%s\"\n", fname);
        exit(EXIT_FAILURE);
    }

    /* allocate and initialize internal data structure */
    init_table(&group_table);
    init_table(&type_table);
    init_table(&dset_table);
    init_prefix(&prefix, prefix_len);

    /* init the find_objs_t */
    info.threshold = 0;
    info.prefix_len = prefix_len;
    info.prefix = calloc((size_t)info.prefix_len, 1);
    info.group_table = group_table;
    info.type_table = type_table;
    info.dset_table = dset_table;
    info.status = d_status;

    if (doxml) {
	/* initialize XML */
	thefile = fid;

	/* find all objects that might be targets of a refernce */
	if ((gid = H5Gopen(fid, "/")) < 0) {
            error_msg(progname, "unable to open root group\n");
            d_status = EXIT_FAILURE;
            goto done;
	}

	ref_path_table_put(gid, "/");
	H5Giterate(fid, "/", NULL, fill_ref_path_table, NULL);
	H5Gclose(gid);

	/* reset prefix! */
	strcpy(prefix, "");

	/* make sure the URI is initialized to something */
	if (xml_dtd_uri == NULL)
	    xml_dtd_uri = DEFAULT_DTD;
    }

    /* find all shared objects */
    H5Giterate(fid, "/", NULL, find_objs, (void *)&info);

    /* does there exist unamed committed data type */
    for (i = 0; i < type_table->nobjs; i++)
        if (type_table->objs[i].recorded == 0)
            unamedtype = 1;

    dump_tables();

    if (info.status) {
        error_msg(progname, "internal error (file %s:line %d)\n",
                  __FILE__, __LINE__);
        d_status = EXIT_FAILURE;
        goto done;
    }

    /* start to dump */
    if (!doxml) {
	begin_obj(dump_header_format->filebegin, fname,
		  dump_header_format->fileblockbegin);
    } else {
	printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	printf("<!DOCTYPE HDF5-File PUBLIC \"HDF5-File.dtd\" \"%s\">\n",
	       xml_dtd_uri);
	printf("<HDF5-File>\n");
    }

    if (display_bb)
	dump_bb();

    if (display_all) {
        if ((gid = H5Gopen(fid, "/")) < 0) {
            error_msg(progname, "unable to open root group\n");
            d_status = EXIT_FAILURE;
        } else {
	    dump_function_table->dump_group_function(gid, "/");
        }

        if (H5Gclose(gid) < 0) {
            error_msg(progname, "unable to close root group\n");
            d_status = EXIT_FAILURE;
        }
    } else {
	if (doxml) {
	    /* Note: this option is not supported for XML */
            error_msg(progname, "internal error (file %s:line %d)\n",
                      __FILE__, __LINE__);
            d_status = EXIT_FAILURE;
	    goto done;
	}

        for (i = 0; i < argc; i++)
            if (hand[i].func)
                hand[i].func(fid, hand[i].obj, hand[i].subset_info);
    }

    if (!doxml) {
	end_obj(dump_header_format->fileend,
		dump_header_format->fileblockend);
    } else {
	printf("%s\n", dump_header_format->fileend);
    }

done:
    if (H5Fclose(fid) < 0)
	d_status = EXIT_FAILURE;

    free_handler(hand, argc);

    free(group_table->objs);
    free(dset_table->objs);
    free(type_table->objs);
    free(prefix);
    free(info.prefix);

    /* To Do:  clean up XML table */

    h5tools_close();
    H5Eset_auto(func, edata);
    return d_status;
}

/*-------------------------------------------------------------------------
 * Function:    print_enum
 *
 * Purpose:     prints the enum data - 
 *
 * Return:      void
 *
 * Programmer:  Patrick Lu
 *
 * Modifications:
 *
 * NOTE: this function was taken from h5ls. should be moved into the toolslib
 * 
 *-----------------------------------------------------------------------*/
static void
print_enum(hid_t type)
{
    char           **name = NULL;   /*member names                   */
    unsigned char   *value = NULL;  /*value array                    */
    int              nmembs;        /*number of members              */
    int              nchars;        /*number of output characters    */
    hid_t            super;         /*enum base integer type         */
    hid_t            native = -1;   /*native integer data type       */
    size_t           dst_size;      /*destination value type size    */
    int              i;

    nmembs = H5Tget_nmembers(type);
    super = H5Tget_super(type);

    /*
     * Determine what data type to use for the native values.  To simplify
     * things we entertain three possibilities:
     *  1. long_long -- the largest native signed integer
     *    2. unsigned long_long -- the largest native unsigned integer
     *    3. raw format
     */
    if (H5Tget_size(type) <= sizeof(long_long)) {
	dst_size = sizeof(long_long);

	if (H5T_SGN_NONE == H5Tget_sign(type)) {
	    native = H5T_NATIVE_ULLONG;
	} else {
	    native = H5T_NATIVE_LLONG;
	}
    } else {
	dst_size = H5Tget_size(type);
    }

    /* Get the names and raw values of all members */
    assert(nmembs>0);
    name = calloc((size_t)nmembs, sizeof(char *));
    value = calloc((size_t)nmembs, MAX(H5Tget_size(type), dst_size));

    for (i = 0; i < nmembs; i++) {
	name[i] = H5Tget_member_name(type, i);
	H5Tget_member_value(type, i, value + i * H5Tget_size(type));
    }

    /* Convert values to native data type */
    if (native > 0)
        H5Tconvert(super, native, (hsize_t)nmembs, value, NULL, H5P_DEFAULT);

    /*
     * Sort members by increasing value
     *    ***not implemented yet***
     */

    /* Print members */
    for (i = 0; i < nmembs; i++) {
	indentation(indent + COL);
	nchars = printf("\"%s\"", name[i]);
	printf("%*s   ", MAX(0, 16 - nchars), "");

	if (native < 0) {
            size_t j;

	    printf("0x");

	    for (j = 0; j < dst_size; j++)
		printf("%02x", value[i * dst_size + j]);
	} else if (H5T_SGN_NONE == H5Tget_sign(native)) {
	    HDfprintf(stdout,"%" PRINTF_LL_WIDTH "u", *((unsigned long_long *)
					      ((void *) (value + i * dst_size))));
	} else {
	    HDfprintf(stdout,"%" PRINTF_LL_WIDTH "d",
		   *((long_long *) ((void *) (value + i * dst_size))));
	}

	printf(";\n");
    }

    /* Release resources */
    for (i = 0; i < nmembs; i++)
	free(name[i]);

    free(name);
    free(value);
    H5Tclose(super);

    if (0 == nmembs)
	printf("\n%*s <empty>", indent + 4, "");
}

/*
 *   XML support
 */

/*
 *  XML needs a table to look up a path name for an object
 *  reference.
 *
 *  This table stores mappings of reference -> path
 *  for all objects in the file that may be the target of
 *  an object reference.
 *
 *  The 'path' is an absolute path by which the object
 *  can be accessed.  When an object has > 1 such path,
 *  only one will be used in the table, with no particular
 *  method of selecting which one.
 */

struct ref_path_table_entry_t {
    hsize_t                 obj;
    hobj_ref_t             *obj_ref;
    char                   *apath;
    struct ref_path_table_entry_t *next;
};

struct ref_path_table_entry_t *ref_path_table = NULL;	/* the table */
int                     npte = 0;	/* number of entries in the table */

/*-------------------------------------------------------------------------
 * Function:    ref_path_table_lookup
 *
 * Purpose:     Looks up a table entry given a path name.
 *              Used during construction of the table.
 *
 * Return:      The table entre (pte) or NULL if not in the
 *              table.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static struct ref_path_table_entry_t *
ref_path_table_lookup(const char *thepath)
{
    int                     i;
    hobj_ref_t             *ref;
    herr_t                  status;
    struct ref_path_table_entry_t *pte = ref_path_table;

    if (ref_path_table == NULL)
	return NULL;

    ref = (hobj_ref_t *) malloc(sizeof(hobj_ref_t));

    if (ref == NULL) {
	/*  fatal error ? */
	return NULL;
    }

    status = H5Rcreate(ref, thefile, thepath, H5R_OBJECT, -1);

    if (status < 0) {
	/*  fatal error ? */
	return NULL;
    }

    for (i = 0; i < npte; i++) {
	if (memcmp(ref, pte->obj_ref, sizeof(hobj_ref_t)) == 0) {
	    return pte;
	}

	pte = pte->next;
    }

    return NULL;
}

/*-------------------------------------------------------------------------
 * Function:    ref_path_table_put
 *
 * Purpose:     Enter the 'obj' with 'path' in the table if
 *              not already there.
 *              Create an object reference, pte, and store them
 *              in the table.
 *
 * Return:      The object reference for the object.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hobj_ref_t *
ref_path_table_put(hid_t obj, const char *path)
{
    hobj_ref_t             *ref;
    herr_t                  status;
    struct ref_path_table_entry_t *pte;

    /* look up 'obj'.  If already in table, return */
    pte = ref_path_table_lookup(path);
    if (pte != NULL)
	return pte->obj_ref;

    /* if not found, then make new entry */

    pte = (struct ref_path_table_entry_t *)
	malloc(sizeof(struct ref_path_table_entry_t));
    if (pte == NULL) {
	/* fatal error? */
	return NULL;
    }

    pte->obj = obj;
    ref = (hobj_ref_t *) malloc(sizeof(hobj_ref_t));
    if (ref == NULL) {
	/* fatal error? */
	free(pte);
	return NULL;
    }

    status = H5Rcreate(ref, thefile, path, H5R_OBJECT, -1);
    if (status < 0) {
	/* fatal error? */
	free(ref);
	free(pte);
	return NULL;
    }

    pte->obj_ref = ref;

    pte->apath = strdup(path);

    pte->next = ref_path_table;
    ref_path_table = pte;

    npte++;

    return ref;
}

/*-------------------------------------------------------------------------
 * Function:    lookup_ref_path
 *
 * Purpose:     Lookup the path to the object with refernce 'ref'.
 *
 * Return:      Return a path to the object, or NULL if not found.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static char                   *
lookup_ref_path(hobj_ref_t * ref)
{
    int                     i;
    struct ref_path_table_entry_t *pte = NULL;

    if (ref_path_table == NULL)
	return NULL;

    pte = ref_path_table;
    if (pte == NULL) {
	/* fatal -- not initialized? */
	return NULL;
    }
    for (i = 0; i < npte; i++) {
	if (memcmp(ref, pte->obj_ref, sizeof(hobj_ref_t)) == 0) {
	    return pte->apath;
	}
	pte = pte->next;
    }
    return NULL;
}

/*-------------------------------------------------------------------------
 * Function:    fill_ref_path_table
 *
 * Purpose:     Called by interator to create references for
 *              all objects and enter them in the table.
 *
 * Return:      Error status.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
fill_ref_path_table(hid_t group, const char *name, void UNUSED * op_data)
{
    hid_t                   obj;
    char                   *tmp;
    H5G_stat_t              statbuf;
    struct ref_path_table_entry_t *pte;
    char                   *thepath;

    H5Gget_objinfo(group, name, FALSE, &statbuf);
    tmp = (char *) malloc(strlen(prefix) + strlen(name) + 2);

    if (tmp == NULL)
	return FAIL;

    thepath = (char *) malloc(strlen(prefix) + strlen(name) + 2);

    if (thepath == NULL) {
	free(tmp);
	return FAIL;
    }

    strcpy(tmp, prefix);

    strcpy(thepath, prefix);
    strcat(thepath, "/");
    strcat(thepath, name);

    switch (statbuf.type) {
    case H5G_DATASET:
	if ((obj = H5Dopen(group, name)) >= 0) {
	    pte = ref_path_table_lookup(thepath);
	    if (pte == NULL) {
		ref_path_table_put(obj, thepath);
	    }
	    H5Dclose(obj);
	} else {
            error_msg(progname, "unable to get dataset \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
	}
	break;
    case H5G_GROUP:
	if ((obj = H5Gopen(group, name)) >= 0) {
	    strcat(strcat(prefix, "/"), name);
	    pte = ref_path_table_lookup(thepath);
	    if (pte == NULL) {
		ref_path_table_put(obj, thepath);
		H5Giterate(obj, ".", NULL, fill_ref_path_table, NULL);
		strcpy(prefix, tmp);
	    }
	    H5Gclose(obj);
	} else {
            error_msg(progname, "unable to dump group \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
	}
	break;
    case H5G_TYPE:
	if ((obj = H5Topen(group, name)) >= 0) {
	    pte = ref_path_table_lookup(thepath);
	    if (pte == NULL) {
		ref_path_table_put(obj, thepath);
	    }
	    H5Tclose(obj);
	} else {
            error_msg(progname, "unable to get dataset \"%s\"\n", name);
	    d_status = EXIT_FAILURE;
	}
	break;
    default:
        break;
    }

    free(tmp);
    free(thepath);
    return 0;
}

static const char      *quote = "&quot;";
static const char      *amp = "&amp;";
static const char      *lt = "&lt;";
static const char      *gt = "&gt;";
static const char      *apos = "&apos;";

/*-------------------------------------------------------------------------
 * Function:    xml_escape_the_name
 *
 * Purpose:     Escape XML reserved chars in a name, so HDF5 strings
 *              and paths can be correctly read back in XML element.
 *
 * Return:      The revised string.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static char                   *
xml_escape_the_name(const char *str)
{
    int                     extra;
    int                     len;
    int                     i;
    const char             *cp;
    char                   *ncp;
    char                   *rcp;

    if (!str)
	return NULL;

    cp = str;
    len = strlen(str);
    extra = 0;

    for (i = 0; i < len; i++) {
	if (*cp == '\"') {
	    extra += (strlen(quote) - 1);
	} else if (*cp == '\'') {
	    extra += (strlen(apos) - 1);
	} else if (*cp == '<') {
	    extra += (strlen(lt) - 1);
	} else if (*cp == '>') {
	    extra += (strlen(gt) - 1);
	} else if (*cp == '&') {
	    extra += (strlen(amp) - 1);
	}

	cp++;
    }

    if (extra == 0)
	return strdup(str);

    cp = str;
    rcp = ncp = calloc((size_t)(len + extra + 1), sizeof(char));

    if (!ncp)
        return NULL;    /* ?? */

    for (i = 0; i < len; i++) {
        if (*cp == '\'') {
            strncpy(ncp, apos, strlen(apos));
            ncp += strlen(apos);
            cp++;
        } else if (*cp == '<') {
            strncpy(ncp, lt, strlen(lt));
            ncp += strlen(lt);
            cp++;
        } else if (*cp == '>') {
            strncpy(ncp, gt, strlen(gt));
            ncp += strlen(gt);
            cp++;
        } else if (*cp == '\"') {
            strncpy(ncp, quote, strlen(quote));
            ncp += strlen(quote);
            cp++;
        } else if (*cp == '&') {
            strncpy(ncp, amp, strlen(amp));
            ncp += strlen(amp);
            cp++;
        } else {
            *ncp++ = *cp++;
        }
    }

    *ncp = '\0';
    return rcp;
}

/*-------------------------------------------------------------------------
 * Function:    xml_escape_the_string
 *
 * Purpose:     Escape XML reserved chars in a string, so HDF5 strings
 *              and paths can be correctly read back in XML CDATA.
 *
 * Return:      The revised string.
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static char                   *
xml_escape_the_string(const char *str, int slen)
{
    int                     extra;
    int                     len;
    int                     i;
    const char             *cp;
    char                   *ncp;
    char                   *rcp;

    if (!str)
	return NULL;

    cp = str;

    if (slen < 0)
	len = strlen(str);
    else
	len = slen;

    extra = 0;

    for (i = 0; i < len; i++) {
	if (*cp == '\\') {
	    extra++;
	} else if (*cp == '\"') {
	    extra++;
	} else if (*cp == '\'') {
	    extra += (strlen(apos) - 1);
	} else if (*cp == '<') {
	    extra += (strlen(lt) - 1);
	} else if (*cp == '>') {
	    extra += (strlen(gt) - 1);
	} else if (*cp == '&') {
	    extra += (strlen(amp) - 1);
	}
	cp++;
    }

    cp = str;
    rcp = ncp = calloc((size_t)(len + extra + 1), sizeof(char));

    if (ncp == NULL)
	return NULL;		/* ?? */

    for (i = 0; i < len; i++) {
	if (*cp == '\\') {
	    *ncp++ = '\\';
	    *ncp++ = *cp++;
	} else if (*cp == '\"') {
	    *ncp++ = '\\';
	    *ncp++ = *cp++;
	} else if (*cp == '\'') {
	    strncpy(ncp, apos, strlen(apos));
	    ncp += strlen(apos);
	    cp++;
	} else if (*cp == '<') {
	    strncpy(ncp, lt, strlen(lt));
	    ncp += strlen(lt);
	    cp++;
	} else if (*cp == '>') {
	    strncpy(ncp, gt, strlen(gt));
	    ncp += strlen(gt);
	    cp++;
	} else if (*cp == '&') {
	    strncpy(ncp, amp, strlen(amp));
	    ncp += strlen(amp);
	    cp++;
	} else {
	    *ncp++ = *cp++;
	}
    }

    *ncp = '\0';
    return rcp;
}

/**
 **  XML print functions--these replace some functions in the
 **  h5tools.c suite.
 **/

/*-------------------------------------------------------------------------
 * Function:    xml_print_datatype
 *
 * Purpose:     Print description of a datatype in XML.
 *              Note:  this is called inside a <DataType> element.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_print_datatype(hid_t type)
{
    char                   *fname;
    hid_t                   nmembers, mtype;
    int                     i, j, ndims, perm[H5DUMP_MAX_RANK];
    size_t                  size;
    hsize_t                 dims[H5DUMP_MAX_RANK];
    H5T_str_t               str_pad;
    H5T_cset_t              cset;
    H5G_stat_t              statbuf;
    hid_t                   super;
    H5T_order_t             ord;
    H5T_sign_t              sgn;
    size_t                  sz;
    size_t                  spos;
    size_t                  epos;
    size_t                  esize;
    size_t                  mpos;
    size_t                  msize;
    int                     nmembs;

    switch (H5Tget_class(type)) {
    case H5T_INTEGER:
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	/* <IntegerType ByteOrder="bo" Sign="torf" Size="bytes"/> */
	ord = H5Tget_order(type);
	sgn = H5Tget_sign(type);
	indentation(indent);
	printf("<IntegerType ByteOrder=\"");
	switch (ord) {
	case H5T_ORDER_LE:
	    printf("LE");
	    break;
	case H5T_ORDER_BE:
	    printf("BE");
	    break;
	case H5T_ORDER_VAX:
	default:
	    printf("ERROR_UNKNOWN");
	}
	printf("\" Sign=\"");
	switch (sgn) {
	case H5T_SGN_NONE:
	    printf("false");
	    break;
	case H5T_SGN_2:
	    printf("true");
	    break;
	default:
	    printf("ERROR_UNKNOWN");
	}
	printf("\" Size=\"");
	sz = H5Tget_size(type);
	printf("%d", sz);
	printf("\" />\n");
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_FLOAT:
	/* <FloatType ByteOrder="bo" Size="bytes" 
	   SignBitLocation="bytes"
	   ExponentBits="eb" ExponentLocation="el" 
	   MantissaBits="mb" MantissaLocation="ml" /> */
	ord = H5Tget_order(type);
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<FloatType ByteOrder=\"");
	switch (ord) {
	case H5T_ORDER_LE:
	    printf("LE");
	    break;
	case H5T_ORDER_BE:
	    printf("BE");
	    break;
	case H5T_ORDER_VAX:
	default:
	    printf("ERROR_UNKNOWN");
	}
	printf("\" Size=\"");
	sz = H5Tget_size(type);
	printf("%d", sz);
	H5Tget_fields(type, &spos, &epos, &esize, &mpos, &msize);
	printf("\" SignBitLocation=\"%d\" ", spos);
	printf("ExponentBits=\"%d\" ExponentLocation=\"%d\" ", esize, epos);
	printf("MantissaBits=\"%d\" MantissaLocation=\"%d\" />\n",
	       msize, mpos);
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_TIME:
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<TimeType />\n");
	printf("<!-- H5T_TIME: not yet implemented -->");
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_STRING:
	/* <StringType Cset="cs" StrSize="chars" StrPad="pad" /> */
	size = H5Tget_size(type);
	str_pad = H5Tget_strpad(type);
	cset = H5Tget_cset(type);

	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<StringType Cset=\"");
	if (cset == H5T_CSET_ASCII) {
	    printf("H5T_CSET_ASCII\" ");
	} else {
	    printf("unknown_cset\" ");
	}
	printf("StrSize=\"%d\" StrPad=\"", (int) size);
	if (str_pad == H5T_STR_NULLTERM) {
	    printf("H5T_STR_NULLTERM\"/>\n");
	} else if (str_pad == H5T_STR_NULLPAD) {
	    printf("H5T_STR_NULLPAD\"/>\n");
	} else if (str_pad == H5T_STR_SPACEPAD) {
	    printf("H5T_STR_SPACEPAD\"/>\n");
	} else {
	    printf("H5T_STR_ERROR\"/>\n");
	}
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_BITFIELD:
	/* <BitfieldType ByteOrder="bo" Size="bytes"/> */
	ord = H5Tget_order(type);
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<BitfieldType ByteOrder=\"");
	switch (ord) {
	case H5T_ORDER_LE:
	    printf("LE");
	    break;
	case H5T_ORDER_BE:
	    printf("BE");
	    break;
	case H5T_ORDER_VAX:
	default:
	    printf("ERROR_UNKNOWN");
	}
	size = H5Tget_size(type);
	printf("\" Size=\"%d\"/>\n", size);
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_OPAQUE:
	/* <OpaqueType Tag="tag" Size="bytes" /> */

	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<OpaqueType Tag=\"%s\" ", H5Tget_tag(type));
	size = H5Tget_size(type);
	printf("Size=\"%d\"/>\n", size);
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_COMPOUND:
	/* recursively describe the components of a compound datatype */
	if (H5Tcommitted(type) > 0) {
	    /* detect a shared datatype, output only once */
	    H5Gget_objinfo(type, ".", TRUE, &statbuf);
	    i = search_obj(type_table, statbuf.objno);

	    if (i >= 0) {
		/* This should be defined somewhere else */
		if (!type_table->objs[i].recorded) {
		    /* 'anonymous' NDT.  Use it's object num.
		       as it's name.  */
		    printf("<NamedDataTypePtr OBJ-XID=\"/#%lu:%lu\"/>\n",
			   type_table->objs[i].objno[0],
			   type_table->objs[i].objno[1]);
		} else {
		    /* point to the NDT by name */
                    char *t_objname = xml_escape_the_name(type_table->objs[i].objname);

		    printf("<NamedDataTypePtr OBJ-XID=\"%s\"/>\n", t_objname);
                    free(t_objname);
		}
	    } else {
		printf("<!-- h5dump error: unknown committed type. -->\n");
		d_status = EXIT_FAILURE;
	    }

	} else {
	    /* type of a dataset */
	    nmembers = H5Tget_nmembers(type);

	    indentation(indent);
	    printf("<CompoundType>\n");

	    /* List each member Field of the type */
	    /*   <Field FieldName="name" > */
	    /*   <DataType > */
	    indent += COL;
	    for (i = 0; i < nmembers; i++) {
                char *t_fname;

		fname = H5Tget_member_name(type, i);
		mtype = H5Tget_member_type(type, i);
		indentation(indent);
                t_fname = xml_escape_the_name(fname);
		printf("<Field FieldName=\"%s\">\n", t_fname);

		free(fname);
		free(t_fname);
		indent += COL;
		indentation(indent);
		printf("<DataType>\n");
		indent += COL;
		xml_print_datatype(mtype);
		indent -= COL;
		indentation(indent);
		printf("%s\n", dump_header_format->datatypeend);
		indent -= COL;

		indentation(indent);
		printf("</Field>\n");
	    }
	    indent -= COL;
	    indentation(indent);
	    printf("</CompoundType>\n");
	}
	break;

    case H5T_REFERENCE:
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	/*  Only Object references supported at this time */
	printf("<ReferenceType>\n");
	indentation(indent + COL);
	printf("<ObjectReferenceType />\n");
	indentation(indent);
	printf("</ReferenceType>\n");
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_ENUM:
	/*  <EnumType Nelems="ne" >
	   list Name, values of enum
	 */
	nmembs = H5Tget_nmembers(type);
	indentation(indent);
	printf("<AtomicType>\n");
	indent += COL;
	indentation(indent);
	printf("<EnumType Nelems=\"%d\">\n", nmembs);
	xml_print_enum(type);
	indentation(indent);
	printf("</EnumType>\n");
	indent -= COL;
	indentation(indent);
	printf("</AtomicType>\n");
	break;

    case H5T_VLEN:
	indentation(indent);
	printf("<VLType>\n");
	super = H5Tget_super(type);
	indent += COL;
	indentation(indent);
	printf("<DataType>\n");
	indent += COL;
	xml_print_datatype(super);
	indent -= COL;
	indentation(indent);
	printf("%s\n", dump_header_format->datatypeend);
	indent -= COL;
	indentation(indent);
	printf("</VLType>\n");
	H5Tclose(super);

	break;

    case H5T_ARRAY:
	/* Get array base type */
	super = H5Tget_super(type);

	/* Print lead-in */
	indentation(indent);
	printf("<ArrayType Ndims=\"");
	ndims = H5Tget_array_ndims(type);
	printf("%d\">\n", ndims);

	/* Get array information */
	H5Tget_array_dims(type, dims, perm);

	/* list of dimensions */
	indent += COL;
	if (perm != NULL) {
	    /* for each dimension, list */
	    for (j = 0; j < ndims; j++) {
		indentation(indent);
		printf("<ArrayDimension DimSize=\"%u\" DimPerm=\"%u\"/>\n",
		       (int) dims[j], (int) perm[j]);
	    }
	} else {
	    for (j = 0; j < ndims; j++) {
		indentation(indent);
		printf("<ArrayDimension DimSize=\"%u\" DimPerm=\"0\"/>\n",
		       (int) dims[j]);
	    }
	}
	indent -= COL;

	indent += COL;
	indentation(indent);
	printf("<DataType>\n");
	indent += COL;
	xml_print_datatype(super);
	indent -= COL;
	indentation(indent);
	printf("%s\n", dump_header_format->datatypeend);
	indent -= COL;
	indentation(indent);
	printf("</ArrayType>\n");
	/* Close array base type */
	H5Tclose(super);
	break;

    default:
	printf("<!-- unknown data type -->");
	d_status = EXIT_FAILURE;
	break;
    }
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_datatype
 *
 * Purpose:     Dump description of a datatype in XML.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_datatype(hid_t type)
{
    int                     i;
    H5G_stat_t              statbuf;

    indent += COL;
    indentation(indent);

    if (H5Tcommitted(type) > 0) {
	/* Data type is a shared or named data type */
	H5Gget_objinfo(type, ".", TRUE, &statbuf);
	i = search_obj(type_table, statbuf.objno);

	if (i >= 0) {
	    /* Shared data type, must be entered as an object  */
	    if (!type_table->objs[i].recorded) {
		/* anonymous stored data type:
		   following the dumper's current
		   practice:
		   use it's object ref as its name
		 */
		printf("<NamedDataTypePtr OBJ-XID=\"/#%lu:%lu\"/>\n",
		       type_table->objs[i].objno[0],
		       type_table->objs[i].objno[1]);
	    } else {
		/* pointer to a named data type already in XML */
                char *t_objname = xml_escape_the_name(type_table->objs[i].objname);

		printf("<NamedDataTypePtr OBJ-XID=\"%s\"/>\n", t_objname);
                free(t_objname);
	    }
	} else {
	    printf("<!-- h5dump error: unknown committed type. -->\n");
	}
	indent -= COL;
	return;
    }
    printf("%s %s\n", dump_header_format->datatypebegin,
	   dump_header_format->datatypeblockbegin);
    indent += COL;
    xml_print_datatype(type);
    indent -= COL;
    indentation(indent);
    printf("%s\n", dump_header_format->datatypeend);
    indent -= COL;
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_dataspace
 *
 * Purpose:     Dump description of a dataspace in XML.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_dataspace(hid_t space)
{
    hsize_t                 size[H5DUMP_MAX_RANK];
    hsize_t                 maxsize[H5DUMP_MAX_RANK];
    int                     ndims =
	H5Sget_simple_extent_dims(space, size, maxsize);
    int                     i;

    indentation(indent + COL);
    printf("%s\n", dump_header_format->dataspacebegin);
    if (H5Sis_simple(space)) {
	indentation(indent + COL + COL);

	if (ndims == 0) {
	    /* scalar dataspace (just a tag, no XML attrs. defined */
	    printf("<ScalarDataspace />\n");
	} else {
	    /* simple dataspace */
	    /* <SimpleDataspace Ndims="nd"> */
	    printf("<SimpleDataspace Ndims=\"%d\">\n", ndims);

	    /* print the <Dimension> elements */
	    for (i = 0; i < ndims; i++) {
		indentation(indent + COL + COL + COL);
		if (maxsize[i] == H5S_UNLIMITED) {
		    HDfprintf(stdout,
			      "<Dimension  DimSize=\"%Hu\" MaxDimSize=\"UNLIMITED\"/>\n",
			      size[i]);
		} else if (maxsize[i] == (hsize_t) 0) {
		    HDfprintf(stdout,
			      "<Dimension  DimSize=\"%Hu\" MaxDimSize=\"%Hu\"/>\n",
			      size[i], size[i]);
		} else {
		    HDfprintf(stdout,
			      "<Dimension  DimSize=\"%Hu\" MaxDimSize=\"%Hu\"/>\n",
			      size[i], maxsize[i]);
		}
	    }
	    indentation(indent + COL + COL);
	    printf("</SimpleDataspace>\n");
	}
    } else {
	printf("<!-- not yet implemented -->\n");
    }

    indentation(indent + COL);
    printf("%s\n", dump_header_format->dataspaceend);

}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_data
 *
 * Purpose:     Dump description of data in XML.
 *              Note that this calls the h5dump_xxx calls in
 *              the h5tools library.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_data(hid_t obj_id, int obj_data, struct subset_t UNUSED *sset)
{
    h5dump_t               *outputformat = &xml_dataformat;
    int                     status = -1;
    void                   *buf;
    hid_t                   space, type, p_type;
    int                     ndims, i;
    hsize_t                 size[64], nelmts = 1;
    int                     depth;
    int                     stdindent = COL;	/* should be 3 */

    outputformat->line_ncols = nCols;
    indent += COL;

    /*
     * the depth will tell us how far we need to indent extra.  we use to just
     * use indent but with the merging of the tools lib we have to do
     * something different for the lib funtions... the normal indentation is 6
     * so when we don't need any extra indentation, depth will be 0.
     */
    depth = indent / stdindent + 1;

    /* Print all the values. */
    indentation(indent);
    printf("%s\n", dump_header_format->databegin);
    indentation(indent + COL);
    printf("<DataFromFile>\n");
    if (obj_data == DATASET_DATA) {
	type = H5Dget_type(obj_id);
	if (H5Tget_class(type) == H5T_REFERENCE) {
	    status = xml_print_refs(obj_id, DATASET_DATA);
	} else if (H5Tget_class(type) == H5T_STRING) {
	    status = xml_print_strs(obj_id, DATASET_DATA);
	} else {
	    status = h5tools_dump_dset(stdout, outputformat, obj_id, -1, NULL, depth);
	}
    } else {
	/* Attribute data */
	type = H5Aget_type(obj_id);

	if (H5Tget_class(type) == H5T_REFERENCE) {
	    /* references are done differently than
	       the standard output:
	       XML dumps a path to the object
	       referenced.
	     */
	    status = xml_print_refs(obj_id, ATTRIBUTE_DATA);
	    H5Tclose(type);
	} else if (H5Tget_class(type) == H5T_STRING) {
	    status = xml_print_strs(obj_id, ATTRIBUTE_DATA);
	} else {
	    /* all other data */
	    p_type = h5tools_fixtype(type);
	    H5Tclose(type);

	    space = H5Aget_space(obj_id);

	    ndims = H5Sget_simple_extent_dims(space, size, NULL);

	    for (i = 0; i < ndims; i++)
		nelmts *= size[i];

	    buf =
		malloc((size_t)(nelmts * MAX(H5Tget_size(type), H5Tget_size(p_type))));
	    assert(buf);

	    if (H5Aread(obj_id, p_type, buf) >= 0)
                status = h5tools_dump_mem(stdout, outputformat, obj_id,
                                          p_type, space, buf, depth);

	    free(buf);
	    H5Tclose(p_type);
	    H5Sclose(space);
	    H5Tclose(type);
	}
    }

    if (status == FAIL) {
	indentation(indent + COL);
	printf("Unable to print data.\n");
	status = 1;
    }

    indentation(indent + COL);
    printf("</DataFromFile>\n");
    indentation(indent);
    printf("%s\n", dump_header_format->dataend);
    indent -= COL;
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_attr
 *
 * Purpose:     Dump a description of an HDF5 attribute in XML.
 *
 * Return:      herr_t
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
xml_dump_attr(hid_t attr, const char *attr_name, void UNUSED * op_data)
{
    hid_t   attr_id, type, space;
    char   *t_aname = xml_escape_the_name(attr_name);

    indentation(indent);
    printf("<Attribute Name=\"%s\">\n", t_aname);
    free(t_aname);

    if ((attr_id = H5Aopen_name(attr, attr_name)) >= 0) {
	type = H5Aget_type(attr_id);
	space = H5Aget_space(attr_id);

	dump_function_table->dump_dataspace_function(space);
	dump_function_table->dump_datatype_function(type);

	if (display_data) {
	    switch (H5Tget_class(type)) {
	    case H5T_INTEGER:
	    case H5T_FLOAT:
	    case H5T_STRING:
	    case H5T_BITFIELD:
	    case H5T_OPAQUE:
	    case H5T_ENUM:
	    case H5T_ARRAY:
		dump_function_table->dump_data_function(attr_id, ATTRIBUTE_DATA, NULL);
		break;

	    case H5T_TIME:
		indent += COL;
		indentation(indent);
		printf("<Data>\n");
		indentation(indent);
		printf("<!-- Time data not yet implemented. -->\n");
		indentation(indent);
		printf("<NoData />\n");
		indentation(indent);
		printf("<Data>\n");
		indent -= COL;
		break;

	    case H5T_COMPOUND:
		indentation(indent);
		printf("<!-- Note: format of compound data not specified -->\n");
		dump_function_table->dump_data_function(attr_id, ATTRIBUTE_DATA, NULL);
		break;

	    case H5T_REFERENCE:
		indentation(indent);
		printf("<Data>\n");
		indentation(indent);
		printf("<DataFromFile>\n");
		xml_print_refs(attr_id, ATTRIBUTE_DATA);
		indentation(indent);
		printf("</DataFromFile>\n");
		indentation(indent);
		printf("</Data>\n");
		break;

	    case H5T_VLEN:
		printf("<!-- Note: format of VL data not specified -->\n");
		dump_function_table->dump_data_function(attr_id, ATTRIBUTE_DATA, NULL);
		break;
	    default:
		indentation(indent);
		printf("<Data>\n");
		indentation(indent);
		printf("<!-- Unknown datatype: %d -->\n", H5Tget_class(type));
		indentation(indent);
		printf("<NoData/>\n");
		indentation(indent);
		printf("</Data>\n");
		break;
	    }
	} else {
	    /* The case of an attribute never yet written ?? */
	    indentation(indent);
	    printf("<Data>\n");
	    indentation(indent + COL);
	    printf("<NoData/>\n");
	    indentation(indent);
	    printf("</Data>\n");
	}

	H5Tclose(type);
	H5Sclose(space);
	H5Aclose(attr_id);
	indentation(indent);
	printf("%s\n", dump_header_format->attributeend);
	return SUCCEED;

    } else {
	/* ?? failed */
	indentation(indent + COL);
	printf("<!-- h5dump error: unable to open attribute. -->\n");
	indentation(indent);
	printf("%s\n", dump_header_format->attributeend);
	d_status = EXIT_FAILURE;
	return FAIL;
    }
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_named_datatype
 *
 * Purpose:     Dump a description of an HDF5 NDT in XML.
 *
 * Return:      herr_t
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_named_datatype(hid_t type, const char *name)
{
    int                     nmembers = 1, x;
    hid_t                   mtype;
    char                   *fname;
    char                   *tmp;

    tmp = malloc(strlen(prefix) + strlen(name) + 2);
    strcpy(tmp, prefix);
    strcat(tmp, "/");
    strcat(tmp, name);

    indentation(indent);
    if (strncmp(name, "#", 1) == 0) {
	/*  Special:  this is an 'anonymous' NDT, deleted but 
	   still in use.
	   We follow the dumper's undocumented practice, and
	   use its object id as its name.
	   Exactly the same as normal, but a separate case
	   in the event we want to do something else in
	   the future.
	 */
        char *t_tmp = xml_escape_the_name(tmp);
        char *t_prefix = xml_escape_the_name(prefix);

	printf("<NamedDataType Name=\"%s\" OBJ-XID=\"%s\" Parents=\"%s\">\n",
	       name, t_tmp, (strcmp(prefix, "") ? t_prefix : "root"));
        free(t_tmp);
        free(t_prefix);
    } else {
        char *t_name = xml_escape_the_name(name);
        char *t_prefix = xml_escape_the_name(prefix);
        char *t_tmp = xml_escape_the_name(tmp);

	printf("<NamedDataType Name=\"%s\" OBJ-XID=\"%s\" Parents=\"%s\">\n",
	       t_name, t_tmp, (strcmp(prefix, "") ? t_prefix : "root"));

        free(t_name);
        free(t_prefix);
        free(t_tmp);
    }

    indent += COL;

    if (H5Tget_class(type) == H5T_COMPOUND) {
	/* Dump this here for sure.  */
	nmembers = H5Tget_nmembers(type);

	indentation(indent);
	printf("<CompoundType>\n");

	indent += COL;
	for (x = 0; x < nmembers; x++) {
            char *t_fname;

	    fname = H5Tget_member_name(type, x);
	    mtype = H5Tget_member_type(type, x);
	    indentation(indent);
            t_fname = xml_escape_the_name(fname);
	    printf("<Field FieldName=\"%s\">\n", t_fname);
	    free(fname);
	    free(t_fname);

	    if ((H5Tget_class(mtype) == H5T_COMPOUND)
		|| (H5Tget_class(mtype) == H5T_VLEN)
		|| (H5Tget_class(mtype) == H5T_ARRAY)) {
		indent += COL;

		/*  Nested compound type:  recur */
		indentation(indent);
		printf("%s %s\n", dump_header_format->datatypebegin,
		       dump_header_format->datatypeblockbegin);
		indent += COL;
		xml_print_datatype(mtype);
		indent -= COL;
		indentation(indent);
		printf("%s\n", dump_header_format->datatypeend);
		indent -= COL;
	    } else {
		indent += COL;
		indentation(indent);
		printf("%s %s\n", dump_header_format->datatypebegin,
		       dump_header_format->datatypeblockbegin);
		indent += COL;
		xml_print_datatype(mtype);
		indent -= COL;
		indentation(indent);
		printf("%s\n", dump_header_format->datatypeend);
		indent -= COL;
	    }

	    indentation(indent);
	    printf("</Field>\n");
	}

	indent -= COL;
	indentation(indent);
	printf("</CompoundType>\n");
    } else {
	/* Other data types: call print_datatype */
	indent += COL;
	xml_print_datatype(type);
	indent -= COL;
    }

    indent -= COL;
    indentation(indent);
    printf("</NamedDataType>\n");
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_group
 *
 * Purpose:     Dump a description of an HDF5 Group (and its members) in XML.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_group(hid_t gid, const char *name)
{
    H5G_stat_t              statbuf;
    char                   *cp;
    hid_t                   dset, type;
    char                    typename[1024], *tmp = NULL;
    char                   *par = NULL;
    int                     i;
    int                     isRoot = 0;
    int                     xtype;

    if (strcmp(name, "/") == 0) {
	isRoot = 1;
    } else {
	tmp = malloc(strlen(prefix) + strlen(name) + 2);
	strcpy(tmp, prefix);
	par = strdup(tmp);
	cp = strrchr(par, '/');
	if (cp != NULL) {
	    if ((cp == par) && strlen(par) > 1) {
		*(cp + 1) = '\0';
	    } else {
		*cp = '\0';
	    }
	}
    }

    indentation(indent);

    if (isRoot) {
	printf("<RootGroup OBJ-XID=\"root\">\n");
    } else {
        char *t_name = xml_escape_the_name(name);
        char *t_tmp = xml_escape_the_name(tmp);
        char *t_par = xml_escape_the_name(par);

	printf("<Group Name=\"%s\" OBJ-XID=\"%s\" Parents=\"%s\" >\n",
               t_name, t_tmp, (strcmp(prefix, "") ? t_par : "root"));
        free(t_name);
        free(t_tmp);
        free(t_par);
    }

    indent += COL;
    H5Gget_objinfo(gid, ".", TRUE, &statbuf);

    if (statbuf.nlink > 1) {
	/* Group with more than one link to it... */
	i = search_obj(group_table, statbuf.objno);

	if (i < 0) {
	    indentation(indent);
            error_msg(progname, "internal error (file %s:line %d)\n",
                      __FILE__, __LINE__);
	    d_status = EXIT_FAILURE;
	} else if (group_table->objs[i].displayed) {
	    /* already seen: enter a groupptr */
            char *t_objname = xml_escape_the_name(group_table->objs[i].objname);

	    indentation(indent + COL);
	    printf("<GroupPtr OBJ-XID=\"%s\"/>\n", t_objname);
            free(t_objname);
	} else {
	    /* first time this group has been seen -- describe it  */
	    strcpy(group_table->objs[i].objname, prefix);
	    group_table->objs[i].displayed = 1;

	    /* 1.  do all the attributes of the group */
	    H5Aiterate(gid, NULL,
		       dump_function_table->dump_attribute_function, NULL);

	    if (!strcmp(name, "/") && unamedtype) {
		/* Very special case: dump unamed type in root group */
		for (i = 0; i < type_table->nobjs; i++) {
		    if (!type_table->objs[i].recorded) {
			dset = H5Dopen(gid, type_table->objs[i].objname);
			type = H5Dget_type(dset);
			sprintf(typename, "#%lu:%lu",
				type_table->objs[i].objno[0],
				type_table->objs[i].objno[1]);
			dump_function_table->dump_named_datatype_function(type, typename);
			H5Tclose(type);
			H5Dclose(dset);
		    }
		}
	    }

	    /* iterate through all the members */
	    xtype = H5G_TYPE;
	    H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	    xtype = H5G_DATASET;
	    H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	    xtype = H5G_GROUP;
	    H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	    xtype = H5G_LINK;
	    H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	}
    } else {
	/* 1.  do all the attributes of the group */
	H5Aiterate(gid, NULL, dump_function_table->dump_attribute_function, NULL);

	if (!strcmp(name, "/") && unamedtype) {
	    /* Very special case: dump unamed type in root group */
	    for (i = 0; i < type_table->nobjs; i++) {
		if (!type_table->objs[i].recorded) {
		    dset = H5Dopen(gid, type_table->objs[i].objname);
		    type = H5Dget_type(dset);
		    sprintf(typename, "#%lu:%lu",
			    type_table->objs[i].objno[0],
			    type_table->objs[i].objno[1]);
		    dump_function_table->dump_named_datatype_function(type, typename);
		    H5Tclose(type);
		    H5Dclose(dset);
		}
	    }
	}

  	/* iterate through all the members */
	xtype = H5G_TYPE;
	H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
  	xtype = H5G_DATASET;
  	H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
  	xtype = H5G_GROUP;
  	H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
	xtype = H5G_LINK;
	H5Giterate(gid, ".", NULL, dump_all, (void *) &xtype);
    }

    indent -= COL;
    indentation(indent);
    if (isRoot) {
	printf("</RootGroup>\n");
    } else {
	printf("%s\n", dump_header_format->groupend);
    }
/*  don't free this!!!
    free(tmp);
*/
}

/*-------------------------------------------------------------------------
 * Function:    xml_print_refs
 *
 * Purpose:     Print a path to the objects referenced by HDF5 Referneces.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
xml_print_refs(hid_t did, int source)
{
    herr_t                  e;
    hid_t                   type, space;
    char                   *buf;
    hobj_ref_t             *refbuf;
    char                   *path;
    hsize_t                 ssiz;
    hsize_t                 i;

    if (source == DATASET_DATA) {
	type = H5Dget_type(did);
    } else if (source == ATTRIBUTE_DATA) {
	type = H5Aget_type(did);
    } else {
	/* return an error */
	return FAIL;
    }
    if (H5Tget_class(type) != H5T_REFERENCE) {
	/* return an error */
	return FAIL;
    }
    if (!H5Tequal(type, H5T_STD_REF_OBJ)) {
	/* region ref not supported yet... */
	/* return an error */
	return FAIL;
    }
    if (source == DATASET_DATA) {
	space = H5Dget_space(did);
	ssiz = H5Sget_simple_extent_npoints(space);
	ssiz *= H5Tget_size(type);

	buf = calloc((size_t)ssiz, sizeof(char));
	if (buf == NULL) {
	    return FAIL;
	}
	e = H5Dread(did, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
	/* need to check result here */
	if (e < 0) {
	    free(buf);
	    return FAIL;
	}

    } else if (source == ATTRIBUTE_DATA) {
	space = H5Aget_space(did);
	ssiz = H5Sget_simple_extent_npoints(space);
	ssiz *= H5Tget_size(type);

	buf = calloc((size_t)ssiz, sizeof(char));
	if (buf == NULL) {
	    free(buf);
	    return FAIL;
	}
	e = H5Aread(did, H5T_STD_REF_OBJ, buf);
	/* need to check the result here */
    } else {
	/* error */
	return FAIL;
    }

    refbuf = (hobj_ref_t *) buf;
    ssiz = H5Sget_simple_extent_npoints(space);

    for (i = 0; i < ssiz; i++) {
	path = lookup_ref_path(refbuf);
	indentation(indent + COL);

	if (!path) {
	    printf("\"%s\"\n", "NULL");
	} else {
            char *t_path = xml_escape_the_string(path, -1);

	    printf("\"%s\"\n", t_path);
            free(t_path);
	}

	refbuf++;
    }

    return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    xml_print_strs
 *
 * Purpose:     Print strings.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
xml_print_strs(hid_t did, int source)
{
    herr_t                  e;
    hid_t                   type, space;
    char                   *buf;
    char                   *bp;
    char                   *onestring;
    hsize_t                 ssiz;
    size_t                  tsiz;
    size_t                  i;
    if (source == DATASET_DATA) {
	type = H5Dget_type(did);
    } else if (source == ATTRIBUTE_DATA) {
	type = H5Aget_type(did);
    } else {
	/* return an error */
	return FAIL;
    }
    if (H5Tget_class(type) != H5T_STRING) {
	/* return an error */
	return FAIL;
    }
    if (source == DATASET_DATA) {
	space = H5Dget_space(did);
	ssiz = H5Sget_simple_extent_npoints(space);
	ssiz *= H5Tget_size(type);

	buf = calloc((size_t)ssiz, sizeof(char));

	if (buf == NULL) {
	    return FAIL;
	}

	e = H5Dread(did, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);

	if (e < 0) {
	    free(buf);
	    return FAIL;
	}
    } else if (source == ATTRIBUTE_DATA) {
	space = H5Aget_space(did);
	ssiz = H5Sget_simple_extent_npoints(space);
	ssiz *= H5Tget_size(type);

	buf = calloc((size_t)ssiz, sizeof(char));
	if (buf == NULL) {
	    return FAIL;
	}
	e = H5Aread(did, type, buf);
	if (e < 0) {
	    free(buf);
	    return FAIL;
	}
    } else {
	/* error */
	return FAIL;
    }

/*  pull out each string... */
    ssiz = H5Sget_simple_extent_npoints(space);

    tsiz = H5Tget_size(type);
    onestring = (char *) calloc((size_t)tsiz, sizeof(char));
    bp = buf;

    for (i = 0; i < ssiz; i++) {
	strncpy(onestring, bp, tsiz);
	indentation(indent + COL);

	if (!onestring) {
	    printf("\"%s\"\n", "NULL");
	} else {
            char *t_onestring = xml_escape_the_string(onestring, (int)tsiz);

	    printf("\"%s\"\n", xml_escape_the_string(onestring, (int)tsiz));
            free(t_onestring);
	}

	bp += tsiz;
    }
    return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    check_compression
 *
 * Purpose:     private function to check for compression and
 *              put a comment in the XML.  (Not fully implemented.)
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
check_compression(hid_t dcpl)
{
    int                     nfilt;
    int                     i;
    H5Z_filter_t            filter;
    char                    namebuf[20];
    size_t                  cd_nelmts = 1;
    unsigned int            cd_values;
    unsigned int            flags;
/*  not used yet:  will need to do somehting more elaborate to handle future
 * compression methods.
    char                   *t1 = "H5Z_FILTER_DEFLATE";
*/

    nfilt = H5Pget_nfilters(dcpl);
    if (nfilt <= 0)
	return;
    for (i = 0; i < nfilt; i++) {
	filter = H5Pget_filter(dcpl, i, &flags,
			       (size_t *) &cd_nelmts,
			       &cd_values, 20, namebuf);
	if (filter == H5Z_FILTER_DEFLATE) {
	    indentation(indent + COL);
	    printf("<Compression />\n");
	    indentation(indent + COL);
	    printf("<!-- Compression parameter %d -->\n", cd_values);
	}
    }
}

/*-------------------------------------------------------------------------
 * Function:    xml_dump_group
 *
 * Purpose:     Dump a description of an HDF5 Group (and its members) in XML.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_dump_dataset(hid_t did, const char *name, struct subset_t UNUSED *sset)
{
    hid_t                   type, space;
    hid_t                   dcpl;
    int                     maxdims;
    hsize_t                *chsize;
    int                     ndims;
    int                     i;
    char                   *tmp;
    char                   *t_name, *t_tmp, *t_prefix;

    tmp = malloc(strlen(prefix) + strlen(name) + 2);
    strcpy(tmp, prefix);
    strcat(tmp, "/");
    strcat(tmp, name);
    indentation(indent);

    t_name = xml_escape_the_name(name);
    t_tmp = xml_escape_the_name(tmp);
    t_prefix = xml_escape_the_name(prefix);

    printf("<Dataset Name=\"%s\" OBJ-XID=\"%s\" Parents=\"%s\">\n",
	   t_name, t_tmp, (strcmp(prefix, "") ? t_prefix : "root"));

    free(t_name);
    free(t_tmp);
    free(t_prefix);

    dcpl = H5Dget_create_plist(did);
    type = H5Dget_type(did);
    space = H5Dget_space(did);

    /* Print information about chunked storage */
    if (H5D_CHUNKED == H5Pget_layout(dcpl)) {
	maxdims = H5Sget_simple_extent_ndims(space);
	chsize = (hsize_t *) malloc(maxdims * sizeof(hsize_t));
	indent += COL;
	indentation(indent);
	printf("<StorageLayout>\n");
	indent += COL;
	indentation(indent);
	printf("<ChunkedLayout ");
	ndims = H5Pget_chunk(dcpl, maxdims, chsize);
	printf("Ndims=\"%d\">\n", ndims);
	/* check for compression and tell about it... */

	check_compression(dcpl);

	indent += COL;

	for (i = 0; i < ndims; i++) {
	    indentation(indent);
	    HDfprintf(stdout, "<ChunkDimension DimSize=\"%Hu\" />\n", chsize[i]);
	}

	indent -= COL;

	indentation(indent);
	printf("</ChunkedLayout>\n");
	indent -= COL;
	indentation(indent);
	printf("</StorageLayout>\n");
	indent -= COL;
	free(chsize);
    }
    /* and check for external.... */

    dump_function_table->dump_dataspace_function(space);
    dump_function_table->dump_datatype_function(type);

    indent += COL;
    H5Aiterate(did, NULL, dump_function_table->dump_attribute_function, NULL);
    indent -= COL;
    i = H5Dget_storage_size(did);

    if (display_data && (i > 0)) {
	switch (H5Tget_class(type)) {
	case H5T_INTEGER:
	case H5T_FLOAT:
	case H5T_STRING:
	case H5T_BITFIELD:
	case H5T_OPAQUE:
	case H5T_ENUM:
	case H5T_ARRAY:
	    dump_function_table->dump_data_function(did, DATASET_DATA, NULL);
	    break;

	case H5T_TIME:
	    indent += COL;
	    indentation(indent);
	    printf("<Data>\n");
	    indentation(indent);
	    printf("<!-- Time data not yet implemented. -->\n");
	    indentation(indent);
	    printf("<NoData />\n");
	    indentation(indent);
	    printf("<Data>\n");
	    indent -= COL;
	    break;

	case H5T_COMPOUND:
	    indentation(indent);
	    printf("<!-- Note: format of compound data not specified -->\n");
	    dump_function_table->dump_data_function(did, DATASET_DATA, NULL);
	    break;

	case H5T_REFERENCE:
	    indentation(indent);
	    printf("<Data>\n");
	    indentation(indent);
	    printf("<DataFromFile>\n");
	    xml_print_refs(did, DATASET_DATA);
	    indentation(indent);
	    printf("</DataFromFile>\n");
	    indentation(indent);
	    printf("</Data>\n");
	    break;

	case H5T_VLEN:
	    printf("<!-- Note: format of VL data not specified -->\n");
	    dump_function_table->dump_data_function(did, DATASET_DATA, NULL);
	    break;
	default:
	    indentation(indent);
	    printf("<Data>\n");
	    indentation(indent);
	    printf("<!-- Unknown datatype: %d -->\n", H5Tget_class(type));
	    indentation(indent);
	    printf("<NoData/>\n");
	    indentation(indent);
	    printf("</Data>\n");
	    break;
	}
    } else {
	/* no data written */
	indentation(indent);
	printf("<Data>\n");
	indentation(indent);
	printf("<NoData/>\n");
	indentation(indent);
	printf("</Data>\n");
    }

/*
    free(tmp);
*/
    H5Tclose(type);
    H5Sclose(space);
    indentation(indent);
    printf("%s\n", dump_header_format->datasetend);
}

/*-------------------------------------------------------------------------
 * Function:    xml_print_enum
 *
 * Purpose:     Print the values of an HDF5 ENUM in XML.
 *              Very similar to regular DDL output.
 *
 * Return:      void
 *
 * Programmer:  REMcG
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
xml_print_enum(hid_t type)
{
    char                  **name = NULL;	/*member names                    */
    unsigned char          *value = NULL;	/*value array                    */
    int                     nmembs;	/*number of members                */
    hid_t                   super;	/*enum base integer type        */
    hid_t                   native = -1;	/*native integer data type        */
    size_t                  dst_size;	/*destination value type size    */
    int                     i;	/*miscellaneous counters        */
    size_t                  j;

    nmembs = H5Tget_nmembers(type);
    super = H5Tget_super(type);

    /*
     * Determine what data type to use for the native values.  To simplify
     * things we entertain three possibilities:
     *  1. long_long -- the largest native signed integer
     *    2. unsigned long_long -- the largest native unsigned integer
     *    3. raw format
     */
    if (H5Tget_size(type) <= sizeof(long_long)) {
	dst_size = sizeof(long_long);

	if (H5T_SGN_NONE == H5Tget_sign(type)) {
	    native = H5T_NATIVE_ULLONG;
	} else {
	    native = H5T_NATIVE_LLONG;
	}
    } else {
	dst_size = H5Tget_size(type);
    }

    /* Get the names and raw values of all members */
    name = calloc((size_t)nmembs, sizeof(char *));
    value = calloc((size_t)nmembs, MAX(H5Tget_size(type), dst_size));

    for (i = 0; i < nmembs; i++) {
	name[i] = H5Tget_member_name(type, i);
	H5Tget_member_value(type, i, value + i * H5Tget_size(type));
    }

    /* Convert values to native data type */
    if (native > 0)
	H5Tconvert(super, native, (hsize_t)nmembs, value, NULL, H5P_DEFAULT);

    /* Sort members by increasing value */
    /*not implemented yet */

    /* Print members */
    indent += COL;
    for (i = 0; i < nmembs; i++) {
        char *t_name = xml_escape_the_name(name[i]);

	indentation(indent);
	printf("<EnumElement>\n");
	indentation(indent + COL);
	printf("%s\n", t_name);
        free(t_name);
	indentation(indent);
	printf("</EnumElement>\n");
	indentation(indent);
	printf("<EnumValue>\n");
	indentation(indent + COL);
	if (native < 0) {
	    printf("0x");

	    for (j = 0; j < dst_size; j++)
		printf("%02x", value[i * dst_size + j]);
	} else if (H5T_SGN_NONE == H5Tget_sign(native)) {
	    HDfprintf(stdout,"%" PRINTF_LL_WIDTH "u", *((unsigned long_long *)
					      ((void *) (value + i * dst_size))));
	} else {
	    HDfprintf(stdout,"%" PRINTF_LL_WIDTH "d",
		   *((long_long *) ((void *) (value + i * dst_size))));
	}
	printf("\n");
	indentation(indent);
	printf("</EnumValue>\n");

    }
    indent -= COL;

    /* Release resources */
    for (i = 0; i < nmembs; i++)
	free(name[i]);

    free(name);
    free(value);
    H5Tclose(super);
}