/*
 *   Creating a dataset.  Check what objects are still open.
 */

#include "hdf5.h"
#define FILE "dset.h5"

int whatisopen(hid_t fid) {
        int cnt;
        int howmany;
        int i;
        H5I_type_t ot;
        hid_t anobj;
        hid_t *objs;
        char name[1024];
        herr_t status;

        cnt = H5Fget_obj_count(fid, H5F_OBJ_ALL);

        if (cnt <= 0) return cnt;

        printf("%d object(s) open\n", cnt);

        objs = malloc(cnt * sizeof(hid_t));

        howmany = H5Fget_obj_ids(fid,H5F_OBJ_ALL,cnt,objs);

        printf("open objects:\n");

        for (i = 0; i < howmany; i++ ) {
             anobj = *objs++;
             ot = H5Iget_type(anobj);
             status = H5Iget_name(anobj, name, 1024);
             printf(" %d: type %d, name %s\n",i,ot,name);
        }
         
        return howmany;
}
main() {

   hid_t       file_id, dataset_id, dataspace_id;  /* identifiers */
   hsize_t     dims[2];
   herr_t      status;
   H5I_type_t  itype;

   /* Create a new file using default properties. */
   file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /* Create the data space for the dataset. */
   dims[0] = 4; 
   dims[1] = 6; 
   dataspace_id = H5Screate_simple(2, dims, NULL);

   /* Create the dataset. */
   dataset_id = H5Dcreate(file_id, "/dset", H5T_IEEE_F32BE, dataspace_id, H5P_DEFAULT);

   /* End access to the dataset and release resources used by it. */
   status = H5Dclose(dataset_id);

   /* Terminate access to the data space. */ 
   status = H5Sclose(dataspace_id);

   /* Close the file. Check if anything open first*/
   whatisopen (file_id);

   status = H5Fclose(file_id);
}

