String+File.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. internal 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. }
  47. public static var PATH_SEPARATOR = "/"
  48. public func openNewForWriting() throws -> File {
  49. return try openFileForMode(self, "wb")
  50. }
  51. public func openForReading() throws -> File {
  52. return try openFileForMode(self, "rb")
  53. }
  54. public func openForWritingAndReading() throws -> File {
  55. return try openFileForMode(self, "r+b")
  56. }
  57. public func openFileForMode(_ path: String, _ mode: String) throws -> File {
  58. guard let file = path.withCString({ pathPointer in mode.withCString({ fopen(pathPointer, $0) }) }) else {
  59. throw FileError.error(errno)
  60. }
  61. return File(file)
  62. }
  63. public func exists() throws -> Bool {
  64. return try self.withStat {
  65. if let _ = $0 {
  66. return true
  67. }
  68. return false
  69. }
  70. }
  71. public func directory() throws -> Bool {
  72. return try self.withStat {
  73. if let stat = $0 {
  74. return stat.st_mode & S_IFMT == S_IFDIR
  75. }
  76. return false
  77. }
  78. }
  79. public func files() throws -> [String] {
  80. guard let dir = self.withCString({ opendir($0) }) else {
  81. throw FileError.error(errno)
  82. }
  83. defer { closedir(dir) }
  84. var results = [String]()
  85. while let ent = readdir(dir) {
  86. var name = ent.pointee.d_name
  87. let fileName = withUnsafePointer(to: &name) { (ptr) -> String? in
  88. #if os(Linux)
  89. return String.fromCString([CChar](UnsafeBufferPointer<CChar>(start: UnsafePointer(unsafeBitCast(ptr, UnsafePointer<CChar>.self)), count: 256)))
  90. #else
  91. var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, to: UnsafePointer<CChar>.self), count: Int(ent.pointee.d_namlen)))
  92. buffer.append(0)
  93. return String(validatingUTF8: buffer)
  94. #endif
  95. }
  96. if let fileName = fileName {
  97. results.append(fileName)
  98. }
  99. }
  100. return results
  101. }
  102. public static func currentWorkingDirectory() throws -> String {
  103. guard let path = getcwd(nil, 0) else {
  104. throw FileError.error(errno)
  105. }
  106. return String(cString: path)
  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. }