github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/file/write.go (about)

     1  package file
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"github.com/lmorg/murex/lang/stdio"
     8  	"github.com/lmorg/murex/utils"
     9  )
    10  
    11  // Write is the io.Writer interface
    12  func (f *File) Write(b []byte) (int, error) {
    13  	if f.file == nil {
    14  		return 0, errors.New("no file open")
    15  	}
    16  
    17  	f.mutex.Lock()
    18  	dep := f.dependents
    19  	f.mutex.Unlock()
    20  
    21  	if dep < 1 {
    22  		return 0, io.ErrClosedPipe
    23  	}
    24  
    25  	i, err := f.file.Write(b)
    26  
    27  	f.mutex.Lock()
    28  	f.bWritten += uint64(i)
    29  	f.mutex.Unlock()
    30  
    31  	return i, err
    32  }
    33  
    34  // Writeln is the io.Writeln interface
    35  func (f *File) Writeln(b []byte) (int, error) {
    36  	return f.Write(append(b, utils.NewLineByte...))
    37  }
    38  
    39  // WriteArray performs data type specific buffered writes to an stdio.Io interface
    40  func (f *File) WriteArray(dataType string) (stdio.ArrayWriter, error) {
    41  	return stdio.WriteArray(f, dataType)
    42  }