This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/file/file.go

124 lines
3.5 KiB
Go
Raw Normal View History

package file
import "os"
2022-08-08 23:02:03 -06:00
import "bufio"
2022-08-03 11:28:37 -06:00
// File represents a read only file that can print out formatted errors.
type File struct {
path string
file *os.File
2022-08-08 23:02:03 -06:00
reader *bufio.Reader
currentLine int
currentColumn int
2022-08-08 23:07:14 -06:00
lines []string
}
2022-08-03 11:28:37 -06:00
// Open opens the file specified by path and returns a new File struct.
func Open (path string) (file *File, err error) {
file = &File {
2022-08-03 11:28:37 -06:00
path: path,
2022-08-08 23:07:14 -06:00
lines: []string { "" },
}
file.file, err = os.OpenFile(path, os.O_RDONLY, 0660)
if err != nil { return }
2022-08-08 23:02:03 -06:00
file.reader = bufio.NewReader(file.file)
return
}
// Stat returns the FileInfo structure describing file. If there is an error, it
// will be of type *PathError.
func (file *File) Stat () (fileInfo os.FileInfo, err error) {
return file.file.Stat()
}
// Read reads up to len(bytes) bytes from the File and stores them in bytes. It
// returns the number of bytes read and any error encountered. At end of file,
// Read returns 0, io.EOF. This method stores all data read into the file struct
// so it can be used later for error reporting.
func (file *File) Read (bytes []byte) (amountRead int, err error) {
2022-08-08 23:02:03 -06:00
amountRead, err = file.reader.Read(bytes)
2022-08-03 11:28:37 -06:00
// store the character in the file
for _, char := range bytes {
if char == '\n' {
2022-08-08 23:07:14 -06:00
file.lines = append(file.lines, "")
file.currentLine ++
file.currentColumn = 0
} else {
2022-08-08 23:07:14 -06:00
file.lines[file.currentLine] += string(char)
file.currentColumn ++
}
}
return
}
2022-08-08 23:07:14 -06:00
// ReadRune reads a single UTF-8 encoded Unicode character and returns the rune
// and its size in bytes. If the encoded rune is invalid, it consumes one byte
// and returns unicode.ReplacementChar (U+FFFD) with a size of 1.
func (file *File) ReadRune () (char rune, size int, err error) {
char, size, err = file.reader.ReadRune()
if char == '\n' {
file.lines = append(file.lines, "")
file.currentLine ++
file.currentColumn = 0
} else {
file.lines[file.currentLine] += string(char)
file.currentColumn ++
}
return
}
2022-08-08 23:13:49 -06:00
// ReadString reads until the first occurrence of delimiter in the input,
// returning a string containing the data up to and including the delimiter. If
// ReadString encounters an error before finding a delimiter, it returns the
// data read before the error and the error itself (often io.EOF). ReadString
// returns err != nil if and only if the returned data does not end in delim.
func (file *File) ReadString (delimiter byte) (read string, err error) {
read, err = file.reader.ReadString(delimiter)
// store the character in the file
for _, char := range read {
if char == '\n' {
file.lines = append(file.lines, "")
file.currentLine ++
file.currentColumn = 0
} else {
file.lines[file.currentLine] += string(char)
file.currentColumn ++
}
}
return
}
// Close closes the file. After the file is closed, data that has been read will
// still be retained, and errors can be reported.
func (file *File) Close () {
file.file.Close()
}
2022-08-03 11:28:37 -06:00
// Location returns a location struct describing the current position inside of
// the file. This can be stored and used to print errors.
func (file *File) Location (width int) (location Location) {
return Location {
file: file,
row: file.currentLine,
column: file.currentColumn,
width: width,
}
}
2022-08-17 22:50:39 -06:00
// Path returns the path that teh file is located at.
func (file *File) Path () (path string) {
return file.path
}
// GetLine returns the line at the specified index.
func (file *File) GetLine (index int) (line string) {
return file.lines[index]
}