File.swift 4.1 KB

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