Commit 27a5b3b9 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Set up binary wrapper for HDF5

parent 40ed5118
Loading
Loading
Loading
Loading

src/include/file_io.h

0 → 100644
+76 −0
Original line number Diff line number Diff line
/*! \file file_io.h
 *
 * \brief Library to handle I/O operations with files.
 */
#ifndef INCLUDE_FILE_IO_H_
#define INCLUDE_FILE_IO_H_

/*! \class FileSchema
 *
 * \brief File content descriptor.
 *
 * Accessing binary files requires detailed knowledge of their contents. The `FileSchema`
 * class is intended to encapsulate this information and use it as a wrapper to control
 * I/O operations towards different file formats. Any file can be thought of as a sequence
 * of records, which may further contain arbitrarily complex structures. By describing the
 * structure of records, it is possible to support virtually any type of format.
 */
class FileSchema {
 protected:
  //! \brief Number of records conained in the file.
  int num_records;
  //! \brief Array of record descriptors.
  std::string *record_types;

 public:
  /*! \brief FileSchema instance constructor.
   *
   * \param num_rec: `int` Number of records in the file.
   * \param rec_types: `string *` Description of the records in the file.
   */
  FileSchema(int num_rec, std::string *rec_types);

  /*! \brief FileSchema instance destroyer.
   */
  ~FileSchema();
};

/*! \class HDFFile
 *
 * \brief HDF5 I/O wrapper class.
 *
 * This class manages I/O operations toward HDF5 format files.
 */
class HDFFile {
 protected:
  std::string file_name;
  bool file_open_flag;
  hid_t file_id;
  herr_t status;

 public:
  /*! \brief HDFFile instance constructor.
   *
   * \param name: `string` Name of the file.
   * \param flags: `unsigned int` File access flags.
   * \param fcpl_id: `hid_t` File creation property list identifier.
   * \param fapl_id: `hid_t` File access property list identifier.
   */
  HDFFile(
	  std::string name, unsigned int flags = H5F_ACC_EXCL,
	  hid_t fcpl_id = H5P_DEFAULT, hid_t fapl_id = H5P_DEFAULT
);

  /*! \brief Close the current file.
   */
  herr_t close();

  /*! \brief Get current status.
   */
  herr_t get_status() { return status; }
  
  /*! \brief Check whether the attached file is currently open.
   */
  bool is_open() { return file_open_flag; }
};
#endif
+33 −0
Original line number Diff line number Diff line
/*! \file file_io.cpp
 *
 * \brief Implementation of file I/O operations.
 */
#include <string>
#include "hdf5.h"

#ifndef INCLUDE_FILE_IO_H_
#include "../include/file_io.h"
#endif

using namespace std;

FileSchema::FileSchema(int num_rec, string *rec_types) {
  num_records = num_rec;
  record_types = new string[num_rec];
  for (int i = 0; i < num_rec; i++) record_types[i] = rec_types[i];
}

FileSchema::~FileSchema() { delete[] record_types; }

HDFFile::HDFFile(string name, unsigned int flags, hid_t fcpl_id, hid_t fapl_id) {
  file_name = name;
  file_id = H5Fcreate(name.c_str(), flags, fcpl_id, fapl_id);
  if (file_id != H5I_INVALID_HID) file_open_flag = true;
  status = (herr_t)0;
}

herr_t HDFFile::close() {
  status = H5Fclose(file_id);
  if (status == 0) file_open_flag = false;
  return status;
}