blob: 633034230c65b4ead4df39be51e7ec6d26632758 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()
}
}
|