From d8b92e8aa5010cf41744ac864ca99615899f9115 Mon Sep 17 00:00:00 2001 From: Giovanni La Mura <giovanni.lamura@inaf.it> Date: Fri, 24 May 2024 18:55:34 +0200 Subject: [PATCH] Define VirtualAsciiFile class --- src/include/file_io.h | 46 +++++++++++++++++++++++++++++++++++++++++ src/libnptm/file_io.cpp | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/include/file_io.h b/src/include/file_io.h index e5c99008..312517f1 100644 --- a/src/include/file_io.h +++ b/src/include/file_io.h @@ -5,6 +5,8 @@ #ifndef INCLUDE_FILE_IO_H_ #define INCLUDE_FILE_IO_H_ +#include <vector> + /*! \class FileSchema * * \brief File content descriptor. @@ -156,4 +158,48 @@ class HDFFile { hid_t dapl_id=H5P_DEFAULT, hid_t dxpl_id=H5P_DEFAULT ); }; + +/*! \class VirtualAsciiFile + * + * \brief Virtual representation of an ASCII file. + */ +class VirtualAsciiFile { +protected: + //! \brief A vector of strings representing the file lines. + std::vector<std::string> *file_lines; + //! \brief The name of the file. + std::string _file_name; + +public: + const std::string& file_name = _file_name; + + /*! \brief VirtualAsciiFile instance constructor. + * + * \param name: `const string&` Reference to a string for the file name. + */ + VirtualAsciiFile(const std::string& name); + + /*! \brief VirtualAsciiFile copy constructor. + * + * \param rhs: `const VirtualAsciiFile&` Reference to a VirtualAsciiFile instance. + * \param name: `const string&` Name of the copy (optional, default is the same as original). + */ + VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::string& name = ""); + + /*! \brief VirtualAsciiFile instance destroyer. + */ + ~VirtualAsciiFile(); + + /*! \brief Append a line at the end of the file. + * + * \param line: `const string&` Reference to a string representing the line. + */ + void append(const std::string& line); + + /*! \brief Write virtual file contents to a real file on disk. + * + * \return result: `int` A result code (0 if successful). + */ + int write_to_disk(); +}; #endif diff --git a/src/libnptm/file_io.cpp b/src/libnptm/file_io.cpp index c196d515..b7a4b404 100644 --- a/src/libnptm/file_io.cpp +++ b/src/libnptm/file_io.cpp @@ -3,6 +3,7 @@ * \brief Implementation of file I/O operations. */ #include <exception> +#include <fstream> #include <regex> #include <string> #include <hdf5.h> @@ -21,6 +22,7 @@ using namespace std; +/* >>> FileSchema class implementation <<< */ FileSchema::FileSchema(int num_rec, const std::string *rec_types, const std::string *rec_names) { num_records = num_rec; record_types = new string[num_rec]; @@ -48,7 +50,9 @@ string* FileSchema::get_record_types() { for (int i = 0; i < num_records; i++) rec_types[i] = record_types[i]; return rec_types; } +/* >>> End of FileSchema class implementation <<< */ +/* >>> HDFFile class implementation <<< */ HDFFile::HDFFile(const std::string& name, unsigned int flags, hid_t fcpl_id, hid_t fapl_id) { file_name = name; if (flags == H5F_ACC_EXCL || flags == H5F_ACC_TRUNC) @@ -222,3 +226,45 @@ herr_t HDFFile::write( } return status; } +/* >>> End of HDFFile class implementation <<< */ + +/* >>> VirtualAsciiFile class implementation <<< */ +VirtualAsciiFile::VirtualAsciiFile(const std::string& name) { + _file_name = name; + _file_lines = new vector<string>(); +} + +VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::string& name) { + if (name.compare("") == 0) { + _file_name = rhs._file_name; + } else { + _file_name = name; + } + _file_lines = new vector<string>(); + for (vector<string>::iterator it = rhs._file_lines->begin(); it != rhs._file_lines->end(); ++it) { + _file_lines->push_back(*it); + } +} + +VirtualAsciiFile::~VirtualAsciiFile() { + if (_file_lines != NULL) delete _file_lines; +} + +void VirtualAsciiFile::append(const string& line) { + _file_lines.push_back(line); +} + +int VirtualAsciiFile::write_to_disk() { + int result = 0; + fstream output_file; + output_file.open(file_name, ios::out); + if (output_file.is_open()) { + for (vector<string>::iterator it = _file_lines->begin(); it != _file_lines->end(); ++it) { + output_file << *it; + } + } else { + result = 1; + } + return result; +} +/* >>> End of VirtualAsciiFile class implementation <<< */ -- GitLab