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