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