// // 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 readAllUInt8() 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 readAllInt8() 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! } } //https://forums.swift.org/t/swift-5-read-binary-file-problem-with-unsafe-mutable-pointers/31048/6 func readAllInt16() throws -> [Int16] { var ret_arr:[Int16]? = nil if let file = fileHandle { do { if let databuf = try file.readToEnd() { ret_arr = [Int16](asInt16(databuf)) } print(ret_arr![0],ret_arr![1],ret_arr![2],ret_arr![3]) } catch { print("ReadAll i8 error:\(error)") throw FileReaderError.readError } } if ret_arr == nil { return [] } else { return ret_arr! } } func readAllUInt16() throws -> [UInt16] { var ret_arr:[UInt16]? = nil if let file = fileHandle { do { if let databuf = try file.readToEnd() { ret_arr = [UInt16](asUInt16(databuf)) } } catch { print("ReadAll i8 error:\(error)") throw FileReaderError.readError } } if ret_arr == nil { return [] } else { return ret_arr! } } 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() } }