1
0

File.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: ErrorProtocol {
  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. return String(cString: path)
  50. }
  51. private let pointer: UnsafeMutablePointer<FILE>
  52. public init(_ pointer: UnsafeMutablePointer<FILE>) {
  53. self.pointer = pointer
  54. }
  55. public func close() -> Void {
  56. fclose(pointer)
  57. }
  58. public func read(data: inout [UInt8]) throws -> Int {
  59. if data.count <= 0 {
  60. return data.count
  61. }
  62. let count = fread(&data, 1, data.count, self.pointer)
  63. if count == data.count {
  64. return count
  65. }
  66. if feof(self.pointer) != 0 {
  67. return count
  68. }
  69. if ferror(self.pointer) != 0 {
  70. throw FileError.ReadFailed(File.descriptionOfLastError())
  71. }
  72. throw FileError.ReadFailed("Unknown file read error occured.")
  73. }
  74. public func write(data: [UInt8]) throws -> Void {
  75. if data.count <= 0 {
  76. return
  77. }
  78. try data.withUnsafeBufferPointer {
  79. if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
  80. throw FileError.WriteFailed(File.descriptionOfLastError())
  81. }
  82. }
  83. }
  84. public func seek(offset: Int) throws -> Void {
  85. if fseek(self.pointer, offset, SEEK_SET) != 0 {
  86. throw FileError.SeekFailed(File.descriptionOfLastError())
  87. }
  88. }
  89. private static func descriptionOfLastError() -> String {
  90. return String(cString: UnsafePointer(strerror(errno))) ?? "Error: \(errno)"
  91. }
  92. }
  93. extension File {
  94. public static func withNewFileOpenedForWriting<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  95. return try withFileOpenedForMode(path, mode: "wb", f)
  96. }
  97. public static func withFileOpenedForReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  98. return try withFileOpenedForMode(path, mode: "rb", f)
  99. }
  100. public static func withFileOpenedForWritingAndReading<Result>(path: String, _ f: File throws -> Result) throws -> Result {
  101. return try withFileOpenedForMode(path, mode: "r+b", f)
  102. }
  103. public static func withFileOpenedForMode<Result>(path: String, mode: String, _ f: File throws -> Result) throws -> Result {
  104. let file = try File.openFileForMode(path, mode)
  105. defer {
  106. file.close()
  107. }
  108. return try f(file)
  109. }
  110. }