1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright by The HDF Group. *
3 * Copyright by the Board of Trustees of the University of Illinois. *
4 * All rights reserved. *
5 * *
6 * This file is part of HDF5. The full HDF5 copyright notice, including *
7 * terms governing use, modification, and redistribution, is contained in *
8 * the COPYING file, which can be found at the root of the source code *
9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14 /*
15 * Module Info: This module contains the functionality for array datatypes in
16 * the H5T interface.
17 */
18
19 /****************/
20 /* Module Setup */
21 /****************/
22
23 #include "H5Tmodule.h" /* This source code file is part of the H5T module */
24
25
26 /***********/
27 /* Headers */
28 /***********/
29 #include "H5private.h" /* Generic Functions */
30 #include "H5Eprivate.h" /* Error handling */
31 #include "H5Iprivate.h" /* IDs */
32 #include "H5Tpkg.h" /* Datatypes */
33
34
35 /****************/
36 /* Local Macros */
37 /****************/
38
39
40 /******************/
41 /* Local Typedefs */
42 /******************/
43
44
45 /********************/
46 /* Package Typedefs */
47 /********************/
48
49
50 /********************/
51 /* Local Prototypes */
52 /********************/
53
54
55 /*********************/
56 /* Public Variables */
57 /*********************/
58
59
60 /*********************/
61 /* Package Variables */
62 /*********************/
63
64
65 /*****************************/
66 /* Library Private Variables */
67 /*****************************/
68
69
70 /*******************/
71 /* Local Variables */
72 /*******************/
73
74
75
76 /*-------------------------------------------------------------------------
77 * Function: H5Tarray_create2
78 *
79 * Purpose: Create a new array datatype based on the specified BASE_TYPE.
80 * The type is an array with NDIMS dimensionality and the size of the
81 * array is DIMS. The total member size should be relatively small.
82 * Array datatypes are currently limited to H5S_MAX_RANK number of
83 * dimensions and must have the number of dimensions set greater than
84 * 0. (i.e. 0 > ndims <= H5S_MAX_RANK) All dimensions sizes must be greater
85 * than 0 also.
86 *
87 * Return: Success: ID of new array datatype
88 * Failure: Negative
89 *
90 * Programmer: Quincey Koziol
91 * Thursday, Oct 17, 2007
92 *
93 *-------------------------------------------------------------------------
94 */
95 hid_t
H5Tarray_create2(hid_t base_id,unsigned ndims,const hsize_t dim[])96 H5Tarray_create2(hid_t base_id, unsigned ndims, const hsize_t dim[/* ndims */])
97 {
98 H5T_t *base; /* base datatype */
99 H5T_t *dt = NULL; /* new array datatype */
100 unsigned u; /* local index variable */
101 hid_t ret_value; /* return value */
102
103 FUNC_ENTER_API(H5I_INVALID_HID)
104 H5TRACE3("i", "iIu*h", base_id, ndims, dim);
105
106 /* Check args */
107 if(ndims < 1 || ndims > H5S_MAX_RANK)
108 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "invalid dimensionality")
109 if(!dim)
110 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "no dimensions specified")
111 for(u = 0; u < ndims; u++)
112 if(!(dim[u] > 0))
113 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "zero-sized dimension specified")
114 if(NULL == (base = (H5T_t *)H5I_object_verify(base_id, H5I_DATATYPE)))
115 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not an valid base datatype")
116
117 /* Create the array datatype */
118 if(NULL == (dt = H5T__array_create(base, ndims, dim)))
119 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to create datatype")
120
121 /* Atomize the type */
122 if((ret_value = H5I_register(H5I_DATATYPE, dt, TRUE)) < 0)
123 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register datatype")
124
125 done:
126 if(ret_value < 0)
127 if(dt && H5T_close_real(dt) < 0)
128 HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, H5I_INVALID_HID, "can't release datatype")
129
130 FUNC_LEAVE_API(ret_value)
131 } /* end H5Tarray_create2() */
132
133
134 /*-------------------------------------------------------------------------
135 * Function: H5T__array_create
136 *
137 * Purpose: Internal routine to create a new array data type based on the
138 * specified BASE_TYPE. The type is an array with NDIMS dimensionality
139 * and the size of the array is DIMS.
140 * Array datatypes are currently limited to H5S_MAX_RANK number
141 * of dimensions.
142 *
143 * Return: Success: ID of new array data type
144 * Failure: Negative
145 *
146 * Programmer: Quincey Koziol
147 * Thursday, Oct 26, 2000
148 *
149 *-------------------------------------------------------------------------
150 */
151 H5T_t *
H5T__array_create(H5T_t * base,unsigned ndims,const hsize_t dim[])152 H5T__array_create(H5T_t *base, unsigned ndims, const hsize_t dim[/* ndims */])
153 {
154 unsigned u; /* Local index variable */
155 H5T_t *ret_value = NULL; /* New array data type */
156
157 FUNC_ENTER_PACKAGE
158
159 HDassert(base);
160 HDassert(ndims <= H5S_MAX_RANK);
161 HDassert(dim);
162
163 /* Build new type */
164 if(NULL == (ret_value = H5T__alloc()))
165 HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
166 ret_value->shared->type = H5T_ARRAY;
167
168 /* Copy the base type of the array */
169 if(NULL == (ret_value->shared->parent = H5T_copy(base, H5T_COPY_ALL)))
170 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCOPY, NULL, "unable to copy base datatype")
171
172 /* Set the array parameters */
173 ret_value->shared->u.array.ndims = ndims;
174
175 /* Copy the array dimensions & compute the # of elements in the array */
176 for(u = 0, ret_value->shared->u.array.nelem = 1; u < ndims; u++) {
177 H5_CHECKED_ASSIGN(ret_value->shared->u.array.dim[u], size_t, dim[u], hsize_t);
178 ret_value->shared->u.array.nelem *= (size_t)dim[u];
179 } /* end for */
180
181 /* Set the array's size (number of elements * element datatype's size) */
182 ret_value->shared->size = ret_value->shared->parent->shared->size * ret_value->shared->u.array.nelem;
183
184 /* Set the "force conversion" flag if the base datatype indicates */
185 if(base->shared->force_conv == TRUE)
186 ret_value->shared->force_conv = TRUE;
187
188 /* Array datatypes need a later version of the datatype object header message */
189 ret_value->shared->version = MAX(base->shared->version, H5O_DTYPE_VERSION_2);
190
191 done:
192 FUNC_LEAVE_NOAPI(ret_value)
193 } /* end H5T__array_create */
194
195
196 /*-------------------------------------------------------------------------
197 * Function: H5Tget_array_ndims
198 *
199 * Purpose: Query the number of dimensions for an array datatype.
200 *
201 * Return: Success: Number of dimensions of the array datatype
202 * Failure: Negative
203 *
204 * Programmer: Quincey Koziol
205 * Monday, November 6, 2000
206 *
207 *-------------------------------------------------------------------------
208 */
209 int
H5Tget_array_ndims(hid_t type_id)210 H5Tget_array_ndims(hid_t type_id)
211 {
212 H5T_t *dt; /* pointer to array datatype */
213 int ret_value; /* return value */
214
215 FUNC_ENTER_API(FAIL)
216 H5TRACE1("Is", "i", type_id);
217
218 /* Check args */
219 if(NULL == (dt = (H5T_t *)H5I_object_verify(type_id,H5I_DATATYPE)))
220 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype object")
221 if(dt->shared->type != H5T_ARRAY)
222 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not an array datatype")
223
224 /* Retrieve the number of dimensions */
225 ret_value = H5T__get_array_ndims(dt);
226
227 done:
228 FUNC_LEAVE_API(ret_value)
229 } /* end H5Tget_array_ndims */
230
231
232 /*-------------------------------------------------------------------------
233 * Function: H5T__get_array_ndims
234 *
235 * Purpose: Private function for H5T__get_array_ndims. Query the number
236 * of dimensions for an array datatype.
237 *
238 * Return: Success: Number of dimensions of the array datatype
239 * Failure: Negative
240 *
241 * Programmer: Raymond Lu
242 * October 10, 2002
243 *
244 *-------------------------------------------------------------------------
245 */
246 int
H5T__get_array_ndims(const H5T_t * dt)247 H5T__get_array_ndims(const H5T_t *dt)
248 {
249 FUNC_ENTER_PACKAGE_NOERR
250
251 HDassert(dt);
252 HDassert(dt->shared->type == H5T_ARRAY);
253
254 /* Retrieve the number of dimensions */
255 FUNC_LEAVE_NOAPI((int)dt->shared->u.array.ndims)
256 } /* end H5T__get_array_ndims */
257
258
259 /*-------------------------------------------------------------------------
260 * Function: H5Tget_array_dims2
261 *
262 * Purpose: Query the sizes of dimensions for an array datatype.
263 *
264 * Return: Success: Number of dimensions of the array type
265 * Failure: Negative
266 *
267 * Programmer: Quincey Koziol
268 * Thursday, October 17, 2007
269 *
270 *-------------------------------------------------------------------------
271 */
272 int
H5Tget_array_dims2(hid_t type_id,hsize_t dims[])273 H5Tget_array_dims2(hid_t type_id, hsize_t dims[])
274 {
275 H5T_t *dt; /* pointer to array data type */
276 int ret_value; /* return value */
277
278 FUNC_ENTER_API(FAIL)
279 H5TRACE2("Is", "i*h", type_id, dims);
280
281 /* Check args */
282 if(NULL == (dt = (H5T_t *)H5I_object_verify(type_id,H5I_DATATYPE)))
283 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype object")
284 if(dt->shared->type != H5T_ARRAY)
285 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not an array datatype")
286
287 /* Retrieve the sizes of the dimensions */
288 if((ret_value = H5T__get_array_dims(dt, dims)) < 0)
289 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "unable to get dimension sizes")
290 done:
291 FUNC_LEAVE_API(ret_value)
292 } /* end H5Tget_array_dims2() */
293
294
295 /*-------------------------------------------------------------------------
296 * Function: H5T__get_array_dims
297 *
298 * Purpose: Private function for H5T__get_array_dims. Query the sizes
299 * of dimensions for an array datatype.
300 *
301 * Return: Success: Number of dimensions of the array type
302 * Failure: Negative
303 *
304 * Programmer: Raymond Lu
305 * October 10, 2002
306 *
307 *-------------------------------------------------------------------------
308 */
309 int
H5T__get_array_dims(const H5T_t * dt,hsize_t dims[])310 H5T__get_array_dims(const H5T_t *dt, hsize_t dims[])
311 {
312 unsigned u; /* Local index variable */
313
314 FUNC_ENTER_PACKAGE_NOERR
315
316 HDassert(dt);
317 HDassert(dt->shared->type == H5T_ARRAY);
318
319 /* Retrieve the sizes of the dimensions */
320 if(dims)
321 for(u = 0; u < dt->shared->u.array.ndims; u++)
322 dims[u] = dt->shared->u.array.dim[u];
323
324 /* Pass along the array rank as the return value */
325 FUNC_LEAVE_NOAPI((int)dt->shared->u.array.ndims)
326 } /* end H5T__get_array_dims */
327
328 #ifndef H5_NO_DEPRECATED_SYMBOLS
329
330 /*-------------------------------------------------------------------------
331 * Function: H5Tarray_create1
332 *
333 * Purpose: Create a new array datatype based on the specified BASE_TYPE.
334 * The type is an array with NDIMS dimensionality and the size of the
335 * array is DIMS. The total member size should be relatively small.
336 * Array datatypes are currently limited to H5S_MAX_RANK number of
337 * dimensions and must have the number of dimensions set greater than
338 * 0. (i.e. 0 > ndims <= H5S_MAX_RANK) All dimensions sizes must be greater
339 * than 0 also.
340 *
341 * Return: Success: ID of new array datatype
342 * Failure: Negative
343 *
344 * Programmer: Quincey Koziol
345 * Thursday, Oct 26, 2000
346 *
347 *-------------------------------------------------------------------------
348 */
349 hid_t
H5Tarray_create1(hid_t base_id,int ndims,const hsize_t dim[],const int H5_ATTR_UNUSED perm[])350 H5Tarray_create1(hid_t base_id, int ndims, const hsize_t dim[/* ndims */],
351 const int H5_ATTR_UNUSED perm[/* ndims */])
352 {
353 H5T_t *base; /* base datatype */
354 H5T_t *dt = NULL; /* new array datatype */
355 unsigned u; /* local index variable */
356 hid_t ret_value; /* return value */
357
358 FUNC_ENTER_API(H5I_INVALID_HID)
359 H5TRACE4("i", "iIs*h*Is", base_id, ndims, dim, perm);
360
361 /* Check args */
362 if(ndims < 1 || ndims > H5S_MAX_RANK)
363 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "invalid dimensionality")
364 if(!dim)
365 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "no dimensions specified")
366 for(u = 0; u < (unsigned)ndims; u++)
367 if(!(dim[u] > 0))
368 HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, H5I_INVALID_HID, "zero-sized dimension specified")
369 if(NULL == (base = (H5T_t *)H5I_object_verify(base_id, H5I_DATATYPE)))
370 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "not an valid base datatype")
371
372 /* Create the array datatype */
373 if(NULL == (dt = H5T__array_create(base, (unsigned)ndims, dim)))
374 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to create datatype")
375
376 /* Atomize the type */
377 if((ret_value = H5I_register(H5I_DATATYPE, dt, TRUE)) < 0)
378 HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register datatype")
379
380 done:
381 if(ret_value < 0)
382 if(dt && H5T_close_real(dt) < 0)
383 HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, H5I_INVALID_HID, "can't release datatype")
384
385 FUNC_LEAVE_API(ret_value)
386 } /* end H5Tarray_create1() */
387
388
389 /*-------------------------------------------------------------------------
390 * Function: H5Tget_array_dims1
391 *
392 * Purpose: Query the sizes of dimensions for an array datatype.
393 *
394 * Return: Success: Number of dimensions of the array type
395 * Failure: Negative
396 *
397 * Programmer: Quincey Koziol
398 * Monday, November 6, 2000
399 *
400 *-------------------------------------------------------------------------
401 */
402 int
H5Tget_array_dims1(hid_t type_id,hsize_t dims[],int H5_ATTR_UNUSED perm[])403 H5Tget_array_dims1(hid_t type_id, hsize_t dims[], int H5_ATTR_UNUSED perm[])
404 {
405 H5T_t *dt; /* Array datatype to query */
406 int ret_value; /* return value */
407
408 FUNC_ENTER_API(FAIL)
409 H5TRACE3("Is", "i*h*Is", type_id, dims, perm);
410
411 /* Check args */
412 if(NULL == (dt = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
413 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype object")
414 if(dt->shared->type != H5T_ARRAY)
415 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not an array datatype")
416
417 /* Retrieve the sizes of the dimensions */
418 if((ret_value = H5T__get_array_dims(dt, dims)) < 0)
419 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "unable to get dimension sizes")
420
421 done:
422 FUNC_LEAVE_API(ret_value)
423 } /* end H5Tget_array_dims1() */
424 #endif /* H5_NO_DEPRECATED_SYMBOLS */
425
426