Last active
November 29, 2023 12:32
-
-
Save kristopherjohnson/4bafee0b489add42a61f to your computer and use it in GitHub Desktop.
Example of using opendir/readdir/closedir with Swift
This file contains 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 | |
// Get current working directory | |
var wdbuf: [Int8] = Array(count: Int(MAXNAMLEN), repeatedValue: 0) | |
let workingDirectory = getcwd(&wdbuf, UInt(MAXNAMLEN)) | |
println("Working directory: \(String.fromCString(workingDirectory)!)") | |
println("\nContents:") | |
// Open the directory | |
let dir = opendir(workingDirectory) | |
if dir != nil { | |
// Use readdir to get each element | |
var entry = readdir(dir) | |
while entry != nil { | |
let d_namlen = entry.memory.d_namlen | |
let d_name = entry.memory.d_name | |
// dirent.d_name is defined as a tuple with | |
// MAXNAMLEN elements. We want to convert | |
// that to a null-terminated C string. The | |
// only way to iterate over tuple elements | |
// in Swift is via reflection. | |
// | |
// Credit: dankogi at http://stackoverflow.com/questions/24299045/any-way-to-iterate-a-tuple-in-swift | |
var nameBuf: [CChar] = Array() | |
let mirror = reflect(d_name) | |
for i in 0..<d_namlen { | |
let (_, elem) = mirror[Int(i)] | |
nameBuf.append(elem.value as Int8) | |
} | |
// Null-terminate it and convert to a String for display | |
nameBuf.append(0) | |
if let name = String.fromCString(nameBuf) { | |
println("- \(name)") | |
} | |
entry = readdir(dir) | |
} | |
closedir(dir) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a similar version of this code adopted to Swift 5.9. It excludes ".", ".." and similar files because I find them useless. Also it returns
[URL]
instead of[String]
.