// // 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() // Do any additional setup after loading the view. } @IBAction func convert(sender: NSButton) { convert.isEnabled = false (NSApp.delegate as! AppDelegate).isConverting = true progress.stringValue = "File \(self.filesDone) of \(self.filesTotal)" 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 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); } } } }