#!/usr/bin/env swift; /****************************************************************************** * This program simply copies one file to another, line by line. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation import Utils func copyTextFile(_ fromFilespec:String, _ toFilespec:String) throws -> Void { let ifh:StreamReader? = StreamReader(fromFilespec) FileManager.default.createFile(atPath: toFilespec, contents: nil) let ofh:FileHandle? = FileHandle(forWritingAtPath: toFilespec) if ifh == nil { throw RuntimeError("Problem opening input file!") } if ofh == nil { throw RuntimeError("Problem opening output file!") } var line:String? line = ifh!.getLine() while line != nil { ofh!.write(line!.data(using: .utf8)!);ofh!.write("\n".data(using: .utf8)!) line = ifh!.getLine() } ifh!.close() try ofh!.close() } // Begin Main if CommandLine.arguments.count != 3 { print("Syntax: " + CommandLine.arguments[0] + " {fromFilespec} {toFilespec}") exit(1) } var fromFilespec:String = CommandLine.arguments[1] var toFilespec:String = CommandLine.arguments[2] do { try copyTextFile(fromFilespec, toFilespec) } catch let ex as RuntimeError { print("Error: " + ex.localizedDescription) } exit(EXIT_SUCCESS)