Check-in [e72b637df4]
Overview
SHA1:e72b637df4da3b4267e03d12fedcf6a0d31efe8c
Date: 2014-12-26 16:02:18
User: spaskalev
Comment:Initial implementation of commands/mtf (based on commands/pdc)
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-12-26
21:35
[129d90b4a8] Added 0dev.org/types, providing aliases that implement sort.Interface for [u]int{8|16|32|64} (user: spaskalev, tags: trunk)
16:02
[e72b637df4] Initial implementation of commands/mtf (based on commands/pdc) (user: spaskalev, tags: trunk)
15:50
[ad5c25d9ab] Initial implementation of package mtf - a move-to-front transform. (user: spaskalev, tags: trunk)
Changes

Added src/0dev.org/commands/mtf/main.go version [b672c1daee].

            1  +package main
            2  +
            3  +import (
            4  +	iou "0dev.org/ioutil"
            5  +	mtf "0dev.org/mtf"
            6  +	"fmt"
            7  +	"io"
            8  +	"os"
            9  +)
           10  +
           11  +func main() {
           12  +	var code int
           13  +	switch {
           14  +	case len(os.Args) == 1:
           15  +		code = transform(os.Stdout, os.Stdin)
           16  +	case len(os.Args) == 2 && os.Args[1] == "-d":
           17  +		code = reverse(os.Stdout, os.Stdin)
           18  +	default:
           19  +		fmt.Fprintln(os.Stdout, "Usage: mtf [-d]")
           20  +	}
           21  +	os.Exit(code)
           22  +}
           23  +
           24  +// Transforms the data according to the move-to-front algorithm
           25  +// I/O is buffered for better performance
           26  +func transform(output io.Writer, input io.Reader) int {
           27  +	var (
           28  +		err     error
           29  +		buffer  io.Writer = iou.SizedWriter(output, 4096)
           30  +		encoder io.Writer = mtf.Encoder(buffer)
           31  +	)
           32  +
           33  +	_, err = io.Copy(encoder, input)
           34  +	if err != nil {
           35  +		fmt.Fprintln(os.Stderr, "Error while encoding.\n", err)
           36  +		return 1
           37  +	}
           38  +
           39  +	// Flush the buffer
           40  +	_, err = buffer.Write(nil)
           41  +	if err != nil {
           42  +		fmt.Fprintln(os.Stderr, "Error while flushing output buffer.\n", err)
           43  +		return 1
           44  +	}
           45  +
           46  +	return 0
           47  +}
           48  +
           49  +// Reverses MTF`ed data and writes back the original bytes
           50  +// I/O is buffered for better performance
           51  +func reverse(output io.Writer, input io.Reader) int {
           52  +	var (
           53  +		err     error
           54  +		decoder io.Reader = mtf.Decoder(iou.SizedReader(input, 4096))
           55  +	)
           56  +
           57  +	_, err = io.Copy(output, decoder)
           58  +	if err != nil {
           59  +		fmt.Fprintln(os.Stderr, "Error while decoding.\n", err)
           60  +		return 1
           61  +	}
           62  +
           63  +	return 0
           64  +}