Documentation ¶
Overview ¶
Package fsys handles the opening, reading and writing of files.
Index ¶
- Variables
- func CR() [2]rune
- func CRLF() [2]rune
- func Columns(r io.Reader, lb [2]rune) (int, error)
- func Controls(r io.Reader) (int, error)
- func InsertTar(t *tar.Writer, name string) error
- func InsertZip(z *zip.Writer, name string) error
- func IsPipe() (bool, error)
- func LF() [2]rune
- func LFCR() [2]rune
- func LineBreak(r [2]rune, extraInfo bool) string
- func LineBreaks(utf bool, runes ...rune) [2]rune
- func NEL() [2]rune
- func NL() [2]rune
- func Read(name string) ([]byte, error)
- func ReadAllBytes(name string) ([]byte, error)
- func ReadChunk(name string, chars int) ([]byte, error)
- func ReadColumns(name string) (int, error)
- func ReadControls(name string) (int, error)
- func ReadLine(name string, sys nl.System) (string, error)
- func ReadLineBreaks(name string) ([2]rune, error)
- func ReadLines(name string) (int, error)
- func ReadPipe() ([]byte, error)
- func ReadRunes(name string) (int, error)
- func ReadTail(name string, offset int) ([]byte, error)
- func ReadText(name string) (string, error)
- func ReadWords(name string) (int, error)
- func Runes(r io.Reader) (int, error)
- func SaveTemp(name string, b ...byte) (string, error)
- func Tar(name string, files ...string) error
- func Touch(name string) (string, error)
- func UniqueName(name string) (string, error)
- func Word(s string) bool
- func Words(r io.Reader) (int, error)
- func WordsEBCDIC(r io.Reader) (int, error)
- func Write(name string, b ...byte) (int, string, error)
- type Files
- type Zip
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 LineBreaks ¶
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 ReadAllBytes ¶
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 ReadColumns ¶
ReadColumns counts the number of characters used per line in the named file.
func ReadControls ¶
ReadControls counts the number of ANSI escape sequences in the named file.
func ReadLine ¶
ReadLine reads a named file location or a named temporary file and returns its content.
func ReadLineBreaks ¶
ReadLineBreaks scans the named file for the most commonly used line break method.
func ReadPipe ¶
ReadPipe reads data piped by the operating system's STDIN. If no data is detected the program will exit.
func ReadTail ¶
ReadTail reads the named file from the offset position relative to the end of the file.
func ReadText ¶
ReadText reads a named file location or a named temporary file and returns its content.
func SaveTemp ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 WordsEBCDIC ¶
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 ¶
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 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.