fileio

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: BSD-3-Clause Imports: 13 Imported by: 18

Documentation

Overview

Package fileio provides wrappers of the builtin golang Reader/Writer utilities for ease of use and automatic gzip handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreEqual

func AreEqual(a string, b string) bool

AreEqual returns true if input files are equal. Expects files to end with a newline character.

func AreEqualIgnoreComments

func AreEqualIgnoreComments(a string, b string) bool

AreEqualIgnoreComments returns true if input files are equal. This function ignores lines beginning with #.

func AreEqualIgnoreOrder

func AreEqualIgnoreOrder(a string, b string) bool

AreEqualIgnoreOrder returns true if input files contain the same lines, although the order of the lines does not matter. This program sorts the two files and compares the contents, so it is not well suited for large files as the whole contents are read into memory.

func CatUrl

func CatUrl(url string) string

CatUrl will process a url link and print it out to stdout.

func CatchErrThrowEOF

func CatchErrThrowEOF(err error)

CatchErrThrowEOF handles EOF errors silently and panics on others.

func EasyNextLine

func EasyNextLine(file *EasyReader) (string, bool)

EasyNextLine returns the next line of the input EasyReader. Returns true at EOF.

func EasyNextRealLine

func EasyNextRealLine(file *EasyReader) (string, bool)

EasyNextRealLine returns the next line of the input EasyReader that does not begin with '#'. Returns true at EOF.

func EasyPeekReal

func EasyPeekReal(file *EasyReader, n int) ([]byte, error)

EasyPeekReal will advance a reader past any lines beginning with '#' and read the first n bytes without advancing the reader.

func EasyReadHeader

func EasyReadHeader(file *EasyReader) ([]string, error)

EasyReadHeader will read any leading comments lines from the file and return them as a slice of strings with each element in the slice being a comment line.

func EasyRemove

func EasyRemove(filename string)

EasyRemove deletes the input file.

func IntSliceToString

func IntSliceToString(nums []int) string

IntSliceToString converts int slices to comma-separated strings.

func IntToString

func IntToString(i int) string

IntToString converts an int to a string.

func IsGzip

func IsGzip(r io.ReadSeeker) bool

IsGzip checks if the magic gzip number is present at the start of the input io.ReadSeeker. After check for the magic number, IsGzip seeks back to the initial position.

func MustCreate

func MustCreate(filename string) *os.File

MustCreate creates a file with the input name. Fatal/Panics when appropriate.

func MustOpen

func MustOpen(filename string) *os.File

MustOpen opens the input file. Fatal/Panics when appropriate.

func MustRemove

func MustRemove(filename string)

MustRemove deletes the input file. Fatal/Panics when appropriate.

func NextLine

func NextLine(reader *bufio.Reader) (string, bool)

NextLine returns the next line of the file (might be a comment line). Returns true if the file is done.

func NextRealLine

func NextRealLine(reader *bufio.Reader) (string, bool)

NextRealLine returns the next line of the file that is not a comment line. Returns true if the file is done.

func PeekReal

func PeekReal(reader *bufio.Reader, n int) ([]byte, error)

PeekReal will advance a reader past any lines beginning with '#' and read the first n bytes without advancing the reader.

func Read

func Read(filename string) []string

Read inputs a file and returns each line in the file as a string.

func ReadFileToSingleLineString

func ReadFileToSingleLineString(filename string) string

ReadFileToSingleLineString reads in any file type and returns contents without any \n.

func ReadHeader

func ReadHeader(reader *bufio.Reader) ([]string, error)

ReadHeader will advance a reader past initial lines that begin with '#', returning a slice of these comments lines and leaving the reader at the first non-comment line.

func ReadLine

func ReadLine(reader *ByteReader) (*bytes.Buffer, bool)

ReadLine reads a line into Buffer, indicating if more lines are available.

func StringToIntSlice

func StringToIntSlice(line string) []int

StringToIntSlice converts comma-separated strings to int slices.

func Write

func Write(filename string, records []string)

Write writes a slice of strings to a file with a newline placed after each element of the slice.

func WriteToFileHandle

func WriteToFileHandle(file io.Writer, rec string)

WriteToFileHandle will write a string to an io.Writer and panic on any error.

Types

type ByteReader

type ByteReader struct {
	*bufio.Reader
	File *os.File

	Buffer *bytes.Buffer
	// contains filtered or unexported fields
}

ByteReader wraps bufio.Reader for efficient byte-level file parsing.

func NewByteReader

func NewByteReader(filename string) *ByteReader

NewByteReader initializes a ByteReader for given filename, supporting p/gzip.

func (*ByteReader) Close

func (br *ByteReader) Close() error

Close closes the internal File and gzip Reader if present.

func (*ByteReader) Read

func (reader *ByteReader) Read(b []byte) (n int, err error)

Read implements io.Reader, reading data into b from the file or gzip stream.

type EasyReader

type EasyReader struct {
	File *os.File

	BuffReader *bufio.Reader
	// contains filtered or unexported fields
}

EasyReader provides a simplified wrapper of the builtin golang io functions. Will silently handle reading gziped files. EasyReader is a valid io.Reader.

func EasyHttp

func EasyHttp(url string) *EasyReader

EasyHttp will fetch data from files uploaded to an internet server and stream data into EasyReader.

func EasyOpen

func EasyOpen(filename string) *EasyReader

EasyOpen opens the input file. Panics if errors are encountered.

func (*EasyReader) Close

func (er *EasyReader) Close() error

Close the receiving EasyReader.

func (*EasyReader) Peek

func (er *EasyReader) Peek(n int) ([]byte, error)

Peek retrieves the next n bytes of the receiving EasyReader without advancing the reader.

func (*EasyReader) Read

func (er *EasyReader) Read(p []byte) (n int, err error)

Read retrieves n bytes from the receiving EasyReader.

type EasyWriter

type EasyWriter struct {
	// contains filtered or unexported fields
}

EasyWriter provides a simplified wrapper of the builtin golang io functions. Will silently gzip output files when the input filename ends with '.gz'. EasyWriter is a valid io.Writer.

func EasyCreate

func EasyCreate(filename string) *EasyWriter

EasyCreate creates a file with the input name. Panics if errors are encountered.

func (*EasyWriter) Close

func (ew *EasyWriter) Close() error

Close the receiving EasyWriter.

func (*EasyWriter) Write

func (ew *EasyWriter) Write(p []byte) (n int, err error)

Write bytes to the receiving EasyWriter.

Jump to

Keyboard shortcuts

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