0001 // 0002 // File.swift 0003 // Swifter 0004 // 0005 // Copyright (c) 2014-2016 Damian KoĊakowski. All rights reserved. 0006 // 0007 0008 #if os(Linux) 0009 import Glibc 0010 #else 0011 import Foundation 0012 #endif 0013 0014 public enum FileError: ErrorType { 0015 case OpenFailed
File.swift:40 throw FileError.OpenFailed(descriptionOfLastError())File.swift:48 throw FileError.IsDirectoryFailed(descriptionOfLastError())File.swift:56 throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError())File.swift:59 throw FileError.GetCurrentWorkingDirectoryFailed("Could not convert getcwd(...)'s result (\(path)) to String.")File.swift:86 throw FileError.ReadFailed(File.descriptionOfLastError())File.swift:88 throw FileError.ReadFailed("Unknown file read error occured.")File.swift:97 throw FileError.WriteFailed(File.descriptionOfLastError())File.swift:104 throw FileError.SeekFailed(File.descriptionOfLastError())(String) 0016 case WriteFailed
File.swift:40 throw FileError.OpenFailed(descriptionOfLastError())(String) 0017 case ReadFailed
File.swift:97 throw FileError.WriteFailed(File.descriptionOfLastError())(String) 0018 case SeekFailed
File.swift:86 throw FileError.ReadFailed(File.descriptionOfLastError())File.swift:88 throw FileError.ReadFailed("Unknown file read error occured.")(String) 0019 case GetCurrentWorkingDirectoryFailed
File.swift:104 throw FileError.SeekFailed(File.descriptionOfLastError())(String) 0020 case IsDirectoryFailed
File.swift:56 throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError())File.swift:59 throw FileError.GetCurrentWorkingDirectoryFailed("Could not convert getcwd(...)'s result (\(path)) to String.")(String) 0021 } 0022 0023 public class File
File.swift:48 throw FileError.IsDirectoryFailed(descriptionOfLastError()){ 0024 0025 public static func openNewForWriting(path: String) throws -> File { 0026 return try openFileForMode(path, "wb") 0027 } 0028 0029 public static func openForReading
File.swift:25 public static func openNewForWriting(path: String) throws -> File {File.swift:29 public static func openForReading(path: String) throws -> File {File.swift:33 public static func openForWritingAndReading(path: String) throws -> File {File.swift:37 public static func openFileForMode(path: String, _ mode: String) throws -> File {File.swift:42 return File(file)File.swift:86 throw FileError.ReadFailed(File.descriptionOfLastError())File.swift:97 throw FileError.WriteFailed(File.descriptionOfLastError())File.swift:104 throw FileError.SeekFailed(File.descriptionOfLastError())File.swift:113 extension File {File.swift:115 public static func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result {File.swift:119 public static func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {File.swift:123 public static func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {File.swift:127 public static func withFileOpenedForMode<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result {File.swift:128 let file = try File.openFileForMode(path, mode)HttpHandlers+Files.swift:18 guard let file = try? File.openForReading(absolutePath) else {(path: String) throws -> File { 0030 return try openFileForMode(path, "rb") 0031 } 0032 0033 public static func openForWritingAndReading(path: String) throws -> File { 0034 return try openFileForMode(path, "r+b") 0035 } 0036 0037 public static func openFileForMode
HttpHandlers+Files.swift:18 guard let file = try? File.openForReading(absolutePath) else {(path: String, _ mode: String) throws -> File { 0038 let file = fopen(path.withCString({ $0 }), mode.withCString({ $0 })) 0039 guard file != nil else { 0040 throw FileError.OpenFailed(descriptionOfLastError()) 0041 } 0042 return File(file) 0043 } 0044 0045 public static func isDirectory(path: String) throws -> Bool { 0046 var s = stat() 0047 guard stat(path, &s) == 0 else { 0048 throw FileError.IsDirectoryFailed(descriptionOfLastError()) 0049 } 0050 return s.st_mode & S_IFMT == S_IFDIR 0051 } 0052 0053 public static func currentWorkingDirectory() throws -> String { 0054 let path = getcwd(nil, 0) 0055 if path == nil { 0056 throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError()) 0057 } 0058 guard let result = String.fromCString(path) else { 0059 throw FileError.GetCurrentWorkingDirectoryFailed("Could not convert getcwd(...)'s result (\(path)) to String.") 0060 } 0061 return result 0062 } 0063 0064 private let pointer
File.swift:26 return try openFileForMode(path, "wb")File.swift:30 return try openFileForMode(path, "rb")File.swift:34 return try openFileForMode(path, "r+b")File.swift:128 let file = try File.openFileForMode(path, mode): UnsafeMutablePointer<FILE> 0065 0066 public init
File.swift:67 self.pointer = pointerFile.swift:71 fclose(pointer)File.swift:78 let count = fread(&data, 1, data.count, self.pointer)File.swift:82 if feof(self.pointer) != 0 {File.swift:85 if ferror(self.pointer) != 0 {File.swift:96 if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {File.swift:103 if fseek(self.pointer, offset, SEEK_SET) != 0 {(_ pointer: UnsafeMutablePointer<FILE>) { 0067 self.pointer = pointer 0068 } 0069 0070 public func close
File.swift:42 return File(file)() -> Void { 0071 fclose(pointer) 0072 } 0073 0074 public func read
File.swift:130 file.close()HttpHandlers+Files.swift:26 file.close()(inout data: [UInt8]) throws -> Int { 0075 if data.count <= 0 { 0076 return data.count 0077 } 0078 let count = fread(&data, 1, data.count, self.pointer) 0079 if count == data.count { 0080 return count 0081 } 0082 if feof(self.pointer) != 0 { 0083 return count 0084 } 0085 if ferror(self.pointer) != 0 { 0086 throw FileError.ReadFailed(File.descriptionOfLastError()) 0087 } 0088 throw FileError.ReadFailed("Unknown file read error occured.") 0089 } 0090 0091 public func write(data: [UInt8]) throws -> Void { 0092 if data.count <= 0 { 0093 return 0094 } 0095 try data.withUnsafeBufferPointer { 0096 if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count { 0097 throw FileError.WriteFailed(File.descriptionOfLastError()) 0098 } 0099 } 0100 } 0101 0102 public func seek(offset: Int) throws -> Void { 0103 if fseek(self.pointer, offset, SEEK_SET) != 0 { 0104 throw FileError.SeekFailed(File.descriptionOfLastError()) 0105 } 0106 } 0107 0108 private static func descriptionOfLastError
HttpHandlers+Files.swift:23 while let count = try? file.read(&buffer) where count > 0 {() -> String { 0109 return String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)" 0110 } 0111 } 0112 0113 extension File { 0114 0115 public static func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result { 0116 return try withFileOpenedForMode(path, mode: "wb", f) 0117 } 0118 0119 public static func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result { 0120 return try withFileOpenedForMode(path, mode: "rb", f) 0121 } 0122 0123 public static func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result { 0124 return try withFileOpenedForMode(path, mode: "r+b", f) 0125 } 0126 0127 public static func withFileOpenedForMode
File.swift:40 throw FileError.OpenFailed(descriptionOfLastError())File.swift:48 throw FileError.IsDirectoryFailed(descriptionOfLastError())File.swift:56 throw FileError.GetCurrentWorkingDirectoryFailed(descriptionOfLastError())File.swift:86 throw FileError.ReadFailed(File.descriptionOfLastError())File.swift:97 throw FileError.WriteFailed(File.descriptionOfLastError())File.swift:104 throw FileError.SeekFailed(File.descriptionOfLastError())<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result { 0128 let file = try File.openFileForMode(path, mode) 0129 defer { 0130 file.close() 0131 } 0132 return try f(file) 0133 } 0134 } 0135
File.swift:116 return try withFileOpenedForMode(path, mode: "wb", f)File.swift:120 return try withFileOpenedForMode(path, mode: "rb", f)File.swift:124 return try withFileOpenedForMode(path, mode: "r+b", f)