Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active November 29, 2023 12:32
Show Gist options
  • Select an option

  • Save kristopherjohnson/4bafee0b489add42a61f to your computer and use it in GitHub Desktop.

Select an option

Save kristopherjohnson/4bafee0b489add42a61f to your computer and use it in GitHub Desktop.

Revisions

  1. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ if dir != nil {

    // dirent.d_name is defined as a tuple with
    // MAXNAMLEN elements. We want to convert
    // that to a null-terminated string. The
    // that to a null-terminated C string. The
    // only way to iterate over tuple elements
    // in Swift is via reflection.
    //
  2. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -13,27 +13,28 @@ if dir != nil {
    // Use readdir to get each element
    var entry = readdir(dir)
    while entry != nil {
    var name: [CChar] = Array()
    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. The only way to
    // iterate over those elements is via
    // reflection
    // MAXNAMLEN elements. We want to convert
    // that to a null-terminated 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

    let d_namlen = entry.memory.d_namlen
    let d_name = entry.memory.d_name
    var nameBuf: [CChar] = Array()
    let mirror = reflect(d_name)
    for i in 0..<d_namlen {
    let (s, m) = mirror[Int(i)]
    name.append(m.value as Int8)
    let (_, elem) = mirror[Int(i)]
    nameBuf.append(elem.value as Int8)
    }

    // Null-terminate it and convert to a String for display
    name.append(0)
    if let s = String.fromCString(name) {
    println("- \(s)")
    nameBuf.append(0)
    if let name = String.fromCString(nameBuf) {
    println("- \(name)")
    }

    entry = readdir(dir)
  3. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,16 @@ 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 dirent = readdir(dir)
    while dirent != nil {
    var entry = readdir(dir)
    while entry != nil {
    var name: [CChar] = Array()

    // dirent.d_name is defined as a tuple with
    @@ -19,21 +22,21 @@ if dir != nil {
    //
    // Credit: dankogi at http://stackoverflow.com/questions/24299045/any-way-to-iterate-a-tuple-in-swift

    let d_namlen = dirent.memory.d_namlen
    let d_name = dirent.memory.d_name
    let d_namlen = entry.memory.d_namlen
    let d_name = entry.memory.d_name
    let mirror = reflect(d_name)
    for i in 0..<d_namlen {
    let (s, m) = mirror[Int(i)]
    name.append(m.value as Int8)
    }

    // Null-terminate it and convert to a String
    // Null-terminate it and convert to a String for display
    name.append(0)
    if let s = String.fromCString(name) {
    println(s)
    println("- \(s)")
    }

    dirent = readdir(dir)
    entry = readdir(dir)
    }
    closedir(dir)
    }
  4. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ if dir != nil {
    name.append(m.value as Int8)
    }

    // null-terminate it and convert to a string
    // Null-terminate it and convert to a String
    name.append(0)
    if let s = String.fromCString(name) {
    println(s)
  5. kristopherjohnson revised this gist Dec 31, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,7 @@ if dir != nil {
    name.append(m.value as Int8)
    }

    // null-terminate it and convert to a string
    name.append(0)
    if let s = String.fromCString(name) {
    println(s)
  6. kristopherjohnson created this gist Dec 31, 2014.
    38 changes: 38 additions & 0 deletions listFiles.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import Foundation

    // Get current working directory
    var wdbuf: [Int8] = Array(count: Int(MAXNAMLEN), repeatedValue: 0)
    let workingDirectory = getcwd(&wdbuf, UInt(MAXNAMLEN))

    // Open the directory
    let dir = opendir(workingDirectory)
    if dir != nil {
    // Use readdir to get each element
    var dirent = readdir(dir)
    while dirent != nil {
    var name: [CChar] = Array()

    // dirent.d_name is defined as a tuple with
    // MAXNAMLEN elements. The only way to
    // iterate over those elements is via
    // reflection
    //
    // Credit: dankogi at http://stackoverflow.com/questions/24299045/any-way-to-iterate-a-tuple-in-swift

    let d_namlen = dirent.memory.d_namlen
    let d_name = dirent.memory.d_name
    let mirror = reflect(d_name)
    for i in 0..<d_namlen {
    let (s, m) = mirror[Int(i)]
    name.append(m.value as Int8)
    }

    name.append(0)
    if let s = String.fromCString(name) {
    println(s)
    }

    dirent = readdir(dir)
    }
    closedir(dir)
    }