-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstream.go
More file actions
162 lines (138 loc) · 3.29 KB
/
stream.go
File metadata and controls
162 lines (138 loc) · 3.29 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
package gonativeextractor
/*
#cgo CFLAGS: -I/usr/include/nativeextractor
#cgo LDFLAGS: -lnativeextractor -lglib-2.0 -ldl
#include <nativeextractor/common.h>
#include <nativeextractor/extractor.h>
#include <nativeextractor/stream.h>
bool extractor_c_add_miner_from_so(extractor_c * self, const char * miner_so_path, const char * miner_name, void * params );
const char * extractor_get_last_error(extractor_c * self);
void stream_c_destroy(stream_c * self);
*/
import "C"
import (
"fmt"
"io"
"os"
"unsafe"
)
/*
Interface for streams.
*/
type Streamer interface {
GetStream() *C.struct_stream_c
Check() bool
io.Closer
}
/*
Structure representing stream from file.
*/
type FileStream struct {
Ptr *C.struct_stream_file_c
Path string
}
/*
Gets the inner stream structure.
Returns:
- pointer to the C struct stream_c.
*/
func (ego *FileStream) GetStream() *C.struct_stream_c {
return &ego.Ptr.stream
}
/*
Checks if an error occurred in a FileStream.
Returns:
- true if an error occurred, false otherwise.
*/
func (ego *FileStream) Check() bool {
return ego.Ptr.stream.state_flags&C.STREAM_FAILED == 0
}
/*
Creates a new FileStream.
Parameters:
- path - path to a file.
Returns:
- pointer to a new instance of FileStream.
- error if any occurred, nil otherwise.
*/
func NewFileStream(path string) (*FileStream, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist")
}
out := FileStream{Path: path}
out.Ptr = C.stream_file_c_new(C.CString(path))
if !out.Check() {
return nil, fmt.Errorf("unable to create FileStream")
}
return &out, nil
}
/*
Closes a FileStream.
Returns:
- error if the stream has been already closed, nil otherwise.
*/
func (ego *FileStream) Close() error {
if ego.Ptr == nil {
return fmt.Errorf("fileStream has been already closed")
}
C.stream_c_destroy(&ego.Ptr.stream)
C.free(unsafe.Pointer(ego.Ptr))
ego.Ptr = nil
return nil
}
/*
Structure representing buffer over heap memory.
*/
type BufferStream struct {
Buffer []byte
Ptr *C.struct_stream_buffer_c
}
/*
Creates a new BufferStream.
Parameters:
- buffer - byte array for stream initialization (has to be terminated with \x00).
Returns:
- pointer to a new instance of BufferStream.
- error if any occurred, nil otherwise.
*/
func NewBufferStream(buffer []byte) (*BufferStream, error) {
if buffer == nil {
return nil, fmt.Errorf("nil buffer given")
}
out := BufferStream{Buffer: buffer}
out.Ptr = C.stream_buffer_c_new((*C.uchar)(&buffer[0]), C.ulong(len(buffer)))
if !out.Check() {
return nil, fmt.Errorf("unable to create BufferStream")
}
return &out, nil
}
/*
Gets the inner stream structure.
Returns:
- pointer to the C struct stream_c.
*/
func (ego *BufferStream) GetStream() *C.struct_stream_c {
return &ego.Ptr.stream
}
/*
Checks if an error occurred in a BufferStream.
Returns:
- true if an error occurred, false otherwise.
*/
func (ego *BufferStream) Check() bool {
return ego.Ptr.stream.state_flags&C.STREAM_FAILED == 0
}
/*
Closes a BufferStream.
Returns:
- error if the stream has been already closed, nil otherwise.
*/
func (ego *BufferStream) Close() error {
if ego.Ptr == nil {
return fmt.Errorf("bufferStream has been already closed")
}
C.stream_c_destroy(&ego.Ptr.stream)
C.free(unsafe.Pointer(ego.Ptr))
ego.Ptr = nil
return nil
}