This repository was archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the part of code that was missing for exploitation (and for co…
…mpilation)
- Loading branch information
Millaume Guichel
committed
May 5, 2019
1 parent
a8a558c
commit b89d92e
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|