1 /**\file record.c
2  * \brief MINC 2.0 Record Functions
3  * \author Bert Vincent
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /*HAVE_CONFIG_H*/
8 
9 #include <hdf5.h>
10 #include "minc2.h"
11 #include "minc2_private.h"
12 
13 /** This method gets the name of the record dimension
14  * TODO: set record name??
15  */
miget_record_name(mihandle_t volume,char ** name)16 int  miget_record_name(mihandle_t volume,
17                   char **name)
18 {
19     return (MI_NOERROR);
20 }
21 
22 
23 
24 /** This method gets the length (i.e., number of fields in the case of
25  * uniform records and number of bytes for non_uniform ones) of the
26  * record.
27  */
miget_record_length(mihandle_t volume,int * length)28 int miget_record_length(mihandle_t volume,
29                     int *length)
30 {
31     if (volume == NULL || length == NULL) {
32         return (MI_ERROR);
33     }
34     if (volume->volume_class == MI_CLASS_UNIFORM_RECORD ||
35         volume->volume_class == MI_CLASS_NON_UNIFORM_RECORD) {
36         *length = H5Tget_nmembers(volume->ftype_id);
37         return (MI_NOERROR);
38     }
39     return (MI_ERROR);
40 }
41 
42 /** This method returns the field name for the given field index.  Memory
43  * for returned string is allocated on the heap and should be released using
44  * mifree_name().
45  */
miget_record_field_name(mihandle_t volume,int index,char ** name)46 int miget_record_field_name(mihandle_t volume,
47                         int index,
48                         char **name)
49 {
50     if (volume == NULL || name == NULL) {
51         return (MI_ERROR);
52     }
53     /* Get the field name.  The H5Tget_member_name() function allocates
54      * the memory for the string using malloc(), so we can return the
55      * pointer directly without any further manipulations.
56      */
57     *name = H5Tget_member_name(volume->ftype_id, index);
58     if (*name == NULL) {
59         return (MI_ERROR);
60     }
61     return (MI_NOERROR);
62 }
63 
64 /** This method sets a field name for the volume record. The volume
65  * must be of class "MI_CLASS_UNIFORM_RECORD".  The size of record
66  * type will be increased if necessary to accomodate the new field.
67  */
miset_record_field_name(mihandle_t volume,int index,const char * name)68 int miset_record_field_name(mihandle_t volume,
69                         int index,
70                         const char *name)
71 {
72     hid_t mtype_id;
73     hid_t ftype_id;
74     size_t offset;
75 
76     if (volume == NULL || name == NULL) {
77         return (MI_ERROR);
78     }
79     if (volume->volume_class != MI_CLASS_UNIFORM_RECORD &&
80         volume->volume_class != MI_CLASS_NON_UNIFORM_RECORD) {
81         return (MI_ERROR);
82     }
83     /* Get the type of the record's fields.  This is recorded as the
84      * type of the volume.
85      */
86     ftype_id = mitype_to_hdftype(volume->volume_type, FALSE);
87     mtype_id = mitype_to_hdftype(volume->volume_type, TRUE);
88 
89     /* Calculate the offset of the new member.
90      */
91     offset = index * H5Tget_size(ftype_id);
92 
93     /* If the offset plus the size of the member is larger than the
94      * current size of the structure, increase the size of the structure.
95      */
96     if (offset + H5Tget_size(ftype_id) > H5Tget_size(volume->ftype_id)) {
97         H5Tset_size(volume->ftype_id, offset + H5Tget_size(ftype_id));
98     }
99 
100     if (offset + H5Tget_size(mtype_id) > H5Tget_size(volume->mtype_id)) {
101         H5Tset_size(volume->mtype_id, offset + H5Tget_size(mtype_id));
102     }
103 
104     /* Actually define the field within the structure.
105      */
106     H5Tinsert(volume->ftype_id, name, offset, ftype_id);
107     H5Tinsert(volume->mtype_id, name, offset, mtype_id);
108 
109     /* Delete the HDF5 type object returned by mitype_to_hdftype().
110      */
111     H5Tclose(ftype_id);
112     H5Tclose(mtype_id);
113 
114     return (MI_NOERROR);
115 }
116 
117 /* kate: indent-mode cstyle; indent-width 2; replace-tabs on; */
118