File.swift 3.9 KB

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