// // FileReader.swift // PrySDR // // Created by Jacky Jack on 06/01/2025. // import Foundation enum FileReaderError: Error { case noFile case readError case noData case seekError } class FileReader { var filepath: String? let filemgr = FileManager.default var fileHandle: FileHandle? init() { } func open(filename: String) throws { let dir = getCurrentExecutableDir() print(dir+"/"+filename) if !filemgr.fileExists(atPath: filename) { print("Cant find file \(filename)") throw FileReaderError.noFile } fileHandle = FileHandle(forReadingAtPath: dir+"/"+filename) if (fileHandle == nil) { print("FileHandler failed") } } func readBuffer(_ count: Int) -> [UInt8] { print("FileReader not implemented") return [] } func readBuffer(_ count: Int) -> [Int8] { print("FileReader not implemented") return [] } func readBuffer(_ count: Int) -> [UInt16] { print("FileReader not implemented") return [] } func readBuffer(_ count: Int) -> [Int16] { print("FileReader not implemented") return [] } func readBuffer(_ count: Int) -> [Float] { print("FileReader not implemented") return [] } func readAll() throws -> [UInt8] { var ret_arr:[UInt8]? = nil if let file = fileHandle { do { if let databuf = try file.readToEnd() { //print("read \(databuf.count) \(databuf) bytes") //let temp_arr = [Int8](databuf) ret_arr = [UInt8](repeating: 0, count: databuf.count) for i in 0..Int8 ret_arr![i] = databuf[i] } } } catch { print("ReadAll u8 error:\(error)") throw FileReaderError.readError } } if ret_arr == nil { return [] } else { return ret_arr! } } func readAll() throws -> [Int8] { var ret_arr:[Int8]? = nil if let file = fileHandle { do { if let databuf = try file.readToEnd() { //print("read \(databuf.count) \(databuf) bytes") //let temp_arr = [Int8](databuf) ret_arr = [Int8](repeating: 0, count: databuf.count) for i in 0..Int8 let val = databuf[i] if val <= 127 { ret_arr![i] = Int8(val)-127 } else { ret_arr![i] = Int8(val-128) } //print(String(format:"%02X", i8_arr![i]),terminator: "") } } } catch { print("ReadAll i8 error:\(error)") throw FileReaderError.readError } } if ret_arr == nil { return [] } else { return ret_arr! } } func readAll() throws -> [UInt16] { print("FileReader not implemented") return [] } func readAll() throws -> [Int16] { print("FileReader not implemented") return [] } func readAll() throws -> [Float] { print("FileReader not implemented") return [] } func seek(toFileOffset: UInt64) throws { if let file = self.fileHandle { file.seek(toFileOffset: toFileOffset) } } func close() { fileHandle?.closeFile() } }