String+File.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 openFile(forMode mode: String) throws -> File {
  55. guard let file = self.withCString({ pathPointer in mode.withCString({ fopen(pathPointer, $0) }) }) else {
  56. throw FileError.error(errno)
  57. }
  58. return File(file)
  59. }
  60. public func exists() throws -> Bool {
  61. return try self.withStat {
  62. if let _ = $0 {
  63. return true
  64. }
  65. return false
  66. }
  67. }
  68. public func directory() throws -> Bool {
  69. return try self.withStat {
  70. if let stat = $0 {
  71. return stat.st_mode & S_IFMT == S_IFDIR
  72. }
  73. return false
  74. }
  75. }
  76. public func files() throws -> [String] {
  77. guard let dir = self.withCString({ opendir($0) }) else {
  78. throw FileError.error(errno)
  79. }
  80. defer { closedir(dir) }
  81. var results = [String]()
  82. while let ent = readdir(dir) {
  83. var name = ent.pointee.d_name
  84. let fileName = withUnsafePointer(to: &name) { (ptr) -> String? in
  85. #if os(Linux)
  86. return String(validatingUTF8: [CChar](UnsafeBufferPointer<CChar>(start: UnsafePointer(unsafeBitCast(ptr, to: UnsafePointer<CChar>.self)), count: 256)))
  87. #else
  88. var buffer = [CChar](UnsafeBufferPointer(start: unsafeBitCast(ptr, to: UnsafePointer<CChar>.self), count: Int(ent.pointee.d_namlen)))
  89. buffer.append(0)
  90. return String(validatingUTF8: buffer)
  91. #endif
  92. }
  93. if let fileName = fileName {
  94. results.append(fileName)
  95. }
  96. }
  97. return results
  98. }
  99. private func withStat<T>(_ closure: ((stat?) throws -> T)) throws -> T {
  100. return try self.withCString({
  101. var statBuffer = stat()
  102. if stat($0, &statBuffer) == 0 {
  103. return try closure(statBuffer)
  104. }
  105. if errno == ENOENT {
  106. return try closure(nil)
  107. }
  108. throw FileError.error(errno)
  109. })
  110. }
  111. }