Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Unable to change pointer position / tell in hsl handles. #43

Closed
azjezz opened this issue Apr 8, 2019 · 2 comments
Closed

Unable to change pointer position / tell in hsl handles. #43

azjezz opened this issue Apr 8, 2019 · 2 comments

Comments

@azjezz
Copy link
Contributor

azjezz commented Apr 8, 2019

such feature should be included in hsl, there's already builtin function to achieve this when using builtin resources : \ftell() and \fseek().

simplest implementation :

enum SeekWhence

/**
 * Specifies how the cursor position will be calculated
 * based on the seek offset.
 */
enum SeekWhence: int {
  /**
   * Set position equal to offset bytes.
   */
  SET = \SEEK_SET;
  /**
   * Set position to current location plus offset.
   */
  CURRENT = \SEEK_CUR;
  /**
   * Set position to end-of-file plus offset.
   */
  END = \SEEK_END;
}

SeekableHandle :

<<__Sealed(
  SeekableWriteHandle::class,
  SekkableReadHandle::claas,
)>>
interface SeekableHandle {
  /**
   * Seek to a position in the stream.
   */
  public function seek(
    int $offset,
    SeekWhence $whence = SeekWhence::SET,
  ): void;

  /**
   * Seek to the beginning of the stream.
   *
   * If the stream is not seekable, this method will raise an exception;
   * otherwise, it will perform a seek(0).
   */
  public function rewind(): void;

  /**
   * Returns the current position of the file read/write pointer
   *
   * @return int Position of the file pointer
   */
  public function tell(): int;
}

interface SeekableReadHandle
  extends ReadHandle, SeekableHandle {}

interface SeekableWriteHandle
  extends WriteHandle, SeekableHandle {}

interface DisposableSeekableWriteHandle
  extends SeekableWriteHandle, DisposableWriteHandle {}

interface DisposableSeekableReadHandle
  extends SeekableReadHandle, DisposableReadHandle {}

Hanlde::seek(int $offset, SeekWhence $whence = SeekWhence::SET): void :

  public function seek(
    int $offset,
    SeekWhence $whence = SeekWhence::SET,
  ): void {
    $meta = @\stream_get_meta_data($this->impl);

    if (!$meta['seekable']) {
      throw new Exception('Stream is not seekable');
    }

    $retval = @\fseek($this->impl, $offset, $whence as int);

    if ($retval === -1) {
      throw
        new Exception('Error seeking within stream');
    }
  }

Handle::rewind(): void

  public function rewind(): void {
    $this->seek(0);
  }

Handle::tell(): int :

  public function tell(): int {
    $result = @\ftell($this->impl);

    if (false === $result) {
      throw new Exception('Error occurred during tell operation');
    }

    return $result;
  }
@fredemmott
Copy link
Contributor

We also currently have a separation between seeking for read and seeking for write. We're going to have to investigate portability of unifying them (we were hoping to implement them using fdup, but the result is non-portable). That includes testing the C behavior on platforms we don't currently support.

@azjezz
Copy link
Contributor Author

azjezz commented Jan 1, 2020

fixed in #68 #71

@azjezz azjezz closed this as completed Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants