Branch: Tag:

2018-11-04

2018-11-04 11:18:14 by Arne Goedeke <el@laramies.com>

Stdio.File()->read: add optional offset argument and documentation

1300:    *! @seealso    *! @[read_oob()], @[write()], @[receive_fd()], @[send_fd()]    */ +  + /*! +  *! @decl int read(Stdio.Buffer|String.Buffer dst) +  *! +  *! Reads data from a file or stream into the buffer @[dst]. Tries to +  *! read as many bytes as buffer space available. +  *! Will advance the write position in @[dst] by the number of bytes +  *! read. +  *! +  *! @returns +  *! The number of bytes read. Returns @expr{-1@} on error and +  *! @[errno()] will return the corresponding error code. +  */ +  + /*! +  *! @decl int read(System.Memory dst, void|int(0..) offset) +  *! +  *! Reads data from a file or stream into the buffer @[dst] at offset +  *! @[offset]. Tries to read as many bytes as buffer space available. +  *! +  *! @returns +  *! The number of bytes read. Returns @expr{-1@} on error and +  *! @[errno()] will return the corresponding error code. +  */   static void file_read(INT32 args)   {    struct my_file *file = THIS;
1321:    if (m.shift)    Pike_error("Cannot read into wide-string buffer.\n");    +  if (args > 1) +  { +  INT_TYPE offset; +  +  if (TYPEOF(Pike_sp[-args+1]) != PIKE_T_INT || Pike_sp[-args+1].u.integer < 0) +  SIMPLE_BAD_ARG_ERROR("read()", 2, "int(0..)"); +  +  if (type != MEMOBJ_SYSTEM_MEMORY) +  SIMPLE_BAD_ARG_ERROR("read()", 1, "System.Memory"); +  +  offset = Pike_sp[-args+1].u.integer; +  +  if ((size_t)offset > m.len) +  Pike_error("Offset out of bounds.\n"); +  +  m.len -= offset; +  m.ptr = (char*)m.ptr + offset; +  } +     if (!m.len)    Pike_error("No buffer space.\n");