Commit cfd96b52 authored by Ayush's avatar Ayush

Compilation functioning

parent b19ad184
<?php <?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 = file_get_contents("php://input");
$postData = json_decode($postData, true); $postData = json_decode($postData, true);
...@@ -9,25 +15,24 @@ $language = $postData['language']; ...@@ -9,25 +15,24 @@ $language = $postData['language'];
$ret_stat = 0; $ret_stat = 0;
if(strcmp($language, "cpp") == 0) if(strcmp($language, ".cpp") == 0)
{ {
$base = basename($filename, ".cpp"); system("g++ ../users/" . $uname . "/" . $filename . ".cpp -o ../users/" . $uname . "/" . $filename , $ret_stat);
system("g++ ../users/" . $uname . "/" . $filename . " -o ../users/" . $uname . "/" . $base , $ret_stat);
if($ret_stat != 0) 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 . '.java' , $ret_stat);
system("javac ../users/" . $uname . "/" . $filename , $ret_stat);
if($ret_stat != 0) if($ret_stat != 0)
{ {
unlink("../users/" . $uname . "/" . $base . ".class"); unlink("../users/" . $uname . "/" . $filename . ".class");
throw new Exception("Compilation Error!");
} }
} }
......
<?php <?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 = file_get_contents("php://input");
$postData = json_decode($postData, true); $postData = json_decode($postData, true);
$uname = $postData['username']; $uname = $postData['file']['username'];
$filename = $postData['filename']; $filename = $postData['file']['filename'];
$language = $postData['language']; $language = $postData['file']['language'];
$input_data = $postData['input_data']; $input_data = $postData['input_data'];
$ret_stat = 0; $ret_stat = 0;
...@@ -19,16 +25,14 @@ fclose($file); ...@@ -19,16 +25,14 @@ fclose($file);
$out_fname = "../users/" . $uname . "/" . "temp_out" . $template; $out_fname = "../users/" . $uname . "/" . "temp_out" . $template;
if(strcmp($language, "cpp") == 0) if(strcmp($language, ".cpp") == 0)
{ {
$base = basename($filename, ".cpp"); system("./../users/" . $uname . "/" . $filename . " < " . $in_fname . " > " . $out_fname, $ret_stat);
system("./../users/" . $uname . "/" . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
} }
else if(strcmp($language, "java") == 0) else if(strcmp($language, "java") == 0)
{ {
$base = basename($filename, ".java"); system("java -cp ../users/" . $uname . "/ " . $filename . " < " . $in_fname . " > " . $out_fname, $ret_stat);
system("java -cp ../users/" . $uname . "/ " . $base . " < " . $in_fname . " > " . $out_fname, $ret_stat);
} }
else if(strcmp($language, "python") == 0) else if(strcmp($language, "python") == 0)
...@@ -38,13 +42,13 @@ else if(strcmp($language, "python") == 0) ...@@ -38,13 +42,13 @@ else if(strcmp($language, "python") == 0)
if($ret_stat != 0) if($ret_stat != 0)
{ {
echo "Runtime Error!!"; throw new Exception("Runtime Error!");
} }
else else
{ {
$file = fopen($out_fname, 'r'); $file = fopen($out_fname, 'r');
echo fread($file, filesize($out_fname)); echo json_encode(fread($file, filesize($out_fname)));
fclose($file); fclose($file);
} }
......
...@@ -11,21 +11,21 @@ if (isset($postData) && !empty($postData)) { ...@@ -11,21 +11,21 @@ if (isset($postData) && !empty($postData)) {
$lang = trim($request->language); $lang = trim($request->language);
$path = '../users/' . $username . '/' . trim($request->path) . '/' . $filename . $lang; $path = '../users/' . $username . '/' . trim($request->path) . '/' . $filename . $lang;
if (file_exists($path)) { // if (file_exists($path)) {
$msg = "file already exists"; // $msg = "file already exists";
clearstatcache(); // clearstatcache();
echo json_encode($msg); // echo json_encode($msg);
} // }
else { // else {
$sql1 = "SELECT n_files from userdata where username = $username"; // $sql1 = "SELECT n_files from userdata where username = $username";
$sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username "; // $sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username ";
$result = mysqli_query($mysqli, $sql1); // $result = mysqli_query($mysqli, $sql1);
if($result == 10){ // if($result == 10){
$msg = "maximum limit(10) reached"; // $msg = "maximum limit(10) reached";
echo json_encode($msg); // echo json_encode($msg);
} // }
else{ // else{
if($result = mysqli_query($mysqli, $sql2)) { // if($result = mysqli_query($mysqli, $sql2)) {
$myfile = fopen($path, "w"); $myfile = fopen($path, "w");
fwrite($myfile, $data); fwrite($myfile, $data);
fclose($myfile); fclose($myfile);
...@@ -33,12 +33,12 @@ if (isset($postData) && !empty($postData)) { ...@@ -33,12 +33,12 @@ if (isset($postData) && !empty($postData)) {
$msg = "file successfully uploaded"; $msg = "file successfully uploaded";
clearstatcache(); clearstatcache();
echo json_encode($msg); echo json_encode($msg);
} // }
else{ // else{
http_response_code(404); // http_response_code(404);
} // }
} // }
} // }
} }
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
<div id="toggle-lang">C++</div> <div id="toggle-lang">C++</div>
<div id="attempt"> <div id="attempt">
<button id="input" (click)="inputField.setState(true)">Custom Input</button> <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="try" (click)="tryCode(0)" [class.disabled]="compilationError || isCompiling">{{compilationError ? 'Error' : (isCompiling ? 'Compiling...' : 'Try Code')}}</button>
<button id="submit" (click)="tryCode(1)" [class.disabled]="compilationError">{{compilationError ? 'Encountered' : 'Submit Code'}}</button> <button id="submit" (click)="tryCode(1)" [class.disabled]="compilationError || isCompiling">{{compilationError ? 'Error' : (isCompiling ? 'Compiling...' : 'Try Code')}}</button>
</div> </div>
<label for="editor"></label> <label for="editor"></label>
<textarea id="editor" name="editor"></textarea> <textarea id="editor" name="editor"></textarea>
......
...@@ -20,6 +20,7 @@ export class ArenaComponent implements OnInit { ...@@ -20,6 +20,7 @@ export class ArenaComponent implements OnInit {
problem: Problem; problem: Problem;
customInput = ''; customInput = '';
compilationError = false; compilationError = false;
isCompiling = false;
@ViewChild(InputComponent) inputField: InputComponent; @ViewChild(InputComponent) inputField: InputComponent;
@ViewChild(SubmitTryCodeComponent) submitField: SubmitTryCodeComponent; @ViewChild(SubmitTryCodeComponent) submitField: SubmitTryCodeComponent;
...@@ -76,13 +77,16 @@ export class ArenaComponent implements OnInit { ...@@ -76,13 +77,16 @@ export class ArenaComponent implements OnInit {
} }
tryCode(c): void { tryCode(c): void {
if (document.getElementById('try').classList.contains('disabled')) {return; }
this.isCompiling = true;
this.runCodeService.compileProblemFile(this.problem) this.runCodeService.compileProblemFile(this.problem)
.subscribe(data => { .subscribe(data => {
if (document.getElementById('try').classList.contains('disabled')) {return; }
this.submitField.submitting = c; this.submitField.submitting = c;
this.submitField.reset(); this.submitField.reset();
this.submitField.isActive = true; this.submitField.isActive = true;
this.isCompiling = false;
}, error => { }, error => {
this.isCompiling = false;
this.compilationError = true; this.compilationError = true;
setTimeout(() => { setTimeout(() => {
this.compilationError = false; this.compilationError = false;
......
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
<div [class.open]="isActive" id="compile-popup"> <div [class.open]="isActive" id="compile-popup">
<p [innerHTML]="statusVal" id="compile-status"></p> <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> </button>
</div> </div>
...@@ -7,7 +7,6 @@ import {Component, Input, OnInit} from '@angular/core'; ...@@ -7,7 +7,6 @@ import {Component, Input, OnInit} from '@angular/core';
}) })
export class IdeCompileComponent implements OnInit { export class IdeCompileComponent implements OnInit {
@Input() statusVal: string; @Input() statusVal: string;
@Input() resultVal: number;
isActive = false; isActive = false;
constructor() { constructor() {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<span *ngIf="!isUpToDate"></span> <span *ngIf="!isUpToDate"></span>
<span id="ideFileName">{{file.filename + file.language}}</span> <span id="ideFileName">{{file.filename + file.language}}</span>
<button (click)="inputField.setState(true);" id="inputBtn">Input</button> <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();" <button (click)="!isSaved ? this.saveField.setState(true) : updateFile();"
[class.disabled]="isUploading || isUpToDate" [class.disabled]="isUploading || isUpToDate"
id="saveBtn">{{isUploading ? "Saving File..." : "Save File"}}</button> id="saveBtn">{{isUploading ? "Saving File..." : "Save File"}}</button>
...@@ -13,5 +13,5 @@ ...@@ -13,5 +13,5 @@
<textarea id="editor" name="editor" value="{{inp}}"></textarea> <textarea id="editor" name="editor" value="{{inp}}"></textarea>
</div> </div>
<app-input (valueEmit)="updateInput($event)"></app-input> <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> <app-save-file (savedFile)="isSaved = isUpToDate = true;" [file]="file"></app-save-file>
...@@ -21,6 +21,8 @@ export class IdeComponent implements OnInit { ...@@ -21,6 +21,8 @@ export class IdeComponent implements OnInit {
extensions = ['.cpp', '.py', '.java']; extensions = ['.cpp', '.py', '.java'];
isSaved = false; isSaved = false;
isUploading = false; isUploading = false;
isCompiling = false;
isError = false;
isUpToDate = false; isUpToDate = false;
file: File = { file: File = {
filename: 'Untitled', filename: 'Untitled',
...@@ -113,4 +115,33 @@ int main() { ...@@ -113,4 +115,33 @@ int main() {
btn.innerHTML = 'Save File'; 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 {Injectable} from '@angular/core';
import {Problem} from './problem'; import {Problem} from './problem';
import {File} from './file';
import {Observable, of, throwError} from 'rxjs'; import {Observable, of, throwError} from 'rxjs';
import {HttpClient} from '@angular/common/http';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
...@@ -9,33 +11,23 @@ export class RunCodeService { ...@@ -9,33 +11,23 @@ export class RunCodeService {
runStatus = 'Compiling...'; runStatus = 'Compiling...';
result = 0; result = 0;
private baseUrl = 'http://localhost/sfcode/backend/';
constructor() { constructor(private httpClient: HttpClient) {
} }
run(): void { compileFile(file: File): Observable<any> {
const output = `25 return this.httpClient.post(this.baseUrl + 'compile.php', file);
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;
} }
setTimeout(() => { executeFile(file: File, input: string): Observable<any> {
this.result = 1; return this.httpClient.post(this.baseUrl + 'execute.php', {
}, 6000); file,
input_data: input
});
} }
compileProblemFile(prob: Problem): Observable<string> { compileProblemFile(prob: Problem): Observable<any> {
if (Math.floor(Math.random() * 2) === 1) {return of('done'); } if (Math.floor(Math.random() * 2) === 1) {return of('done'); }
else {return throwError('error'); } 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