DOCSIS-Toolkit/FileOperations.swift

52 lines
1.9 KiB
Swift

//
// FileOperations.swift
// Docsis Toolkit
//
// Created by Kilian Hofmann on 19.06.17.
// Copyright © 2017 Kilian Hofmann. All rights reserved.
//
import Cocoa
class FileOperations: NSObject {
static func log(data: String, toLog path: String) {
let seperated: Array<String> = data.components(separatedBy: ":")
let hours: UInt32 = UInt32(seperated[0])!
let minutes: UInt32 = UInt32(seperated[1])!
let seconds: UInt32 = UInt32(seperated[2])!
var dataToWrite: UInt32 = 0
dataToWrite = ((((dataToWrite | hours) << 6) | minutes) << 8) | seconds
let file: UnsafeMutableRawPointer = UnsafeMutableRawPointer.allocate(bytes: 3, alignedTo: 1)
file.storeBytes(of: dataToWrite, as: UInt32.self)
do {
let fileData: NSMutableData = try NSMutableData(contentsOf: URL(fileURLWithPath: path))
fileData.append(file, length: 3)
fileData.write(to: URL(fileURLWithPath: path), atomically: true)
} catch _ {
let fileData: NSMutableData = NSMutableData()
fileData.append(file, length: 3)
fileData.write(to: URL(fileURLWithPath: path), atomically: true)
}
}
static func load(log Data: NSData) -> String? {
var returnString: String? = ""
var i: Int = 0
while i < Data.length - 3 {
let data: UnsafeMutableRawPointer = UnsafeMutableRawPointer.allocate(bytes: 3, alignedTo: 1)
Data.getBytes(data, range: NSRange(location: i, length: 3))
let compound: UInt32 = data.load(as: UInt32.self)
let seconds: UInt32 = (compound) & 0b111111
let minutes: UInt32 = (compound >> 8) & 0b111111
let hours: UInt32 = (compound >> 14) & 0b11111
returnString?.append("\(hours):\(minutes):\(seconds)\n")
i += 3
}
if returnString != "" {
return returnString
}
return nil
}
}