Skip to content

Commit 0406a6c

Browse files
committed
Add Cpp executable code
0 parents  commit 0406a6c

8 files changed

Lines changed: 3377 additions & 0 deletions

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PORT=8080

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

api/cppApi.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const express = require('express');
2+
const { CPP, CppCompile } = require('../compiler/cpp');
3+
const router = express.Router();
4+
5+
router.post('/', async (req, res) => {
6+
res.json(req.body);
7+
})
8+
9+
router.post('/cpp', async (req, res) => {
10+
const InputCode = Buffer.from(req.body.code, 'base64').toString('binary')
11+
const DeCode = Buffer.from(req.body.input, 'base64').toString('binary')
12+
let response = await CppCompile(InputCode, DeCode);
13+
if (response.statusMes === "Compiler Error") {
14+
res.status(202).json(response)
15+
} else if (response.statusMes === "Run Time Error") {
16+
res.status(201).json(response)
17+
} else {
18+
res.status(200).json(response)
19+
}
20+
21+
});
22+
23+
24+
25+
26+
module.exports = router;

compiler/cpp.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
const { v4: uuid } = require('uuid');
2+
const { exec } = require('child_process');
3+
const fs = require('fs');
4+
const os = require('os');
5+
const path = require('path');
6+
const saveFile = (file, data) => {
7+
return new Promise((resolve, reject) => {
8+
fs.writeFile(file, data, function (err) {
9+
if (err) {
10+
reject(err);
11+
} else {
12+
resolve([file]);
13+
}
14+
});
15+
});
16+
};
17+
18+
async function deleteFiles(cppPath, inputPath, exePath) {
19+
if (fs.existsSync(cppPath)) {
20+
await fs.unlinkSync(cppPath);
21+
}
22+
23+
if (fs.existsSync(inputPath)) {
24+
await fs.unlinkSync(inputPath);
25+
}
26+
27+
if (fs.existsSync(exePath)) {
28+
await fs.unlinkSync(exePath);
29+
}
30+
}
31+
32+
function getRunCommand(executable, input) {
33+
return `${executable} < ${input}`;
34+
}
35+
36+
function getExecutablePath(fileName) {
37+
// console.log(os.platform());
38+
if (os.platform() === 'win32') {
39+
return `${path.join(__dirname, '..', 'upload', fileName)}.exe`;
40+
}
41+
if (os.platform() === 'linux') {
42+
return `${path.join(__dirname, '..', 'upload', fileName)}`;
43+
}
44+
}
45+
46+
function getCPPPath(fileName) {
47+
return `${path.join(__dirname, '..', 'upload', fileName)}.cpp`;
48+
}
49+
50+
function getInputPath(fileName) {
51+
return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`;
52+
}
53+
54+
function compileProgram(cppPath, exePath) {
55+
return new Promise((resolve, reject) => {
56+
exec(`g++ -o ${exePath} ${cppPath}`, (error, stdout, stderr) => {
57+
if (error) {
58+
reject({ error, stdout, stderr });
59+
} else {
60+
resolve({ stdout, stderr });
61+
}
62+
});
63+
});
64+
}
65+
66+
function runProgram(exePath, inputPath) {
67+
return new Promise((resolve, reject) => {
68+
exec(getRunCommand(exePath, inputPath), (error, stdout, stderr) => {
69+
if (error) {
70+
reject({ error, stdout, stderr });
71+
} else {
72+
resolve({ stdout, stderr });
73+
}
74+
});
75+
});
76+
}
77+
78+
79+
const CppCompile = async (code, input) => {
80+
let state = {
81+
stdout: null,
82+
stderr: null,
83+
statusMes : "",
84+
}
85+
let uniqueFileName = uuid();
86+
let executePath = getExecutablePath(uniqueFileName)
87+
let cppPath = getCPPPath(uniqueFileName)
88+
let ipPath = getInputPath(uniqueFileName)
89+
90+
await saveFile(cppPath, code);
91+
await saveFile(ipPath, input);
92+
93+
try {
94+
await compileProgram(cppPath, executePath);
95+
} catch (err) {
96+
state.stderr = err.stderr;
97+
state.statusMes = "Compiler Error";
98+
deleteFiles(cppPath, ipPath, executePath);
99+
return state;
100+
}
101+
102+
try {
103+
let { stdout, stderr } = await runProgram(executePath, ipPath);
104+
state.stdout = stdout;
105+
state.stderr = stderr;
106+
} catch (err) {
107+
state.stderr = err.stderr;
108+
state.statusMes = "Run Time Error";
109+
deleteFiles(cppPath, ipPath, executePath);
110+
}
111+
112+
if (state.stderr === '') {
113+
state.stderr = null;
114+
}
115+
state.statusMes = "Successfully Compiled";
116+
await deleteFiles(cppPath, ipPath, executePath);
117+
return state;
118+
119+
}
120+
121+
122+
123+
124+
module.exports = { CppCompile };

0 commit comments

Comments
 (0)