-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandler.cpp
83 lines (71 loc) · 2.22 KB
/
FileHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <stdexcept>
#include "FileHandler.h"
using std::string;
using std::vector;
/**
* Constructor for FileHandler
* @param filePath The path to the file to be read
* @param loadLines Whether or not to load and store the lines of the file
*/
FileHandler::FileHandler(const string& filePath, bool loadLines=true) : filePath(filePath)
{
setFilePath(filePath);
if (loadLines) this->loadLines();
}
/**
* Sets the path to the file to be read
* @param filePath The path to the file to be read
*/
void FileHandler::setFilePath(const string& filePath) {
if (this->endsWithExtension(filePath, ".txt") || this->endsWithExtension(filePath, ".csv"))
this->filePath = filePath;
else
throw std::invalid_argument("File must be a .txt or .csv file.");
}
/**
* Writes the given data to the file
* @param data The data to be written to the file
* @param append Whether or not to append the data to the file
* @throws runtime_error if the file cannot be opened
*/
void FileHandler::write(const vector<string>& data, bool append = false)
{
std::ofstream outputFile(filePath, append ? std::ios_base::app : std::ios_base::out);
if (!outputFile.is_open())
throw std::runtime_error("Failed to open the output file for writing.");
for (const string& line : data)
outputFile << line << '\n';
outputFile.close();
this->loadLines();
}
/**
* Reads the file and stores each line in the lines vector
* @throws runtime_error if the file cannot be opened
*/
void FileHandler::loadLines()
{
std::ifstream file(this->filePath);
if (!file.is_open())
throw std::runtime_error("Failed to open file");
string line;
int i = 0;
while (getline(file, line))
if (i++ > 0)
lines.push_back(line);
file.close();
}
/**
* Returns the number of lines in the file
* @return The number of lines in the file
*/
bool FileHandler::endsWithExtension(const std::string& str, const std::string& extension) {
if (str.length() >= extension.length()) {
return (str.compare(str.length() - extension.length(), extension.length(), extension) == 0);
}
return false;
}