-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler.cpp
More file actions
73 lines (60 loc) · 2.25 KB
/
filehandler.cpp
File metadata and controls
73 lines (60 loc) · 2.25 KB
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
#include "filehandler.h"
#include <QDebug>
#include <stdexcept> // For using std::runtime_error
FileHandler::FileHandler(QObject *parent) : QObject(parent) {}
QString FileHandler::questionFilePath() const {
return m_questionFilePath;
}
void FileHandler::setQuestionFilePath(const QString &filePath) {
if (m_questionFilePath != filePath) {
m_questionFilePath = filePath;
emit questionFilePathChanged();
}
}
QString FileHandler::exampleOutputFilePath() const {
return m_exampleOutputFilePath;
}
void FileHandler::setExampleOutputFilePath(const QString &filePath) {
if (m_exampleOutputFilePath != filePath) {
m_exampleOutputFilePath = filePath;
emit exampleOutputFilePathChanged();
}
}
QString FileHandler::readQuestion() {
try {
if (m_questionFilePath.isEmpty()) {
throw std::runtime_error("Question file path is empty!");
}
QFile file(m_questionFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw std::runtime_error("Error opening question file: " + file.errorString().toStdString());
}
QTextStream in(&file);
QString content = in.readAll();
file.close();
qDebug() << "Question Content:\n" << content;
return content.trimmed(); // Trim and return the content
} catch (const std::runtime_error &e) {
qWarning() << "Exception occurred in readQuestion:" << e.what();
return QString();
}
}
QString FileHandler::readExampleOutput() {
try {
if (m_exampleOutputFilePath.isEmpty()) {
throw std::runtime_error("Example output file path is empty!");
}
QFile file(m_exampleOutputFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw std::runtime_error("Error opening example output file: " + file.errorString().toStdString());
}
QTextStream in(&file);
QString content = in.readAll();
file.close();
qDebug() << "Example Output Content:\n" << content;
return content.trimmed(); // Trim and return the content
} catch (const std::runtime_error &e) {
qWarning() << "Exception occurred in readExampleOutput:" << e.what();
return QString();
}
}