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