-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSPathUtilities.swift
407 lines (355 loc) · 15.3 KB
/
NSPathUtilities.swift
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
internal extension String {
internal var _startOfLastPathComponent : String.CharacterView.Index {
precondition(!hasSuffix("/") && length > 1)
let characterView = characters
let startPos = characterView.startIndex
let endPos = characterView.endIndex
var curPos = endPos
// Find the beginning of the component
while curPos > startPos {
let prevPos = curPos.predecessor()
if characterView[prevPos] == "/" {
break
}
curPos = prevPos
}
return curPos
}
internal var _startOfPathExtension : String.CharacterView.Index? {
precondition(!hasSuffix("/"))
let characterView = self.characters
let endPos = characterView.endIndex
var curPos = endPos
let lastCompStartPos = _startOfLastPathComponent
// Find the beginning of the extension
while curPos > lastCompStartPos {
let prevPos = curPos.predecessor()
let char = characterView[prevPos]
if char == "/" {
return nil
} else if char == "." {
if lastCompStartPos == prevPos {
return nil
} else {
return curPos
}
}
curPos = prevPos
}
return nil
}
internal var absolutePath: Bool {
return hasPrefix("~") || hasPrefix("/")
}
internal static func pathWithComponents(components: [String]) -> String {
var result = ""
for comp in components.prefix(components.count - 1) {
result = result._stringByAppendingPathComponent(comp._stringByFixingSlashes(), doneAppending: false)
}
if let last = components.last {
result = result._stringByAppendingPathComponent(last._stringByFixingSlashes(), doneAppending: true)
}
return result
}
internal var pathComponents : [String] {
var result = [String]()
if length == 0 {
return result
} else {
let characterView = characters
var curPos = characterView.startIndex
let endPos = characterView.endIndex
if characterView[curPos] == "/" {
result.append("/")
}
while curPos < endPos {
while curPos < endPos && characterView[curPos] == "/" {
curPos++
}
if curPos == endPos {
break
}
var curEnd = curPos
while curEnd < endPos && characterView[curEnd] != "/" {
curEnd++
}
result.append(String(characterView[curPos ..< curEnd]))
curPos = curEnd
}
}
if length > 1 && hasSuffix("/") {
result.append("/")
}
return result
}
internal var lastPathComponent : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return fixedSelf
}
return String(fixedSelf.characters.suffixFrom(fixedSelf._startOfLastPathComponent))
}
internal var pathExtension : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return ""
}
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.characters.suffixFrom(extensionPos))
} else {
return ""
}
}
internal func _stringByAppendingPathComponent(str: String, doneAppending : Bool = true) -> String {
if str.length == 0 {
return self
}
if self == "" {
return "/" + str
}
if self == "/" {
return self + str
}
return self + "/" + str
}
internal func _stringByFixingSlashes(compress compress : Bool = true, stripTrailing: Bool = true) -> String {
var result = self
if compress {
result.withMutableCharacters { characterView in
let startPos = characterView.startIndex
var endPos = characterView.endIndex
var curPos = startPos
while curPos < endPos {
if characterView[curPos] == "/" {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
afterLastSlashPos = afterLastSlashPos.successor()
}
if afterLastSlashPos != curPos.successor() {
characterView.replaceRange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = characterView.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = curPos.successor()
}
}
}
}
if stripTrailing && result.length > 1 && result.hasSuffix("/") {
result.removeAtIndex(result.characters.endIndex.predecessor())
}
return result
}
}
public extension NSString {
public var absolutePath: Bool {
return hasPrefix("~") || hasPrefix("/")
}
public static func pathWithComponents(components: [String]) -> String {
var result = ""
for comp in components.prefix(components.count - 1) {
result = result._stringByAppendingPathComponent(comp._stringByFixingSlashes(), doneAppending: false)
}
if let last = components.last {
result = result._stringByAppendingPathComponent(last._stringByFixingSlashes(), doneAppending: true)
}
return result
}
public var pathComponents : [String] {
var result = [String]()
if length == 0 {
return result
} else {
let characterView = _swiftObject.characters
var curPos = characterView.startIndex
let endPos = characterView.endIndex
if characterView[curPos] == "/" {
result.append("/")
}
while curPos < endPos {
while curPos < endPos && characterView[curPos] == "/" {
curPos++
}
if curPos == endPos {
break
}
var curEnd = curPos
while curEnd < endPos && characterView[curEnd] != "/" {
curEnd++
}
result.append(String(characterView[curPos ..< curEnd]))
curPos = curEnd
}
}
if length > 1 && hasSuffix("/") {
result.append("/")
}
return result
}
public var lastPathComponent : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return fixedSelf
}
return String(fixedSelf.characters.suffixFrom(fixedSelf._startOfLastPathComponent))
}
public var stringByDeletingLastPathComponent : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf == "/" {
return fixedSelf
}
if fixedSelf.length <= 1 {
return ""
}
return String(fixedSelf.characters.prefixUpTo(fixedSelf._startOfLastPathComponent))
}
internal func _stringByFixingSlashes(compress compress : Bool = true, stripTrailing: Bool = true) -> String {
var result = _swiftObject
if compress {
result.withMutableCharacters { characterView in
let startPos = characterView.startIndex
var endPos = characterView.endIndex
var curPos = startPos
while curPos < endPos {
if characterView[curPos] == "/" {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && characterView[afterLastSlashPos] == "/" {
afterLastSlashPos = afterLastSlashPos.successor()
}
if afterLastSlashPos != curPos.successor() {
characterView.replaceRange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = characterView.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = curPos.successor()
}
}
}
}
if stripTrailing && result.hasSuffix("/") {
result.removeAtIndex(result.characters.endIndex.predecessor())
}
return result
}
internal func _stringByAppendingPathComponent(str: String, doneAppending : Bool = true) -> String {
if str.length == 0 {
return _swiftObject
}
if self == "" {
return "/" + str
}
if self == "/" {
return _swiftObject + str
}
return _swiftObject + "/" + str
}
public func stringByAppendingPathComponent(str: String) -> String {
return _stringByAppendingPathComponent(str)
}
public var pathExtension : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return ""
}
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.characters.suffixFrom(extensionPos))
} else {
return ""
}
}
public var stringByDeletingPathExtension: String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return fixedSelf
}
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.characters.prefixUpTo(extensionPos))
} else {
return fixedSelf
}
}
public func stringByAppendingPathExtension(str: String) -> String? {
if str.hasPrefix("/") || self == "" || self == "/" {
print("Cannot append extension \(str) to path \(self)")
return nil
}
let result = _swiftObject + str._stringByFixingSlashes(compress: false, stripTrailing: true)
return result._stringByFixingSlashes()
}
public var stringByStandardizingPath: String {
NSUnimplemented()
}
public var stringByResolvingSymlinksInPath: String {
NSUnimplemented()
}
public func stringsByAppendingPaths(paths: [String]) -> [String] {
if self == "" {
return paths
}
return paths.map(stringByAppendingPathComponent)
}
public func completePathIntoString(inout outputName: NSString?, caseSensitive flag: Bool, inout matchesIntoArray outputArray: [NSString], filterTypes: [String]?) -> Int {
NSUnimplemented()
}
public var fileSystemRepresentation : UnsafePointer<Int8> {
NSUnimplemented()
}
public func getFileSystemRepresentation(cname: UnsafeMutablePointer<Int8>, maxLength max: Int) -> Bool {
guard self.length > 0 else {
return false
}
return CFStringGetFileSystemRepresentation(self._cfObject, cname, max)
}
}
public enum NSSearchPathDirectory : UInt {
case ApplicationDirectory // supported applications (Applications)
case DemoApplicationDirectory // unsupported applications, demonstration versions (Demos)
case DeveloperApplicationDirectory // developer applications (Developer/Applications). DEPRECATED - there is no one single Developer directory.
case AdminApplicationDirectory // system and network administration applications (Administration)
case LibraryDirectory // various documentation, support, and configuration files, resources (Library)
case DeveloperDirectory // developer resources (Developer) DEPRECATED - there is no one single Developer directory.
case UserDirectory // user home directories (Users)
case DocumentationDirectory // documentation (Documentation)
case DocumentDirectory // documents (Documents)
case CoreServiceDirectory // location of CoreServices directory (System/Library/CoreServices)
case AutosavedInformationDirectory // location of autosaved documents (Documents/Autosaved)
case DesktopDirectory // location of user's desktop
case CachesDirectory // location of discardable cache files (Library/Caches)
case ApplicationSupportDirectory // location of application support files (plug-ins, etc) (Library/Application Support)
case DownloadsDirectory // location of the user's "Downloads" directory
case InputMethodsDirectory // input methods (Library/Input Methods)
case MoviesDirectory // location of user's Movies directory (~/Movies)
case MusicDirectory // location of user's Music directory (~/Music)
case PicturesDirectory // location of user's Pictures directory (~/Pictures)
case PrinterDescriptionDirectory // location of system's PPDs directory (Library/Printers/PPDs)
case SharedPublicDirectory // location of user's Public sharing directory (~/Public)
case PreferencePanesDirectory // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
case ApplicationScriptsDirectory // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
case ItemReplacementDirectory // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
case AllApplicationsDirectory // all directories where applications can occur
case AllLibrariesDirectory // all directories where resources can occur
case TrashDirectory // location of Trash directory
}
public struct NSSearchPathDomainMask : OptionSetType {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }
public static let UserDomainMask = NSSearchPathDomainMask(rawValue: 1) // user's home directory --- place to install user's personal items (~)
public static let LocalDomainMask = NSSearchPathDomainMask(rawValue: 2) // local to the current machine --- place to install items available to everyone on this machine (/Library)
public static let NetworkDomainMask = NSSearchPathDomainMask(rawValue: 4) // publically available location in the local area network --- place to install items available on the network (/Network)
public static let SystemDomainMask = NSSearchPathDomainMask(rawValue: 8) // provided by Apple, unmodifiable (/System)
public static let AllDomainsMask = NSSearchPathDomainMask(rawValue: 0x0ffff) // all domains: all of the above and future items
}
public func NSSearchPathForDirectoriesInDomains(directory: NSSearchPathDirectory, _ domainMask: NSSearchPathDomainMask, _ expandTilde: Bool) -> [String] {
NSUnimplemented()
}