Skip to content
Snippets Groups Projects
Commit 38bfc070 authored by Mulas, Giacomo's avatar Mulas, Giacomo
Browse files

add some comments to VirtualAsciiFile

parent 9ac1d876
No related branches found
No related tags found
No related merge requests found
...@@ -245,6 +245,8 @@ VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs) { ...@@ -245,6 +245,8 @@ VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs) {
} }
VirtualAsciiFile::~VirtualAsciiFile() { VirtualAsciiFile::~VirtualAsciiFile() {
// is it necessary to pop them out one by one? isn't there the dedicated method of std::vector to clean the vector?
// besides, shouldn't this be done anyway by the destructor of std:vector?
while (!_file_lines->size() > 0) { while (!_file_lines->size() > 0) {
_file_lines->pop_back(); _file_lines->pop_back();
} }
...@@ -252,16 +254,20 @@ VirtualAsciiFile::~VirtualAsciiFile() { ...@@ -252,16 +254,20 @@ VirtualAsciiFile::~VirtualAsciiFile() {
} }
void VirtualAsciiFile::append(const VirtualAsciiFile& rhs) { void VirtualAsciiFile::append(const VirtualAsciiFile& rhs) {
// concatenate the virtualasciifile pointed by rhs to the current one
// can't we use the dedicated method insert of std::vector to do the appending, instead of an explicit loop?
for (vector<string>::iterator it = rhs._file_lines->begin(); it != rhs._file_lines->end(); ++it) { for (vector<string>::iterator it = rhs._file_lines->begin(); it != rhs._file_lines->end(); ++it) {
_file_lines->push_back(*it); _file_lines->push_back(*it);
} }
} }
void VirtualAsciiFile::append_line(const string& line) { void VirtualAsciiFile::append_line(const string& line) {
// append a line of output to the virtualasciifile
_file_lines->push_back(line); _file_lines->push_back(line);
} }
int VirtualAsciiFile::append_to_disk(const std::string& file_name) { int VirtualAsciiFile::append_to_disk(const std::string& file_name) {
// dump to disk the contents of the virtualasciifile, appending at the end of the given file_name
int result = 0; int result = 0;
fstream output_file; fstream output_file;
output_file.open(file_name, ios::app); output_file.open(file_name, ios::app);
...@@ -283,6 +289,7 @@ int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t st ...@@ -283,6 +289,7 @@ int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t st
int32_t final_index = position + end - start; int32_t final_index = position + end - start;
if (final_index <= number_of_lines()) { if (final_index <= number_of_lines()) {
for (int32_t li = start; li < end; li++) { for (int32_t li = start; li < end; li++) {
// since here we are replacing the previous placeholder empty strings, make sure they are properly released when they are replaced (i.e. try it with a simple hello world example and pass it through valgrind)
_file_lines->at(position++) = rhs._file_lines->at(li); _file_lines->at(position++) = rhs._file_lines->at(li);
} }
} else { } else {
...@@ -293,6 +300,7 @@ int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t st ...@@ -293,6 +300,7 @@ int VirtualAsciiFile::insert(int32_t position, VirtualAsciiFile& rhs, int32_t st
} }
int VirtualAsciiFile::write_to_disk(const std::string& file_name) { int VirtualAsciiFile::write_to_disk(const std::string& file_name) {
// dump to disk the contents of the virtualasciifile, replacing the given file_name
int result = 0; int result = 0;
fstream output_file; fstream output_file;
output_file.open(file_name, ios::out); output_file.open(file_name, ios::out);
......
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