Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Created January 8, 2016 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlanhaskins/11c4d599df1af9565b37 to your computer and use it in GitHub Desktop.
Save harlanhaskins/11c4d599df1af9565b37 to your computer and use it in GitHub Desktop.
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