summaryrefslogtreecommitdiff
path: root/Utils/FileReader.swift
blob: d4be92d0ff116dbad0b047b4e1d740abe8e12536 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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()
    }
    
}