-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilerprocess.cpp
More file actions
81 lines (69 loc) · 2.12 KB
/
Copy pathcompilerprocess.cpp
File metadata and controls
81 lines (69 loc) · 2.12 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
74
75
76
77
78
79
80
81
#include "compilerprocess.h"
CompilerProcess::CompilerProcess(QObject *parent)
: QObject{parent}
{
compiler = new QProcess(this);
connect(compiler, &QProcess::readyReadStandardOutput,
this, &CompilerProcess::handleCompileOutput);
connect(compiler, &QProcess::readyReadStandardError,
this, &CompilerProcess::handleCompileError);
connect(compiler, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &CompilerProcess::compilationFinished);
}
CompilerProcess::~CompilerProcess()
{
if (compiler->state() == QProcess::Running)
{
compiler->kill();
}
}
void CompilerProcess::compile(const QString &sourceCode)
{
if (sourceCode.trimmed().isEmpty())
{
emit error("Error: No Input");
return;
}
QTemporaryFile sourceFile;
sourceFile.setFileTemplate(QDir::tempPath() + "/XXXXXX.cpp");
sourceFile.setAutoRemove(false);
if (!sourceFile.open())
{
emit error("錯誤:無法創建臨時文件!");
return;
}
QTextStream stream(&sourceFile);
stream << sourceCode;
sourceFile.close();
tempFilePath = sourceFile.fileName();
QString outputPath = QFileInfo(tempFilePath).path() + "/output.exe";
QStringList arguments;
arguments << tempFilePath
<< "-o" << outputPath
<< "-Wall"
<< "-Wextra"
<< "-std=c++17";
emit compileStarted();
qDebug() << "Start compile";
compiler->start("g++", arguments);
}
void CompilerProcess::handleCompileOutput()
{
QString output = QString::fromLocal8Bit(compiler->readAllStandardOutput());
emit outputReceived(output);
}
void CompilerProcess::handleCompileError()
{
QString errorMsg = QString::fromLocal8Bit(compiler->readAllStandardError());
emit outputReceived("<span style='color: red'>" + errorMsg + "</span>");
}
void CompilerProcess::compilationFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
//if (exitStatus == QProcess::NormalExit && exitCode == 0)
//{
//}
//else
//{
//}
emit compileFinished(exitCode, exitStatus, tempFilePath);
}