summaryrefslogtreecommitdiff
path: root/Utils/FileWriter.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Utils/FileWriter.swift')
-rw-r--r--Utils/FileWriter.swift112
1 files changed, 107 insertions, 5 deletions
diff --git a/Utils/FileWriter.swift b/Utils/FileWriter.swift
index 6330342..e4c3b7a 100644
--- a/Utils/FileWriter.swift
+++ b/Utils/FileWriter.swift
@@ -28,7 +28,7 @@ class FileWriter {
print(dir+"/"+filename)
if !filemgr.fileExists(atPath: filename) {
print("Cant find file \(filename)")
- throw FileReaderError.noFile
+ throw FileWriterError.noFile
}
fileHandle = FileHandle(forReadingAtPath: dir+"/"+filename)
@@ -73,11 +73,113 @@ class FileWriter {
}
func writeAll(_ arr: [Float32]) throws {
- //convert float to data
- let databuf = arr.withUnsafeBytes { Data($0) }
+
+ var byteArray = [UInt8](repeating: 0, count: 0)
+ var b1:Data = Data()
+
+ byteArray.append(contentsOf: [0x11,0x22,0x33,0x44])
+
+ arr.withUnsafeBytes {
+ byteArray.append(contentsOf: $0)
+ }
+
+ for i in 0...arr.count {
+ b1.append(contentsOf: [byteArray[i*4+0],byteArray[i*4+1],byteArray[i*4+2],byteArray[i*4+3]])
+ }
+
+ do {
+ print("try to write \(b1.count) bytes")
+ try fileHandle?.write(contentsOf: b1)
+ } catch {
+ print("Cant write to file \(self.filepath) : \(error)")
+ throw FileWriterError.writeError
+ }
+ }
+
+ func writeAll(_ arr: [Int8]) throws {
+ var byteArray = [UInt8](repeating: 0, count: 0)
+ var b1:Data = Data()
+
+ arr.withUnsafeBytes {
+ byteArray.append(contentsOf: $0)
+ }
+
+ for i in 0..<arr.count {
+ b1.append(contentsOf: [byteArray[i]])
+ }
+
+ do {
+ print("try to write \(b1.count) bytes")
+ try fileHandle?.write(contentsOf: b1)
+ } catch {
+ print("Cant write to file \(self.filepath) : \(error)")
+ throw FileWriterError.writeError
+ }
+ }
+
+ func writeAll(_ arr: [UInt8]) throws {
+ //var byteArray = [UInt8](repeating: 0, count: 0)
+ var b1:Data = Data()
+
+ //arr.withUnsafeBytes {
+ // byteArray.append(contentsOf: $0)
+ //}
+
+ for i in 0..<arr.count {
+ b1.append(contentsOf: [arr[i]])
+ }
+
+ do {
+ print("try to write \(b1.count) bytes")
+ try fileHandle?.write(contentsOf: b1)
+ } catch {
+ print("Cant write to file \(self.filepath) : \(error)")
+ throw FileWriterError.writeError
+ }
+ }
+
+ func writeAll(_ arr: [Int16]) throws {
+
+ var byteArray = [UInt8](repeating: 0, count: 0)
+ var b1:Data = Data()
+
+ //byteArray.append(contentsOf: [0x11,0x22])
+
+ arr.withUnsafeBytes {
+ byteArray.append(contentsOf: $0)
+ }
+
+ for i in 0..<arr.count {
+ b1.append(contentsOf: [byteArray[i*2+0],byteArray[i*2+1]])
+ }
+
+ do {
+ print("try to write \(b1.count) bytes")
+ try fileHandle?.write(contentsOf: b1)
+ } catch {
+ print("Cant write to file \(self.filepath) : \(error)")
+ throw FileWriterError.writeError
+ }
+ }
+
+ func writeAll(_ arr: [UInt16]) throws {
+
+ var byteArray = [UInt8](repeating: 0, count: 0)
+ var b1:Data = Data()
+
+ //byteArray.append(contentsOf: [0x11,0x22])
+
+ arr.withUnsafeBytes {
+ byteArray.append(contentsOf: $0)
+ }
+
+ for i in 0..<arr.count {
+ b1.append(contentsOf: [byteArray[i*2+0],byteArray[i*2+1]])
+ }
+
do {
- print("try to write \(databuf.count) bytes")
- try fileHandle?.write(contentsOf: databuf)
+ print("try to write \(b1.count) bytes")
+ try fileHandle?.write(contentsOf: b1)
} catch {
print("Cant write to file \(self.filepath) : \(error)")
throw FileWriterError.writeError