Diff

Differences From Artifact [a9b931eec8]:

To Artifact [a0aa5f4298]:


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
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



+



-





-
+


-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+

-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
-
-
+
-
-
-
+
+

-
-

package main

import (
	fib "0dev.org/encoding/fibonacci"
	mtf "0dev.org/encoding/mtf"
	iou "0dev.org/ioutil"
	"flag"
	"fmt"
	"io"
	"os"
)

func main() {
	d := flag.Bool("d", false, "Use to toggle decode mode")
	d := flag.Bool("d", false, "Toggle decode mode.")
	flag.Parse()

	var code int
	if *d {
		code = decode(os.Stdout, os.Stdin)
	} else {
	var (
		input  io.Reader = iou.SizedReader(os.Stdin, 4096)
		output io.Writer = iou.SizedWriter(os.Stdout, 4096)
		code   int
		code = encode(os.Stdout, os.Stdin)
	}

	os.Exit(code)
}
	)

	// Exit handler
	defer func() {
		os.Exit(code)
	}()

// Transforms the data according to the move-to-front algorithm
// I/O is buffered for better performance
func encode(output io.Writer, input io.Reader) int {
	var (
		err     error
		encoder io.Reader = mtf.Encoder(iou.SizedReader(input, 4096))
	)

	_, err = io.Copy(output, encoder)
	// Flush the output buffer
	defer output.Write(nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while encoding.\n", err)
		return 1
	}

	return 0
}


	if *d {
		input = mtf.Decoder(fib.Decoder(input))
	} else {
		input = mtf.Encoder(input)

		// Encode output as fibonacci integers
		output = fib.Encoder(output)
		defer output.Write(nil)
	}
// Reverses MTF`ed data and writes back the original bytes
// I/O is buffered for better performance
func decode(output io.Writer, input io.Reader) int {
	var (
		err     error
		decoder io.Reader = mtf.Decoder(iou.SizedReader(input, 4096))
	)


	_, err = io.Copy(output, decoder)
	if _, err := io.Copy(output, input); err != nil {
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decoding.\n", err)
		return 1
		os.Stderr.WriteString("Error while transforming data.\n" + err.Error())
		code = 1
	}

	return 0
}