/src/hdf5/src/H5Tcomplex.c
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 complex number |
15 | | * datatypes in 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: H5Tcomplex_create |
66 | | * |
67 | | * Purpose: Create a new complex number datatype based on the specified |
68 | | * base datatype ID. |
69 | | * |
70 | | * Return: Success: ID of new complex number datatype |
71 | | * Failure: H5I_INVALID_HID |
72 | | * |
73 | | *------------------------------------------------------------------------- |
74 | | */ |
75 | | hid_t |
76 | | H5Tcomplex_create(hid_t base_type_id) |
77 | 0 | { |
78 | 0 | H5T_t *base = NULL; /* base datatype */ |
79 | 0 | H5T_t *dt = NULL; /* new datatype */ |
80 | 0 | hid_t ret_value = H5I_INVALID_HID; /* return value */ |
81 | |
|
82 | 0 | FUNC_ENTER_API(H5I_INVALID_HID) |
83 | |
|
84 | 0 | if (NULL == (base = (H5T_t *)H5I_object_verify(base_type_id, H5I_DATATYPE))) |
85 | 0 | HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5I_INVALID_HID, "invalid base datatype ID"); |
86 | | |
87 | 0 | if (NULL == (dt = H5T__complex_create(base))) |
88 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, H5I_INVALID_HID, |
89 | 0 | "can't create complex number datatype from base datatype"); |
90 | | |
91 | 0 | if ((ret_value = H5I_register(H5I_DATATYPE, dt, true)) < 0) |
92 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, H5I_INVALID_HID, "unable to register datatype"); |
93 | | |
94 | 0 | done: |
95 | 0 | FUNC_LEAVE_API(ret_value); |
96 | 0 | } |
97 | | |
98 | | /*------------------------------------------------------------------------- |
99 | | * Function: H5T__complex_create |
100 | | * |
101 | | * Purpose: Create a new complex number datatype based on the specified |
102 | | * base datatype. |
103 | | * |
104 | | * Return: Success: new complex number datatype |
105 | | * Failure: NULL |
106 | | * |
107 | | *------------------------------------------------------------------------- |
108 | | */ |
109 | | H5T_t * |
110 | | H5T__complex_create(const H5T_t *base) |
111 | 10 | { |
112 | 10 | H5T_t *dt = NULL; /* New complex number datatype */ |
113 | 10 | H5T_t *ret_value = NULL; /* Return value */ |
114 | | |
115 | 10 | FUNC_ENTER_PACKAGE |
116 | | |
117 | | /* Check args */ |
118 | 10 | assert(base); |
119 | | |
120 | | /* Currently, only floating-point base datatypes are supported. */ |
121 | 10 | if (base->shared->type != H5T_FLOAT) |
122 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, NULL, "base datatype is not a H5T_FLOAT datatype"); |
123 | 10 | if (base->shared->size == 0) |
124 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, NULL, "invalid base datatype size"); |
125 | 10 | if (base->shared->size > SIZE_MAX / 2) |
126 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, NULL, |
127 | 10 | "base datatype size too large - new datatype size would overflow"); |
128 | | |
129 | | /* Build new type */ |
130 | 10 | if (NULL == (dt = H5T__alloc())) |
131 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_CANTALLOC, NULL, "memory allocation failed"); |
132 | 10 | dt->shared->type = H5T_COMPLEX; |
133 | 10 | dt->shared->size = 2 * base->shared->size; |
134 | | |
135 | 10 | if (NULL == (dt->shared->parent = H5T_copy(base, H5T_COPY_ALL))) |
136 | 0 | HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCOPY, NULL, "can't copy base datatype"); |
137 | | |
138 | | /* Set complex number-specific fields */ |
139 | 10 | dt->shared->u.cplx.form = H5T_COMPLEX_RECTANGULAR; /* Only rectangular form is currently supported */ |
140 | | |
141 | | /* Complex number datatypes use a later version of the datatype object header message */ |
142 | 10 | dt->shared->version = MAX(base->shared->version, H5O_DTYPE_VERSION_5); |
143 | | |
144 | 10 | ret_value = dt; |
145 | | |
146 | 10 | done: |
147 | 10 | if (!ret_value) |
148 | 0 | if (dt && H5T_close(dt) < 0) |
149 | 0 | HDONE_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, NULL, "can't close datatype"); |
150 | | |
151 | 10 | FUNC_LEAVE_NOAPI(ret_value) |
152 | 10 | } |