From 27a5b3b98ec99784cd9efc4e5799a73aa38fe695 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Tue, 9 Jan 2024 18:01:23 +0100
Subject: [PATCH] Set up binary wrapper for HDF5

---
 src/include/file_io.h   | 76 +++++++++++++++++++++++++++++++++++++++++
 src/libnptm/file_io.cpp | 33 ++++++++++++++++++
 2 files changed, 109 insertions(+)
 create mode 100644 src/include/file_io.h
 create mode 100644 src/libnptm/file_io.cpp

diff --git a/src/include/file_io.h b/src/include/file_io.h
new file mode 100644
index 00000000..180dd676
--- /dev/null
+++ b/src/include/file_io.h
@@ -0,0 +1,76 @@
+/*! \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
diff --git a/src/libnptm/file_io.cpp b/src/libnptm/file_io.cpp
new file mode 100644
index 00000000..f73d8ff9
--- /dev/null
+++ b/src/libnptm/file_io.cpp
@@ -0,0 +1,33 @@
+/*! \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;
+}
-- 
GitLab