Skip to content

Commit e3912fa

Browse files
committed
2 parents ade8d5d + 81b1cde commit e3912fa

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

rce-server/server/api/services/code.service.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class CodeService {
3838
lang,
3939
file,
4040
inputFile,
41-
id
41+
id,
42+
code
4243
);
4344

4445
//executing the file
@@ -48,7 +49,8 @@ class CodeService {
4849
id,
4950
file,
5051
inputFile,
51-
lang
52+
lang,
53+
code
5254
);
5355

5456
if (OUTPUT) {
@@ -101,7 +103,7 @@ class CodeService {
101103
}
102104
}
103105

104-
async writeCommand(lang, file, input, id) {
106+
async writeCommand(lang, file, input, id, code) {
105107
let command = '';
106108
switch (lang) {
107109
case 'javascript': {
@@ -117,7 +119,10 @@ class CodeService {
117119
break;
118120
}
119121
case 'java': {
120-
command = `cd ${TARGET_DIR} && javac ${file} && java Input < ${input}`;
122+
let className = await this.extractJavaClassName(code);
123+
className = className.split(/\s/).join('');
124+
console.log('class ', className);
125+
command = `cd ${TARGET_DIR} && javac ${file} && java ${className} < ${input}`;
121126
break;
122127
}
123128
case 'c': {
@@ -138,7 +143,7 @@ class CodeService {
138143
return { runCode, runContainer };
139144
}
140145

141-
async execChild(runCode, runContainer, id, file, inputFile, lang) {
146+
async execChild(runCode, runContainer, id, file, inputFile, lang, code) {
142147
return new Promise((resolve, reject) => {
143148
const execCont = exec(`${runContainer}`);
144149
execCont.on('error', err => {
@@ -147,7 +152,7 @@ class CodeService {
147152
execCont.stdout.on('data', () => {
148153
exec(`${runCode}`, async (error, stdout, stderr) => {
149154
await this.endContainer(id);
150-
await this.deleteFiles(file, inputFile, lang, id);
155+
await this.deleteFiles(file, inputFile, lang, id, code);
151156
if (stderr) {
152157
reject({ message: stderr });
153158
} else {
@@ -158,7 +163,7 @@ class CodeService {
158163
});
159164
}
160165

161-
async deleteFiles(fileName, inputName, lang, id) {
166+
async deleteFiles(fileName, inputName, lang, id, code) {
162167
fs.unlinkSync(path.join(SOURCE_DIR, fileName), err => {
163168
if (err) throw { message: err };
164169
});
@@ -174,8 +179,11 @@ class CodeService {
174179
});
175180
}
176181
if (lang == 'java') {
177-
if (fs.existsSync(path.join(SOURCE_DIR, 'Input.class')))
178-
fs.unlinkSync(path.join(SOURCE_DIR, 'Input.class'), err => {
182+
let className = await this.extractJavaClassName(code);
183+
className = className.split(/\s/).join('');
184+
console.log('delete', className);
185+
if (fs.existsSync(path.join(SOURCE_DIR, `${className}.class`)))
186+
fs.unlinkSync(path.join(SOURCE_DIR, `${className}.class`), err => {
179187
if (err) throw err;
180188
});
181189
}
@@ -190,6 +198,26 @@ class CodeService {
190198
} else console.log('Container stoped and deleted');
191199
});
192200
}
201+
202+
async extractJavaClassName(s) {
203+
let prefix = 'class';
204+
let suffix = '{';
205+
let i = s.indexOf(prefix);
206+
if (i >= 0) {
207+
s = s.substring(i + prefix.length);
208+
} else {
209+
return '';
210+
}
211+
if (suffix) {
212+
i = s.indexOf(suffix);
213+
if (i >= 0) {
214+
s = s.substring(0, i);
215+
} else {
216+
return '';
217+
}
218+
}
219+
return s;
220+
}
193221
}
194222

195223
export default new CodeService();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function (s, prefix, suffix) {
2+
var i = s.indexOf(prefix);
3+
if (i >= 0) {
4+
s = s.substring(i + prefix.length);
5+
} else {
6+
return '';
7+
}
8+
if (suffix) {
9+
i = s.indexOf(suffix);
10+
if (i >= 0) {
11+
s = s.substring(0, i);
12+
} else {
13+
return '';
14+
}
15+
}
16+
return s;
17+
}

0 commit comments

Comments
 (0)