cpconv

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2020 License: MIT Imports: 4 Imported by: 0

README

cpconv

A codepage converter. Has a Go API as well as a command line tool. Use it to convert streams from one codepage into another.



Installation

Go get it with

go get github.com/tsatke/cpconv/... 

or just import it with

import "github.com/tsatke/cpconv"

and see a usage example below

Usage (as command line tool)

Usage:
  cpconv [flags]

Examples:
cat myFile.txt | cpconv --from IBM037 --to CP1252

Flags:
      --from string   --from IBM037
  -h, --help          help for cpconv
      --to string     --to CP1252
      --version       version for cpconv

This tool does not read from files, it just reads from stdin and writes to stdout. Pipes are your best friends with this tool, as you can see in the example.

Supported codepages

Codepage supported aliases
IBM037 IBM-037
CP1252 CP-1252

Usage (with the Go API)

Please note that there are no restrictions on what codepages are supported when using the Go API. This is because we are not limited to strings here. As long as you can provide an implementation, the conversion will work.

Working example:

package main

import (
    "os"

    "github.com/tsatke/cpconv"
    "golang.org/x/text/encoding/charmap"
)

func main() {
    // this will convert EBCDIC input on stdin to CP1252 output on stdout 
    if err := cpconv.Convert(os.Stdin, charmap.CodePage037, os.Stdout, charmap.Windows1252); err != nil {
        panic(err)
    }
}

Please find other examples in the examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(from io.Reader, sourceCodePage Codepage, to io.Writer, targetCodePage Codepage) error

Convert converts the given reader and writes the result to the given writer. The given codepages are used for decoding and encoding. The buffer size used for this is 1024. If you want to specify a different buffer size, use a Converter.

Types

type Codepage

type Codepage interface {
	NewDecoder() *encoding.Decoder
	NewEncoder() *encoding.Encoder
}

Codepage is a component that deterministically provides an encoder and decoder. It describes the concept of being able to interpret and encode characters from and to bytes (codepage, charmap, charset etc.).

type Converter

type Converter struct {
	// FromCodepage is the codepage that is used to decode the reader.
	FromCodepage Codepage
	// ToCodepage is the codepage that will be used to encode decoded bytes from the reader.
	// Bytes of this codepage will be written to the writer.
	ToCodepage Codepage
	// BufferSize is the size of the buffer that will be used for conversion.
	BufferSize int
}

Converter is an object that can copy a reader to a writer while performing a codepage conversion. See Convert for more info.

func (Converter) Convert

func (c Converter) Convert(from io.Reader, to io.Writer) error

Convert performs a codepage conversion of bytes from the reader and writes them to the writer. If the buffer size of this converter is zero (i.e. not specified), this will fail. If the converter uses the same codepage for reading as for writing, the reader will simply be copied to the writer, using the buffer size of this converter.

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL