-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
86 lines (78 loc) · 1.53 KB
/
buffer.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
/*
Copyright © 2024 Martin Marsh [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
*/
package nmea_mux
import (
"fmt"
)
type circular_byte_buffer struct {
end int
buffer []byte
ret_buffer []byte
read_pos int
write_pos int
count int
cr_count int
ret_size int
}
func MakeByteBuffer(size int, ret_size int) *circular_byte_buffer {
p := circular_byte_buffer{
end: size - 1,
buffer: make([]byte, size),
ret_buffer: make([]byte, ret_size),
read_pos: 0,
write_pos: 0,
count: 0,
cr_count: 0,
ret_size: ret_size,
}
return &p
}
func (cb *circular_byte_buffer) Write_byte(b byte) {
if cb.write_pos >= cb.end {
cb.write_pos = 0
}
cb.buffer[cb.write_pos] = b
cb.write_pos++
cb.count++
if b == 13 {
cb.cr_count++
}
}
func (cb *circular_byte_buffer) ReadString() (string, error) {
var err error = nil
if cb.cr_count > 0 {
i := 0
for {
b, _ := cb.Read_byte()
if b != 13 {
cb.ret_buffer[i] = b
} else {
return string(cb.ret_buffer[:i]), err
}
i++
if i >= cb.ret_size {
err := fmt.Errorf(fmt.Sprintf("No CR in string corrupt o/p = %s\n", string(cb.ret_buffer[:i])))
return string(cb.ret_buffer[:i]), err
}
}
} else {
return "", err
}
}
func (cb *circular_byte_buffer) Read_byte() (byte, error) {
if cb.count == 0 {
return 0, fmt.Errorf("Empty")
}
if cb.read_pos >= cb.end {
cb.read_pos = 0
}
b := cb.buffer[cb.read_pos]
cb.read_pos++
cb.count--
if b == 13 {
cb.cr_count--
}
return b, nil
}