-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineCompiler.swift
72 lines (63 loc) · 2.59 KB
/
OnlineCompiler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// OnlineCompiler.swift
// Brace-Editor
//
// Created by Dan jarvis on 2/2/15.
// Copyright (c) 2015 Dan jarvis. All rights reserved.
//
import Foundation
import Kanna
class OnlineCompiler {
var program: String!
var language: String!
let specialEncoding = ["simiCol": "%3B", "plus": "%2B", "backSlash": "%5C"]
let notificationKey = "com.Danwakeem.compile-output"
var outPut: String!
var compileStatus: String!
let url = NSURL(string: "http://codepad.org/")
var manager: AnyObject!
init (){
self.program = ""
self.language = "C"
}
func encodeSourceCode(sourceCode: String) -> String {
var source = sourceCode
for (key, val) in specialEncoding {
switch(key) {
case "simiCol":
source = source.stringByReplacingOccurrencesOfString(";", withString: val)
case "plus" :
source = source.stringByReplacingOccurrencesOfString("+", withString: val)
case "backSlash":
source = source.stringByReplacingOccurrencesOfString("\\", withString: val)
default:
println("Did not find encoding")
}
}
return source
}
func compileProgram(targetLanguage: String, sourceCode: String) {
language = targetLanguage
let source = encodeSourceCode(sourceCode)
//So C++ gets encoded as well
language = encodeSourceCode(language)
var session = NSURLSession.sharedSession()
var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
var body = "lang=\(language)&code=\(source)&run=True&submit=Submit"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let doc: HTMLDocument = Kanna.HTML(html: data, encoding: NSUTF8StringEncoding){
let result = doc.xpath("/html/body/div/table/tr/td/div[2]/table/tr/td[2]/div")
if result.text != nil {
self.outPut = result.text
NSNotificationCenter.defaultCenter().postNotificationName(self.notificationKey, object: result.text as? AnyObject)
} else {
self.outPut = "Network connection problem. Try again."
NSNotificationCenter.defaultCenter().postNotificationName(self.notificationKey, object: "Sorry")
}
}
})
task.resume()
}
}