Skip to content

Instantly share code, notes, and snippets.

View MatiMax's full-sized avatar
🏠
Working from home

Matthias M. Schneider MatiMax

🏠
Working from home
View GitHub Profile
@MatiMax
MatiMax / swift_repair_perms.fish
Created July 27, 2017 12:15
This fish-shell script modifies the permissions of the C-header files used by Swift to receive group and world read access.
#!/usr/bin/fish
set SWIFT_PATH /opt
sudo find $SWIFT_PATH/swift/ -name "*.h" ! -perm -go=r -exec chmod go+r "{}" \;
@MatiMax
MatiMax / ANSITerminalColour.swift
Created July 25, 2017 12:14
Using ANSI terminal colours — The Swift-way
#!/usr/bin/swift
/*
* File: ANSITerminalColour.swift
* Language: Apple Swift 3 or 4
* Author: Matthias M. Schneider
* Purpose: Demonstrate how to use an `enum` to use the ANSI colour control characters in String folding for console output.
* Version: 1.0
* Copyright: IDC (I don't care)
*/
@MatiMax
MatiMax / switch_swift.fish
Last active July 27, 2017 12:08
This fish-shell script detects and switches the Swift version. It has been tested with fish-shell version 2.2.0 under Ubuntu Linux 16.04 LTS.
#!/usr/bin/fish
set SWIFT_PATH /opt
set swift_versions (sudo find $SWIFT_PATH -maxdepth 1 -name "swift[0-9][0-9][0-9]" -type d -printf "%f\n" | sort)
set swift_active_version (stat $SWIFT_PATH/swift -c "%N" | sed -r "s/.+(swift[0-9][0-9][0-9]).+/\1/g")
echo -e "\n\e[44;1mI found these Swift versions:\e[0m"
set i 1
for v in $swift_versions
@MatiMax
MatiMax / DNSResolve3.swift
Last active October 9, 2021 09:44
This Playground shows how to use Core Foundation's CFHost in *Swift 3* to try to produce an array of Strings containing the host name and all its aliases for a given IP address. Unfortunately, CFHostGetNames() does not supply the aliases of a host, so we're left alone with the gethostbyaddr() C function.
//: # How to retrieve a host name and associated aliases from an IP address using Core Fondation's `CFHost` in Swift 3
import Foundation
import PlaygroundSupport
//: In order to get the callback working we use a simple class to implement the showcase.
class DNSResolve {
//: The IP address may be a Swift `String` thanks to the toll-free bridging to C strings.
let ip: String = "17.172.224.47"
//: We use an optional `CFHost` variable because CFHost neither comes with an initializer nor is conforming to the Nullable protocol.
var host: CFHost?
//: We use this array of `String`s to store the resolved host names.
@MatiMax
MatiMax / IPAddressHostResolution3.swift
Last active September 13, 2016 11:04
This Swift Playground shows how to use the legacy gethostbyaddr() in *Swift 3* under Linux to produce an array of Strings containing the host name and all its aliases for a given IP address.
//: # How to retrieve a host name and associated aliases from an IP address
//: Rename the file extension to `playground` and run it directly in Xcode 8 with Swift 3.
import SwiftGlibc
//: ## Using the C `struct`s in Swift
//: We can safely use a Swift `String` for storing the IP address in character format as Swift supports toll-fee bridging to C strings.
// let ip = "17.172.224.47" // Apple
let ip = "104.86.147.173" // Akamai Technologies
//: In order to use the `hostent` C structure as a reference value (pointer) we have to declare it as an `UnsafeMutablePointer` of the corresponding type.
//: We use `let` as the value itself will never change, but only the reference to the value. As such, the value (not the reference, or pointer for that matter) will be a constant.
let he: UnsafeMutablePointer<hostent>?
@MatiMax
MatiMax / UnderstandingTheOptionalImplementation.swift
Last active September 11, 2015 09:32
Understanding the implementaition of Swift Optionals. Save the file and rename the extension to "playground" to run it in Xcode's Playground.
/*:
Swift `Optional`s, as noted in Apple's documentation, are implemented as an enumeration with two cases:
* None
* Some
The question is how to declare a type-agnostic enumeration. The answer is simple: By using a generic.
*/
enum Opt<T>: CustomStringConvertible, NilLiteralConvertible {
case None
case Some(T)
@MatiMax
MatiMax / DNSResolve.swift
Last active November 14, 2016 05:48
This Swift Playground shows how to use Core Foundation's CFHost in Swift 2 to try to produce an array of Strings containing the host name and all its aliases for a given IP address. Unfortunately, CFHostGetNames() does not supply the aliases of a host, so we're left alone with the gethostbyaddr() C function.
//: # How to retrieve a host name and associated aliases from an IP address using Core Fondation's `CFHost`
import Cocoa
import XCPlayground
//: In order to get the callback working we use a simple class to implement the showcase.
class DNSResolve {
//: The IP address may be a Swift `String` thanks to the toll-free bridging to C strings.
let ip: String = "17.172.224.47"
//: We use an optional `CFHost` variable because CFHost neither comes with an initializer nor is conforming to the Nullable protocol.
var host: CFHost?
//: We use this array of `String`s to store the resolved host names.
@MatiMax
MatiMax / IPAddressHostResolution.swift
Created September 7, 2015 17:51
This Swift Playground shows how to use the legacy gethostbyaddr() in Swift 2 to produce an array of Strings containing the host name and all its aliases for a given IP address.
//: # How to retrieve a host name and associated aliases from an IP address
//: Rename the file extension to `playground` and run it directly in Xcode 7 with Swift 2.
import Cocoa
//: ## Using the C `struct`s in Swift
//: We can safely use a Swift `String` for storing the IP address in charachter format as Swift supports toll-fee bridging to C strings.
let ip = "17.172.224.47"
//: In order to use the `hostent` C structure as a reference value (pointer) we have to declare it as an `UnsafeMutablePointer` of the corresponding type.
//: We use `let` as the value itself will never change, but only the reference to the value. As such, the value (not the reference, or pointer for that matter) will be a constant.
let he: UnsafeMutablePointer<hostent>
//: We can declare another constant of type `hostent` which will be type-compatible with the memory location of `he`'s pointer to the C structure.