// // ViewController.swift // Converter // // Created by Kilian Hofmann on 20.06.17. // Copyright © 2017 Kilian Hofmann. All rights reserved. // import Cocoa class ViewController: NSViewController { @IBOutlet var months: NSTextField! @IBOutlet var days: NSTextField! @IBOutlet var files: NSTextField! @IBOutlet var progress: NSTextField! @IBOutlet var convert: NSButton! @IBOutlet var inPath: NSTextField! var fileWrappers: [String : [String : FileWrapper]]! var filesDone: Int = 0 var filesTotal: Int = 0 override func viewDidLoad() { super.viewDidLoad() title = "" // Do any additional setup after loading the view. } @IBAction func convert(sender: NSButton) { convert.isEnabled = false (NSApp.delegate as! AppDelegate).isConverting = true let opQueue: OperationQueue = OperationQueue() let fileQueue: OperationQueue = OperationQueue() fileQueue.maxConcurrentOperationCount = 4 let myActivity = ProcessInfo.processInfo.beginActivity(options: .userInitiated, reason: "Converting") opQueue.addOperation { defer { fileQueue.waitUntilAllOperationsAreFinished() ProcessInfo.processInfo.endActivity(myActivity) OperationQueue.main.addOperation { self.convert.isEnabled = true (NSApp.delegate as! AppDelegate).isConverting = false } } let inP: String = self.inPath.stringValue var days: Int = 0 do { let directory = try FileWrapper(url: URL(fileURLWithPath: inP), options: .immediate) for months in directory.fileWrappers! { guard months.value.isDirectory else { continue } days += (months.value.fileWrappers?.count)! if (months.value.fileWrappers?.contains(where: {$0.0 == ".DS_Store" }))! { days -= 1 } for days in months.value.fileWrappers! { guard days.value.isDirectory else { continue } self.filesTotal += (days.value.fileWrappers?.count)! if (days.value.fileWrappers?.contains(where: {$0.0 == ".DS_Store" }))! { self.filesTotal -= 1 } if (days.value.fileWrappers?.contains(where: {$0.0 == "Content.plist" }))! { self.filesTotal -= 1 } } } OperationQueue.main.addOperation { self.months.stringValue = "\(directory.fileWrappers!.count)" self.days.stringValue = "\(days)" self.files.stringValue = "\(self.filesTotal)" self.progress.stringValue = "File \(self.filesDone) of \(self.filesTotal)" } } catch _ { OperationQueue.main.addOperation { self.convert.isEnabled = true (NSApp.delegate as! AppDelegate).isConverting = false } } for month in 1...12 { for day in 1...31 { let path: String = inP + String(format: "/%02d", month) + String(format: "/%02d", day) do { let directory = try FileWrapper(url: URL(fileURLWithPath: path), options: .immediate) for file in directory.fileWrappers! { if file.key == "KDLog.txt" { let pathOut = path.appending("/" + file.key.replacingOccurrences(of: ".txt", with: ".hex")).replacingOccurrences(of: ".docsisplist2", with: "C.docsisplist2") //print(path.appending(file.key) + " to " + pathOut) self.makeSubDir(path: pathOut.replacingOccurrences(of: file.key.replacingOccurrences(of: ".txt", with: ".hex"), with: "")) FileOperations.convertLoss(log: path.appending("/" + file.key), toFile: pathOut) OperationQueue.main.addOperation { self.filesDone += 1 self.progress.stringValue = "File \(self.filesDone) of \(self.filesTotal)" } } else if file.key.contains("Upstream") { let pathOut = path.appending("/" + file.key.replacingOccurrences(of: ".csv", with: ".hex")).replacingOccurrences(of: ".docsisplist2", with: "C.docsisplist2") //print(path.appending(file.key) + " to " + pathOut) self.makeSubDir(path: pathOut.replacingOccurrences(of: file.key.replacingOccurrences(of: ".csv", with: ".hex"), with: "")) FileOperations.convertUpstream(log: path.appending("/" + file.key), toFile: pathOut) OperationQueue.main.addOperation { self.filesDone += 1 self.progress.stringValue = "File \(self.filesDone) of \(self.filesTotal)" } } else if file.key.contains("Downstream") { let pathOut = path.appending("/" + file.key.replacingOccurrences(of: ".csv", with: ".hex")).replacingOccurrences(of: ".docsisplist2", with: "C.docsisplist2") //print(path.appending(file.key) + " to " + pathOut) self.makeSubDir(path: pathOut.replacingOccurrences(of: file.key.replacingOccurrences(of: ".csv", with: ".hex"), with: "")) FileOperations.convertDownstream(log: path.appending("/" + file.key), toFile: pathOut) OperationQueue.main.addOperation { self.filesDone += 1 self.progress.stringValue = "File \(self.filesDone) of \(self.filesTotal)" } } } } catch _ { continue } } } } } func makeSubDir(path: String) { // Check if directory exists, create or exit let fileManager = FileManager.default var directory: ObjCBool = ObjCBool(false) let exists: Bool = fileManager.fileExists(atPath: (path as NSString).expandingTildeInPath, isDirectory: &directory) if exists && directory.boolValue { } else if exists { NSLog("FILE WITH NAME KDLog EXISTS, REMOVE IT") exit(1); } else { do { try fileManager.createDirectory(atPath: (path as NSString).expandingTildeInPath, withIntermediateDirectories: true, attributes: nil) } catch let error as NSError { NSLog("ERROR ON DIRECTORY CREATION: \(error.localizedDescription)") exit(1); } } } }