Commit a43c3245 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Implement VirtualAsciiFile local interface

parent d8b92e8a
Loading
Loading
Loading
Loading
+46 −11
Original line number Diff line number Diff line
@@ -165,41 +165,76 @@ class HDFFile {
 */
class VirtualAsciiFile {
protected:
  //! \brief The number of lines.
  int32_t _num_lines;
  //! \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;
  std::vector<std::string> *_file_lines;

public:
  const std::string& file_name = _file_name;

  const int32_t &num_lines = _num_lines;
  /*! \brief VirtualAsciiFile instance constructor.
   *
   * \param name: `const string&` Reference to a string for the file name.
   * \param lines: `int32_t` Number of lines, if known in advance (optional, default is 0).
   */
  VirtualAsciiFile(const std::string& name);
  VirtualAsciiFile(int32_t lines = 0);

  /*! \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 = "");
  VirtualAsciiFile(const VirtualAsciiFile& rhs);

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

  /*! \brief Append another VirtualAsciiFile at the end of the current instance.
   *
   * \param rhs: `const VirtualAsciiFile&` Reference to the VirtualAsciiFile to be appended.
   */
  void append(const VirtualAsciiFile& rhs);

  /*! \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);
  void append_line(const std::string& line);

  /*! \brief Append the contents of the VirtualAsciiFile to a physical file on disk.
   *
   * \param file_name: `const string&` Name of the file to append contents to.
   * \return result: `int` A result code (0 if successful).
   */
  int append_to_disk(const std::string& file_name);

  /*! \brief Insert another VirtualAsciiFile at a given position.
   *
   * This function inserts a target VirtualAsciiFile in the current one at the given
   * position. Optionally, a range of lines to be inserted can be specified, otherwise
   * the full content of the target file is inserted. This function DOES NOT increase
   * the size of the inner storage and it can only be used if the inner storage has
   * already been adjusted to contain the insertion target.
   *
   * \param position: `int32_t` The position at which the other file is inserted in this one.
   * \param rhs: `const VirtualAsciiFile&` The refence to the VirtualAsciiFile to be inserted.
   * \param start: `int32_t` The first line to be inserted (optional, default is 0).
   * \param end: `int32_t` The last line to be inserted (optional, default is 0 to read all).
   * \param line: `const string&` Reference to a string representing the line.
   * \return result: `int` A result code (0 if successful).
   */
  int insert(int32_t position, VirtualAsciiFile& rhs, int32_t start = 0, int32_t end = 0);
  
  /*! \brief Get the number of lines in the current instance.
   *
   * \return size: `int32_t` The number of lines in the VirtualAsciiFile instance.
   */
  int32_t number_of_lines() { return _file_lines->size(); }
    
  /*! \brief Write virtual file contents to a real file on disk.
   *
   * \param file_name: `const string&` Name of the file to append contents to.
   * \return result: `int` A result code (0 if successful).
   */
  int write_to_disk();
  int write_to_disk(const std::string& file_name);
};
#endif
+49 −11
Original line number Diff line number Diff line
@@ -229,17 +229,15 @@ herr_t HDFFile::write(
/* >>> End of HDFFile class implementation <<< */

/* >>> VirtualAsciiFile class implementation <<< */
VirtualAsciiFile::VirtualAsciiFile(const std::string& name) {
  _file_name = name;
VirtualAsciiFile::VirtualAsciiFile(int32_t lines) {
  _file_lines = new vector<string>();
  for (int32_t li = 0; li < lines; li++) {
    _file_lines->push_back("");
  }

VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::string& name) {
  if (name.compare("") == 0) {
    _file_name = rhs._file_name;
  } else {
    _file_name = name;
}

VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs) {
  _num_lines = rhs._num_lines;
  _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);
@@ -247,14 +245,54 @@ VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::strin
}

VirtualAsciiFile::~VirtualAsciiFile() {
  while (!_file_lines->size() > 0) {
    _file_lines->pop_back();
  }
  if (_file_lines != NULL) delete _file_lines;
}

void VirtualAsciiFile::append(const string& line) {
  _file_lines.push_back(line);
void VirtualAsciiFile::append(const VirtualAsciiFile& rhs) {
  for (vector<string>::iterator it = rhs._file_lines->begin(); it != rhs._file_lines->end(); ++it) {
    _file_lines->push_back(*it);
  }
}

void VirtualAsciiFile::append_line(const string& line) {
  _file_lines->push_back(line);
}

int VirtualAsciiFile::append_to_disk(const std::string& file_name) {
  int result = 0;
  fstream output_file;
  output_file.open(file_name, ios::app);
  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;
}

int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t start, int32_t end) {
  int result = 0;
  if (start == 0 && end == 0) {
    end = rhs.number_of_lines();
  }
  int32_t final_index = position + end - start;
  if (final_index <= number_of_lines()) {
    for (int32_t li = start; li < end; li++) {
      _file_lines->at(position++) = rhs._file_lines->at(li);
    }
  } else {
    // ERROR: target file is too long;
    result = 1;
  }
  return result;
}

int VirtualAsciiFile::write_to_disk() {
int VirtualAsciiFile::write_to_disk(const std::string& file_name) {
  int result = 0;
  fstream output_file;
  output_file.open(file_name, ios::out);