Add local function "toURLOrError".

This will print an error message and halt immediately if the input is not a valid URL. Though I have not yet been able to create an invalid URL. NSURL even accepts * and : in the path itself.
This commit is contained in:
Kare Morstol
2014-12-02 01:15:47 +01:00
parent e74189d008
commit 6cc240a138
2 changed files with 33 additions and 34 deletions
+23 -33
View File
@@ -67,25 +67,20 @@ extension FileHandle: WriteableStreamType {
/** Open a file for reading, and exit if an error occurs. */
public func open (path: String) -> ReadableStreamType {
if let url = NSURL(fileURLWithPath: path) {
let url = toURLOrError(path)
var error: NSError?
let filehandle = FileHandle(forReadingFromURL: url, error: &error)
var error: NSError?
let filehandle = FileHandle(forReadingFromURL: url, error: &error)
if let error = error {
var fileaccesserror: NSError?
url.checkResourceIsReachableAndReturnError(&fileaccesserror)
printErrorAndExit( fileaccesserror?.localizedDescription ?? error.localizedDescription )
}
return filehandle!
} else {
printErrorAndExit("Invalid file path: \(path)")
if let error = error {
var fileaccesserror: NSError?
url.checkResourceIsReachableAndReturnError(&fileaccesserror)
printErrorAndExit( fileaccesserror?.localizedDescription ?? error.localizedDescription )
}
return filehandle!
}
/**
/**
Open a file for writing, create it if it doesn't exist, and exit if an error occurs.
If the file already exists and overwrite=false, the writing will begin at the end of the file.
@@ -93,27 +88,22 @@ If the file already exists and overwrite=false, the writing will begin at the en
*/
public func open (forWriting path: String, overwrite: Bool = false) -> WriteableStreamType {
if let url = NSURL(fileURLWithPath: path) {
let url = toURLOrError(path)
let filemanager = NSFileManager.defaultManager()
if overwrite || !filemanager.fileExistsAtPath(url.path!) {
filemanager.createFileAtPath(url.path!, contents: nil, attributes: nil)
}
let filemanager = NSFileManager.defaultManager()
if overwrite || !filemanager.fileExistsAtPath(url.path!) {
filemanager.createFileAtPath(url.path!, contents: nil, attributes: nil)
}
var error: NSError?
let filehandle = FileHandle(forWritingToURL: url, error: &error)
if let error = error {
var fileaccesserror: NSError?
url.checkResourceIsReachableAndReturnError(&fileaccesserror)
printErrorAndExit( fileaccesserror?.localizedDescription ?? error.localizedDescription )
} else {
filehandle!.seekToEndOfFile()
return filehandle!
}
var error: NSError?
let filehandle = FileHandle(forWritingToURL: url, error: &error)
if let error = error {
var fileaccesserror: NSError?
url.checkResourceIsReachableAndReturnError(&fileaccesserror)
printErrorAndExit( fileaccesserror?.localizedDescription ?? error.localizedDescription )
} else {
printErrorAndExit("Invalid file path: \(path)")
filehandle!.seekToEndOfFile()
return filehandle!
}
}
+10 -1
View File
@@ -9,7 +9,16 @@
import Foundation
/**
func toURLOrError (path: String) -> NSURL {
if let url = NSURL(fileURLWithPath: path) {
return url
} else {
printErrorAndExit("Invalid file path: \(path)")
}
}
/**
The tempdirectory is unique each time a script is run and is created the first time it is used.
It lies in the user's temporary directory and will be automatically deleted at some point.
*/