File.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  17. public class File {
  18. public static func openNewForWriting(path: String) throws -> File {
  19. return try openFileForMode(path, "wb")
  20. }
  21. public static func openForReading(path: String) throws -> File {
  22. return try openFileForMode(path, "rb")
  23. }
  24. public static func openForWritingAndReading(path: String) throws -> File {
  25. return try openFileForMode(path, "r+b")
  26. }
  27. public static func openFileForMode(path: String, _ mode: String) throws -> File {
  28. let file = fopen(path.withCString({ $0 }), mode.withCString({ $0 }))
  29. guard file != nil else {
  30. throw FileError.OpenFailed(descriptionOfLastError())
  31. }
  32. return File(file)
  33. }
  34. private let pointer: UnsafeMutablePointer<FILE>
  35. public init(_ pointer: UnsafeMutablePointer<FILE>) {
  36. self.pointer = pointer
  37. }
  38. public func close() -> Void {
  39. fclose(pointer)
  40. }
  41. public func read(inout data: [UInt8]) throws -> Int {
  42. if data.count <= 0 {
  43. return data.count
  44. }
  45. let count = fread(&data, 1, data.count, self.pointer)
  46. if count == data.count {
  47. return count
  48. }
  49. if feof(self.pointer) != 0 {
  50. return count
  51. }
  52. if ferror(self.pointer) != 0 {
  53. throw FileError.ReadFailed(File.descriptionOfLastError())
  54. }
  55. throw FileError.ReadFailed("Unknown file read error occured.")
  56. }
  57. public func write(data: [UInt8]) throws -> Void {
  58. if data.count <= 0 {
  59. return
  60. }
  61. try data.withUnsafeBufferPointer {
  62. if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
  63. throw FileError.WriteFailed(File.descriptionOfLastError())
  64. }
  65. }
  66. }
  67. private static func descriptionOfLastError() -> String {
  68. return String.fromCString(UnsafePointer(strerror(errno))) ?? "Error: \(errno)"
  69. }
  70. }
  71. extension File {
  72. public static func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  73. return try withFileOpenedForMode(path, mode: "wb", f)
  74. }
  75. public static func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  76. return try withFileOpenedForMode(path, mode: "rb", f)
  77. }
  78. public static func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  79. return try withFileOpenedForMode(path, mode: "r+b", f)
  80. }
  81. public static func withFileOpenedForMode<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result {
  82. let file = try File.openFileForMode(path, mode)
  83. defer {
  84. file.close()
  85. }
  86. return try f(file)
  87. }
  88. }