1 | /* |
---|
2 | |
---|
3 | This file is part of netcdf-4, a netCDF-like interface for HDF5, or a |
---|
4 | HDF5 backend for netCDF, depending on your point of view. |
---|
5 | |
---|
6 | This file handles the nc4 user-defined type functions (i.e. compound |
---|
7 | and opaque types). |
---|
8 | |
---|
9 | Copyright 2005, University Corporation for Atmospheric Research. See |
---|
10 | the COPYRIGHT file for copying and redistribution conditions. |
---|
11 | |
---|
12 | $Id: nc4type.c,v 1.73 2010/05/25 17:54:24 dmh Exp $ |
---|
13 | */ |
---|
14 | |
---|
15 | #include "nc4internal.h" |
---|
16 | |
---|
17 | #define NUM_ATOMIC_TYPES 13 |
---|
18 | char atomic_name[NUM_ATOMIC_TYPES][NC_MAX_NAME + 1] = {"none", "byte", "char", |
---|
19 | "short", "int", "float", |
---|
20 | "double", "ubyte", |
---|
21 | "ushort", "uint", |
---|
22 | "int64", "uint64", "string"}; |
---|
23 | |
---|
24 | EXTERNL int |
---|
25 | NC4_inq_type_equal(int ncid1, nc_type typeid1, int ncid2, |
---|
26 | nc_type typeid2, int *equalp) |
---|
27 | { |
---|
28 | NC_GRP_INFO_T *grp1, *grp2; |
---|
29 | NC_TYPE_INFO_T *type1, *type2; |
---|
30 | int retval; |
---|
31 | |
---|
32 | LOG((2, "nc_inq_type_equal: ncid1 0x%x typeid1 %d ncid2 0x%x typeid2 %d", |
---|
33 | ncid1, typeid1, ncid2, typeid2)); |
---|
34 | |
---|
35 | /* Check input. */ |
---|
36 | if(equalp == NULL) return NC_NOERR; |
---|
37 | |
---|
38 | if (typeid1 <= NC_NAT || typeid2 <= NC_NAT) |
---|
39 | return NC_EINVAL; |
---|
40 | |
---|
41 | /* If one is atomic, and the other user-defined, the types are not |
---|
42 | * equal. */ |
---|
43 | if ((typeid1 <= NC_STRING && typeid2 > NC_STRING) || |
---|
44 | (typeid2 <= NC_STRING && typeid1 > NC_STRING)) |
---|
45 | { |
---|
46 | if (equalp) *equalp = 0; |
---|
47 | return NC_NOERR; |
---|
48 | } |
---|
49 | |
---|
50 | /* If both are atomic types, the answer is easy. */ |
---|
51 | if (typeid1 <= NUM_ATOMIC_TYPES) |
---|
52 | { |
---|
53 | if (equalp) |
---|
54 | { |
---|
55 | if (typeid1 == typeid2) |
---|
56 | *equalp = 1; |
---|
57 | else |
---|
58 | *equalp = 0; |
---|
59 | } |
---|
60 | return NC_NOERR; |
---|
61 | } |
---|
62 | |
---|
63 | /* Not atomic types - so find type1 and type2 information. */ |
---|
64 | if ((retval = nc4_find_nc4_grp(ncid1, &grp1))) |
---|
65 | return retval; |
---|
66 | if (!(type1 = nc4_rec_find_nc_type(grp1->file->nc4_info->root_grp, |
---|
67 | typeid1))) |
---|
68 | return NC_EBADTYPE; |
---|
69 | if ((retval = nc4_find_nc4_grp(ncid2, &grp2))) |
---|
70 | return retval; |
---|
71 | if (!(type2 = nc4_rec_find_nc_type(grp2->file->nc4_info->root_grp, |
---|
72 | typeid2))) |
---|
73 | return NC_EBADTYPE; |
---|
74 | |
---|
75 | /* Are the two types equal? */ |
---|
76 | if (equalp) |
---|
77 | *equalp = (int)H5Tequal(type1->native_typeid, type2->native_typeid); |
---|
78 | |
---|
79 | return NC_NOERR; |
---|
80 | } |
---|
81 | |
---|
82 | /* Get the id of a type from the name. */ |
---|
83 | EXTERNL int |
---|
84 | NC4_inq_typeid(int ncid, const char *name, nc_type *typeidp) |
---|
85 | { |
---|
86 | NC_GRP_INFO_T *grp, *grp2; |
---|
87 | NC_HDF5_FILE_INFO_T *h5; |
---|
88 | NC_TYPE_INFO_T *type = NULL; |
---|
89 | char *norm_name; |
---|
90 | int i, retval; |
---|
91 | |
---|
92 | for (i = 0; i < NUM_ATOMIC_TYPES; i++) |
---|
93 | if (!strcmp(name, atomic_name[i])) |
---|
94 | { |
---|
95 | if (typeidp) |
---|
96 | *typeidp = i; |
---|
97 | return NC_NOERR; |
---|
98 | } |
---|
99 | |
---|
100 | /* Find info for this file and group, and set pointer to each. */ |
---|
101 | if ((retval = nc4_find_grp_h5(ncid, &grp, &h5))) |
---|
102 | return retval; |
---|
103 | |
---|
104 | /* Must be a netCDF-4 file. */ |
---|
105 | if (!h5) |
---|
106 | return NC_ENOTNC4; |
---|
107 | |
---|
108 | /* If the first char is a /, this is a fully-qualified |
---|
109 | * name. Otherwise, this had better be a local name (i.e. no / in |
---|
110 | * the middle). */ |
---|
111 | if (name[0] != '/' && strstr(name, "/")) |
---|
112 | return NC_EINVAL; |
---|
113 | |
---|
114 | /* Normalize name. */ |
---|
115 | if (!(norm_name = malloc(strlen(name) + 1))) |
---|
116 | return NC_ENOMEM; |
---|
117 | if ((retval = nc4_normalize_name(name, norm_name))) |
---|
118 | return retval; |
---|
119 | |
---|
120 | /* Is the type in this group? If not, search parents. */ |
---|
121 | for (grp2 = grp; grp2; grp2 = grp2->parent) |
---|
122 | for (type = grp2->type; type; type = type->next) |
---|
123 | if (!strcmp(norm_name, type->name)) |
---|
124 | { |
---|
125 | if (typeidp) |
---|
126 | *typeidp = type->nc_typeid; |
---|
127 | break; |
---|
128 | } |
---|
129 | |
---|
130 | /* Still didn't find type? Search file recursively, starting at the |
---|
131 | * root group. */ |
---|
132 | if (!type) |
---|
133 | if ((type = nc4_rec_find_named_type(grp->file->nc4_info->root_grp, norm_name))) |
---|
134 | if (typeidp) |
---|
135 | *typeidp = type->nc_typeid; |
---|
136 | |
---|
137 | free(norm_name); |
---|
138 | |
---|
139 | /* OK, I give up already! */ |
---|
140 | if (!type) |
---|
141 | return NC_EBADTYPE; |
---|
142 | |
---|
143 | return NC_NOERR; |
---|
144 | } |
---|
145 | |
---|
146 | /* Find all user-defined types for a location. This finds all |
---|
147 | * user-defined types in a group. */ |
---|
148 | int |
---|
149 | NC4_inq_typeids(int ncid, int *ntypes, int *typeids) |
---|
150 | { |
---|
151 | NC_GRP_INFO_T *grp; |
---|
152 | NC_HDF5_FILE_INFO_T *h5; |
---|
153 | NC_TYPE_INFO_T *type; |
---|
154 | int num = 0; |
---|
155 | int retval; |
---|
156 | |
---|
157 | LOG((2, "nc_inq_typeids: ncid 0x%x", ncid)); |
---|
158 | |
---|
159 | /* Find info for this file and group, and set pointer to each. */ |
---|
160 | if ((retval = nc4_find_grp_h5(ncid, &grp, &h5))) |
---|
161 | return retval; |
---|
162 | |
---|
163 | /* If this is a netCDF-4 file, count types. */ |
---|
164 | if (h5 && grp->type) |
---|
165 | for (type = grp->type; type; type = type->next) |
---|
166 | { |
---|
167 | if (typeids) |
---|
168 | typeids[num] = type->nc_typeid; |
---|
169 | num++; |
---|
170 | } |
---|
171 | |
---|
172 | /* Give the count to the user. */ |
---|
173 | if (ntypes) |
---|
174 | *ntypes = num; |
---|
175 | |
---|
176 | return NC_NOERR; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | /* This internal function adds a new user defined type to the metadata |
---|
181 | * of a group of an open file. */ |
---|
182 | static int |
---|
183 | add_user_type(int ncid, size_t size, const char *name, nc_type base_typeid, |
---|
184 | nc_type type_class, nc_type *typeidp) |
---|
185 | { |
---|
186 | NC_HDF5_FILE_INFO_T *h5; |
---|
187 | NC_GRP_INFO_T *grp; |
---|
188 | NC_TYPE_INFO_T *type; |
---|
189 | char norm_name[NC_MAX_NAME + 1]; |
---|
190 | int retval; |
---|
191 | |
---|
192 | /* Check and normalize the name. */ |
---|
193 | if ((retval = nc4_check_name(name, norm_name))) |
---|
194 | return retval; |
---|
195 | |
---|
196 | LOG((2, "add_user_type: ncid 0x%x size %d name %s base_typeid %d ", |
---|
197 | ncid, size, norm_name, base_typeid)); |
---|
198 | |
---|
199 | /* Find group metadata. */ |
---|
200 | if ((retval = nc4_find_grp_h5(ncid, &grp, &h5))) |
---|
201 | return retval; |
---|
202 | |
---|
203 | /* Only netcdf-4 files! */ |
---|
204 | if (!h5) |
---|
205 | return NC_ENOTNC4; |
---|
206 | |
---|
207 | /* Turn on define mode if it is not on. */ |
---|
208 | if (!(h5->cmode & NC_INDEF)) |
---|
209 | if ((retval = nc_redef(ncid))) |
---|
210 | return retval; |
---|
211 | |
---|
212 | /* No size is provided for vlens or enums, get it from the base type. */ |
---|
213 | if (type_class == NC_VLEN || type_class == NC_ENUM) |
---|
214 | { |
---|
215 | if ((retval = nc4_get_typelen_mem(grp->file->nc4_info, base_typeid, 0, |
---|
216 | &size))) |
---|
217 | return retval; |
---|
218 | } |
---|
219 | else if (size <= 0) |
---|
220 | return NC_EINVAL; |
---|
221 | |
---|
222 | /* Check that this name is not in use as a var, grp, or type. */ |
---|
223 | if ((retval = nc4_check_dup_name(grp, norm_name))) |
---|
224 | return retval; |
---|
225 | |
---|
226 | /* Add to our list of types. */ |
---|
227 | if ((retval = nc4_type_list_add(&(grp->type), &type))) |
---|
228 | return retval; |
---|
229 | |
---|
230 | /* Remember info about this type. */ |
---|
231 | type->nc_typeid = grp->file->nc4_info->next_typeid++; |
---|
232 | type->size = size; |
---|
233 | if (!(type->name = malloc((strlen(norm_name) + 1) * sizeof(char)))) |
---|
234 | return NC_ENOMEM; |
---|
235 | strcpy(type->name, norm_name); |
---|
236 | type->class = type_class; |
---|
237 | type->base_nc_type = base_typeid; |
---|
238 | |
---|
239 | /* Return the typeid to the user. */ |
---|
240 | if (typeidp) |
---|
241 | *typeidp = type->nc_typeid; |
---|
242 | |
---|
243 | return NC_NOERR; |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | /* The sizes of types may vary from platform to platform, but within |
---|
248 | * netCDF files, type sizes are fixed. */ |
---|
249 | #define NC_CHAR_LEN sizeof(char) |
---|
250 | #define NC_STRING_LEN sizeof(char *) |
---|
251 | #define NC_BYTE_LEN 1 |
---|
252 | #define NC_SHORT_LEN 2 |
---|
253 | #define NC_INT_LEN 4 |
---|
254 | #define NC_FLOAT_LEN 4 |
---|
255 | #define NC_DOUBLE_LEN 8 |
---|
256 | #define NC_INT64_LEN 8 |
---|
257 | |
---|
258 | /* Get the name and size of a type. For strings, 1 is returned. For |
---|
259 | * VLEN the base type len is returned. */ |
---|
260 | int |
---|
261 | NC4_inq_type(int ncid, nc_type typeid, char *name, size_t *size) |
---|
262 | { |
---|
263 | NC_GRP_INFO_T *grp; |
---|
264 | NC_TYPE_INFO_T *type; |
---|
265 | int atomic_size[NUM_ATOMIC_TYPES] = {0, NC_BYTE_LEN, NC_CHAR_LEN, NC_SHORT_LEN, |
---|
266 | NC_INT_LEN, NC_FLOAT_LEN, NC_DOUBLE_LEN, |
---|
267 | NC_BYTE_LEN, NC_SHORT_LEN, NC_INT_LEN, NC_INT64_LEN, |
---|
268 | NC_INT64_LEN, NC_STRING_LEN}; |
---|
269 | |
---|
270 | int retval; |
---|
271 | |
---|
272 | LOG((2, "nc_inq_type: ncid 0x%x typeid %d", ncid, typeid)); |
---|
273 | |
---|
274 | /* If this is an atomic type, the answer is easy. */ |
---|
275 | if (typeid <= NUM_ATOMIC_TYPES) |
---|
276 | { |
---|
277 | if (name) |
---|
278 | strcpy(name, atomic_name[typeid]); |
---|
279 | if (size) |
---|
280 | *size = atomic_size[typeid]; |
---|
281 | return NC_NOERR; |
---|
282 | } |
---|
283 | |
---|
284 | /* Not an atomic type - so find group. */ |
---|
285 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
286 | return retval; |
---|
287 | |
---|
288 | /* Find this type. */ |
---|
289 | if (!(type = nc4_rec_find_nc_type(grp->file->nc4_info->root_grp, typeid))) |
---|
290 | return NC_EBADTYPE; |
---|
291 | |
---|
292 | if (name) |
---|
293 | strcpy(name, type->name); |
---|
294 | |
---|
295 | if (size) |
---|
296 | { |
---|
297 | if (type->class != NC_VLEN) |
---|
298 | *size = type->size; |
---|
299 | else |
---|
300 | *size = sizeof(nc_vlen_t); |
---|
301 | } |
---|
302 | |
---|
303 | return NC_NOERR; |
---|
304 | } |
---|
305 | |
---|
306 | /* Create a compound type. */ |
---|
307 | int |
---|
308 | NC4_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp) |
---|
309 | { |
---|
310 | return add_user_type(ncid, size, name, 0, NC_COMPOUND, typeidp); |
---|
311 | } |
---|
312 | |
---|
313 | /* Insert a named field into a compound type. */ |
---|
314 | int |
---|
315 | NC4_insert_compound(int ncid, nc_type typeid, const char *name, size_t offset, |
---|
316 | nc_type field_typeid) |
---|
317 | { |
---|
318 | return nc_insert_array_compound(ncid, typeid, name, offset, |
---|
319 | field_typeid, 0, NULL); |
---|
320 | } |
---|
321 | |
---|
322 | /* Insert a named array into a compound type. */ |
---|
323 | EXTERNL int |
---|
324 | NC4_insert_array_compound(int ncid, int typeid, const char *name, |
---|
325 | size_t offset, nc_type field_typeid, |
---|
326 | int ndims, const int *dim_sizesp) |
---|
327 | { |
---|
328 | NC_GRP_INFO_T *grp; |
---|
329 | NC_TYPE_INFO_T *type; |
---|
330 | char norm_name[NC_MAX_NAME + 1]; |
---|
331 | int retval; |
---|
332 | |
---|
333 | LOG((2, "nc_insert_array_compound: ncid 0x%x, typeid %d name %s " |
---|
334 | "offset %d field_typeid %d ndims %d", ncid, typeid, |
---|
335 | name, offset, field_typeid, ndims)); |
---|
336 | |
---|
337 | /* Check and normalize the name. */ |
---|
338 | if ((retval = nc4_check_name(name, norm_name))) |
---|
339 | return retval; |
---|
340 | |
---|
341 | /* Find file metadata. */ |
---|
342 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
343 | return retval; |
---|
344 | |
---|
345 | /* Find type metadata. */ |
---|
346 | if ((retval = nc4_find_type(grp->file->nc4_info, typeid, &type))) |
---|
347 | return retval; |
---|
348 | |
---|
349 | /* Did the user give us a good compound type typeid? */ |
---|
350 | if (!type || type->class != NC_COMPOUND) |
---|
351 | return NC_EBADTYPE; |
---|
352 | |
---|
353 | /* If this type has already been written to the file, you can't |
---|
354 | * change it. */ |
---|
355 | if (type->committed) |
---|
356 | return NC_ETYPDEFINED; |
---|
357 | |
---|
358 | /* Insert new field into this type's list of fields. */ |
---|
359 | if ((retval = nc4_field_list_add(&type->field, type->num_fields, |
---|
360 | norm_name, offset, 0, 0, field_typeid, |
---|
361 | ndims, dim_sizesp))) |
---|
362 | return retval; |
---|
363 | |
---|
364 | type->num_fields++; |
---|
365 | |
---|
366 | return NC_NOERR; |
---|
367 | } |
---|
368 | |
---|
369 | /* Find info about any user defined type. */ |
---|
370 | int |
---|
371 | NC4_inq_user_type(int ncid, nc_type typeid, char *name, size_t *size, |
---|
372 | nc_type *base_nc_typep, size_t *nfieldsp, int *classp) |
---|
373 | { |
---|
374 | NC_GRP_INFO_T *grp; |
---|
375 | NC_TYPE_INFO_T *type; |
---|
376 | NC_FIELD_INFO_T *field; |
---|
377 | int retval; |
---|
378 | |
---|
379 | LOG((2, "nc_inq_user_type: ncid 0x%x typeid %d", ncid, typeid)); |
---|
380 | |
---|
381 | /* Find group metadata. */ |
---|
382 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
383 | return retval; |
---|
384 | |
---|
385 | /* Find this type. */ |
---|
386 | if (!(type = nc4_rec_find_nc_type(grp->file->nc4_info->root_grp, typeid))) |
---|
387 | return NC_EBADTYPE; |
---|
388 | |
---|
389 | /* Count the number of fields. */ |
---|
390 | if (nfieldsp) |
---|
391 | { |
---|
392 | *nfieldsp = 0; |
---|
393 | if (type->class == NC_COMPOUND) |
---|
394 | for (field = type->field; field; field = field->next) |
---|
395 | (*nfieldsp)++; |
---|
396 | else if (type->class == NC_ENUM) |
---|
397 | *nfieldsp = type->num_enum_members; |
---|
398 | } |
---|
399 | |
---|
400 | /* Fill in size and name info, if desired. */ |
---|
401 | if (size) |
---|
402 | { |
---|
403 | if (type->class != NC_VLEN) |
---|
404 | *size = type->size; |
---|
405 | else |
---|
406 | *size = sizeof(nc_vlen_t); |
---|
407 | } |
---|
408 | if (name) |
---|
409 | strcpy(name, type->name); |
---|
410 | |
---|
411 | /* VLENS and ENUMs have a base type - that is, they type they are |
---|
412 | * arrays of or enums of. */ |
---|
413 | if (base_nc_typep) |
---|
414 | *base_nc_typep = type->base_nc_type; |
---|
415 | |
---|
416 | /* If the user wants it, tell whether this is a compound, opaque, |
---|
417 | * vlen, enum, or string class of type. */ |
---|
418 | if (classp) |
---|
419 | *classp = type->class; |
---|
420 | |
---|
421 | return NC_NOERR; |
---|
422 | } |
---|
423 | |
---|
424 | /* Given the ncid, typeid and fieldid, get info about the field. */ |
---|
425 | int |
---|
426 | NC4_inq_compound_field(int ncid, nc_type typeid, int fieldid, char *name, |
---|
427 | size_t *offsetp, nc_type *field_typeidp, int *ndimsp, |
---|
428 | int *dim_sizesp) |
---|
429 | { |
---|
430 | NC_GRP_INFO_T *grp; |
---|
431 | NC_TYPE_INFO_T *type; |
---|
432 | NC_FIELD_INFO_T *field; |
---|
433 | int d, retval; |
---|
434 | |
---|
435 | /* Find file metadata. */ |
---|
436 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
437 | return retval; |
---|
438 | |
---|
439 | /* Find this type. */ |
---|
440 | if (!(type = nc4_rec_find_nc_type(grp->file->nc4_info->root_grp, typeid))) |
---|
441 | return NC_EBADTYPE; |
---|
442 | |
---|
443 | /* Find the field. */ |
---|
444 | for (field = type->field; field; field = field->next) |
---|
445 | if (field->fieldid == fieldid) |
---|
446 | { |
---|
447 | if (name) |
---|
448 | strcpy(name, field->name); |
---|
449 | if (offsetp) |
---|
450 | *offsetp = field->offset; |
---|
451 | if (field_typeidp) |
---|
452 | *field_typeidp = field->nctype; |
---|
453 | if (ndimsp) |
---|
454 | *ndimsp = field->ndims; |
---|
455 | if (dim_sizesp) |
---|
456 | for (d = 0; d < field->ndims; d++) |
---|
457 | dim_sizesp[d] = field->dim_size[d]; |
---|
458 | return NC_NOERR; |
---|
459 | } |
---|
460 | |
---|
461 | return NC_EBADFIELD; |
---|
462 | } |
---|
463 | |
---|
464 | /* Find a netcdf-4 file. THis will return an error if it finds a |
---|
465 | * netcdf-3 file, or a netcdf-4 file with strict nc3 rules. */ |
---|
466 | static int |
---|
467 | find_nc4_file(int ncid, NC_FILE_INFO_T **nc) |
---|
468 | { |
---|
469 | |
---|
470 | /* Find file metadata. */ |
---|
471 | if (!((*nc) = nc4_find_nc_file(ncid))) |
---|
472 | return NC_EBADID; |
---|
473 | |
---|
474 | /* Check for netcdf-3 files or netcdf-3 rules. */ |
---|
475 | if (!(*nc)->nc4_info) |
---|
476 | return NC_ENOTNC4; |
---|
477 | if ((*nc)->nc4_info->cmode & NC_CLASSIC_MODEL) |
---|
478 | return NC_ESTRICTNC3; |
---|
479 | |
---|
480 | return NC_NOERR; |
---|
481 | } |
---|
482 | |
---|
483 | /* Given the typeid and the name, get the fieldid. */ |
---|
484 | int |
---|
485 | NC4_inq_compound_fieldindex(int ncid, nc_type typeid, const char *name, int *fieldidp) |
---|
486 | { |
---|
487 | NC_FILE_INFO_T *nc; |
---|
488 | NC_TYPE_INFO_T *type; |
---|
489 | NC_FIELD_INFO_T *field; |
---|
490 | char norm_name[NC_MAX_NAME + 1]; |
---|
491 | int retval; |
---|
492 | |
---|
493 | LOG((2, "nc_inq_compound_fieldindex: ncid 0x%x typeid %d name %s", |
---|
494 | ncid, typeid, name)); |
---|
495 | |
---|
496 | /* Find file metadata. */ |
---|
497 | if ((retval = find_nc4_file(ncid, &nc))) |
---|
498 | return retval; |
---|
499 | |
---|
500 | /* Find the type. */ |
---|
501 | if ((retval = nc4_find_type(nc->nc4_info, typeid, &type))) |
---|
502 | return retval; |
---|
503 | |
---|
504 | /* Did the user give us a good compound type typeid? */ |
---|
505 | if (!type || type->class != NC_COMPOUND) |
---|
506 | return NC_EBADTYPE; |
---|
507 | |
---|
508 | /* Normalize name. */ |
---|
509 | if ((retval = nc4_normalize_name(name, norm_name))) |
---|
510 | return retval; |
---|
511 | |
---|
512 | /* Find the field with this name. */ |
---|
513 | for (field = type->field; field; field = field->next) |
---|
514 | if (!strcmp(field->name, norm_name)) |
---|
515 | break; |
---|
516 | |
---|
517 | if (!field) |
---|
518 | return NC_EBADFIELD; |
---|
519 | |
---|
520 | if (fieldidp) |
---|
521 | *fieldidp = field->fieldid; |
---|
522 | return NC_NOERR; |
---|
523 | } |
---|
524 | |
---|
525 | |
---|
526 | /* Opaque type. */ |
---|
527 | |
---|
528 | /* Create an opaque type. Provide a size and a name. */ |
---|
529 | int |
---|
530 | NC4_def_opaque(int ncid, size_t datum_size, const char *name, |
---|
531 | nc_type *typeidp) |
---|
532 | { |
---|
533 | return add_user_type(ncid, datum_size, name, 0, NC_OPAQUE, typeidp); |
---|
534 | } |
---|
535 | |
---|
536 | |
---|
537 | /* Define a variable length type. */ |
---|
538 | int |
---|
539 | NC4_def_vlen(int ncid, const char *name, nc_type base_typeid, |
---|
540 | nc_type *typeidp) |
---|
541 | { |
---|
542 | return add_user_type(ncid, 0, name, base_typeid, NC_VLEN, typeidp); |
---|
543 | } |
---|
544 | |
---|
545 | /* Create an enum type. Provide a base type and a name. At the moment |
---|
546 | * only ints are accepted as base types. */ |
---|
547 | int |
---|
548 | NC4_def_enum(int ncid, nc_type base_typeid, const char *name, |
---|
549 | nc_type *typeidp) |
---|
550 | { |
---|
551 | return add_user_type(ncid, 0, name, base_typeid, NC_ENUM, typeidp); |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | /* Get enum name from enum value. Name size will be <= NC_MAX_NAME. */ |
---|
556 | int |
---|
557 | NC4_inq_enum_ident(int ncid, nc_type xtype, long long value, char *identifier) |
---|
558 | { |
---|
559 | NC_GRP_INFO_T *grp; |
---|
560 | NC_TYPE_INFO_T *type; |
---|
561 | NC_ENUM_MEMBER_INFO_T *enum_member; |
---|
562 | long long ll_val; |
---|
563 | int i; |
---|
564 | int retval; |
---|
565 | |
---|
566 | LOG((3, "nc_inq_enum_ident: xtype %d value %d\n", xtype, value)); |
---|
567 | |
---|
568 | /* Find group metadata. */ |
---|
569 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
570 | return retval; |
---|
571 | |
---|
572 | /* Find this type. */ |
---|
573 | if (!(type = nc4_rec_find_nc_type(grp->file->nc4_info->root_grp, xtype))) |
---|
574 | return NC_EBADTYPE; |
---|
575 | |
---|
576 | /* Complain if they are confused about the type. */ |
---|
577 | if (type->class != NC_ENUM) |
---|
578 | return NC_EBADTYPE; |
---|
579 | |
---|
580 | /* Move to the desired enum member in the list. */ |
---|
581 | enum_member = type->enum_member; |
---|
582 | for (i = 0; i < type->num_enum_members; i++) |
---|
583 | { |
---|
584 | switch (type->base_nc_type) |
---|
585 | { |
---|
586 | case NC_BYTE: |
---|
587 | ll_val = *(char *)enum_member->value; |
---|
588 | break; |
---|
589 | case NC_UBYTE: |
---|
590 | ll_val = *(unsigned char *)enum_member->value; |
---|
591 | break; |
---|
592 | case NC_SHORT: |
---|
593 | ll_val = *(short *)enum_member->value; |
---|
594 | break; |
---|
595 | case NC_USHORT: |
---|
596 | ll_val = *(unsigned short *)enum_member->value; |
---|
597 | break; |
---|
598 | case NC_INT: |
---|
599 | ll_val = *(int *)enum_member->value; |
---|
600 | break; |
---|
601 | case NC_UINT: |
---|
602 | ll_val = *(unsigned int *)enum_member->value; |
---|
603 | break; |
---|
604 | case NC_INT64: |
---|
605 | case NC_UINT64: |
---|
606 | ll_val = *(long long *)enum_member->value; |
---|
607 | break; |
---|
608 | default: |
---|
609 | return NC_EINVAL; |
---|
610 | } |
---|
611 | LOG((4, "ll_val=%d", ll_val)); |
---|
612 | if (ll_val == value) |
---|
613 | { |
---|
614 | if (identifier) |
---|
615 | strcpy(identifier, enum_member->name); |
---|
616 | break; |
---|
617 | } |
---|
618 | else |
---|
619 | enum_member = enum_member->next; |
---|
620 | } |
---|
621 | |
---|
622 | /* If we didn't find it, life sucks for us. :-( */ |
---|
623 | if (i == type->num_enum_members) |
---|
624 | return NC_EINVAL; |
---|
625 | |
---|
626 | return NC_NOERR; |
---|
627 | } |
---|
628 | |
---|
629 | /* Get information about an enum member: an identifier and |
---|
630 | * value. Identifier size will be <= NC_MAX_NAME. */ |
---|
631 | int |
---|
632 | NC4_inq_enum_member(int ncid, nc_type typeid, int idx, char *identifier, |
---|
633 | void *value) |
---|
634 | { |
---|
635 | NC_GRP_INFO_T *grp; |
---|
636 | NC_TYPE_INFO_T *type; |
---|
637 | NC_ENUM_MEMBER_INFO_T *enum_member; |
---|
638 | int i; |
---|
639 | int retval; |
---|
640 | |
---|
641 | LOG((2, "nc_inq_enum_member: ncid 0x%x typeid %d", ncid, typeid)); |
---|
642 | |
---|
643 | /* Find group metadata. */ |
---|
644 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
645 | return retval; |
---|
646 | |
---|
647 | /* Find this type. */ |
---|
648 | if (!(type = nc4_rec_find_nc_type(grp->file->nc4_info->root_grp, typeid))) |
---|
649 | return NC_EBADTYPE; |
---|
650 | |
---|
651 | /* Complain if they are confused about the type. */ |
---|
652 | if (type->class != NC_ENUM) |
---|
653 | return NC_EBADTYPE; |
---|
654 | |
---|
655 | /* Check index. */ |
---|
656 | if (idx >= type->num_enum_members) |
---|
657 | return NC_EINVAL; |
---|
658 | |
---|
659 | /* Move to the desired enum member in the list. */ |
---|
660 | enum_member = type->enum_member; |
---|
661 | for (i = 0; i < idx; i++) |
---|
662 | enum_member = enum_member->next; |
---|
663 | |
---|
664 | /* Give the people what they want. */ |
---|
665 | if (identifier) |
---|
666 | strcpy(identifier, enum_member->name); |
---|
667 | if (value) |
---|
668 | memcpy(value, enum_member->value, type->size); |
---|
669 | |
---|
670 | return NC_NOERR; |
---|
671 | } |
---|
672 | |
---|
673 | /* Insert a identifierd value into an enum type. The value must fit within |
---|
674 | * the size of the enum type, the identifier size must be <= NC_MAX_NAME. */ |
---|
675 | int |
---|
676 | NC4_insert_enum(int ncid, nc_type typeid, const char *identifier, |
---|
677 | const void *value) |
---|
678 | { |
---|
679 | NC_GRP_INFO_T *grp; |
---|
680 | NC_TYPE_INFO_T *type; |
---|
681 | char norm_name[NC_MAX_NAME + 1]; |
---|
682 | int retval; |
---|
683 | |
---|
684 | LOG((2, "nc_insert_enum: ncid 0x%x, typeid %d identifier %s value %d", ncid, |
---|
685 | typeid, identifier, value)); |
---|
686 | |
---|
687 | /* Check and normalize the name. */ |
---|
688 | if ((retval = nc4_check_name(identifier, norm_name))) |
---|
689 | return retval; |
---|
690 | |
---|
691 | /* Find file metadata. */ |
---|
692 | if ((retval = nc4_find_nc4_grp(ncid, &grp))) |
---|
693 | return retval; |
---|
694 | |
---|
695 | /* Find type metadata. */ |
---|
696 | if ((retval = nc4_find_type(grp->file->nc4_info, typeid, &type))) |
---|
697 | return retval; |
---|
698 | |
---|
699 | /* Did the user give us a good enum typeid? */ |
---|
700 | if (!type || type->class != NC_ENUM) |
---|
701 | return NC_EBADTYPE; |
---|
702 | |
---|
703 | /* If this type has already been written to the file, you can't |
---|
704 | * change it. */ |
---|
705 | if (type->committed) |
---|
706 | return NC_ETYPDEFINED; |
---|
707 | |
---|
708 | /* Insert new field into this type's list of fields. */ |
---|
709 | if ((retval = nc4_enum_member_add(&type->enum_member, type->size, |
---|
710 | norm_name, value))) |
---|
711 | return retval; |
---|
712 | |
---|
713 | type->num_enum_members++; |
---|
714 | |
---|
715 | return NC_NOERR; |
---|
716 | } |
---|
717 | |
---|
718 | /* Insert one element into an already allocated vlen array element. */ |
---|
719 | int |
---|
720 | NC4_put_vlen_element(int ncid, int typeid, void *vlen_element, |
---|
721 | size_t len, const void *data) |
---|
722 | { |
---|
723 | nc_vlen_t *tmp = vlen_element; |
---|
724 | tmp->len = len; |
---|
725 | tmp->p = (void *)data; |
---|
726 | return NC_NOERR; |
---|
727 | } |
---|
728 | |
---|
729 | /* Insert one element into an already allocated vlen array element. */ |
---|
730 | int |
---|
731 | NC4_get_vlen_element(int ncid, int typeid, const void *vlen_element, |
---|
732 | size_t *len, void *data) |
---|
733 | { |
---|
734 | const nc_vlen_t *tmp = vlen_element; |
---|
735 | int type_size = 4; |
---|
736 | |
---|
737 | *len = tmp->len; |
---|
738 | memcpy(data, tmp->p, tmp->len * type_size); |
---|
739 | return NC_NOERR; |
---|
740 | } |
---|
741 | |
---|