Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
adding the part of code that was missing for exploitation (and for co…
Browse files Browse the repository at this point in the history
…mpilation)
  • Loading branch information
Millaume Guichel committed May 5, 2019
1 parent a8a558c commit b89d92e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# not ignoring the content of subdirectories
!*/
.build/
files/
./files/

# not ignoring cpp files
!*.cpp
Expand Down
16 changes: 16 additions & 0 deletions src/files/File.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* File.cpp
* Implementation of the File.h file. Super class providing some tools method for FileReader and FileWriter classes.
*
* @author Alexandre Chambet
*/

#include "../../include/File.h"

using namespace std;

size_t File::fileSize() const {
// Open the file
ifstream file( filename, ios::binary | ios::ate);
return (size_t) file.tellg();
}
43 changes: 43 additions & 0 deletions src/files/FileReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* FileReader.cpp
* Implementation of the FileReader.h file. Reads a file and returns it under different forms.
*
* @author Alexandre Chambet
*/

#include "../../include/FileReader.h"

using namespace std;

FileReader::FileReader(string filename) {
// Append the basepath to the filename

// Check if file exists
std::ifstream ifile(filename.c_str());
if (!(bool) ifile) {
// File doesn't exist, we throw an exception
throw Exception(ERR_FILE_NOT_FOUND);
}

this->filename = std::move(filename);
}

void FileReader::readFileVector(std::vector<std::string> &file) const {
// Open the file
std::ifstream in(this->filename);

// Check if object is valid
if (!in) {
throw Exception(ERR_CANNOT_OPEN_FILE);
}

std::string str;

// Read the next line from File until it reaches the end.
while (std::getline(in, str)) {
file.push_back(str);
}

//Close The File
in.close();
}
59 changes: 59 additions & 0 deletions src/files/FileWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* FileWriter.cpp
* Implementation of the FileWriter.h file. Writes stuff to a file.
*
* @author Alexandre Chambet
*/

#include "../../include/FileWriter.h"

FileWriter::FileWriter(string filename) {
this->filename = std::move(filename);
}

void FileWriter::write(std::vector <std::string> data) {
// Basically write each line consecutively to the file (could find a better way to do it such as including \n and then write once for all)
vector<string>::iterator it;

for (it = data.begin(); it != data.end(); ++it) {
this->writeLine(*it);
}
}

void FileWriter::writeLine(string line, bool bypassNewLine) {
// Create a file that is writable
// Using the ios::app to make data appendable
ofstream file(this->filename, ios::app);

// Check the file was properly opened
if (!file) {
throw Exception(ERR_CANNOT_OPEN_FILE);
}

// Add the return otherwise it just overwrites the file
if (!bypassNewLine) {
line += "\n";
}

// Write it to the file
file << line;

file.close();
}

void FileWriter::clearFile() {
// Clear the file but does not remove it
ofstream file(this->filename);

// Check the file was properly opened
if (!file.is_open()) {
perror("open");
throw Exception(ERR_CANNOT_OPEN_FILE);
}

file << "";

file.close();
}


0 comments on commit b89d92e

Please sign in to comment.