pike.git / src / modules / _Stdio / file.c

version» Context lines:

pike.git/src/modules/_Stdio/file.c:1293:    *!    *! @note    *! When at the end of a file or stream, repeated calls to @[read()]    *! will return the empty string since it's not considered an error.    *! The empty string is never returned in other cases, unless nonblocking    *! mode is used or @[len] is zero.    *!    *! @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;       int fd = file->box.fd;       if(fd < 0)    Pike_error("File not open.\n");       if (args && TYPEOF(Pike_sp[-args]) == PIKE_T_OBJECT) {
pike.git/src/modules/_Stdio/file.c:1314:    struct object *o = Pike_sp[-args].u.object;    enum memobj_type type = pike_get_memory_object(o, &m, 1);    ptrdiff_t bytes_read;       if (type == MEMOBJ_NONE)    SIMPLE_BAD_ARG_ERROR("read()", 1, "int(0..)|Stdio.Buffer|String.Buffer|System.Memory");       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");       bytes_read = do_read_into_buffer(fd, m.ptr, (size_t)m.len, &file->my_errno);       if (bytes_read > 0)    pike_advance_memory_object(o, type, bytes_read);       pop_n_elems(args);    push_int(bytes_read);