String+File.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // String+File.swift
  3. // Swifter
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import Foundation
  8. extension String {
  9. public enum FileError: Error {
  10. case error(Int32)
  11. }
  12. public class File {
  13. let pointer: UnsafeMutablePointer<FILE>
  14. public init(_ pointer: UnsafeMutablePointer<FILE>) {
  15. self.pointer = pointer
  16. }
  17. public func close() -> Void {
  18. fclose(pointer)
  19. }
  20. public func read(_ data: inout [UInt8]) throws -> Int {
  21. if data.count <= 0 {
  22. return data.count
  23. }
  24. let count = fread(&data, 1, data.count, self.pointer)
  25. if count == data.count {
  26. return count
  27. }
  28. if feof(self.pointer) != 0 {
  29. return count
  30. }
  31. if ferror(self.pointer) != 0 {
  32. throw FileError.error(errno)
  33. }
  34. throw FileError.error(0)
  35. }
  36. public func write(_ data: [UInt8]) throws -> Void {
  37. if data.count <= 0 {
  38. return
  39. }
  40. try data.withUnsafeBufferPointer {
  41. if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
  42. throw FileError.error(errno)
  43. }
  44. }
  45. }
  46. public static func currentWorkingDirectory() throws -> String {
  47. guard let path = getcwd(nil, 0) else {
  48. throw FileError.error(errno)
  49. }
  50. return String(cString: path)
  51. }
  52. }
  53. public static var pathSeparator = "/"
  54. public func openNewForWriting() throws -> File {
  55. return try openFileForMode(self, "wb")
  56. }
  57. public func openForReading() throws -> File {
  58. return try openFileForMode(self, "rb")
  59. }
  60. public func openForWritingAndReading() throws -> File {
  61. return try openFileForMode(self, "r+b")
  62. }
  63. public func openFileForMode(_ path: String, _ mode: String) throws -> File {
  64. guard let file = path.withCString({ pathPointer in mode.withCString({ fopen(pathPointer, $0) }) }) else {
  65. throw FileError.error(errno)
  66. }
  67. return File(file)
  68. }
  69. public func exists() throws -> Bool {
  70. return try self.withStat {
  71. if let _ = $0 {
  72. return true
  73. }
  74. return false
  75. }
  76. }
  77. public func directory() throws -> Bool {
  78. return try self.withStat {
  79. if let stat = $0 {
  80. return stat.st_mode & S_IFMT == S_IFDIR
  81. }
  82. return false
  83. }
  84. }
  85. public func files() throws -> [String] {
  86. guard let dir = self.withCString({ opendir($0) }) else {
  87. throw FileError.error(errno)
  88. }
  89. defer { closedir(dir) }
  90. var results = [String]()
  91. while let ent = readdir(dir) {
  92. var name = ent.pointee.d_name
  93. let fileName = withUnsafePointer(to: &name) { (ptr) -> String? in
  94. #if os(Linux)
  95. return String(validatingUTF8: [CChar](UnsafeBufferPointer<CChar>(start: UnsafePointer(unsafeBitCast(ptr, to: UnsafePointer<CChar>.self)), count: 256)))
  96. #else
  97. var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, to: UnsafePointer<CChar>.self), count: Int(ent.pointee.d_namlen)))
  98. buffer.append(0)
  99. return String(validatingUTF8: buffer)
  100. #endif
  101. }
  102. if let fileName = fileName {
  103. results.append(fileName)
  104. }
  105. }
  106. return results
  107. }
  108. private func withStat<T>(_ closure: ((stat?) throws -> T)) throws -> T {
  109. return try self.withCString({
  110. var statBuffer = stat()
  111. if stat($0, &statBuffer) == 0 {
  112. return try closure(statBuffer)
  113. }
  114. if errno == ENOENT {
  115. return try closure(nil)
  116. }
  117. throw FileError.error(errno)
  118. })
  119. }
  120. }