String+File.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 seek(_ offset: Int) -> Bool {
  21. return (fseek(pointer, offset, SEEK_SET) == 0)
  22. }
  23. public func read(_ data: inout [UInt8]) throws -> Int {
  24. if data.count <= 0 {
  25. return data.count
  26. }
  27. let count = fread(&data, 1, data.count, self.pointer)
  28. if count == data.count {
  29. return count
  30. }
  31. if feof(self.pointer) != 0 {
  32. return count
  33. }
  34. if ferror(self.pointer) != 0 {
  35. throw FileError.error(errno)
  36. }
  37. throw FileError.error(0)
  38. }
  39. public func write(_ data: [UInt8]) throws -> Void {
  40. if data.count <= 0 {
  41. return
  42. }
  43. try data.withUnsafeBufferPointer {
  44. if fwrite($0.baseAddress, 1, data.count, self.pointer) != data.count {
  45. throw FileError.error(errno)
  46. }
  47. }
  48. }
  49. public static func currentWorkingDirectory() throws -> String {
  50. guard let path = getcwd(nil, 0) else {
  51. throw FileError.error(errno)
  52. }
  53. return String(cString: path)
  54. }
  55. }
  56. public static var pathSeparator = "/"
  57. public func openNewForWriting() throws -> File {
  58. return try openFileForMode(self, "wb")
  59. }
  60. public func openForReading() throws -> File {
  61. return try openFileForMode(self, "rb")
  62. }
  63. public func openForWritingAndReading() throws -> File {
  64. return try openFileForMode(self, "r+b")
  65. }
  66. public func openFileForMode(_ path: String, _ mode: String) throws -> File {
  67. guard let file = path.withCString({ pathPointer in mode.withCString({ fopen(pathPointer, $0) }) }) else {
  68. throw FileError.error(errno)
  69. }
  70. return File(file)
  71. }
  72. public func exists() throws -> Bool {
  73. return try self.withStat {
  74. if let _ = $0 {
  75. return true
  76. }
  77. return false
  78. }
  79. }
  80. public func directory() throws -> Bool {
  81. return try self.withStat {
  82. if let stat = $0 {
  83. return stat.st_mode & S_IFMT == S_IFDIR
  84. }
  85. return false
  86. }
  87. }
  88. public func files() throws -> [String] {
  89. guard let dir = self.withCString({ opendir($0) }) else {
  90. throw FileError.error(errno)
  91. }
  92. defer { closedir(dir) }
  93. var results = [String]()
  94. while let ent = readdir(dir) {
  95. var name = ent.pointee.d_name
  96. let fileName = withUnsafePointer(to: &name) { (ptr) -> String? in
  97. #if os(Linux)
  98. return String(validatingUTF8: [CChar](UnsafeBufferPointer<CChar>(start: UnsafePointer(unsafeBitCast(ptr, to: UnsafePointer<CChar>.self)), count: 256)))
  99. #else
  100. var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, to: UnsafePointer<CChar>.self), count: Int(ent.pointee.d_namlen)))
  101. buffer.append(0)
  102. return String(validatingUTF8: buffer)
  103. #endif
  104. }
  105. if let fileName = fileName {
  106. results.append(fileName)
  107. }
  108. }
  109. return results
  110. }
  111. private func withStat<T>(_ closure: ((stat?) throws -> T)) throws -> T {
  112. return try self.withCString({
  113. var statBuffer = stat()
  114. if stat($0, &statBuffer) == 0 {
  115. return try closure(statBuffer)
  116. }
  117. if errno == ENOENT {
  118. return try closure(nil)
  119. }
  120. throw FileError.error(errno)
  121. })
  122. }
  123. }