1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
/** \file grpattr.c
* \brief MINC 2.0 group/attribute functions
* \author Bert Vincent and Leila Baghdadi
*
* Functions to manipulate attributes and groups.
************************************************************************/
#include <stdlib.h>
#include <hdf5.h>
#include "minc2.h"
#include "minc2_private.h"
#define MILIST_MAX_PATH 256
#define MILIST_RECURSE 0x0001
struct milistframe {
struct milistframe *next;
hid_t grp_id;
int att_idx;
int grp_idx;
char relpath[MILIST_MAX_PATH];
};
struct milistdata {
int flags;
char *name_ptr;
int name_len;
struct milistframe *frame_ptr; /* recursive frame */
};
/*! Start listing the objects in a group.
*/
int
milist_start(mihandle_t vol, const char *path, int flags,
milisthandle_t *handle)
{
hid_t grp_id;
char fullpath[256];
struct milistdata *data;
struct milistframe *frame;
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
grp_id = H5Gopen1(vol->hdf_id, fullpath);
if (grp_id < 0) {
return (MI_ERROR);
}
data = (struct milistdata *) malloc(sizeof (struct milistdata));
if (data == NULL) {
return (MI_ERROR);
}
frame = (struct milistframe *) malloc(sizeof (struct milistframe));
frame->next = NULL;
frame->grp_id = grp_id;
frame->att_idx = 0;
frame->grp_idx = 0;
strcpy(frame->relpath, path);
data->frame_ptr = frame;
data->flags = flags;
*handle = data;
return (MI_NOERROR);
}
static int
milist_recursion(milisthandle_t handle, char *path)
{
struct milistdata *data = (struct milistdata *) handle;
herr_t r;
struct milistframe *frame;
for(;;){
H5E_BEGIN_TRY {
r = H5Gget_objtype_by_idx(data->frame_ptr->grp_id,
data->frame_ptr->grp_idx);
} H5E_END_TRY;
r--;
if (r < 0) {
/* End of this group, need to pop the frame. */
frame = data->frame_ptr->next;
H5Gclose(data->frame_ptr->grp_id);
free(data->frame_ptr);
data->frame_ptr = frame;
if (frame == NULL) {
return (MI_ERROR);
}
}
else {
data->frame_ptr->grp_idx++;
/* If the object is a group, we need to recurse into it.
*/
r = 1;
if (r == H5G_GROUP) {
char tmp[256];
int l;
H5Gget_objname_by_idx(data->frame_ptr->grp_id,
data->frame_ptr->grp_idx - 1,
tmp, sizeof(tmp));
frame = malloc(sizeof(struct milistframe));
if (frame == NULL) {
return (MI_ERROR);
}
frame->grp_idx = 0;
frame->att_idx = 0;
frame->grp_id = H5Gopen1(data->frame_ptr->grp_id, tmp);
strcpy(frame->relpath, data->frame_ptr->relpath);
l = strlen(frame->relpath);
if (l > 0 && frame->relpath[l - 1] != '/') {
strcat(frame->relpath, "/");
}
strcat(frame->relpath, tmp);
/* Start working on the next frame.
*/
frame->next = data->frame_ptr;
data->frame_ptr = frame;
strncpy(path, data->frame_ptr->relpath, MILIST_MAX_PATH);
break; /* Out of inner for(;;) */
}
}
}
return (MI_NOERROR);
}
static herr_t
milist_attr_op(hid_t loc_id, const char *attr_name, void *op_data)
{
struct milistdata *data = (struct milistdata *) op_data;
strncpy(data->name_ptr, attr_name, data->name_len);
return (1);
}
/*! Get attributes at a given path
*/
int
milist_attr_next(mihandle_t vol, milisthandle_t handle,
char *path, int maxpath,
char *name, int maxname)
{
struct milistdata *data = (struct milistdata *) handle;
herr_t r;
data->name_ptr = name;
data->name_len = maxname;
for (;;) {
H5E_BEGIN_TRY {
r = H5Aiterate1(data->frame_ptr->grp_id,
&data->frame_ptr->att_idx, milist_attr_op, data);
} H5E_END_TRY;
if (r > 0) {
strncpy(path, data->frame_ptr->relpath, maxpath);
return (MI_NOERROR);
}
else if (data->flags & MILIST_RECURSE) {
r = milist_recursion(handle, path);
if ( r == MI_ERROR)
{
return(MI_ERROR);
}
}
else {
return (MI_ERROR);
}
}
return (MI_NOERROR);
}
/*! Finish listing attributes or groups
*/
int
milist_finish(milisthandle_t handle)
{
struct milistdata *data = (struct milistdata *) handle;
struct milistframe *frame;
if (data == NULL) {
return (MI_ERROR);
}
while ((frame = data->frame_ptr) != NULL) {
data->frame_ptr = frame->next;
H5Gclose(frame->grp_id);
free(frame);
}
free(data);
return (MI_NOERROR);
}
static herr_t
milist_grp_op(hid_t loc_id, const char *name, void *op_data)
{
struct milistdata *data = (struct milistdata *) op_data;
H5G_stat_t statbuf;
H5Gget_objinfo(loc_id,name, FALSE, &statbuf);
if (statbuf.type == H5G_GROUP)
{
int l;
l = strlen(data->frame_ptr->relpath);
if (l > 0 && data->frame_ptr->relpath[l-1] != '/') {
strcat(data->frame_ptr->relpath, "/");
}
strcat(data->frame_ptr->relpath, name);
}
return(1);
}
/*! Get the group at given path
*/
int
milist_grp_next(milisthandle_t handle, char *path, int maxpath)
{
struct milistdata *data = (struct milistdata *) handle;
herr_t r;
if (!(data->flags & MILIST_RECURSE))
{
char fullpath[256];
char tmp[256];
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
strncat(fullpath, data->frame_ptr->relpath, sizeof (fullpath) - strlen(fullpath));
strcpy(tmp, data->frame_ptr->relpath);
H5E_BEGIN_TRY {
r = H5Giterate(data->frame_ptr->grp_id,fullpath,
&data->frame_ptr->grp_idx, milist_grp_op, data);
} H5E_END_TRY;
if ( r > 0 )
{
strncpy(path, data->frame_ptr->relpath, maxpath);
strncpy(data->frame_ptr->relpath, tmp, maxpath);
return (MI_NOERROR);
}
else
{
return (MI_ERROR);
}
}
else if (data->flags & MILIST_RECURSE) {
r = milist_recursion(handle, path);
if ( r == MI_ERROR) {
return(MI_ERROR);
}
}
else {
return (MI_ERROR);
}
return (MI_NOERROR);
}
/*! Create a group at "path" using "name".
*/
int
micreate_group(mihandle_t vol, const char *path, const char *name)
{
hid_t hdf_file;
hid_t hdf_grp;
hid_t hdf_new_grp;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
H5E_BEGIN_TRY {
/* See if the group already exists. If so, just return.
*/
hdf_new_grp = H5Gopen1(hdf_grp, name);
if (hdf_new_grp >= 0) {
H5Gclose(hdf_new_grp);
return (MI_NOERROR);
}
/* Actually create the requested group.
*/
hdf_new_grp = H5Gcreate1(hdf_grp, name, 0);
if (hdf_new_grp < 0) {
return (MI_ERROR);
}
} H5E_END_TRY;
/* Close the handles we created.
*/
H5Gclose(hdf_new_grp);
H5Gclose(hdf_grp);
return (MI_NOERROR);
}
/*! Delete the named attribute.
*/
int
midelete_attr(mihandle_t vol, const char *path, const char *name)
{
hid_t hdf_file;
hid_t hdf_grp;
herr_t hdf_result;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
/* Delete the attribute from the path.
*/
hdf_result = H5Adelete(hdf_grp, name);
if (hdf_result < 0) {
return (MI_ERROR);
}
/* Close the handles we created.
*/
H5Gclose(hdf_grp);
return (MI_NOERROR);
}
/** Delete the subgroup \a name from the group \a path
*/
int
midelete_group(mihandle_t vol, const char *path, const char *name)
{
hid_t hdf_file;
hid_t hdf_grp;
herr_t hdf_result;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
H5E_BEGIN_TRY {
/* Delete the group (or any object, really) from the path.
*/
hdf_result = H5Gunlink(hdf_grp, name);
if (hdf_result < 0) {
hdf_result = MI_ERROR;
}
else {
hdf_result = MI_NOERROR;
}
} H5E_END_TRY;
/* Close the handles we created.
*/
H5Gclose(hdf_grp);
return (hdf_result);
}
/** Get the length of a attribute
*/
int
miget_attr_length(mihandle_t vol, const char *path, const char *name,
int *length)
{
hid_t hdf_file;
hid_t hdf_grp;
hid_t hdf_attr;
hsize_t hdf_dims[1]; /* TODO: symbolic constant for "1" here? */
hid_t hdf_space;
hid_t hdf_type;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
hdf_attr = H5Aopen_name(hdf_grp, name);
if (hdf_attr < 0) {
return (MI_ERROR);
}
hdf_space = H5Aget_space(hdf_attr);
if (hdf_space < 0) {
return (MI_ERROR);
}
hdf_type = H5Aget_type(hdf_attr);
if (hdf_type < 0) {
return (MI_ERROR);
}
switch (H5Sget_simple_extent_ndims(hdf_space)) {
case 0: /* Scalar */
/* String types need to return the length of the string.
*/
if (H5Tget_class(hdf_type) == H5T_STRING) {
*length = H5Tget_size(hdf_type);
}
else {
*length = 1;
}
break;
case 1:
H5Sget_simple_extent_dims(hdf_space, hdf_dims, NULL);
*length = hdf_dims[0];
break;
default:
/* For now, we allow only scalars and vectors. No multidimensional
* arrays for MINC 2.0 attributes.
*/
return (MI_ERROR);
}
H5Tclose(hdf_type);
H5Sclose(hdf_space);
H5Aclose(hdf_attr);
H5Gclose(hdf_grp);
return (MI_NOERROR);
}
/** Get the type of an attribute.
*/
int
miget_attr_type(mihandle_t vol, const char *path, const char *name,
mitype_t *data_type)
{
hid_t hdf_file;
hid_t hdf_grp;
hid_t hdf_attr;
hid_t hdf_type;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
hdf_attr = H5Aopen_name(hdf_grp, name);
if (hdf_attr < 0) {
return (MI_ERROR);
}
hdf_type = H5Aget_type(hdf_attr);
switch (H5Tget_class(hdf_type)) {
case H5T_FLOAT:
if (H5Tget_size(hdf_type) == sizeof(float)) {
*data_type = MI_TYPE_FLOAT;
}
else {
*data_type = MI_TYPE_DOUBLE;
}
break;
case H5T_STRING:
*data_type = MI_TYPE_STRING;
break;
case H5T_INTEGER:
*data_type = MI_TYPE_INT;
break;
default:
return (MI_ERROR);
}
H5Tclose(hdf_type);
H5Aclose(hdf_attr);
H5Gclose(hdf_grp);
return (MI_NOERROR);
}
/** Get the values of an attribute.
*/
int
miget_attr_values(mihandle_t vol, mitype_t data_type, const char *path,
const char *name, int length, void *values)
{
hid_t hdf_file;
hid_t hdf_grp;
hid_t mtyp_id;
hid_t hdf_space;
hid_t hdf_attr;
char fullpath[256];
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
hdf_attr = H5Aopen_name(hdf_grp, name);
if (hdf_attr < 0) {
return (MI_ERROR);
}
switch (data_type) {
case MI_TYPE_INT:
mtyp_id = H5Tcopy(H5T_NATIVE_INT);
break;
case MI_TYPE_FLOAT:
mtyp_id = H5Tcopy(H5T_NATIVE_FLOAT);
break;
case MI_TYPE_DOUBLE:
mtyp_id = H5Tcopy(H5T_NATIVE_DOUBLE);
break;
case MI_TYPE_STRING:
mtyp_id = H5Tcopy(H5T_C_S1);
H5Tset_size(mtyp_id, length);
break;
default:
return (MI_ERROR);
}
hdf_space = H5Aget_space(hdf_attr);
if (hdf_space < 0) {
return (MI_ERROR);
}
/* If we're retrieving a vector, make certain the length passed into this
* function is sufficient.
*/
if (H5Sget_simple_extent_ndims(hdf_space) == 1) {
hsize_t hdf_dims[1];
H5Sget_simple_extent_dims(hdf_space, hdf_dims, NULL);
if (length < hdf_dims[0]) {
return (MI_ERROR);
}
}
H5Aread(hdf_attr, mtyp_id, values);
H5Aclose(hdf_attr);
H5Tclose(mtyp_id);
H5Sclose(hdf_space);
H5Gclose(hdf_grp);
return (MI_NOERROR);
}
/** Set the values of an attribute.
*/
int
miset_attr_values(mihandle_t vol, mitype_t data_type, const char *path,
const char *name, int length, const void *values)
{
hid_t hdf_file;
hid_t hdf_grp;
int result;
char fullpath[256];
hid_t tmp_id;
char *std_name;
char *pch;
int i,slength;
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
strncpy(fullpath, MI_ROOT_PATH "/" MI_INFO_NAME, sizeof (fullpath));
if (*path != '/') {
strncat(fullpath, "/", sizeof (fullpath) - strlen(fullpath));
}
strncat(fullpath, path, sizeof (fullpath) - strlen(fullpath));
/* find last occurance of '/' */
pch = strrchr(path,'/');
if(pch !=NULL) {
slength = strlen(path) - (pch-path);
std_name = malloc(slength);
for (i=0; i < slength; i++)
std_name[i] = path[pch-path+1+i];
std_name[slength]='\0';
}
else {
std_name = malloc(strlen(path) + 1);
strcpy(std_name, path);
}
/* might need to create standard dataset first*/
if (!strcmp(std_name,"acquisition") ||
!strcmp(std_name,"patient") ||
!strcmp(std_name,"study") )
{
H5E_BEGIN_TRY {
tmp_id = H5Dopen1(hdf_file, fullpath);
if (tmp_id < 0) {
create_standard_dataset(hdf_file,std_name);
}
else {
H5Dclose(tmp_id);
}
} H5E_END_TRY;
}
free(std_name);
/* Search through the path, descending into each group encountered.
*/
hdf_grp = midescend_path(hdf_file, fullpath);
if (hdf_grp < 0) {
return (MI_ERROR);
}
result = miset_attr_at_loc(hdf_grp, name, data_type, length, values);
if (result < 0) {
return (MI_ERROR);
}
/* added the following instead H5Gclose(hdf_grp) */
H5E_BEGIN_TRY {
tmp_id = H5Gclose(hdf_grp);
if (tmp_id < 0) {
tmp_id = H5Dclose(hdf_grp);
}
} H5E_END_TRY;
return (MI_NOERROR);
}
int
miadd_history_attr(mihandle_t vol, int length, const void *values)
{
int result;
hid_t hdf_file;
hid_t hdf_grp;
hid_t tmp_id;
/* Get a handle to the actual HDF file
*/
hdf_file = vol->hdf_id;
if (hdf_file < 0) {
return (MI_ERROR);
}
hdf_grp = midescend_path(hdf_file, "/minc-2.0");
if (hdf_grp < 0) {
return (MI_ERROR);
}
result = miset_attr_at_loc(hdf_grp, "history", MI_TYPE_STRING, length, values);
if (result < 0) {
return (MI_ERROR);
}
/* added the following instead H5Gclose(hdf_grp) */
H5E_BEGIN_TRY {
tmp_id = H5Gclose(hdf_grp);
if (tmp_id < 0) {
tmp_id = H5Dclose(hdf_grp);
}
} H5E_END_TRY;
return (MI_NOERROR);
}
|