diff options
Diffstat (limited to 'Utils')
-rw-r--r-- | Utils/FileReader.swift | 150 | ||||
-rw-r--r-- | Utils/PathUtils.swift | 1 |
2 files changed, 151 insertions, 0 deletions
diff --git a/Utils/FileReader.swift b/Utils/FileReader.swift new file mode 100644 index 0000000..d4be92d --- /dev/null +++ b/Utils/FileReader.swift @@ -0,0 +1,150 @@ +// +// 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..<ret_arr!.count { + //print(String(format:"%02X", databuf[i]),terminator: "") + //convert from 0..255 to -127..128, from UInt8->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..<ret_arr!.count { + //print(String(format:"%02X", databuf[i]),terminator: "") + //convert from 0..255 to -127..128, from UInt8->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() + } + +} diff --git a/Utils/PathUtils.swift b/Utils/PathUtils.swift index a82261a..164baa1 100644 --- a/Utils/PathUtils.swift +++ b/Utils/PathUtils.swift @@ -10,4 +10,5 @@ import Foundation //get current run directory func getCurrentExecutableDir() -> String { return Process().currentDirectoryPath + //return FileManager.default.currentDirectoryPath } |