Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…utor into main
  • Loading branch information
Tushar3099 committed Jan 6, 2021
2 parents ade8d5d + 81b1cde commit e3912fa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
46 changes: 37 additions & 9 deletions rce-server/server/api/services/code.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class CodeService {
lang,
file,
inputFile,
id
id,
code
);

//executing the file
Expand All @@ -48,7 +49,8 @@ class CodeService {
id,
file,
inputFile,
lang
lang,
code
);

if (OUTPUT) {
Expand Down Expand Up @@ -101,7 +103,7 @@ class CodeService {
}
}

async writeCommand(lang, file, input, id) {
async writeCommand(lang, file, input, id, code) {
let command = '';
switch (lang) {
case 'javascript': {
Expand All @@ -117,7 +119,10 @@ class CodeService {
break;
}
case 'java': {
command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`;
let className = await this.extractJavaClassName(code);
className = className.split(/\s/).join('');
console.log('class ', className);
command = `cd ${TARGET_DIR} && javac ${file} && java ${className} < ${input}`;
break;
}
case 'c': {
Expand All @@ -138,7 +143,7 @@ class CodeService {
return { runCode, runContainer };
}

async execChild(runCode, runContainer, id, file, inputFile, lang) {
async execChild(runCode, runContainer, id, file, inputFile, lang, code) {
return new Promise((resolve, reject) => {
const execCont = exec(`${runContainer}`);
execCont.on('error', err => {
Expand All @@ -147,7 +152,7 @@ class CodeService {
execCont.stdout.on('data', () => {
exec(`${runCode}`, async (error, stdout, stderr) => {
await this.endContainer(id);
await this.deleteFiles(file, inputFile, lang, id);
await this.deleteFiles(file, inputFile, lang, id, code);
if (stderr) {
reject({ message: stderr });
} else {
Expand All @@ -158,7 +163,7 @@ class CodeService {
});
}

async deleteFiles(fileName, inputName, lang, id) {
async deleteFiles(fileName, inputName, lang, id, code) {
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
if (err) throw { message: err };
});
Expand All @@ -174,8 +179,11 @@ class CodeService {
});
}
if (lang == 'java') {
if (fs.existsSync(path.join(SOURCE_DIR, 'Input.class')))
fs.unlinkSync(path.join(SOURCE_DIR, 'Input.class'), err => {
let className = await this.extractJavaClassName(code);
className = className.split(/\s/).join('');
console.log('delete', className);
if (fs.existsSync(path.join(SOURCE_DIR, `${className}.class`)))
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), err => {
if (err) throw err;
});
}
Expand All @@ -190,6 +198,26 @@ class CodeService {
} else console.log('Container stoped and deleted');
});
}

async extractJavaClassName(s) {
let prefix = 'class';
let suffix = '{';
let i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
} else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
} else {
return '';
}
}
return s;
}
}

export default new CodeService();
17 changes: 17 additions & 0 deletions rce-server/server/api/services/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function (s, prefix, suffix) {
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
} else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
} else {
return '';
}
}
return s;
}

0 comments on commit e3912fa

Please sign in to comment.