Commit cfd96b52 authored by Ayush's avatar Ayush

Compilation functioning

parent b19ad184
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");
$postData = file_get_contents("php://input");
$postData = json_decode($postData, true);
......@@ -9,27 +15,26 @@ $language = $postData['language'];
$ret_stat = 0;
if(strcmp($language, "cpp") == 0)
if(strcmp($language, ".cpp") == 0)
{
$base = basename($filename, ".cpp");
system("g++ ../users/" . $uname . "/" . $filename . " -o ../users/" . $uname . "/" . $base , $ret_stat);
system("g++ ../users/" . $uname . "/" . $filename . ".cpp -o ../users/" . $uname . "/" . $filename , $ret_stat);
if($ret_stat != 0)
{
unlink("../users/" . $uname . "/" . $base);
unlink("../users/" . $uname . "/" . $filename);
throw new Exception("Compilation Error!");
}
}
else if(strcmp($language, "java") == 0)
else if(strcmp($language, ".java") == 0)
{
$base = basename($filename, ".java");
system("javac ../users/" . $uname . "/" . $filename , $ret_stat);
system("javac ../users/" . $uname . "/" . $filename . '.java' , $ret_stat);
if($ret_stat != 0)
{
unlink("../users/" . $uname . "/" . $base . ".class");
unlink("../users/" . $uname . "/" . $filename . ".class");
throw new Exception("Compilation Error!");
}
}
echo $ret_stat;
?>
\ No newline at end of file
?>
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");
$postData = file_get_contents("php://input");
$postData = json_decode($postData, true);
$uname = $postData['username'];
$filename = $postData['filename'];
$language = $postData['language'];
$uname = $postData['file']['username'];
$filename = $postData['file']['filename'];
$language = $postData['file']['language'];
$input_data = $postData['input_data'];
$ret_stat = 0;
......@@ -19,16 +25,14 @@ fclose($file);
$out_fname = "../users/" . $uname . "/" . "temp_out" . $template;
if(strcmp($language, "cpp") == 0)
if(strcmp($language, ".cpp") == 0)
{
$base = basename($filename, ".cpp");
system("./../users/" . $uname . "/" . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
system("./../users/" . $uname . "/" . $filename . " < " . $in_fname . " > " . $out_fname, $ret_stat);
}
else if(strcmp($language, "java") == 0)
{
$base = basename($filename, ".java");
system("java -cp ../users/" . $uname . "/ " . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
system("java -cp ../users/" . $uname . "/ " . $filename . " < " . $in_fname . " > " . $out_fname, $ret_stat);
}
else if(strcmp($language, "python") == 0)
......@@ -38,13 +42,13 @@ else if(strcmp($language, "python") == 0)
if($ret_stat != 0)
{
echo "Runtime Error!!";
throw new Exception("Runtime Error!");
}
else
{
$file = fopen($out_fname, 'r');
echo fread($file, filesize($out_fname));
echo json_encode(fread($file, filesize($out_fname)));
fclose($file);
}
......
......@@ -11,21 +11,21 @@ if (isset($postData) && !empty($postData)) {
$lang = trim($request->language);
$path = '../users/' . $username . '/' . trim($request->path) . '/' . $filename . $lang;
if (file_exists($path)) {
$msg = "file already exists";
clearstatcache();
echo json_encode($msg);
}
else {
$sql1 = "SELECT n_files from userdata where username = $username";
$sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username ";
$result = mysqli_query($mysqli, $sql1);
if($result == 10){
$msg = "maximum limit(10) reached";
echo json_encode($msg);
}
else{
if($result = mysqli_query($mysqli, $sql2)) {
// if (file_exists($path)) {
// $msg = "file already exists";
// clearstatcache();
// echo json_encode($msg);
// }
// else {
// $sql1 = "SELECT n_files from userdata where username = $username";
// $sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username ";
// $result = mysqli_query($mysqli, $sql1);
// if($result == 10){
// $msg = "maximum limit(10) reached";
// echo json_encode($msg);
// }
// else{
// if($result = mysqli_query($mysqli, $sql2)) {
$myfile = fopen($path, "w");
fwrite($myfile, $data);
fclose($myfile);
......@@ -33,12 +33,12 @@ if (isset($postData) && !empty($postData)) {
$msg = "file successfully uploaded";
clearstatcache();
echo json_encode($msg);
}
else{
http_response_code(404);
}
}
}
// }
// else{
// http_response_code(404);
// }
// }
// }
}
......
......@@ -4,8 +4,8 @@
<div id="toggle-lang">C++</div>
<div id="attempt">
<button id="input" (click)="inputField.setState(true)">Custom Input</button>
<button id="try" (click)="tryCode(0)" [class.disabled]="compilationError">{{compilationError ? 'Error' : 'Try Code'}}</button>
<button id="submit" (click)="tryCode(1)" [class.disabled]="compilationError">{{compilationError ? 'Encountered' : 'Submit Code'}}</button>
<button id="try" (click)="tryCode(0)" [class.disabled]="compilationError || isCompiling">{{compilationError ? 'Error' : (isCompiling ? 'Compiling...' : 'Try Code')}}</button>
<button id="submit" (click)="tryCode(1)" [class.disabled]="compilationError || isCompiling">{{compilationError ? 'Error' : (isCompiling ? 'Compiling...' : 'Try Code')}}</button>
</div>
<label for="editor"></label>
<textarea id="editor" name="editor"></textarea>
......
......@@ -20,6 +20,7 @@ export class ArenaComponent implements OnInit {
problem: Problem;
customInput = '';
compilationError = false;
isCompiling = false;
@ViewChild(InputComponent) inputField: InputComponent;
@ViewChild(SubmitTryCodeComponent) submitField: SubmitTryCodeComponent;
......@@ -76,13 +77,16 @@ export class ArenaComponent implements OnInit {
}
tryCode(c): void {
if (document.getElementById('try').classList.contains('disabled')) {return; }
this.isCompiling = true;
this.runCodeService.compileProblemFile(this.problem)
.subscribe(data => {
if (document.getElementById('try').classList.contains('disabled')) {return; }
this.submitField.submitting = c;
this.submitField.reset();
this.submitField.isActive = true;
this.isCompiling = false;
}, error => {
this.isCompiling = false;
this.compilationError = true;
setTimeout(() => {
this.compilationError = false;
......
......@@ -2,6 +2,6 @@
<div [class.open]="isActive" id="compile-popup">
<p [innerHTML]="statusVal" id="compile-status"></p>
<button (click)="resultVal !== 0 && setState(false)" [class.disabled]="resultVal === 0" id="compile-done">Done
<button (click)="setState(false)" id="compile-done">Done
</button>
</div>
......@@ -7,7 +7,6 @@ import {Component, Input, OnInit} from '@angular/core';
})
export class IdeCompileComponent implements OnInit {
@Input() statusVal: string;
@Input() resultVal: number;
isActive = false;
constructor() {
......
......@@ -4,7 +4,7 @@
<span *ngIf="!isUpToDate"></span>
<span id="ideFileName">{{file.filename + file.language}}</span>
<button (click)="inputField.setState(true);" id="inputBtn">Input</button>
<button (click)="this.runField.isActive = true; this.runCodeService.run()" id="runBtn">Run Code</button>
<button (click)="runFile()" id="runBtn" [class.disabled]="isCompiling || isError">{{isError ? 'Error' : (isCompiling ? 'Compiling...' : 'Run File')}}</button>
<button (click)="!isSaved ? this.saveField.setState(true) : updateFile();"
[class.disabled]="isUploading || isUpToDate"
id="saveBtn">{{isUploading ? "Saving File..." : "Save File"}}</button>
......@@ -13,5 +13,5 @@
<textarea id="editor" name="editor" value="{{inp}}"></textarea>
</div>
<app-input (valueEmit)="updateInput($event)"></app-input>
<app-ide-compile [resultVal]="runCodeService.result" [statusVal]="runCodeService.runStatus"></app-ide-compile>
<app-ide-compile [statusVal]="runCodeService.runStatus"></app-ide-compile>
<app-save-file (savedFile)="isSaved = isUpToDate = true;" [file]="file"></app-save-file>
......@@ -21,6 +21,8 @@ export class IdeComponent implements OnInit {
extensions = ['.cpp', '.py', '.java'];
isSaved = false;
isUploading = false;
isCompiling = false;
isError = false;
isUpToDate = false;
file: File = {
filename: 'Untitled',
......@@ -113,4 +115,33 @@ int main() {
btn.innerHTML = 'Save File';
});
}
runFile(): void {
const btn = document.getElementById('runBtn');
if (btn.classList.contains('disabled')) {return; }
this.isCompiling = true;
this.runCodeService.compileFile(this.file)
.subscribe(data => {
this.runCodeService.executeFile(this.file, this.inp)
.subscribe(d => {
this.runField.status = '<div class="code">' + d + '</div>';
this.runField.setState(true);
this.isCompiling = false;
}, err => {
console.log(err);
this.isCompiling = false;
this.isError = true;
this.runField.status = '<div class="error">Runtime Error!</div>';
this.runField.setState(true);
setTimeout(() => {this.isError = false; }, 3000);
});
}, error => {
console.log(error);
this.isCompiling = false;
this.isError = true;
this.runField.status = '<div class="error">Compilation Error!</div>';
this.runField.setState(true);
setTimeout(() => {this.isError = false; }, 3000);
});
}
}
import {Injectable} from '@angular/core';
import {Problem} from './problem';
import {File} from './file';
import {Observable, of, throwError} from 'rxjs';
import {HttpClient} from '@angular/common/http';
@Injectable({
providedIn: 'root'
......@@ -9,33 +11,23 @@ export class RunCodeService {
runStatus = 'Compiling...';
result = 0;
private baseUrl = 'http://localhost/sfcode/backend/';
constructor() {
constructor(private httpClient: HttpClient) {
}
run(): void {
const output = `25
abcd
150`;
const statusArray = ['Uploading...', 'Compiling...', 'Running...', `Done!
<div class="code">${output}</div>`];
this.result = 0;
let time = 0;
for (const status of statusArray) {
setTimeout(() => {
this.runStatus = status;
}, time);
time += 2000;
}
compileFile(file: File): Observable<any> {
return this.httpClient.post(this.baseUrl + 'compile.php', file);
}
setTimeout(() => {
this.result = 1;
}, 6000);
executeFile(file: File, input: string): Observable<any> {
return this.httpClient.post(this.baseUrl + 'execute.php', {
file,
input_data: input
});
}
compileProblemFile(prob: Problem): Observable<string> {
compileProblemFile(prob: Problem): Observable<any> {
if (Math.floor(Math.random() * 2) === 1) {return of('done'); }
else {return throwError('error'); }
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment