#include <string>

#include <iostream.h>
#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

const string	FILE_NAME( "Group.h5" );

// Operator function
herr_t get_all_groups(hid_t loc_id, const char *name, void *opdata);

typedef struct {
    int index;               // index of the current object 
} iter_info;

int main(void)
{
   try
   {
      H5File* file = new H5File(FILE_NAME, H5F_ACC_RDWR);

      /*
       * Use iterator to see the names of the objects in the file
       * root directory and identify all the groups.
       */
      cout << endl << "Iterating over elements in the file" << endl;
      iter_info info;
      int idx = 0;
      info.index = 0;
      idx = file->iterateElems("/", NULL, get_all_groups, &info);
      cout << endl;

      /* Close the file. */
      delete file;

   }  // end of try block

   // catch failure caused by the H5File operations
   catch (FileIException error)
   {
      error.printError();
      return -1;
   }
   return 0;
}

// At this time, iterateElems is not completely coded in C++ yet. 
// Use H5G_iterate_t for the 'op'. (You need to deal with the 
// hid_t type here.)  opdata holds the current index for the elements 
// so H5Gget_objtype_by_idx can be used to get the object type.  
// Other things can be put in opdata by changing iter_info at
// the top.
herr_t
get_all_groups(hid_t loc_id, const char *name, void *opdata)
{
    iter_info *info=(iter_info *)opdata;

    // Here you can do whatever with the name... 
    cout << "Name : " << name << endl;

    // you can use this call to select just the groups
    // H5G_LINK    0  Object is a symbolic link.  
    // H5G_GROUP   1  Object is a group.  
    // H5G_DATASET 2  Object is a dataset.  
    // H5G_TYPE    3  Object is a named datatype.  
    int obj_type = H5Gget_objtype_by_idx(loc_id, info->index);
    if(obj_type == H5G_GROUP)
	cout << "        is a group" << endl;

    (info->index)++;
    return 0;
 }


