fsys

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: LGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package fsys handles the opening, reading and writing of files.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrLB        = errors.New("linebreak runes cannot be empty")
	ErrMax       = errors.New("maximum attempts reached")
	ErrName      = errors.New("name file cannot be a directory")
	ErrNotFound  = errors.New("cannot find the file or sample file")
	ErrPipeEmpty = errors.New("empty text stream from piped stdin (standard input)")
	ErrReader    = errors.New("the r reader cannot be nil")
	ErrWriter    = errors.New("the w writer cannot be nil")
)

Functions

func CR

func CR() [2]rune

CR carriage return.

func CRLF

func CRLF() [2]rune

CRLF carriage return + linefeed.

func Columns

func Columns(r io.Reader, lb [2]rune) (int, error)

Columns counts the number of characters used per line in the reader interface.

func Controls

func Controls(r io.Reader) (int, error)

Controls counts the number of ANSI escape controls in the reader interface.

func InsertTar

func InsertTar(t *tar.Writer, name string) error

InsertTar inserts the named file to the TAR writer.

func InsertZip

func InsertZip(z *zip.Writer, name string) error

InsertZip adds the named file to a zip archive.

func IsPipe

func IsPipe() (bool, error)

IsPipe reports whether Stdin (standard input) is piped from another command.

func LF

func LF() [2]rune

LF linefeed.

func LFCR

func LFCR() [2]rune

LFCR linefeed + carriage return.

func LineBreak

func LineBreak(r [2]rune, extraInfo bool) string

LineBreak humanizes the value of LineBreaks().

func LineBreaks

func LineBreaks(utf bool, runes ...rune) [2]rune

LineBreaks will try to guess the line break representation as a 2 byte value. A guess of Unix will return [10, 0], Windows [13, 10], otherwise a [0, 0] value is returned.

func NEL

func NEL() [2]rune

NEL next line.

func NL

func NL() [2]rune

NL new line.

func Read

func Read(name string) ([]byte, error)

Read opens and returns the content of the named file.

func ReadAllBytes

func ReadAllBytes(name string) ([]byte, error)

ReadAllBytes reads the named file and returns the content as a byte array. Create a word and random character generator to make files larger than 64k.

func ReadChunk

func ReadChunk(name string, chars int) ([]byte, error)

ReadChunk reads and returns the start of the named file.

func ReadColumns

func ReadColumns(name string) (int, error)

ReadColumns counts the number of characters used per line in the named file.

func ReadControls

func ReadControls(name string) (int, error)

ReadControls counts the number of ANSI escape sequences in the named file.

func ReadLine

func ReadLine(name string, sys nl.System) (string, error)

ReadLine reads a named file location or a named temporary file and returns its content.

func ReadLineBreaks

func ReadLineBreaks(name string) ([2]rune, error)

ReadLineBreaks scans the named file for the most commonly used line break method.

func ReadLines

func ReadLines(name string) (int, error)

ReadLines counts the number of lines in the named file.

func ReadPipe

func ReadPipe() ([]byte, error)

ReadPipe reads data piped by the operating system's STDIN. If no data is detected the program will exit.

func ReadRunes

func ReadRunes(name string) (int, error)

ReadRunes returns the number of runes in the named file.

func ReadTail

func ReadTail(name string, offset int) ([]byte, error)

ReadTail reads the named file from the offset position relative to the end of the file.

func ReadText

func ReadText(name string) (string, error)

ReadText reads a named file location or a named temporary file and returns its content.

func ReadWords

func ReadWords(name string) (int, error)

ReadWords counts the number of spaced words in the named file.

func Runes

func Runes(r io.Reader) (int, error)

Runes returns the number of runes in the reader interface.

func SaveTemp

func SaveTemp(name string, b ...byte) (string, error)

SaveTemp saves bytes to a named temporary file. The path to the file is returned.

Example
package main

import (
	"fmt"
	"os"

	"github.com/bengarrett/retrotxtgo/fsys"
)

func main() {
	file, _ := fsys.SaveTemp("example.txt", []byte("hello world")...)
	defer os.Remove(file)
	s, _ := os.Stat(file)
	fmt.Printf("%s, %d", s.Name(), s.Size())
}
Output:

example.txt, 11

func Tar

func Tar(name string, files ...string) error

Tar add files to a named tar file archive.

Example
package main

import (
	"fmt"
	"os"

	"github.com/bengarrett/retrotxtgo/fsys"
	"github.com/bengarrett/retrotxtgo/internal/tmp"
)

func main() {
	name := tmp.File("tar_test.tar")
	file, _ := fsys.SaveTemp(name, []byte("x")...)
	defer os.Remove(file)
	_ = fsys.Tar(name, file)
	s, _ := os.Stat(file)
	fmt.Printf("%s, %d", s.Name(), s.Size())
}
Output:

tar_test.tar, 1536

func Touch

func Touch(name string) (string, error)

Touch creates an empty file at the named location.

Example
package main

import (
	"fmt"
	"os"

	"github.com/bengarrett/retrotxtgo/fsys"
)

func main() {
	file, _ := fsys.Touch("example.txt")
	defer os.Remove(file)
	s, _ := os.Stat(file)
	fmt.Printf("%s, %d", s.Name(), s.Size())
}
Output:

example.txt, 0

func UniqueName

func UniqueName(name string) (string, error)

UniqueName confirms the file name doesn't conflict with an existing file. If there is a conflict, a new incremental name will be returned.

Example
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/bengarrett/retrotxtgo/fsys"
	"github.com/bengarrett/retrotxtgo/internal/tmp"
)

func main() {
	name := "retrotxtgo_uniquetest.txt"

	// Create a temporary 1 byte file in the temporary directory
	tmpFile, err := fsys.SaveTemp(tmp.File(name), []byte("x")...)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(tmpFile)

	// Use UniqueName to find a new unique filename
	// so not to conflict with the previously saved file
	u, err := fsys.UniqueName(tmpFile)
	if err != nil {
		log.Print(err)
		return
	}

	// In Linux the new name will be retrotxtgo_uniquetest_1.txt
	// In Windows the name be retrotxtgo_uniquetest (1).txt
	newName := filepath.Base(u)

	// As the new unique names vary based on the host operating system
	// Compare the name lengths to confirm the creation of a new filename
	unique := bool(len(newName) > len(name))
	fmt.Fprint(os.Stdout, unique)
}
Output:

true

func Word

func Word(s string) bool

Word reports whether content of s contains only characters that are comprised of digits, letters and punctuation. If a space or line break is encountered the scan ends.

func Words

func Words(r io.Reader) (int, error)

Words counts the number of spaced words in the reader interface.

func WordsEBCDIC

func WordsEBCDIC(r io.Reader) (int, error)

WordsEBCDIC counts the number of spaced words in the EBCDIC encoded reader interface.

Example
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/bengarrett/retrotxtgo/fsys"
	"github.com/bengarrett/retrotxtgo/sample"
)

func main() {
	b, err := sample.File.ReadFile("plaintext/cp037.txt")
	if err != nil {
		log.Fatal(err)
	}
	nr := bytes.NewReader(b)
	words, err := fsys.WordsEBCDIC(nr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(os.Stdout, "%d words", words)
}
Output:

16 words

func Write

func Write(name string, b ...byte) (int, string, error)

Write b to the named file. The number of bytes written and the path to the file are returned.

Example
package main

import (
	"fmt"
	"os"

	"github.com/bengarrett/retrotxtgo/fsys"
)

func main() {
	file, _ := fsys.Touch("example.txt")
	defer os.Remove(file)
	i, _, _ := fsys.Write(file, []byte("hello world")...)
	s, _ := os.Stat(file)
	fmt.Printf("%s, %d", s.Name(), i)
}
Output:

example.txt, 10

Types

type Files

type Files []string

Files to zip.

func (*Files) Zip

func (files *Files) Zip(w io.Writer, name, comment string, ow bool) error

Zip packages and compresses files to an archive using the provided name.

type Zip

type Zip struct {
	// Zip path and filename.
	Name string
	// Root path of the directory to archive.
	Root string
	// Comment to embed.
	Comment string
	// Overwrite an existing named zip file if encountered.
	Overwrite bool
	// Writer for all the non-error messages, or use io.Discard to suppress.
	Writer io.Writer
}

Zip archive details.

func (*Zip) Create

func (z *Zip) Create() error

Create zip packages and compresses files contained the root directory into an archive using the provided name.

Jump to

Keyboard shortcuts

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