H5LTopen_file_image for C++ API

Hi,

I'm using a C++ library that read in a HDF5 file with the C++ HDF5 API,
using a H5File instance.

I would like to use this library to read a file image (file in memory)
instead of a file in the disk.

In C, this can be done with the 'H5LTopen_file_image' function, from HDF5
Lite, which return a hid_t for the opened file, that can be pass to other
functions (H5Dopen, ...). How could I build a H5File from this hid_t, so I
don't have to rewrite the library from the C++ HDF5 to the C HDF5 API?

Or is there another solution to read a file image with the C++ HDF5 API?

Thanks in advance,
David

···

~

Hi David,

It wasn't the intention for file's hid_t to be used that way, but here is a possible workaround.

/* Assuming file_id is the hid_t you have */
H5File file2; /* create a default H5File object */
file2.setId(file_id); /* assign the Id to this object using the existing hid_t */

Please be mindful that this id might be closed when file2 is going out of scope. If premature closing of the id happens, one way to work around that is to reset file2's id to 0, before it's going out of scope. I hope this works for you.

Thank you,
Binh-Minh

···

________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of David Froger <david.froger@inria.fr>
Sent: Thursday, September 12, 2013 9:41 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] H5LTopen_file_image for C++ API

Hi,

I'm using a C++ library that read in a HDF5 file with the C++ HDF5 API,
using a H5File instance.

I would like to use this library to read a file image (file in memory)
instead of a file in the disk.

In C, this can be done with the 'H5LTopen_file_image' function, from HDF5
Lite, which return a hid_t for the opened file, that can be pass to other
functions (H5Dopen, ...). How could I build a H5File from this hid_t, so I
don't have to rewrite the library from the C++ HDF5 to the C HDF5 API?

Or is there another solution to read a file image with the C++ HDF5 API?

Thanks in advance,
David
~

Hi Binh-Minh,

Thanks for the reply, that what I was looking for!

I'll try with this solution.

David

----- Mail original -----

···

De: "Binh-Minh Ribler" <bmribler@hdfgroup.org>
À: "HDF Users Discussion List" <hdf-forum@lists.hdfgroup.org>
Envoyé: Jeudi 12 Septembre 2013 16:08:29
Objet: Re: [Hdf-forum] H5LTopen_file_image for C++ API

Hi David,

It wasn't the intention for file's hid_t to be used that way, but
here is a possible workaround.

/* Assuming file_id is the hid_t you have */
H5File file2; /* create a default H5File object */
file2.setId(file_id); /* assign the Id to this object using the
existing hid_t */

Please be mindful that this id might be closed when file2 is going
out of scope. If premature closing of the id happens, one way to
work around that is to reset file2's id to 0, before it's going out
of scope. I hope this works for you.

Thank you,
Binh-Minh

From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of
David Froger <david.froger@inria.fr>
Sent: Thursday, September 12, 2013 9:41 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] H5LTopen_file_image for C++ API

Hi,

I'm using a C++ library that read in a HDF5 file with the C++ HDF5
API,
using a H5File instance.

I would like to use this library to read a file image (file in
memory)
instead of a file in the disk.

In C, this can be done with the 'H5LTopen_file_image' function, from
HDF5
Lite, which return a hid_t for the opened file, that can be pass to
other
functions (H5Dopen, ...). How could I build a H5File from this hid_t,
so I
don't have to rewrite the library from the C++ HDF5 to the C HDF5
API?

Or is there another solution to read a file image with the C++ HDF5
API?

Thanks in advance,
David
~

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org

You're welcome, David. Please let us know if you need more information.

Binh-Minh

···

________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of David Froger <david.froger@inria.fr>
Sent: Thursday, September 12, 2013 10:20 AM
To: HDF Users Discussion List
Subject: Re: [Hdf-forum] H5LTopen_file_image for C++ API

Hi Binh-Minh,

Thanks for the reply, that what I was looking for!

I'll try with this solution.

David

________________________________
De: "Binh-Minh Ribler" <bmribler@hdfgroup.org>
?: "HDF Users Discussion List" <hdf-forum@lists.hdfgroup.org>
Envoy?: Jeudi 12 Septembre 2013 16:08:29
Objet: Re: [Hdf-forum] H5LTopen_file_image for C++ API

Hi David,

It wasn't the intention for file's hid_t to be used that way, but here is a possible workaround.

/* Assuming file_id is the hid_t you have */
H5File file2; /* create a default H5File object */
file2.setId(file_id); /* assign the Id to this object using the existing hid_t */

Please be mindful that this id might be closed when file2 is going out of scope. If premature closing of the id happens, one way to work around that is to reset file2's id to 0, before it's going out of scope. I hope this works for you.

Thank you,
Binh-Minh
________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of David Froger <david.froger@inria.fr>
Sent: Thursday, September 12, 2013 9:41 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] H5LTopen_file_image for C++ API

Hi,

I'm using a C++ library that read in a HDF5 file with the C++ HDF5 API,
using a H5File instance.

I would like to use this library to read a file image (file in memory)
instead of a file in the disk.

In C, this can be done with the 'H5LTopen_file_image' function, from HDF5
Lite, which return a hid_t for the opened file, that can be pass to other
functions (H5Dopen, ...). How could I build a H5File from this hid_t, so I
don't have to rewrite the library from the C++ HDF5 to the C HDF5 API?

Or is there another solution to read a file image with the C++ HDF5 API?

Thanks in advance,
David
~

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://mail.lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org

Hi David,

I too recently needed H5LTopen_file_image with the C++ API. Since
I was unaware of the method Binh-Minh suggested, I instead
subclassed H5::H5File:

  /**
   * Open a `file image`, i.e. a chunk of memory which contains
   * the contents of a HDF-5 file.

···

*
   * See H5LTopen_file_image for the arguments that the constructor
   * takes.
   **/
  class H5FileImage : public H5::H5File {
   public:
    H5FileImage(void *buf_ptr, size_t buf_size, unsigned flags);

    static const unsigned OPEN_RW;
    static const unsigned DONT_COPY;
    static const unsigned DONT_RELEASE;
  };

  H5FileImage::H5FileImage(void *buf_ptr, size_t buf_size, unsigned flags)
      : H5::H5File() {
    hid_t id = H5LTopen_file_image(buf_ptr, buf_size, flags);
    if (id < 0)
      throw H5::FileIException("H5FileImage constructor",
                               "H5LTopen_file_image failed");
    p_setId(id);
  }

  const unsigned H5FileImage::OPEN_RW = H5LT_FILE_IMAGE_OPEN_RW;
  const unsigned H5FileImage::DONT_COPY = H5LT_FILE_IMAGE_DONT_COPY;
  const unsigned H5FileImage::DONT_RELEASE = H5LT_FILE_IMAGE_DONT_RELEASE;

Usage should be pretty obvious:

  H5FileImage f(buf_ptr, buf_size, H5FileImage::DONT_COPY &
H5FileImage::DONT_RELEASE);
  // Use `f` just as you would H5::H5File.

Cheers,
Brad

________________________________
From: Hdf-forum <hdf-forum-bounces at lists.hdfgroup.org> on behalf of
David Froger <david.froger at inria.fr>
Sent: Thursday, September 12, 2013 9:41 AM
To: hdf-forum at lists.hdfgroup.org
Subject: [Hdf-forum] H5LTopen_file_image for C++ API

Hi,

I'm using a C++ library that read in a HDF5 file with the C++ HDF5 API,
using a H5File instance.

I would like to use this library to read a file image (file in memory)
instead of a file in the disk.

In C, this can be done with the 'H5LTopen_file_image' function, from HDF5
Lite, which return a hid_t for the opened file, that can be pass to other
functions (H5Dopen, ...). How could I build a H5File from this hid_t, so I
don't have to rewrite the library from the C++ HDF5 to the C HDF5 API?

Or is there another solution to read a file image with the C++ HDF5 API?

Thanks in advance,
David
~