00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "h5Attribute.h"
00017 #include "h5Dataset.h"
00018 #include "h5String.h"
00019 #include "hdf5.h"
00020 #include <assert.h>
00021
00022 int H5Attribute_value_to_string(H5Attribute *a, hid_t tid, hid_t sid);
00023
00024
00025 int H5Attribute_init(H5Attribute *a, hid_t aid);
00026
00027
00028
00029
00030
00031
00032
00033 int H5Attribute_read(H5Attribute *a)
00034 {
00035 int ret_value=0, i=0;
00036 unsigned int npoints;
00037 hid_t aid=-1, loc_id=-1, tid=-1, ftid=-1, sid=-1;
00038
00039 assert(a);
00040 H5Eclear();
00041
00042 if (a->obj_type == H5OBJECT_DATASET)
00043 loc_id = H5Dopen(a->fid, a->obj_path);
00044 else if (a->obj_type == H5OBJECT_GROUP)
00045 loc_id = H5Gopen(a->fid, a->obj_path);
00046 else
00047 THROW_ERROR(a->error, "H5Attribute_read",
00048 "Object must be either H5Dataset or H5Group",
00049 ret_value, done);
00050
00051 if (loc_id < 0)
00052 THROW_H5LIBRARY_ERROR(a->error, ret_value, done);
00053
00054 if ( (aid = H5Aopen_name(loc_id, a->name)) < 0)
00055 THROW_H5LIBRARY_ERROR(a->error, ret_value, done);
00056
00057 if ( (ftid = H5Aget_type(aid)) < 0)
00058 THROW_H5LIBRARY_ERROR(a->error, ret_value, done);
00059
00060 tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
00061 H5Tclose(ftid);
00062
00063
00064 if (a->type.order != get_machine_endian())
00065 THROW_ERROR(a->error, "H5Attribute_read",
00066 "Different byte-order between server and client",
00067 ret_value, done);
00068
00069 npoints = 1;
00070 for (i=0; i<a->space.rank; i++)
00071 npoints *= a->space.dims[i];
00072 a->space.npoints = npoints;
00073
00074 if (a->value)
00075 H5Dataset_freeBuffer(a->value, a->space, a->type, a->nvalue);
00076
00077 if (NULL == (a->value = malloc(npoints*a->type.size)) )
00078 THROW_ERROR(a->error, "H5Attribute_read",
00079 "unable to allocate memory to read data from file",
00080 ret_value, done);
00081
00082 a->nvalue = npoints*a->type.size;
00083 if ( H5Aread(aid, tid, a->value) < 0)
00084 {
00085 free (a->value);
00086 a->value = NULL;
00087 THROW_H5LIBRARY_ERROR(a->error, ret_value, done);
00088 }
00089
00090
00091
00092
00093 if ( (H5DATATYPE_VLEN == a->type.tclass )
00094 || (H5DATATYPE_COMPOUND == a->type.tclass)
00095 || (H5DATATYPE_STRING == a->type.tclass )
00096 )
00097 {
00098 sid = H5Aget_space(aid);
00099 H5Attribute_value_to_string(a, tid, sid);
00100 H5Sclose(sid);
00101 }
00102
00103
00104 done:
00105 if (tid > 0 ) H5Tclose(tid);
00106 if (aid > 0) H5Aclose(aid);
00107
00108 if (loc_id > 0)
00109 {
00110 if (a->obj_type == H5OBJECT_DATASET)
00111 H5Dclose(loc_id);
00112 else if (a->obj_type == H5OBJECT_GROUP)
00113 H5Gclose(loc_id);
00114 }
00115
00116 return ret_value;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126 int H5Attribute_create(H5Attribute *a)
00127 {
00128 int ret_value = 0;
00129
00130
00131 THROW_UNSUPPORTED_OPERATION(a->error,
00132 "H5Attribute_create(H5Attribute *a) is not implemented",
00133 ret_value, done);
00134
00135 done:
00136 return ret_value;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 int H5Attribute_delete(H5Attribute *a)
00147 {
00148 int ret_value = 0;
00149
00150
00151 THROW_UNSUPPORTED_OPERATION(a->error,
00152 "H5Attribute_delete(H5Attribute *a) is not implemented",
00153 ret_value, done);
00154
00155 done:
00156 return ret_value;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166 int H5Attribute_write(H5Attribute *a)
00167 {
00168 int ret_value = 0;
00169
00170
00171 THROW_UNSUPPORTED_OPERATION(a->error,
00172 "H5Attribute_write(H5Attribute *a) is not implemented",
00173 ret_value, done);
00174
00175 done:
00176 return ret_value;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186 int H5Attribute_value_to_string(H5Attribute *a, hid_t tid, hid_t sid)
00187 {
00188 int ret_value=0;
00189 unsigned int i=0;
00190 char **strs;
00191 unsigned char *vp=NULL;
00192 h5str_t h5str;
00193 size_t offset=0, tsize=0, valuelen=0;
00194 void *value;
00195
00196 assert(a);
00197 assert(a->value);
00198 value = a->value;
00199 vp = (unsigned char *)a->value;
00200 a->value = (char **)malloc(a->space.npoints*sizeof(char *));
00201 assert(a->value);
00202 strs = (char**)a->value;
00203
00204 offset = 0;
00205 tsize = H5Tget_size(tid);
00206 memset(&h5str, 0, sizeof(h5str_t));
00207 h5str_new(&h5str, 4*tsize);
00208
00209 a->nvalue = 0;
00210 for (i=0; i<a->space.npoints; i++)
00211 {
00212 h5str_empty(&h5str);
00213 ret_value = h5str_sprintf(&h5str, tid, vp + offset, ", ");
00214 if (ret_value > 0)
00215 {
00216 valuelen = strlen(h5str.s)+1;
00217 strs[i] = (char *)malloc(valuelen);
00218 strcpy(strs[i], h5str.s);
00219
00220 a->nvalue++;
00221 }
00222 offset += tsize;
00223 }
00224 h5str_free(&h5str);
00225
00226
00227 if (H5Tdetect_class(tid, H5T_VLEN) > 0)
00228 H5Dvlen_reclaim(tid, sid, H5P_DEFAULT, value);
00229 free (value);
00230
00231 return ret_value;
00232 }
00233
00234
00235 int H5Attribute_init(H5Attribute *a, hid_t aid) {
00236 int ret_value=0, i=0;
00237 hsize_t dims[H5S_MAX_RANK];
00238 hid_t sid=-1, tid=-1, ftid=-1;
00239 unsigned int npoints;
00240
00241 if (NULL == a || aid < 0) {
00242 ret_value = -1;
00243 goto done;
00244 }
00245
00246 if (a->space.rank > 0)
00247 goto done;
00248
00249 ftid = H5Aget_type(aid);
00250 tid = H5Tget_native_type(ftid, H5T_DIR_ASCEND);
00251 if (ftid > 0) H5Tclose(ftid);
00252
00253 a->name = (char *)malloc(MAX_NAME_LEN);
00254 H5Aget_name(aid, MAX_NAME_LEN, a->name);
00255
00256 a->tclass = a->type.tclass = (H5Datatype_class_t)H5Tget_class(tid);
00257 a->type.size = H5Tget_size(tid);
00258
00259 if ( a->type.tclass == H5DATATYPE_INTEGER
00260 || a->type.tclass == H5DATATYPE_FLOAT
00261 || a->type.tclass == H5DATATYPE_BITFIELD
00262 || a->type.tclass == H5DATATYPE_REFERENCE
00263 || a->type.tclass == H5DATATYPE_ENUM ) {
00264 a->type.order = (H5Datatype_order_t)H5Tget_order(tid);
00265 if (a->type.tclass == H5DATATYPE_INTEGER) {
00266 a->type.sign = (H5Datatype_sign_t)H5Tget_sign(tid);
00267 }
00268 }
00269 else if (a->type.tclass == H5DATATYPE_COMPOUND) {
00270 a->type.nmembers = H5Tget_nmembers(tid );
00271 a->type.mnames = (char **)malloc(a->type.nmembers*sizeof(char*));
00272 a->type.mtypes = (int *)malloc(a->type.nmembers*sizeof(int));
00273 for (i=0; i<a->type.nmembers; i++) {
00274 a->type.mnames[i] = H5Tget_member_name(tid, i);
00275 a->type.mtypes[i] = H5Tget_class(H5Tget_member_type(tid, i));
00276 }
00277 }
00278
00279 sid = H5Aget_space(aid);
00280 a->space.rank = H5Sget_simple_extent_ndims(sid);
00281 if ( H5Sget_simple_extent_dims(sid, dims, NULL) < 0 )
00282 THROW_H5LIBRARY_ERROR(a->error,ret_value, done);
00283
00284 if (a->space.rank<=0)
00285 {
00286 a->space.rank = 1;
00287 dims[0] = 1;
00288 }
00289
00290 npoints = 1;
00291 for (i=0; i<a->space.rank; i++)
00292 {
00293 a->space.dims[i] = dims[i];
00294 a->space.start[i] = 0;
00295 a->space.stride[i] = 1;
00296 a->space.count[i] = dims[i];
00297 npoints *= dims[i];
00298 }
00299 a->space.npoints = npoints;
00300
00301 done:
00302 if (sid > 0 ) H5Sclose(sid);
00303 if (tid > 0 ) H5Tclose(tid);
00304
00305 return ret_value;
00306 }
00307