2007-04-26 02:39:41 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Copyright by The HDF Group. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
2017-04-18 03:32:16 +08:00
|
|
|
* the COPYING file, which can be found at the root of the source code *
|
2021-02-17 22:52:36 +08:00
|
|
|
* distribution tree, or in https://www.hdfgroup.org/licenses. *
|
2017-04-18 03:32:16 +08:00
|
|
|
* If you do not have access to either file, you may request a copy from *
|
|
|
|
* help@hdfgroup.org. *
|
2007-04-26 02:39:41 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program checks if group exists in a file and creates it including
|
|
|
|
* all intermediate groups.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hdf5.h"
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
#define H5FILE_NAME "interm_group.h5"
|
2007-04-26 02:39:41 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
hid_t file;
|
|
|
|
hid_t g1_id, g2_id, g3_id;
|
|
|
|
hid_t grp_crt_plist;
|
2007-04-26 02:39:41 +08:00
|
|
|
H5G_info_t g2_info;
|
2020-09-30 22:27:10 +08:00
|
|
|
char name[3];
|
2007-04-26 02:39:41 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
herr_t status;
|
|
|
|
int i;
|
2007-04-26 02:39:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a file using the default properties.
|
|
|
|
*/
|
|
|
|
file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a group in the file.
|
|
|
|
*/
|
2007-08-24 04:25:25 +08:00
|
|
|
g1_id = H5Gcreate2(file, "/G1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
2007-04-26 02:39:41 +08:00
|
|
|
|
|
|
|
H5Gclose(g1_id);
|
|
|
|
H5Fclose(file);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now reopen the file and group in the file.
|
|
|
|
*/
|
|
|
|
file = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if group /G1 exists in the file.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
if (H5Lexists(file, "/G1", H5P_DEFAULT) != FALSE)
|
|
|
|
printf("Group /G1 exists in the file\n");
|
2008-09-16 23:52:51 +08:00
|
|
|
|
2007-04-26 02:39:41 +08:00
|
|
|
/*
|
2008-09-16 23:52:51 +08:00
|
|
|
* Check that group G2/G3 exists in /G1 and if not create it using
|
2007-04-26 02:39:41 +08:00
|
|
|
* intermediate group creation property.
|
|
|
|
*/
|
2007-08-28 23:02:54 +08:00
|
|
|
g1_id = H5Gopen2(file, "/G1", H5P_DEFAULT);
|
2020-09-30 22:27:10 +08:00
|
|
|
/* Next commented call causes error stack to be printed out; the next one
|
|
|
|
* works fine; is it a bug or a feature? EIP 04-25-07
|
|
|
|
*/
|
|
|
|
/* if (H5Lexists(g1_id, "G2/G3", H5P_DEFAULT) !=TRUE) { */
|
|
|
|
if (H5Lexists(g1_id, "G2", H5P_DEFAULT) != TRUE) {
|
|
|
|
|
|
|
|
grp_crt_plist = H5Pcreate(H5P_LINK_CREATE);
|
|
|
|
|
|
|
|
/* Set flag for intermediate group creation */
|
|
|
|
status = H5Pset_create_intermediate_group(grp_crt_plist, TRUE);
|
|
|
|
g3_id = H5Gcreate2(g1_id, "G2/G3", grp_crt_plist, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
H5Gclose(g3_id);
|
2008-09-16 23:52:51 +08:00
|
|
|
}
|
2007-04-26 02:39:41 +08:00
|
|
|
H5Gclose(g1_id);
|
|
|
|
|
|
|
|
/* Now check if group /G1/G2 exists in the file, then open it and print
|
|
|
|
* its members names
|
|
|
|
*/
|
2008-09-16 23:52:51 +08:00
|
|
|
if (H5Lexists(file, "/G1/G2", H5P_DEFAULT)) {
|
2007-04-26 02:39:41 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
g2_id = H5Gopen2(file, "/G1/G2", H5P_DEFAULT);
|
|
|
|
status = H5Gget_info(g2_id, &g2_info);
|
|
|
|
printf("Group /G1/G2 has %d member(s)\n", (int)g2_info.nlinks);
|
2007-04-26 02:39:41 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
for (i = 0; i < (int)g2_info.nlinks; i++) {
|
|
|
|
H5Lget_name_by_idx(g2_id, ".", H5_INDEX_NAME, H5_ITER_NATIVE, (hsize_t)i, name, 3, H5P_DEFAULT);
|
|
|
|
printf("Object's name is %s\n", name);
|
|
|
|
}
|
|
|
|
H5Gclose(g2_id);
|
2008-09-16 23:52:51 +08:00
|
|
|
}
|
2007-04-26 02:39:41 +08:00
|
|
|
H5Fclose(file);
|
|
|
|
return 0;
|
|
|
|
}
|