/
Compiler.cpp
98 lines (87 loc) · 2.7 KB
/
Compiler.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2019-2020 Ashar Khan <ashar786khan@gmail.com>
*
* This file is part of CPEditor.
*
* CPEditor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* I will not be responsible if CPEditor behaves in unexpected way and
* causes your ratings to go down and or loose any important contest.
*
* Believe Software is "Software" and it isn't immune to bugs.
*
*/
#include <Compiler.hpp>
#include <QFileInfo>
namespace Core
{
Compiler::Compiler()
{
compileProcess = new QProcess();
connect(compileProcess, SIGNAL(started()), this, SIGNAL(compilationStarted()));
connect(compileProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this,
SLOT(onProcessFinished(int, QProcess::ExitStatus)));
}
Compiler::~Compiler()
{
if (compileProcess != nullptr)
{
delete compileProcess;
}
}
void Compiler::start(const QString &filePath, const QString &compileCommand, const QString &lang)
{
if (!QFile::exists(filePath))
{
emit compilationErrorOccured("The source file [" + filePath + "] doesn't exist");
return;
}
QString command;
if (lang == "C++")
{
command = compileCommand + " \"" + QFileInfo(filePath).canonicalFilePath() + "\" -o \"" +
QFileInfo(filePath).canonicalPath() + "/" + QFileInfo(filePath).completeBaseName() + "\"";
}
else if (lang == "Java")
{
command = compileCommand + " \"" + QFileInfo(filePath).canonicalFilePath() + "\"";
}
else if (lang == "Python")
{
emit compilationFinished("");
return;
}
else
{
emit compilationErrorOccured("Unsupported programming language \"" + lang + "\"");
return;
}
compileProcess->start(command);
}
bool Compiler::check(const QString &compileCommand)
{
QProcess checkProcess;
checkProcess.start(compileCommand.trimmed().split(' ').front() + " --version");
bool finished = checkProcess.waitForFinished(1000);
if (finished && checkProcess.exitCode() == 0)
return true;
checkProcess.kill();
checkProcess.start(compileCommand.trimmed().split(' ').front() + " -version");
finished = checkProcess.waitForFinished(1000);
return finished && checkProcess.exitCode() == 0;
}
void Compiler::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitCode == 0)
{
emit compilationFinished(compileProcess->readAllStandardError());
}
else
{
emit compilationErrorOccured(compileProcess->readAllStandardError());
}
}
} // namespace Core