File.swift 3.8 KB

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