diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2025-02-03 22:31:45 +0000 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2025-02-03 22:31:45 +0000 |
commit | 972a6d3e4d3b684fbadeb5cd046a8634ec24eb8c (patch) | |
tree | e96c4ce2d649558cc0b7b452d7a64fbe980b65c0 /Utils/FileWriter.swift | |
parent | 1e096e55ca30dc80c3faa8d1ea36de13bc90cc6a (diff) | |
download | PrySDR-main.tar.gz PrySDR-main.zip |
iqconvert: converts from u8 to f32 without issuesmain
Diffstat (limited to 'Utils/FileWriter.swift')
-rw-r--r-- | Utils/FileWriter.swift | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/Utils/FileWriter.swift b/Utils/FileWriter.swift new file mode 100644 index 0000000..6330342 --- /dev/null +++ b/Utils/FileWriter.swift @@ -0,0 +1,91 @@ +// +// FileWriter.swift +// PrySDR +// +// Created by Jacky Jack on 21/01/2025. +// + +import Foundation + +enum FileWriterError: Error { + case noFile + case writeError + case createError +} + +class FileWriter { + var filepath: String? + let filemgr = FileManager.default + var fileHandle: FileHandle? + + init() { + + } + + func openForRead(filename: String) throws { + let dir = getCurrentExecutableDir() + self.filepath = dir+"/"+filename + 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 createForWrite(filename: String) throws { + let dir = getCurrentExecutableDir() + + self.filepath = dir+"/"+filename + + if !filemgr.fileExists(atPath: filename) { + filemgr.createFile(atPath: dir+"/"+filename, contents: nil) + } + fileHandle = FileHandle(forUpdatingAtPath: dir+"/"+filename) + if (fileHandle == nil) { + print("FileHandler failed") + throw FileWriterError.createError + } + } + + func deleteForWrite(filename: String) throws { + let dir = getCurrentExecutableDir() + + self.filepath = dir+"/"+filename + + if filemgr.fileExists(atPath: filename) { + do { + try filemgr.removeItem(atPath: filename) + filemgr.createFile(atPath: dir+"/"+filename, contents: nil) + } catch { + print("couldnt delete file to write: \(error)") + } + } + fileHandle = FileHandle(forWritingAtPath: dir+"/"+filename) + if (fileHandle == nil) { + print("FileHandler failed") + throw FileWriterError.createError + } + } + + func writeAll(_ arr: [Float32]) throws { + //convert float to data + let databuf = arr.withUnsafeBytes { Data($0) } + do { + print("try to write \(databuf.count) bytes") + try fileHandle?.write(contentsOf: databuf) + } catch { + print("Cant write to file \(self.filepath) : \(error)") + throw FileWriterError.writeError + } + } + + func close() { + fileHandle?.closeFile() + } + +} |