Last active
November 29, 2023 12:32
-
-
Save kristopherjohnson/4bafee0b489add42a61f to your computer and use it in GitHub Desktop.
Revisions
-
kristopherjohnson revised this gist
Dec 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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 C string. The // only way to iterate over tuple elements // in Swift is via reflection. // -
kristopherjohnson revised this gist
Dec 31, 2014 . 1 changed file with 12 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal 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 { 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 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) -
kristopherjohnson revised this gist
Dec 31, 2014 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal 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 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 = 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 for display name.append(0) if let s = String.fromCString(name) { println("- \(s)") } entry = readdir(dir) } closedir(dir) } -
kristopherjohnson revised this gist
Dec 31, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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 name.append(0) if let s = String.fromCString(name) { println(s) -
kristopherjohnson revised this gist
Dec 31, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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) -
kristopherjohnson created this gist
Dec 31, 2014 .There are no files selected for viewing
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 charactersOriginal 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) }