File.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // File.swift
  3. // Swifter
  4. //
  5. // Copyright (c) 2014-2016 Damian Kołakowski. All rights reserved.
  6. //
  7. #if os(Linux)
  8. import Glibc
  9. #else
  10. import Foundation
  11. #endif
  12. public enum FileError: ErrorType {
  13. case OpenFailed(String)
  14. case WriteFailed(String)
  15. case ReadFailed(String)
  16. case SeekFailed(String)
  17. }
  18. public class File {
  19. public static func openNewForWriting(path: String) throws -> File {
  20. return try openFileForMode(path, "wb")
  21. }
  22. public static func openForReading(path: String) throws -> File {
  23. return try openFileForMode(path, "rb")
  24. }
  25. public static func openForWritingAndReading(path: String) throws -> File {
  26. return try openFileForMode(path, "r+b")
  27. }
  28. public static func openFileForMode(path: String, _ mode: String) throws -> File {
  29. let file = fopen(path.withCString({ $0 }), mode.withCString({ $0 }))
  30. guard file != nil else {
  31. throw FileError.OpenFailed(descriptionOfLastError())
  32. }
  33. return File(file)
  34. }
  35. private let pointer: UnsafeMutablePointer<FILE>
  36. public init(_ pointer: UnsafeMutablePointer<FILE>) {
  37. self.pointer = pointer
  38. }
  39. public func close() -> Void {
  40. fclose(pointer)
  41. }
  42. public func read(inout data: [UInt8]) throws -> Int {
  43. if data.count <= 0 {
  44. return data.count
  45. }
  46. let count = fread(&data, 1, data.count, self.pointer)
  47. if count == data.count {
  48. return count
  49. }
  50. if feof(self.pointer) != 0 {
  51. return count
  52. }
  53. if ferror(self.pointer) != 0 {
  54. throw FileError.ReadFailed(File.descriptionOfLastError())
  55. }
  56. throw FileError.ReadFailed("Unknown file read error occured.")
  57. }
  58. public func write(data: [UInt8]) throws -> Void {
  59. if data.count <= 0 {
  60. return
  61. }
  62. try data.withUnsafeBufferPointer {
  63. if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
  64. throw FileError.WriteFailed(File.descriptionOfLastError())
  65. }
  66. }
  67. }
  68. public func seek(offset: Int) throws -> Void {
  69. if fseek(self.pointer, offset, SEEK_SET) != 0 {
  70. throw FileError.SeekFailed(File.descriptionOfLastError())
  71. }
  72. }
  73. private static func descriptionOfLastError() -> String {
  74. return String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)"
  75. }
  76. }
  77. extension File {
  78. public static func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  79. return try withFileOpenedForMode(path, mode: "wb", f)
  80. }
  81. public static func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  82. return try withFileOpenedForMode(path, mode: "rb", f)
  83. }
  84. public static func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  85. return try withFileOpenedForMode(path, mode: "r+b", f)
  86. }
  87. public static func withFileOpenedForMode<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result {
  88. let file = try File.openFileForMode(path, mode)
  89. defer {
  90. file.close()
  91. }
  92. return try f(file)
  93. }
  94. }