summaryrefslogtreecommitdiffstats
path: root/src/H5T.c
blob: 2618f1476cb1af9c40748d81d22b41dffa41832f (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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/****************************************************************************
* NCSA HDF                                                                 *
* Software Development Group                                               *
* National Center for Supercomputing Applications                          *
* University of Illinois at Urbana-Champaign                               *
* 605 E. Springfield, Champaign IL 61820                                   *
*                                                                          *
* For conditions of distribution and use, see the accompanying             *
* hdf/COPYING file.                                                        *
*                                                                          *
****************************************************************************/

#ifdef RCSID
static char RcsId[] = "@(#)$Revision$";
#endif

#define H5T_PACKAGE		/*suppress error about including H5Tpkg	*/

#include <H5private.h>		/*generic functions 			*/
#include <H5Aprivate.h>		/*atom functions 			*/
#include <H5Eprivate.h>		/*error handling 			*/
#include <H5Mprivate.h>		/*meta data 				*/
#include <H5MMprivate.h>	/*memory management			*/
#include <H5Pprivate.h>		/*data space 				*/
#include <H5Tpkg.h>		/*data-type functions 			*/

#define PABLO_MASK	H5T_mask

#define H5T_COMPND_INC	64	/*typical max numb of members per struct*/

/* Interface initialization */
static intn interface_initialize_g = FALSE;
#define INTERFACE_INIT H5T_init_interface
static void H5T_term_interface (void);

/*--------------------------------------------------------------------------
NAME
   H5T_init_interface -- Initialize interface-specific information
USAGE
    herr_t H5T_init_interface()
   
RETURNS
   SUCCEED/FAIL
DESCRIPTION
    Initializes any interface-specific data or routines.

--------------------------------------------------------------------------*/
herr_t
H5T_init_interface (void)
{
   herr_t ret_value = SUCCEED;
   FUNC_ENTER (H5T_init_interface, FAIL);

   /* Initialize the atom group for the file IDs */
   if ((ret_value=H5Ainit_group (H5_DATATYPE, H5A_DATATYPEID_HASHSIZE,
				 H5T_RESERVED_ATOMS,
				 (herr_t (*)(void*))H5T_close))!=FAIL) {
      ret_value=H5_add_exit (&H5T_term_interface);
   }

   /* Initialize pre-defined data types */
   ret_value = H5T_init ();
   
   FUNC_LEAVE (ret_value);
}

/*--------------------------------------------------------------------------
 NAME
    H5T_term_interface
 PURPOSE
    Terminate various H5T objects
 USAGE
    void H5T_term_interface()
 RETURNS
    SUCCEED/FAIL
 DESCRIPTION
    Release the atom group and any other resources allocated.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
static void
H5T_term_interface (void)
{
   H5Adestroy_group (H5_DATATYPE);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tcreate
 *
 * Purpose:	Create a new type and initialize it to reasonable values.
 *		The type is a member of type class TYPE and is SIZE bytes.
 *
 * Return:	Success:	A new type identifier.
 *
 *		Failure:	FAIL
 *
 * Errors:
 *		ARGS      BADVALUE      Invalid size. 
 *		DATATYPE  CANTINIT      Can't create type. 
 *		DATATYPE  CANTREGISTER  Can't register data type atom. 
 *
 * Programmer:	Robb Matzke
 *              Friday, December  5, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Tcreate (H5T_class_t type, size_t size)
{
   H5T_t	*dt = NULL;
   hid_t	ret_value = FAIL;
   
   FUNC_ENTER (H5Tcreate, FAIL);

   /* check args */
   if (size<=0) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "invalid size");
   }
   
   /* create the type */
   if (NULL==(dt=H5T_create (type, size))) {
      HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't create type");
   }
   
   /* Make it an atom */
   if ((ret_value=H5Aregister_atom (H5_DATATYPE, dt))<0) {
      HRETURN_ERROR (H5E_DATATYPE, H5E_CANTREGISTER, FAIL,
		     "can't register data type atom");
   }

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_create
 *
 * Purpose:	Creates a new data type and initializes it to reasonable
 *		values.  The new data type is SIZE bytes and an instance of
 *		the class TYPE.
 *
 * Return:	Success:	Pointer to the new type.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Friday, December  5, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5T_t *
H5T_create (H5T_class_t type, size_t size)
{
   H5T_t	*dt = NULL;
   
   FUNC_ENTER (H5T_create, NULL);

   assert (size>0);

   switch (type) {
   case H5T_FIXED:
      /* Default type is a native `int' */
      if (NULL==(dt=H5T_copy (H5Aatom_object (H5T_NATIVE_INT)))) {
	 HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINIT, NULL,
			"can't derive type from native int");
      }
      break;

   case H5T_FLOAT:
      /* Default type is a native `double' */
      if (NULL==(dt=H5T_copy (H5Aatom_object (H5T_NATIVE_DOUBLE)))) {
	 HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINIT, NULL,
			"can't derive type from native double");
      }
      break;

   case H5T_DATE:
   case H5T_STRING:
   case H5T_BITFIELD:
   case H5T_OPAQUE:
      assert ("not implemented yet" && 0);
      HRETURN_ERROR (H5E_DATATYPE, H5E_UNSUPPORTED, NULL,
		     "not implemented yet");

   case H5T_COMPOUND:
      dt = H5MM_xcalloc (1, sizeof(H5T_t));
      dt->type = type;
      break;

   default:
      HRETURN_ERROR (H5E_INTERNAL, H5E_UNSUPPORTED, NULL,
		     "unknown data type class");
   }

   dt->size = size;
   FUNC_LEAVE (dt);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tget_num_members
 *
 * Purpose:	Determines how many members compound data type TYPE_ID has.
 *
 * Return:	Success:	Number of members defined in a compound data
 *				type.
 *
 *		Failure:	FAIL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
intn
H5Tget_num_members (hid_t type_id)
{
   
   H5T_t	*dt = NULL;

   FUNC_ENTER (H5Tget_num_members, FAIL);
   H5ECLEAR;

   /* Check args */
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(dt=H5Aatom_object (type_id)) ||
       H5T_COMPOUND!=dt->type) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a compound data type");
   }

   FUNC_LEAVE (dt->u.compnd.nmembs);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tget_class
 *
 * Purpose:	Returns the data type class identifier for data type TYPE_ID.
 *
 * Return:	Success:	One of the non-negative data type class
 *				constants.
 *
 *		Failure:	H5T_NO_CLASS (-1)
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5T_class_t
H5Tget_class (hid_t type_id)
{
   H5T_t	*dt = NULL;
   
   FUNC_ENTER (H5Tget_class, FAIL);
   H5ECLEAR;

   /* Check args */
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(dt=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
   }

   FUNC_LEAVE (dt->type);
}



/*-------------------------------------------------------------------------
 * Function:	H5Tget_size
 *
 * Purpose:	Determines the total size of a data type in bytes.
 *
 * Return:	Success:	Size of the data type in bytes.  The size of
 *				data type is the size of an instance of that
 *				data type.
 *
 *		Failure:	0 (valid data types are never zero size)
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
size_t
H5Tget_size (hid_t type_id)
{
   H5T_t	*dt = NULL;
   size_t	size;
   
   FUNC_ENTER (H5Tget_size, 0);
   H5ECLEAR;

   /* Check args */
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(dt=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
   }

   /* size */
   size = H5T_get_size (dt);

   FUNC_LEAVE (size);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_get_size
 *
 * Purpose:	Determines the total size of a data type in bytes.
 *
 * Return:	Success:	Size of the data type in bytes.  The size of
 *				the data type is the size of an instance of
 *				that data type.
 *
 *		Failure:	0 (valid data types are never zero size)
 *
 * Programmer:	Robb Matzke
 *              Tuesday, December  9, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
size_t
H5T_get_size (const H5T_t *dt)
{
   FUNC_ENTER (H5T_get_size, 0);

   /* check args */
   assert (dt);

   FUNC_LEAVE (dt->size);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tinsert_member
 *
 * Purpose:	Adds another member to the compound data type PARENT_ID.  The
 *		new member has a NAME which must be unique within the
 *		compound data type. The OFFSET argument defines the start of
 *		the member in an instance of the compound data type, and
 *		MEMBER_ID is the type of the new member.
 *
 * Note:	All members of a compound data type must be atomic; a
 *		compound data type cannot have a member which is a compound
 *		data type.
 *
 * Return:	Success:	SUCCEED, the PARENT_ID compound data type is
 *				modified to include a copy of the member type
 *				MEMBER_ID.
 *
 *		Failure:	FAIL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Tinsert_member (hid_t parent_id, const char *name, off_t offset,
		  hid_t member_id)
{
   H5T_t	*parent = NULL;		/*the compound parent data type	*/
   H5T_t	*member = NULL;		/*the atomic member type	*/
   
   FUNC_ENTER (H5Tinsert_member, FAIL);
   H5ECLEAR;

   /* Check args */
   if (H5_DATATYPE!=H5Aatom_group (parent_id) ||
       NULL==(parent=H5Aatom_object (parent_id)) ||
       H5T_COMPOUND!=parent->type) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a compound data type");
   }
   if (parent->locked) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "parent is locked");
   }
   if (!name || !*name) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "no member name");
   }
   if (H5_DATATYPE!=H5Aatom_group (member_id) ||
       NULL==(member=H5Aatom_object (member_id)) ||
       H5T_COMPOUND==member->type) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not an atomic data type");
   }

   if (H5T_insert_member (parent, name, offset, member)<0) {
      HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINSERT, FAIL,
		     "can't insert member");
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_insert_member
 *
 * Purpose:	Adds a new MEMBER to the compound data type PARENT.  The new
 *		member will have a NAME that is unique within PARENT and an
 *		instance of PARENT will have the member begin at byte offset
 *		OFFSET from the beginning.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5T_insert_member (H5T_t *parent, const char *name, off_t offset,
		   const H5T_t *member)
{
   intn		i;
   H5T_t	*tmp = NULL;
   
   FUNC_ENTER (H5T_insert_member, FAIL);

   /* check args */
   assert (parent && H5T_COMPOUND==parent->type);
   assert (!parent->locked);
   assert (member && H5T_COMPOUND!=member->type);
   assert (name && *name);

   /* Does NAME already exist in PARENT? */
   for (i=0; i<parent->u.compnd.nmembs; i++) {
      if (!HDstrcmp (parent->u.compnd.memb[i].name, name)) {
	 HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINSERT, FAIL,
			"member name is not unique");
      }
   }

   /* Increase member array if necessary */
   if (parent->u.compnd.nmembs>=parent->u.compnd.nalloc) {
      parent->u.compnd.nalloc += H5T_COMPND_INC;
      parent->u.compnd.memb = H5MM_xrealloc (parent->u.compnd.memb,
					     (parent->u.compnd.nalloc*
					      sizeof(H5T_member_t)));
   }

   /* Add member to end of member array */
   i = parent->u.compnd.nmembs;
   parent->u.compnd.memb[i].name = H5MM_xstrdup (name);
   parent->u.compnd.memb[i].offset = offset;
   parent->u.compnd.memb[i].ndims = 0; /*defaults to scalar*/
   
   tmp = H5T_copy (member);
   parent->u.compnd.memb[i].type = *tmp;
   H5MM_xfree (tmp);

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tcopy
 *
 * Purpose:	Copies a data type.  The resulting data type is not locked.
 *
 * Return:	Success:	The ID of a new data type.
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Tuesday, December  9, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Tcopy (hid_t type_id)
{
   H5T_t	*dt = NULL;
   H5T_t	*new_dt = NULL;
   hid_t	ret_value = FAIL;
   
   FUNC_ENTER (H5Tcopy, FAIL);
   H5ECLEAR;

   /* check args */
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(dt=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
   }

   /* copy */
   if (NULL==(new_dt = H5T_copy (dt))) {
      HRETURN_ERROR (H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't copy");
   }

   /* atomize result */
   if ((ret_value=H5Aregister_atom (H5_DATATYPE, new_dt))<0) {
      H5T_close (new_dt);
      HRETURN_ERROR (H5E_DATATYPE, H5E_CANTREGISTER, FAIL,
		     "can't register data type atom");
   }

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_copy
 *
 * Purpose:	Copies datatype OLD_DT.  The resulting data type is not
 *		locked.
 *
 * Return:	Success:	Pointer to a new copy of the OLD_DT argument.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Thursday, December  4, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5T_t *
H5T_copy (const H5T_t *old_dt)
{
   H5T_t	*new_dt = NULL;
   intn		i;
   char		*s;
   
   FUNC_ENTER (H5T_copy, NULL);

   /* check args */
   assert (old_dt);

   /* copy */
   new_dt = H5MM_xcalloc (1, sizeof(H5T_t));
   *new_dt = *old_dt;
   new_dt->locked = FALSE;
   
   if (H5T_COMPOUND==new_dt->type) {
      new_dt->u.compnd.memb = H5MM_xmalloc (new_dt->u.compnd.nmembs *
					    sizeof(H5T_member_t));
      HDmemcpy (new_dt->u.compnd.memb, old_dt->u.compnd.memb,
		new_dt->u.compnd.nmembs * sizeof(H5T_member_t));
      for (i=0; i<new_dt->u.compnd.nmembs; i++) {
	 s = new_dt->u.compnd.memb[i].name;
	 new_dt->u.compnd.memb[i].name = H5MM_xstrdup (s);
      }
   }
   
   FUNC_LEAVE (new_dt);
}


/*-------------------------------------------------------------------------
 * Function:	H5Tclose
 *
 * Purpose:	Frees a data type and all associated memory.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Tuesday, December  9, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Tclose (hid_t type_id)
{
   H5T_t	*dt = NULL;
   
   FUNC_ENTER (H5Tclose, FAIL);
   H5ECLEAR;

   /* check args */
   if (H5_DATATYPE!=H5Aatom_group (type_id) ||
       NULL==(dt=H5Aatom_object (type_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
   }
   if (dt->locked) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "predefined data type");
   }

   /* When the reference count reaches zero the resources are freed */
   if (H5A_dec_ref (type_id)<0) {
      HRETURN_ERROR (H5E_ATOM, H5E_BADATOM, FAIL, "problem freeing id");
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_close
 *
 * Purpose:	Frees a data type and all associated memory.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Monday, December  8, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5T_close (H5T_t *dt)
{
   intn		i;
   
   FUNC_ENTER (H5T_close, FAIL);

   assert (dt);
   assert (!dt->locked);

   if (dt && H5T_COMPOUND==dt->type) {
      for (i=0; i<dt->u.compnd.nmembs; i++) {
	 H5MM_xfree (dt->u.compnd.memb[i].name);
      }
      H5MM_xfree (dt->u.compnd.memb);
      H5MM_xfree (dt);
      
   } else if (dt) {
      H5MM_xfree (dt);
   }

   FUNC_LEAVE (SUCCEED);
}



/*-------------------------------------------------------------------------
 * Function:	H5Tequal
 *
 * Purpose:	Determines if two data types are equal.
 *
 * Return:	Success:	TRUE if equal, FALSE if unequal
 *
 *		Failure:	FAIL
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *              Wednesday, December 10, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hbool_t
H5Tequal (hid_t type1_id, hid_t type2_id)
{
   const H5T_t	*dt1 = NULL;
   const H5T_t	*dt2 = NULL;
   hbool_t	ret_value = FAIL;
   
   FUNC_ENTER (H5Tequal, FAIL);

   /* check args */
   if (H5_DATATYPE!=H5Aatom_group (type1_id) ||
       NULL==(dt1=H5Aatom_object (type1_id)) ||
       H5_DATATYPE!=H5Aatom_group (type2_id) ||
       NULL==(dt2=H5Aatom_object (type2_id))) {
      HRETURN_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
   }

   ret_value = (0==H5T_cmp (dt1, dt2));

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_cmp
 *
 * Purpose:	Compares two data types.
 *
 * Return:	Success:	0 if DT1 and DT2 are equal.
 *				<0 if DT1 is less than DT2.
 * 				>0 if DT1 is greater than DT2.
 *
 *		Failure:	0, never fails
 *
 * Programmer:	Robb Matzke
 *              Wednesday, December 10, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
intn
H5T_cmp (const H5T_t *dt1, const H5T_t *dt2)
{
   intn		*idx1=NULL, *idx2=NULL;
   intn		ret_value = 0;
   intn		i, j, tmp;
   hbool_t	swapped;
   
   FUNC_ENTER (H5T_equal, 0);

   /* check args */
   assert (dt1);
   assert (dt2);

   /* the easy case */
   if (dt1==dt2) HGOTO_DONE (0);

   /* compare */
   if (dt1->type < dt2->type) HGOTO_DONE (-1);
   if (dt1->type > dt2->type) HGOTO_DONE (1);

   if (dt1->size < dt2->size) HGOTO_DONE (-1);
   if (dt1->size > dt2->size) HGOTO_DONE (1);

   if (H5T_COMPOUND==dt1->type) {
      /*
       * Compound data types...
       */
      if (dt1->u.compnd.nmembs < dt2->u.compnd.nmembs) HGOTO_DONE (-1);
      if (dt1->u.compnd.nmembs > dt2->u.compnd.nmembs) HGOTO_DONE (1);

      /* Build an index for each type so the names are sorted */
      idx1 = H5MM_xmalloc (dt1->u.compnd.nmembs * sizeof(intn));
      idx2 = H5MM_xmalloc (dt1->u.compnd.nmembs * sizeof(intn));
      for (i=0; i<dt1->u.compnd.nmembs; i++) idx1[i] = idx2[i] = i;
      for (i=dt1->u.compnd.nmembs-1, swapped=TRUE; swapped && i>=0; --i) {
	 for (j=0, swapped=FALSE; j<i; j++) {
	    if (HDstrcmp (dt1->u.compnd.memb[idx1[j]].name,
			  dt1->u.compnd.memb[idx1[j+1]].name)>0) {
	       tmp = idx1[j];
	       idx1[j] = idx1[j+1];
	       idx1[j+1] = tmp;
	       swapped = TRUE;
	    }
	 }
      }
      for (i=dt1->u.compnd.nmembs-1, swapped=TRUE; swapped && i>=0; --i) {
	 for (j=0, swapped=FALSE; j<i; j++) {
	    if (HDstrcmp (dt2->u.compnd.memb[idx2[j]].name,
			  dt2->u.compnd.memb[idx2[j+1]].name)>0) {
	       tmp = idx2[j];
	       idx2[j] = idx2[j+1];
	       idx2[j+1] = tmp;
	       swapped = TRUE;
	    }
	 }
      }
      
#ifndef NDEBUG
      for (i=0; i<dt1->u.compnd.nmembs; i++) {
	 assert (HDstrcmp (dt1->u.compnd.memb[idx1[i]].name,
			   dt1->u.compnd.memb[idx1[i+1]].name));
	 assert (HDstrcmp (dt2->u.compnd.memb[idx2[i]].name,
			   dt2->u.compnd.memb[idx2[i+1]].name));
      }
#endif

      /* Compare the members */
      for (i=0; i<dt1->u.compnd.nmembs; i++) {
	 tmp = HDstrcmp (dt1->u.compnd.memb[idx1[i]].name,
			 dt2->u.compnd.memb[idx2[i]].name);
	 if (tmp<0) HGOTO_DONE (-1);
	 if (tmp>0) HGOTO_DONE (1);

	 if (dt1->u.compnd.memb[idx1[i]].offset <
	     dt2->u.compnd.memb[idx2[i]].offset) HGOTO_DONE (-1);
	 if (dt1->u.compnd.memb[idx1[i]].offset >
	     dt2->u.compnd.memb[idx2[i]].offset) HGOTO_DONE (1);

	 if (dt1->u.compnd.memb[idx1[i]].ndims <
	     dt2->u.compnd.memb[idx2[i]].ndims) HGOTO_DONE (-1);
	 if (dt1->u.compnd.memb[idx1[i]].ndims >
	     dt2->u.compnd.memb[idx2[i]].ndims) HGOTO_DONE (1);

	 for (j=0; j<dt1->u.compnd.memb[idx1[i]].ndims; j++) {
	    if (dt1->u.compnd.memb[idx1[i]].dim[j] <
		dt2->u.compnd.memb[idx2[i]].dim[j]) HGOTO_DONE (-1);
	    if (dt1->u.compnd.memb[idx1[i]].dim[j] >
		dt2->u.compnd.memb[idx2[i]].dim[j]) HGOTO_DONE (1);
	 }
	 
	 for (j=0; j<dt1->u.compnd.memb[idx1[i]].ndims; j++) {
	    if (dt1->u.compnd.memb[idx1[i]].perm[j] <
		dt2->u.compnd.memb[idx2[i]].perm[j]) HGOTO_DONE (-1);
	    if (dt1->u.compnd.memb[idx1[i]].perm[j] >
		dt2->u.compnd.memb[idx2[i]].perm[j]) HGOTO_DONE (1);
	 }

	 tmp = H5T_cmp (&(dt1->u.compnd.memb[idx1[i]].type),
			&(dt2->u.compnd.memb[idx2[i]].type));
	 if (tmp<0) HGOTO_DONE (-1);
	 if (tmp>0) HGOTO_DONE (1);
      }
      
   } else {
      /*
       * Atomic data types...
       */
      if (dt1->u.atomic.order < dt2->u.atomic.order) HGOTO_DONE (-1);
      if (dt1->u.atomic.order > dt2->u.atomic.order) HGOTO_DONE (1);

      if (dt1->u.atomic.prec < dt2->u.atomic.prec) HGOTO_DONE (-1);
      if (dt1->u.atomic.prec > dt2->u.atomic.prec) HGOTO_DONE (1);

      if (dt1->u.atomic.offset < dt2->u.atomic.offset) HGOTO_DONE (-1);
      if (dt1->u.atomic.offset > dt2->u.atomic.offset) HGOTO_DONE (1);
      
      if (dt1->u.atomic.lo_pad < dt2->u.atomic.lo_pad) HGOTO_DONE (-1);
      if (dt1->u.atomic.lo_pad > dt2->u.atomic.lo_pad) HGOTO_DONE (1);
      
      if (dt1->u.atomic.hi_pad < dt2->u.atomic.hi_pad) HGOTO_DONE (-1);
      if (dt1->u.atomic.hi_pad > dt2->u.atomic.hi_pad) HGOTO_DONE (1);

      switch (dt1->type) {
      case H5T_FIXED:
	 if (dt1->u.atomic.u.i.sign < dt2->u.atomic.u.i.sign) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.i.sign > dt2->u.atomic.u.i.sign) HGOTO_DONE (1);
	 break;

      case H5T_FLOAT:
	 if (dt1->u.atomic.u.f.sign < dt2->u.atomic.u.f.sign) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.sign > dt2->u.atomic.u.f.sign) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.epos < dt2->u.atomic.u.f.epos) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.epos > dt2->u.atomic.u.f.epos) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.esize <
	     dt2->u.atomic.u.f.esize) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.esize >
	     dt2->u.atomic.u.f.esize) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.ebias <
	     dt2->u.atomic.u.f.ebias) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.ebias >
	     dt2->u.atomic.u.f.ebias) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.mpos < dt2->u.atomic.u.f.mpos) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.mpos > dt2->u.atomic.u.f.mpos) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.msize <
	     dt2->u.atomic.u.f.msize) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.msize >
	     dt2->u.atomic.u.f.msize) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.norm < dt2->u.atomic.u.f.norm) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.norm > dt2->u.atomic.u.f.norm) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.f.pad < dt2->u.atomic.u.f.pad) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.f.pad > dt2->u.atomic.u.f.pad) HGOTO_DONE (1);

	 break;

      case H5T_DATE:
	 /*void*/
	 break;

      case H5T_STRING:
	 if (dt1->u.atomic.u.s.cset < dt1->u.atomic.u.s.cset) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.s.cset > dt1->u.atomic.u.s.cset) HGOTO_DONE (1);
	 
	 if (dt1->u.atomic.u.s.spad < dt1->u.atomic.u.s.spad) HGOTO_DONE (-1);
	 if (dt1->u.atomic.u.s.spad > dt1->u.atomic.u.s.spad) HGOTO_DONE (1);

	 break;
	 
      case H5T_BITFIELD:
	 /*void*/
	 break;

      case H5T_OPAQUE:
	 /*void*/
	 break;

      default:
	 assert ("not implemented yet" && 0);
      }
   }

 done:
   H5MM_xfree (idx1);
   H5MM_xfree (idx2);

   FUNC_LEAVE (ret_value);
}