Skip to content
Snippets Groups Projects
Commit d8b92e8a authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Define VirtualAsciiFile class

parent 60678631
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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 <<< */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment