#!/usr/bin/env swift; /****************************************************************************** * This program reads bytes from a file and prints them in decimal format. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import Foundation import Utils func readBinaryFile(_ filespec:String) throws -> Void { let ifh:FileHandle? = FileHandle(forReadingAtPath: filespec) if ifh == nil { throw RuntimeError("Problem opening input file!") } var c:UInt8? var count:Int = 0 c = Utils.getbyte(ifh!) while c != nil { print(String(c!) + " ", terminator: "") count += 1 if count % 20 == 0 { print() } c = Utils.getbyte(ifh!) } if count % 20 != 0 { print() } try ifh!.close() } // Begin Main if CommandLine.arguments.count != 2 { print("Syntax: " + CommandLine.arguments[0] + " {filename}") exit(1) } var filespec:String = CommandLine.arguments[1] do { try readBinaryFile(filespec) } catch let ex as RuntimeError { print("Error: " + ex.localizedDescription) } exit(EXIT_SUCCESS)