Home > Enterprise >  HDF5 Cpp - getting names of all groups in h5 file
HDF5 Cpp - getting names of all groups in h5 file

Time:09-21

I want to get all group names from h5 file. I was able to do that using a global variable that collects the names. My question is how to do it without the global variable? I dont want to have any global variables but i have a vector that will need to contain all the group names.

//this is the global variable i am trying to avoid.
std::vector<std::string> myVec;

herr_t op_func(hid_t loc_id, const char* name, const H5O_info_t* info, void opdata)
{
   if(info->type == H5O_TYPE_GROUP)
   {
       myVec.push_back(std::string(name));
   }
}
//here is my function and i pass a vector that i want the names to be in via reference. 
void getGroupNames(hid_t fileId, std::vector<std::string>& someVec)
{
    herr_t status;
    status = H5Ovisit(fileId, H5_INDEX_NAME, H5_ITER_NATIVE, op_func, NULL);
    someVec = myVec;
}

CodePudding user response:

One way to do that would be to pass someVec to H5Ovisit in the last parameter, i.e.:

status = H5Ovisit(fileId, H5_INDEX_NAME, H5_ITER_NATIVE, op_func, static_cast<void*>(&someVec);

Then in op_func push directly to it:

auto vec = static_cast<std::vector<std::string>*>(opdata);
vec->push_back(std::string(name));

(Note that the last parameter to op_func should be void*)

  • Related