-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathString+File.swift
More file actions
185 lines (164 loc) · 5.47 KB
/
String+File.swift
File metadata and controls
185 lines (164 loc) · 5.47 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// String+File.swift
// Swifter
//
// Copyright © 2016 Damian Kołakowski. All rights reserved.
//
import Foundation
#if os(Windows)
import WinSDK
#endif
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)
}
#if os(Windows)
public func seek(_ offset: Int32) -> Bool {
return (fseek(pointer, offset, SEEK_SET) == 0)
}
#else
public func seek(_ offset: Int) -> Bool {
return (fseek(pointer, offset, SEEK_SET) == 0)
}
#endif
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 {
#if os(Windows)
// Need to disambiguate here.
return Int32(stat.st_mode) & ucrt.S_IFMT == ucrt.S_IFDIR
#else
return stat.st_mode & S_IFMT == S_IFDIR
#endif
}
return false
}
}
public func files() throws -> [String] {
var results = [String]()
#if os(Windows)
var data = WIN32_FIND_DATAW()
let handle = self.withCString(encodedAs: UTF16.self) {
return FindFirstFileW($0, &data)
}
guard handle != INVALID_HANDLE_VALUE else {
throw FileError.error(Int32(GetLastError()))
}
defer { FindClose(handle) }
let appendToResults = {
let fileName = withUnsafePointer(to: &data.cFileName) { (ptr) -> String in
ptr.withMemoryRebound(to: unichar.self, capacity: Int(MAX_PATH * 2)) {
String(utf16CodeUnits: $0, count: wcslen($0))
}
}
results.append(fileName)
}
appendToResults()
while FindNextFileW(handle, &data) {
appendToResults()
}
#else
guard let dir = self.withCString({ opendir($0) }) else {
throw FileError.error(errno)
}
defer { closedir(dir) }
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)
}
}
#endif
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)
})
}
}