Created
December 22, 2015 22:13
-
-
Save kristopherjohnson/66fdfcc5312322a5bb63 to your computer and use it in GitHub Desktop.
Swift wrapper for glob(3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| /// Given a wildcard pattern, return matching paths. | |
| /// | |
| /// - parameter pattern: pathname pattern to be expanded. See docs for `glob(3)`. | |
| /// - returns: array of pathnames that match the pattern | |
| func matchingFilesForPattern(pattern: String) -> [String] { | |
| var result = Array<String>() | |
| var g = glob_t() | |
| glob(pattern, 0, nil, &g) | |
| defer { globfree(&g) } | |
| for i in 0..<g.gl_pathc { | |
| let sz = UnsafeMutablePointer<CChar>(g.gl_pathv[i]) | |
| if let s = String.fromCString(sz) { | |
| result.append(s) | |
| } | |
| } | |
| return result | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment