Sfoglia il codice sorgente

added reader and writer support to decoder and encoder

Keelan Lightfoot 7 anni fa
parent
commit
ced039185a
2 ha cambiato i file con 27 aggiunte e 0 eliminazioni
  1. 10
    0
      decoder.go
  2. 17
    0
      encoder.go

+ 10
- 0
decoder.go Vedi File

@@ -11,6 +11,10 @@
11 11
 
12 12
 package gortty
13 13
 
14
+import(
15
+	"unicode/utf8"
16
+)
17
+
14 18
 // Decoder converts a stream of bytes representing baudot codes into a stream
15 19
 // of unicode runes.
16 20
 type Decoder struct {
@@ -58,3 +62,9 @@ func (d *Decoder) run() {
58 62
 		}
59 63
 	}
60 64
 }
65
+
66
+func (d *Decoder) Read(p []byte) (n int, err error) {
67
+	r := <-d.output
68
+	l := utf8.EncodeRune(p, r)
69
+	return l, nil
70
+}

+ 17
- 0
encoder.go Vedi File

@@ -11,6 +11,10 @@
11 11
 
12 12
 package gortty
13 13
 
14
+import (
15
+	"fmt"
16
+	"bytes"
17
+)
14 18
 
15 19
 // Encoder is fun
16 20
 type Encoder struct {
@@ -166,3 +170,16 @@ func (e *Encoder) run() {
166 170
 	}
167 171
 
168 172
 }
173
+
174
+func (e *Encoder) Write(p []byte) (n int, err error) {
175
+
176
+	runes := bytes.Runes(p)
177
+
178
+	fmt.Print(string(runes))
179
+	
180
+	for _, r := range runes {
181
+		e.input <- r
182
+	}
183
+	return len(p), nil
184
+}
185
+