Module lsh.fio.fh

File input/output handle.

Functions

new (fd) Constructs new file handle and returns it.

Fh methods

type () Returns the instance type.
close (self) Close file handle.
read_to_buf (self, buf, len) Read from file handle to provided buffer.
read (self[, len]) Read from file handle.
write (self, buf[, len]) Write to file handle.
seek (self, position) Seek to position.
lines (self) Returns an iterator function that, each time it is called, returns a new line from the file handle.
getfd (self) Returns file descriptor number.


Functions

new (fd)
Constructs new file handle and returns it.

There is no need to use this method directly, see lsh.memfd and lsh.fio.open for practical usage.

Parameters:

  • fd cdata ljsyscall fd

Returns:

    lsh.fio.fh new fh instance

Usage:

    local sh = require 'lsh'
    
    local fh = memfd()

Fh methods

type ()
Returns the instance type.

Returns:

    the string "fh"

Usage:

    local sh = require 'lsh'
    
    assert(sh.fio.open('/dev/null').type() == 'fh')
close (self)
Close file handle.

Parameters:

  • self lsh.fio.fh

Returns:

    bool true

Or

  1. nil
  2. string error

Usage:

    local sh = require 'lsh'
    
    assert(sh.fio.open('/dev/null'):close())
read_to_buf (self, buf, len)
Read from file handle to provided buffer.

This advanced interface is avalable for micro optimizations. See read for everyday usage.

Parameters:

  • self lsh.fio.fh
  • buf cdata buffer to read
  • len number lenght of the buffer

Returns:

    number the number of bytes read into buffer

Or

  1. nil
  2. string output

Usage:

    local sh = require 'lsh'
    local ffi = require 'ffi'
    
    local buf_len = 4096
    local buf = ffi.new('char[?]', buf_len)
    
    local in_fh = sh.fio.open('in.txt', 'rdonly', 'RUSR')
    local out_fh = sh.fio.open('out.txt', {'creat', 'wronly'}, {'RUSR', 'WUSR'})
    
    repeat
      local len = in_fh:read_to_buf(buf, buf_len)
      out_fh:write(buf, len)
    until len <= 0
read (self[, len])
Read from file handle.

Parameters:

  • self lsh.fio.fh
  • len number maximum number of bytes to read (optional)

Returns:

    string the data that was read

Or

  1. nil
  2. string error

Usage:

    local sh = require 'lsh'
    
    local zeros = sh.fio.open('/dev/zero'):read(3)
    assert(#zeros == 3)
write (self, buf[, len])
Write to file handle.

Parameters:

  • self lsh.fio.fh
  • buf cdata or string buffer to write
  • len int lenght of the buffer (only needed if buf is cdata) (optional)

Returns:

    bool true if buffer contained data

Or

  1. nil
  2. string error

Usage:

    local sh = require 'lsh'
    
    assert(sh.fio.open('/dev/null'):write('abc'))
    
    -- advanced usage, passing cdata pointers
    local ffi = require 'ffi'
    local str = 'abc'
    local buf = ffi.new('char[?]', #str, str)
    assert(sh.fio.open('/dev/null'):write(buf, #str))
seek (self, position)
Seek to position.

This interface is not finalized and it will be changed in incompatible ways!

Parameters:

  • self lsh.fio.fh
  • position number

Returns:

    number position

Usage:

    local sh = require 'lsh'
    
    local position = sh.fio.open('/dev/zero'):seek(3)
lines (self)
Returns an iterator function that, each time it is called, returns a new line from the file handle.

Parameters:

  • self lsh.fio.fh

Returns:

    func function iterator

Usage:

    local sh = require 'lsh'
    
    for line in sh.memfd('foo\nbar'):lines() do
      print(line)
    end
    --> foo
    --> bar
getfd (self)
Returns file descriptor number.

Parameters:

  • self lsh.fio.fh

Returns:

    number file descriptor number

Usage:

    local sh = require 'lsh'
    
    print(sh.fio.open('/dev/null'):getfd())
    --> 3
generated by LDoc 1.4.6 Last updated 1980-01-01 00:00:00