fixedlen

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 9 Imported by: 1

README

fixedlen

This package provides support for reading and writing Fixed-Length format.

Examples

package main

import (
	"os"
	
	"github.com/mithrandie/go-text"
	"github.com/mithrandie/go-text/fixedlen"
)

func main() {
	fp, err := os.Open("example.txt")
	if err != nil {
		panic("file open error")
	}
	defer func() {
		if err = fp.Close(); err != nil {
			panic(err.Error())
		}
	}()
	
	r, _ := fixedlen.NewReader(fp, []int{5, 10, 45, 60}, text.UTF8)
	r.WithoutNull = true
	recordSet, err := r.ReadAll()
	if err != nil {
		panic("fixed-length read error")
	}
	
	lineBreak := r.DetectedLineBreak
	
	wfp, err := os.Create("example_new.txt")
	if err != nil {
		panic("file open error")
	}
	defer func() {
		if err = wfp.Close(); err != nil {
			panic(err.Error())
		}
	}()

	w, err := fixedlen.NewWriter(wfp, []int{5, 10, 45, 60}, lineBreak, text.SJIS)
	if err != nil {
		panic(err.Error)
	}
	
	for _, record := range recordSet {
		r := make([]fixedlen.Field, 0, len(record))
		for _, field := range record {
			r = append(r, fixedlen.NewField(string(field), text.NotAligned))
		}
		if err = w.Write(r); err != nil {
			panic(err.Error())
		}
	}
	if err = w.Flush(); err != nil {
		panic(err.Error())
	}
}

Documentation

Overview

Package fixedlen is a Go library to read and write Fixed-Length format.

Index

Constants

View Source
const OutOfLine = -1

Variables

This section is empty.

Functions

This section is empty.

Types

type Delimiter

type Delimiter struct {
	NoHeader bool
	Encoding text.Encoding
	// contains filtered or unexported fields
}

func NewDelimiter

func NewDelimiter(r io.Reader, enc text.Encoding) (*Delimiter, error)

func (*Delimiter) Delimit

func (d *Delimiter) Delimit() ([]int, error)

type DelimiterPositions

type DelimiterPositions []int

func (DelimiterPositions) Equal

func (DelimiterPositions) Last

func (p DelimiterPositions) Last() int

func (DelimiterPositions) String

func (p DelimiterPositions) String() string

type Field

type Field struct {
	Contents  string
	Alignment text.FieldAlignment
}

func NewField

func NewField(contents string, alignment text.FieldAlignment) Field

type Measure

type Measure struct {
	Encoding text.Encoding
	// contains filtered or unexported fields
}

func NewMeasure

func NewMeasure() *Measure

func (*Measure) GeneratePositions

func (m *Measure) GeneratePositions() DelimiterPositions

func (*Measure) Measure

func (m *Measure) Measure(record []Field)

type PositionStatus

type PositionStatus int
const (
	PositionOut PositionStatus = iota
	PositionInValue
	PositionEndOfValue
	PositionInSpace
	PositionEndOfSpace
)

type Reader

type Reader struct {
	DelimiterPositions DelimiterPositions
	WithoutNull        bool
	Encoding           text.Encoding
	SingleLine         bool

	DetectedLineBreak text.LineBreak
	// contains filtered or unexported fields
}

func NewReader

func NewReader(r io.Reader, positions []int, enc text.Encoding) (*Reader, error)

func (*Reader) Read

func (r *Reader) Read() ([]text.RawText, error)

func (*Reader) ReadAll

func (r *Reader) ReadAll() ([][]text.RawText, error)

func (*Reader) ReadHeader

func (r *Reader) ReadHeader() ([]string, error)

type RecordSpaces

type RecordSpaces []Space

func (RecordSpaces) LineLen

func (r RecordSpaces) LineLen() int

func (RecordSpaces) NextEnd

func (r RecordSpaces) NextEnd(pos int) int

func (RecordSpaces) PrevStart

func (r RecordSpaces) PrevStart(pos int) int

func (RecordSpaces) Status

func (r RecordSpaces) Status(pos int) PositionStatus

type Space

type Space struct {
	Start int
	End   int
}

type TableSpaces

type TableSpaces []RecordSpaces

func (TableSpaces) CountColumnStatus

func (t TableSpaces) CountColumnStatus(pos int) (inValue int, endOfValue int, inSpace int, endOfSpace int, endOfLine int, outOfLine int)

func (TableSpaces) InHeaderValue

func (t TableSpaces) InHeaderValue(pos int) bool

func (TableSpaces) NextSpaceEnd

func (t TableSpaces) NextSpaceEnd(pos int) int

func (TableSpaces) PrevSpaceStart

func (t TableSpaces) PrevSpaceStart(pos int) int

func (TableSpaces) TableLen

func (t TableSpaces) TableLen() int

type Writer

type Writer struct {
	InsertSpace bool
	PadChar     byte
	SingleLine  bool
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(w io.Writer, delimiterPositions DelimiterPositions, lineBreak text.LineBreak, enc text.Encoding) (*Writer, error)

func (*Writer) Flush

func (e *Writer) Flush() error

func (*Writer) Write

func (e *Writer) Write(record []Field) error

Jump to

Keyboard shortcuts

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