Skip to content

Instantly share code, notes, and snippets.

@sonsongithub
Created February 3, 2017 08:01
Show Gist options
  • Select an option

  • Save sonsongithub/849b3f1d2851c6accd75e059ec4684b3 to your computer and use it in GitHub Desktop.

Select an option

Save sonsongithub/849b3f1d2851c6accd75e059ec4684b3 to your computer and use it in GitHub Desktop.
func read(handle: UnsafeMutablePointer<FILE>) throws -> Int {
var buffer: [Int] = [Int](repeating: 0, count: 1)
guard fread(&buffer, Int(MemoryLayout<Int>.size), 1, handle) == 1 else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
return buffer[0]
}
func write(value: Int, handle: UnsafeMutablePointer<FILE>) throws {
do {
guard fwrite([value], MemoryLayout<Int>.size, 1, handle) == 1 else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
} catch {
throw error
}
}
func read(handle: UnsafeMutablePointer<FILE>) throws -> String {
do {
let length: Int = try read(handle: handle)
let buffer = UnsafeMutablePointer<unichar>.allocate(capacity: length)
defer { buffer.deallocate(capacity: length) }
guard fread(buffer, MemoryLayout<unichar>.size, length, handle) == length else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
guard let string = String(bytesNoCopy: buffer, length: MemoryLayout<unichar>.size * length, encoding: String.Encoding.utf16LittleEndian, freeWhenDone: false) else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "Failed to decode binary data to String."])
}
return string
} catch {
throw error
}
}
func write(value: String, handle: UnsafeMutablePointer<FILE>) throws {
do {
let length = (value as NSString).length
try write(value: length, handle: handle)
let buffer = UnsafeMutablePointer<unichar>.allocate(capacity: length)
defer { buffer.deallocate(capacity: length) }
(value as NSString).getCharacters(buffer)
guard fwrite(buffer, MemoryLayout<unichar>.size, length, handle) == length else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
} catch {
throw error
}
}
func readWithStringData(handle: UnsafeMutablePointer<FILE>) throws -> String {
do {
let length: Int = try read(handle: handle)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
defer { buffer.deallocate(capacity: length) }
guard fread(buffer, MemoryLayout<UInt8>.size, length, handle) == length else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
guard let string = String(bytesNoCopy: buffer, length: length, encoding: .utf8, freeWhenDone: false) else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "Failed to decode binary data to String."])
}
return string
} catch {
throw error
}
}
func writeWithStringData(value: String, handle: UnsafeMutablePointer<FILE>) throws {
do {
guard let data = value.data(using: String.Encoding.utf8) else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "Can not decode string to binary."])
}
let length = Int((data as NSData).length)
try write(value: length, handle: handle)
guard fwrite((data as NSData).bytes, Int(1), length, handle) == length else {
throw NSError(domain: "com.sonson.2tch", code: 0, userInfo: ["message": "An error occurred during writing to the handle (errno: \(errno))"])
}
} catch {
throw error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment