Created
January 8, 2016 00:25
-
-
Save harlanhaskins/11c4d599df1af9565b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Darwin | |
func printFile(fileName: String) { | |
var line: UnsafeMutablePointer<Int8> = nil | |
var len = 0 | |
var read = 0 | |
let file = fopen(fileName, "r") | |
guard file != nil else { return } | |
func goto(label: String) { | |
switch label { | |
case "start": | |
goto("cond") | |
case "cond": | |
read = getline(&line, &len, file) | |
if read == -1 { | |
goto("end") | |
} else { | |
goto("body") | |
} | |
case "body": | |
print("Retrieved line of length \(read):") | |
print(String.fromCString(line)!) | |
goto("cond") | |
case "end": | |
if file != nil { fclose(file) } | |
default: break | |
} | |
} | |
goto("start") | |
} | |
if Process.arguments.count > 1 { | |
printFile(Process.arguments[1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment