-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathargument.go
340 lines (273 loc) · 9.83 KB
/
argument.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package osc
import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"github.com/pkg/errors"
)
// Argument represents an OSC argument.
// An OSC argument can have many different types, which is why
// we choose to represent them with an interface.
type Argument interface {
io.WriterTo
Bytes() []byte
Equal(Argument) bool
ReadInt32() (int32, error)
ReadFloat32() (float32, error)
ReadBool() (bool, error)
ReadString() (string, error)
ReadBlob() ([]byte, error)
String() string
Typetag() byte
}
// ReadArguments reads all arguments from the reader and adds it to the OSC message.
func ReadArguments(typetags, data []byte) ([]Argument, error) {
args := []Argument{}
// Strip off the prefix.
if len(typetags) > 0 && typetags[0] == TypetagPrefix {
typetags = typetags[1:]
}
for i, tt := range typetags {
arg, idx, err := ReadArgument(tt, data)
if err != nil {
return nil, errors.Wrapf(err, "read argument %d", i)
}
args = append(args, arg)
data = data[idx:]
}
return args, nil
}
// ReadArgument parses an OSC message argument given a type tag and some data.
func ReadArgument(tt byte, data []byte) (Argument, int64, error) {
switch tt {
case TypetagInt:
return ReadIntFrom(data)
case TypetagFloat:
return ReadFloatFrom(data)
case TypetagTrue:
return Bool(true), 0, nil
case TypetagFalse:
return Bool(false), 0, nil
case TypetagString:
s, idx := ReadString(data)
return String(s), idx, nil
case TypetagBlob:
return ReadBlobFrom(data)
default:
return nil, 0, errors.Wrapf(ErrInvalidTypeTag, "typetag %q", string(tt))
}
}
// Int represents a 32-bit integer.
type Int int32
// ReadIntFrom reads a 32-bit integer from a byte slice.
func ReadIntFrom(data []byte) (Argument, int64, error) {
var i Int
if err := binary.Read(bytes.NewReader(data), byteOrder, &i); err != nil {
return nil, 0, errors.Wrap(err, "read int argument")
}
return i, 4, nil
}
// Bytes converts the arg to a byte slice suitable for adding to the binary representation of an OSC message.
func (i Int) Bytes() []byte {
return []byte{
byte(int32(i) >> 24),
byte(int32(i) >> 16),
byte((int32(i) >> 8)),
byte(i),
}
}
// Equal returns true if the argument equals the other one, false otherwise.
func (i Int) Equal(other Argument) bool {
if other.Typetag() != TypetagInt {
return false
}
i2 := other.(Int)
return i == i2
}
// ReadInt32 reads a 32-bit integer from the arg.
func (i Int) ReadInt32() (int32, error) { return int32(i), nil }
// ReadFloat32 reads a 32-bit float from the arg.
func (i Int) ReadFloat32() (float32, error) { return 0, ErrInvalidTypeTag }
// ReadBool bool reads a boolean from the arg.
func (i Int) ReadBool() (bool, error) { return false, ErrInvalidTypeTag }
// ReadString string reads a string from the arg.
func (i Int) ReadString() (string, error) { return "", ErrInvalidTypeTag }
// ReadBlob reads a slice of bytes from the arg.
func (i Int) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
// String converts the arg to a string.
func (i Int) String() string { return fmt.Sprintf("Int(%d)", i) }
// Typetag returns the argument's type tag.
func (i Int) Typetag() byte { return TypetagInt }
// WriteTo writes the arg to an io.Writer.
func (i Int) WriteTo(w io.Writer) (int64, error) {
written, err := fmt.Fprintf(w, "%d", i)
return int64(written), err
}
// Float represents a 32-bit float.
type Float float32
// ReadFloatFrom reads a 32-bit float from a byte slice.
func ReadFloatFrom(data []byte) (Argument, int64, error) {
var f Float
if err := binary.Read(bytes.NewReader(data), byteOrder, &f); err != nil {
return nil, 0, errors.Wrap(err, "read float argument")
}
return f, 4, nil
}
// Bytes converts the arg to a byte slice suitable for adding to the binary representation of an OSC message.
func (f Float) Bytes() []byte {
var (
buf = &bytes.Buffer{}
_ = binary.Write(buf, byteOrder, float32(f)) // Never fails
)
return buf.Bytes()
}
// Equal returns true if the argument equals the other one, false otherwise.
func (f Float) Equal(other Argument) bool {
if other.Typetag() != TypetagFloat {
return false
}
f2 := other.(Float)
return f == f2
}
// ReadInt32 reads a 32-bit integer from the arg.
func (f Float) ReadInt32() (int32, error) { return 0, ErrInvalidTypeTag }
// ReadFloat32 reads a 32-bit float from the arg.
func (f Float) ReadFloat32() (float32, error) { return float32(f), nil }
// ReadBool bool reads a boolean from the arg.
func (f Float) ReadBool() (bool, error) { return false, ErrInvalidTypeTag }
// ReadString string reads a string from the arg.
func (f Float) ReadString() (string, error) { return "", ErrInvalidTypeTag }
// ReadBlob reads a slice of bytes from the arg.
func (f Float) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
// String converts the arg to a string.
func (f Float) String() string { return fmt.Sprintf("Float(%f)", f) }
// Typetag returns the argument's type tag.
func (f Float) Typetag() byte { return TypetagFloat }
// WriteTo writes the arg to an io.Writer.
func (f Float) WriteTo(w io.Writer) (int64, error) {
written, err := fmt.Fprintf(w, "%f", f)
return int64(written), err
}
// Bool represents a boolean value.
type Bool bool
// Bytes converts the arg to a byte slice suitable for adding to the binary representation of an OSC message.
func (b Bool) Bytes() []byte {
return []byte{}
}
// Equal returns true if the argument equals the other one, false otherwise.
func (b Bool) Equal(other Argument) bool {
if other.Typetag() != TypetagFalse && other.Typetag() != TypetagTrue {
return false
}
b2 := other.(Bool)
return b == b2
}
// ReadInt32 reads a 32-bit integer from the arg.
func (b Bool) ReadInt32() (int32, error) { return 0, ErrInvalidTypeTag }
// ReadFloat32 reads a 32-bit float from the arg.
func (b Bool) ReadFloat32() (float32, error) { return 0, ErrInvalidTypeTag }
// ReadBool bool reads a boolean from the arg.
func (b Bool) ReadBool() (bool, error) { return bool(b), nil }
// ReadString string reads a string from the arg.
func (b Bool) ReadString() (string, error) { return "", ErrInvalidTypeTag }
// ReadBlob reads a slice of bytes from the arg.
func (b Bool) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
// String converts the arg to a string.
func (b Bool) String() string { return fmt.Sprintf("Bool(%t)", b) }
// Typetag returns the argument's type tag.
func (b Bool) Typetag() byte {
if bool(b) {
return TypetagTrue
}
return TypetagFalse
}
// WriteTo writes the arg to an io.Writer.
func (b Bool) WriteTo(w io.Writer) (int64, error) {
written, err := fmt.Fprintf(w, "%t", b)
return int64(written), err
}
// String is a string.
type String string
// Bytes converts the arg to a byte slice suitable for adding to the binary representation of an OSC message.
func (s String) Bytes() []byte {
return ToBytes(string(s))
}
// Equal returns true if the argument equals the other one, false otherwise.
func (s String) Equal(other Argument) bool {
if other.Typetag() != TypetagString {
return false
}
s2 := other.(String)
return s == s2
}
// ReadInt32 reads a 32-bit integer from the arg.
func (s String) ReadInt32() (int32, error) { return 0, ErrInvalidTypeTag }
// ReadFloat32 reads a 32-bit float from the arg.
func (s String) ReadFloat32() (float32, error) { return 0, ErrInvalidTypeTag }
// ReadBool bool reads a boolean from the arg.
func (s String) ReadBool() (bool, error) { return false, ErrInvalidTypeTag }
// ReadString string reads a string from the arg.
func (s String) ReadString() (string, error) { return string(s), nil }
// ReadBlob reads a slice of bytes from the arg.
func (s String) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
// String converts the arg to a string.
func (s String) String() string { return string(s) }
// Typetag returns the argument's type tag.
func (s String) Typetag() byte { return TypetagString }
// WriteTo writes the arg to an io.Writer.
func (s String) WriteTo(w io.Writer) (int64, error) {
written, err := fmt.Fprintf(w, "%s", s)
return int64(written), err
}
// Blob is a slice of bytes.
type Blob []byte
// ReadBlobFrom reads a binary blob from the provided data.
func ReadBlobFrom(data []byte) (Argument, int64, error) {
var length int32
if err := binary.Read(bytes.NewReader(data), byteOrder, &length); err != nil {
return nil, 0, errors.Wrap(err, "read blob argument")
}
b, bl := ReadBlob(length, data[4:])
return Blob(b), bl + 4, nil
}
// Bytes converts the arg to a byte slice suitable for adding to the binary representation of an OSC message.
func (b Blob) Bytes() []byte {
return Pad(bytes.Join([][]byte{
Int(len(b)).Bytes(),
[]byte(b),
}, []byte{}))
}
// Equal returns true if the argument equals the other one, false otherwise.
func (b Blob) Equal(other Argument) bool {
if other.Typetag() != TypetagBlob {
return false
}
b2 := other.(Blob)
if len(b) != len(b2) {
return false
}
return bytes.Equal(b, b2)
}
// ReadInt32 reads a 32-bit integer from the arg.
func (b Blob) ReadInt32() (int32, error) { return 0, ErrInvalidTypeTag }
// ReadFloat32 reads a 32-bit float from the arg.
func (b Blob) ReadFloat32() (float32, error) { return 0, ErrInvalidTypeTag }
// ReadBool bool reads a boolean from the arg.
func (b Blob) ReadBool() (bool, error) { return false, ErrInvalidTypeTag }
// ReadString string reads a string from the arg.
func (b Blob) ReadString() (string, error) { return "", ErrInvalidTypeTag }
// ReadBlob reads a slice of bytes from the arg.
func (b Blob) ReadBlob() ([]byte, error) { return []byte(b), nil }
// String converts the arg to a string.
func (b Blob) String() string { return base64.StdEncoding.EncodeToString([]byte(b)) }
// Typetag returns the argument's type tag.
func (b Blob) Typetag() byte { return TypetagBlob }
// WriteTo writes the arg to an io.Writer.
func (b Blob) WriteTo(w io.Writer) (int64, error) {
written, err := w.Write([]byte(b))
return int64(written), err
}
// Arguments is a slice of Argument.
type Arguments []Argument