00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "h5File.h"
00017 #include "hdf5.h"
00018 #include "h5Dataset.h"
00019 #include <assert.h>
00020
00021
00022 int H5File_get_structure(H5File* f);
00023
00024
00025 int H5File_depth_first(H5Group* g);
00026
00027
00028 int H5File_read_attribute(hid_t loc_id, H5Attribute *attrs);
00029
00030
00031
00032
00033
00034
00035
00036 herr_t obj_info_all(hid_t loc_id, const char *name, void *opdata);
00037 herr_t H5Gget_obj_info_all( hid_t loc_id, const char *group_name,
00038 char **objname, int *type, unsigned long *objno0, unsigned long *objno1 );
00039 int H5File_has_loop(const H5Group *g, const unsigned long ids[]);
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 int H5File_open(H5File* inf, H5File *outf)
00051 {
00052 int ret_value = 0;
00053 hid_t fid=-1;
00054
00055 H5Eclear();
00056
00057
00058 if ( (fid = H5Fopen(inf->filename, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
00059 THROW_H5LIBRARY_ERROR(inf->error,ret_value, done);
00060
00061 inf->fid = fid;
00062
00063 H5File_get_structure(inf);
00064
00065 #ifndef HDF5_LOCAL
00066 memset (outf, 0, sizeof (H5File));
00067
00068
00069
00070 outf->fid = inf->fid;
00071 outf->root = inf->root;
00072 inf->root = NULL;
00073 outf->error = inf->error;
00074 #endif
00075
00076 done:
00077 if (ret_value >= 0)
00078 return inf->fid;
00079 else
00080 return ret_value;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 int H5File_close(H5File* f, H5File*outf)
00092 {
00093 int ret_value=0, i=0, n;
00094 hid_t fid=-1, *pids;
00095
00096 assert( f && (f->fid>0) );
00097 H5Eclear();
00098 pids = NULL;
00099
00100
00101 fid = (hid_t)f->fid;
00102 n = H5Fget_obj_count(fid, H5F_OBJ_DATASET);
00103 if (n > 0) {
00104 pids = (hid_t *) malloc(sizeof(hid_t)*n);
00105 assert(pids);
00106 H5Fget_obj_ids(fid, H5F_OBJ_DATASET, n, pids);
00107 for (i=0; i<n; i++)
00108 H5Dclose(pids[i]);
00109 free(pids);
00110 }
00111
00112
00113 n = H5Fget_obj_count(fid, H5F_OBJ_GROUP );
00114 if (n > 0) {
00115 pids = (hid_t *) malloc(sizeof(hid_t)*n);
00116 H5Fget_obj_ids(fid, H5F_OBJ_GROUP , n, pids);
00117 for (i=0; i<n; i++)
00118 H5Gclose(pids[i]);
00119 free(pids);
00120 }
00121
00122 H5Fflush(fid, H5F_SCOPE_GLOBAL);
00123
00124 if ( H5Fclose(fid) < 0)
00125 THROW_H5LIBRARY_ERROR(f->error,ret_value, done);
00126
00127 done:
00128 #ifndef HDF5_LOCAL
00129 memset (outf, 0, sizeof (H5File));
00130 outf->error = f->error;
00131 #endif
00132 return ret_value;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142 int H5File_create(H5File* f)
00143 {
00144 int ret_value = 0;
00145
00146 THROW_UNSUPPORTED_OPERATION(f->error,
00147 "H5File_create(H5File *f) is not implemented",
00148 ret_value, done);
00149
00150
00151 done:
00152 return ret_value;
00153 }
00154
00155
00156
00157 int H5File_get_structure(H5File* f)
00158 {
00159 int ret_value=0;
00160
00161 assert(f);
00162
00163 f->root = (H5Group *) malloc(sizeof (H5Group));
00164 assert (f->root);
00165
00166 H5Group_ctor(f->root);
00167
00168 f->root->fid = f->fid;
00169 f->root->fullpath = (char *)malloc(2);
00170 strcpy(f->root->fullpath, "/");
00171 f->root->objID[0] = f->root->objID[1] = 0;
00172 f->root->parent = NULL;
00173
00174 ret_value = H5File_depth_first(f->root);
00175
00176 return ret_value;
00177 }
00178
00179
00180 int H5File_depth_first(H5Group* parent)
00181 {
00182 int ret_value=0, obj_type=-1, *mtypes=0;
00183 hsize_t nmembers=0, ngroups=0, ndatasets=0, i=0, gidx=0, didx=0;
00184 unsigned long *objno0=0, *objno1=0;
00185 size_t len=0;
00186 hid_t gid=-1, fid=-1;
00187 char **mnames=0;
00188 H5Group *g=0;
00189 H5Dataset *d=0;
00190
00191 assert (parent);
00192
00193 fid = (hid_t)parent->fid;
00194 if ( (gid=H5Gopen(fid, parent->fullpath)) < 0)
00195 THROW_H5LIBRARY_ERROR(parent->error,ret_value, done);
00196
00197 if (H5Gget_num_objs(gid, &nmembers) < 0)
00198 THROW_H5LIBRARY_ERROR(parent->error,ret_value, done);
00199
00200 if (nmembers <=0)
00201 goto done;
00202
00203 mtypes = (int *)malloc((int)nmembers*sizeof(int));
00204 mnames = (char **)malloc((int)nmembers*sizeof(char*));
00205 objno0 = (unsigned long *) malloc((int)nmembers*sizeof(unsigned long));
00206 objno1 = (unsigned long *) malloc((int)nmembers*sizeof(unsigned long));
00207
00208 if ( H5Gget_obj_info_all
00209 (fid, parent->fullpath, mnames, mtypes, objno0, objno1) < 0
00210 )
00211 THROW_H5LIBRARY_ERROR(parent->error,ret_value, done);
00212
00213 ngroups = ndatasets = 0;
00214 for (i=0; i<nmembers; i++)
00215 {
00216 obj_type = mtypes[i];
00217 if (obj_type == H5G_GROUP)
00218 ngroups++;
00219 else if (obj_type == H5G_DATASET)
00220 ndatasets++;
00221 }
00222
00223 parent->ngroups = ngroups;
00224 parent->ndatasets = ndatasets;
00225
00226 if (ngroups>0)
00227 parent->groups = (H5Group *)malloc((int)ngroups*sizeof(H5Group));
00228
00229 if (ndatasets>0)
00230 parent->datasets = (H5Dataset *)malloc((int)ndatasets*sizeof(H5Dataset));
00231
00232 gidx=didx=0;
00233 for (i=0; i<nmembers; i++)
00234 {
00235 obj_type = mtypes[i];
00236 len = strlen(mnames[i])+strlen(parent->fullpath)+2;
00237 if (obj_type == H5G_GROUP)
00238 {
00239 g = (H5Group*)&parent->groups[gidx];
00240 H5Group_ctor(g);
00241 g->fid = fid;
00242 g->fullpath = (char*)malloc(len);
00243 strcpy(g->fullpath, parent->fullpath);
00244 if (strlen(parent->fullpath)>1) strcat(g->fullpath, "/");
00245 strcat(g->fullpath, mnames[i]);
00246 g->objID[0] = objno0[i];
00247 g->objID[1] = objno1[i];
00248 g->parent = parent;
00249
00250 if (!H5File_has_loop(g, (const long unsigned int*)g->objID))
00251 ret_value = H5File_depth_first( g );
00252 gidx++;
00253 } else if (obj_type == H5G_DATASET)
00254 {
00255 d = (H5Dataset*)&parent->datasets[didx];
00256 H5Dataset_ctor(d);
00257 d->fid = fid;
00258 d->fullpath = (char*)malloc(len*2);
00259 strcpy(d->fullpath, parent->fullpath);
00260 if (strlen(parent->fullpath)>1) strcat(d->fullpath, "/");
00261 strcat(d->fullpath, mnames[i]);
00262 d->objID[0] = objno0[i];
00263 d->objID[1] = objno1[i];
00264 H5Dataset_init(d);
00265 didx++;
00266 }
00267 }
00268
00269 done:
00270 if (mnames)
00271 {
00272 for (i=0; i<nmembers; i++)
00273 {
00274 if (mnames[i]) free(mnames[i]);
00275 }
00276 free (mnames);
00277 }
00278 if (mtypes) free (mtypes);
00279 if (objno0) free (objno0);
00280 if (objno1) free (objno1);
00281 if (gid > 0) H5Gclose(gid);
00282 return ret_value;
00283 }
00284
00285 herr_t obj_info_all(hid_t loc_id, const char *name, void *opdata)
00286 {
00287 H5G_stat_t statbuf;
00288 info_all_t* info = (info_all_t*)opdata;
00289
00290 if (H5Gget_objinfo(loc_id, name, 0, &statbuf) < 0)
00291 {
00292 *(info->type+info->count) = -1;
00293 *(info->objname+info->count) = NULL;
00294 *(info->objno0+info->count) = 0;
00295 *(info->objno1+info->count) = 0;
00296 } else {
00297 *(info->type+info->count) = statbuf.type;
00298 *(info->objname+info->count) = (char *)strdup(name);
00299 *(info->objno0+info->count) = statbuf.objno[0];
00300 *(info->objno1+info->count) = statbuf.objno[1];
00301 }
00302 info->count++;
00303
00304 return 0;
00305 }
00306
00307 herr_t H5Gget_obj_info_all( hid_t loc_id, const char *group_name, char **objname,
00308 int *type, unsigned long *objno0, unsigned long *objno1)
00309 {
00310 info_all_t info;
00311 info.objname = objname;
00312 info.type = type;
00313 info.count = 0;
00314 info.objno0 = objno0;
00315 info.objno1 = objno1;
00316
00317 if(H5Giterate(loc_id, group_name, 0, obj_info_all, (void *)&info)<0)
00318 return -1;
00319
00320 return 0;
00321 }
00322
00323
00324
00325
00326
00327 int H5File_has_loop(const H5Group *g, const unsigned long ids[])
00328 {
00329 int ret_value=0;
00330 H5Group *parent;
00331
00332 assert(g);
00333
00334 if ( (parent = g->parent) )
00335 {
00336 if ( (parent->objID[0] == ids[0]) && (parent->objID[1] == ids[1]) )
00337 ret_value = 1;
00338 else
00339 ret_value = H5File_has_loop(parent, ids);
00340 }
00341
00342 return ret_value;
00343 }
00344
00345 int H5File_read_attribute(hid_t loc_id, H5Attribute *attrs)
00346 {
00347 int ret_value=0, n, i, rank;
00348 hid_t aid=-1;
00349 H5Attribute *a;
00350
00351 if (NULL == attrs || loc_id<0) {
00352 ret_value = -1;
00353 goto done;
00354 }
00355
00356 n = H5Aget_num_attrs(loc_id);
00357 for (i=0; i<n; i++) {
00358 a = &(attrs[i]);
00359 aid = H5Aopen_idx(loc_id, i);
00360 H5Attribute_init(a, aid);
00361 H5Attribute_read(a);
00362 H5Aclose(aid);
00363 }
00364
00365 done:
00366 return ret_value;
00367 }