summaryrefslogtreecommitdiffstats
path: root/src/H5Tfields.c
blob: be62d858f4952333e2180f454c3360ed2c040ea1 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Module Info: This module contains commond functionality for fields in
 *      enumerated & compound datatypes in the H5T interface.
 */

#include "H5Tmodule.h"          /* This source code file is part of the H5T module */


#include "H5private.h"		/*generic functions			  */
#include "H5Eprivate.h"		/*error handling			  */
#include "H5Iprivate.h"		/*ID functions		   		  */
#include "H5MMprivate.h"	/*memory management			  */
#include "H5Tpkg.h"		/*data-type functions			  */


/*-------------------------------------------------------------------------
 * Function:	H5Tget_nmembers
 *
 * Purpose:	Determines how many members TYPE_ID has.  The type must be
 *		either a compound datatype or an enumeration datatype.
 *
 * Return:	Success:	Number of members defined in the datatype.
 *
 *		Failure:	Negative
 *
 * Errors:
 *
 * Programmer:	Robb Matzke
 *		Monday, December  8, 1997
 *
 * Modifications:
 *	Robb Matzke, 22 Dec 1998
 *	Also works with enumeration datatypes.
 *-------------------------------------------------------------------------
 */
int
H5Tget_nmembers(hid_t type_id)
{
    H5T_t *dt;          /* Datatype to query */
    int	ret_value;      /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE1("Is", "i", type_id);

    /* Check args */
    if(NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype")

    if((ret_value = H5T_get_nmembers(dt)) < 0)
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "cannot return member number")

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Tget_nmembers() */


/*-------------------------------------------------------------------------
 * Function:	H5T_get_nmembers
 *
 * Purpose:	Private function for H5Tget_nmembers.  Determines how many
 *              members DTYPE has.  The type must be either a compound data
 *              type or an enumeration datatype.
 *
 * Return:	Success:	Number of members defined in the datatype.
 *
 *		Failure:	Negative
 *
 * Errors:
 *
 * Programmer:  Raymond Lu
 *	        October 8, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5T_get_nmembers(const H5T_t *dt)
{
    int	ret_value = -1;         /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    HDassert(dt);

    if(H5T_COMPOUND == dt->shared->type)
	ret_value = (int)dt->shared->u.compnd.nmembs;
    else if(H5T_ENUM == dt->shared->type)
	ret_value = (int)dt->shared->u.enumer.nmembs;
    else
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "operation not supported for type class")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T_get_nmembers() */


/*-------------------------------------------------------------------------
 * Function:	H5Tget_member_name
 *
 * Purpose:	Returns the name of a member of a compound or enumeration
 *		datatype. Members are stored in no particular order with
 *		numbers 0 through N-1 where N is the value returned by
 *		H5Tget_nmembers().
 *
 * Return:	Success:	Ptr to a string allocated with malloc().  The
 *				caller is responsible for freeing the string.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *		Wednesday, January  7, 1998
 *
 * Modifications:
 *	Robb Matzke, 22 Dec 1998
 *	Also works with enumeration datatypes.
 *-------------------------------------------------------------------------
 */
char *
H5Tget_member_name(hid_t type_id, unsigned membno)
{
    H5T_t	*dt = NULL;
    char	*ret_value;

    FUNC_ENTER_API(NULL)
    H5TRACE2("*s", "iIu", type_id, membno);

    /* Check args */
    if (NULL == (dt = (H5T_t *)H5I_object_verify(type_id,H5I_DATATYPE)))
	HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a datatype")

    if(NULL == (ret_value = H5T__get_member_name(dt, membno)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "unable to get member name")

done:
    FUNC_LEAVE_API(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5T__get_member_name
 *
 * Purpose:	Private function for H5Tget_member_name.  Returns the name
 *              of a member of a compound or enumeration datatype. Members
 *              are stored in no particular order with numbers 0 through
 *              N-1 where N is the value returned by H5Tget_nmembers().
 *
 * Return:	Success:	Ptr to a string allocated with malloc().  The
 *				caller is responsible for freeing the string.
 *
 *		Failure:	NULL
 *
 * Programmer:	Raymond Lu
 *              October 9, 2002
 *
 * Modifications:
 *-------------------------------------------------------------------------
 */
char *
H5T__get_member_name(H5T_t const *dt, unsigned membno)
{
    char	*ret_value = NULL;      /* Return value */

    FUNC_ENTER_PACKAGE

    HDassert(dt);

    switch (dt->shared->type) {
        case H5T_COMPOUND:
            if (membno>=dt->shared->u.compnd.nmembs)
                HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid member number")
            ret_value = H5MM_xstrdup(dt->shared->u.compnd.memb[membno].name);
            break;

        case H5T_ENUM:
            if (membno>=dt->shared->u.enumer.nmembs)
                HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid member number")
            ret_value = H5MM_xstrdup(dt->shared->u.enumer.name[membno]);
            break;

        case H5T_NO_CLASS:
        case H5T_INTEGER:
        case H5T_FLOAT:
        case H5T_TIME:
        case H5T_STRING:
        case H5T_BITFIELD:
        case H5T_OPAQUE:
        case H5T_REFERENCE:
        case H5T_VLEN:
        case H5T_ARRAY:
        case H5T_NCLASSES:
        default:
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "operation not supported for type class")
    } /*lint !e788 All appropriate cases are covered */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5Tget_member_index
 *
 * Purpose:     Returns the index of a member in a compound or enumeration
 *              datatype by given name.Members are stored in no particular
 *              order with numbers 0 through N-1 where N is the value
 *              returned by H5Tget_nmembers().
 *
 * Return:      Success:        index of the member if exists.
 *              Failure:        -1.
 *
 * Programmer:  Raymond Lu
 *              Thursday, April 4, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5Tget_member_index(hid_t type_id, const char *name)
{
    H5T_t       *dt = NULL;
    int         ret_value=FAIL;
    unsigned    i;

    FUNC_ENTER_API(FAIL)
    H5TRACE2("Is", "i*s", type_id, name);

    /* Check arguments */
    HDassert(name);
    if(NULL == (dt = (H5T_t*)H5I_object_verify(type_id, H5I_DATATYPE)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype")

    /* Locate member by name */
    switch(dt->shared->type) {
        case H5T_COMPOUND:
            for(i = 0; i < dt->shared->u.compnd.nmembs; i++)
                if(!HDstrcmp(dt->shared->u.compnd.memb[i].name, name))
                    HGOTO_DONE((int)i)
            break;
        case H5T_ENUM:
            for(i = 0; i < dt->shared->u.enumer.nmembs; i++)
                if(!HDstrcmp(dt->shared->u.enumer.name[i], name))
                    HGOTO_DONE((int)i)
            break;

        case H5T_NO_CLASS:
        case H5T_INTEGER:
        case H5T_FLOAT:
        case H5T_TIME:
        case H5T_STRING:
        case H5T_BITFIELD:
        case H5T_OPAQUE:
        case H5T_REFERENCE:
        case H5T_VLEN:
        case H5T_ARRAY:
        case H5T_NCLASSES:
        default:
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "operation not supported for this type")
    } /*lint !e788 All appropriate cases are covered */

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Tget_member_index() */


/*-------------------------------------------------------------------------
 * Function:	H5T__sort_value
 *
 * Purpose:	Sorts the members of a compound datatype by their offsets;
 *		sorts the members of an enum type by their values. This even
 *		works for locked datatypes since it doesn't change the value
 *		of the type.  MAP is an optional parallel integer array which
 *		is also swapped along with members of DT.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *		Wednesday, January  7, 1998
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5T__sort_value(const H5T_t *dt, int *map)
{
    unsigned	nmembs;                 /* Number of members for datatype */
    size_t	size;
    hbool_t	swapped;                /* Whether we've swapped fields */
    uint8_t	tbuf[32];
    unsigned	i, j;                   /* Local index variables */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_PACKAGE_NOERR

    /* Check args */
    HDassert(dt);
    HDassert(H5T_COMPOUND == dt->shared->type || H5T_ENUM == dt->shared->type);

    /* Use a bubble sort because we can short circuit */
    if(H5T_COMPOUND == dt->shared->type) {
	if(H5T_SORT_VALUE != dt->shared->u.compnd.sorted) {
	    dt->shared->u.compnd.sorted = H5T_SORT_VALUE;
	    nmembs = dt->shared->u.compnd.nmembs;
	    for(i = nmembs - 1, swapped = TRUE; i > 0 && swapped; --i) {
		for(j = 0, swapped = FALSE; j < i; j++) {
		    if(dt->shared->u.compnd.memb[j].offset > dt->shared->u.compnd.memb[j + 1].offset) {
                        H5T_cmemb_t tmp = dt->shared->u.compnd.memb[j];
			dt->shared->u.compnd.memb[j] = dt->shared->u.compnd.memb[j + 1];
			dt->shared->u.compnd.memb[j + 1] = tmp;
			if(map) {
			    int x = map[j];

			    map[j] = map[j + 1];
			    map[j + 1] = x;
			} /* end if */
			swapped = TRUE;
		    } /* end if */
		} /* end for */
	    } /* end for */
#ifndef NDEBUG
	    /* I never trust a sort :-) -RPM */
	    for(i = 0; i < (nmembs - 1); i++)
		HDassert(dt->shared->u.compnd.memb[i].offset < dt->shared->u.compnd.memb[i + 1].offset);
#endif
	} /* end if */
    } else if(H5T_ENUM == dt->shared->type) {
	if(H5T_SORT_VALUE != dt->shared->u.enumer.sorted) {
	    dt->shared->u.enumer.sorted = H5T_SORT_VALUE;
	    nmembs = dt->shared->u.enumer.nmembs;
	    size = dt->shared->size;
	    HDassert(size <= sizeof(tbuf));
	    for(i = (nmembs - 1), swapped = TRUE; i > 0 && swapped; --i) {
		for(j = 0, swapped = FALSE; j < i; j++) {
		    if(HDmemcmp(dt->shared->u.enumer.value + (j * size), dt->shared->u.enumer.value + ((j + 1) * size), size) > 0) {
			/* Swap names */
			char *tmp = dt->shared->u.enumer.name[j];
			dt->shared->u.enumer.name[j] = dt->shared->u.enumer.name[j + 1];
			dt->shared->u.enumer.name[j + 1] = tmp;

			/* Swap values */
			HDmemcpy(tbuf, dt->shared->u.enumer.value + (j * size), size);
			HDmemcpy(dt->shared->u.enumer.value + (j * size),
				 dt->shared->u.enumer.value + ((j + 1) * size), size);
			HDmemcpy(dt->shared->u.enumer.value + ((j + 1) * size), tbuf, size);

			/* Swap map */
			if(map) {
			    int x = map[j];

			    map[j] = map[j + 1];
			    map[j + 1] = x;
			} /* end if */

			swapped = TRUE;
		    } /* end if */
		} /* end for */
	    } /* end for */
#ifndef NDEBUG
	    /* I never trust a sort :-) -RPM */
	    for(i = 0; i < (nmembs - 1); i++)
		HDassert(HDmemcmp(dt->shared->u.enumer.value + (i * size), dt->shared->u.enumer.value + ((i + 1) * size), size) < 0);
#endif
	} /* end if */
    } /* end else */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__sort_value() */


/*-------------------------------------------------------------------------
 * Function:	H5T__sort_name
 *
 * Purpose:	Sorts members of a compound or enumeration datatype by their
 *		names. This even works for locked datatypes since it doesn't
 *		change the value of the types.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, January  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5T__sort_name(const H5T_t *dt, int *map)
{
    unsigned	i, j, nmembs;
    size_t	size;
    hbool_t	swapped;
    uint8_t	tbuf[32];

    FUNC_ENTER_PACKAGE_NOERR

    /* Check args */
    HDassert(dt);
    HDassert(H5T_COMPOUND==dt->shared->type || H5T_ENUM==dt->shared->type);

    /* Use a bubble sort because we can short circuit */
    if (H5T_COMPOUND==dt->shared->type) {
	if (H5T_SORT_NAME!=dt->shared->u.compnd.sorted) {
	    dt->shared->u.compnd.sorted = H5T_SORT_NAME;
	    nmembs = dt->shared->u.compnd.nmembs;
	    for (i=nmembs-1, swapped=TRUE; i>0 && swapped; --i) {
		for (j=0, swapped=FALSE; j<i; j++) {
		    if (HDstrcmp(dt->shared->u.compnd.memb[j].name,
				 dt->shared->u.compnd.memb[j+1].name)>0) {
			H5T_cmemb_t tmp = dt->shared->u.compnd.memb[j];
			dt->shared->u.compnd.memb[j] = dt->shared->u.compnd.memb[j+1];
			dt->shared->u.compnd.memb[j+1] = tmp;
			swapped = TRUE;
			if (map) {
			    int x = map[j];
			    map[j] = map[j+1];
			    map[j+1] = x;
			}
		    }
		}
	    }
#ifndef NDEBUG
	    /* I never trust a sort :-) -RPM */
	    for (i=0; i<nmembs-1; i++) {
		HDassert(HDstrcmp(dt->shared->u.compnd.memb[i].name,
				dt->shared->u.compnd.memb[i+1].name)<0);
	    }
#endif
	}
    } else if (H5T_ENUM==dt->shared->type) {
	if (H5T_SORT_NAME!=dt->shared->u.enumer.sorted) {
	    dt->shared->u.enumer.sorted = H5T_SORT_NAME;
	    nmembs = dt->shared->u.enumer.nmembs;
	    size = dt->shared->size;
	    HDassert(size<=sizeof(tbuf));
	    for (i=nmembs-1, swapped=TRUE; i>0 && swapped; --i) {
		for (j=0, swapped=FALSE; j<i; j++) {
		    if (HDstrcmp(dt->shared->u.enumer.name[j],
				 dt->shared->u.enumer.name[j+1])>0) {
			/* Swap names */
			char *tmp = dt->shared->u.enumer.name[j];
			dt->shared->u.enumer.name[j] = dt->shared->u.enumer.name[j+1];
			dt->shared->u.enumer.name[j+1] = tmp;

			/* Swap values */
			HDmemcpy(tbuf, dt->shared->u.enumer.value+j*size, size);
			HDmemcpy(dt->shared->u.enumer.value+j*size,
				 dt->shared->u.enumer.value+(j+1)*size, size);
			HDmemcpy(dt->shared->u.enumer.value+(j+1)*size, tbuf, size);

			/* Swap map */
			if (map) {
			    int x = map[j];
			    map[j] = map[j+1];
			    map[j+1] = x;
			}

			swapped = TRUE;
		    }
		}
	    }
#ifndef NDEBUG
	    /* I never trust a sort :-) -RPM */
	    for (i=0; i<nmembs-1; i++)
		HDassert(HDstrcmp(dt->shared->u.enumer.name[i], dt->shared->u.enumer.name[i+1])<0);
#endif
	}
    }

    FUNC_LEAVE_NOAPI(SUCCEED)
}