Branch: Tag:

2014-09-15

2014-09-15 10:43:34 by Henrik Grubbström (Grubba) <grubba@grubba.org>

IOBuffer: Added try_read().

This is an analogue to Stdio.File()->read().

1809:    *! Read @[bytes] bytes of data from the buffer.    *!    *! If there is not enough data available this returns 0. +  *! +  *! @seealso +  *! @[try_read()]    */    PIKEFUN string(0..255) read( int bytes )    {
1829:    *!    *! This is basically equivalent to (string)buffer, but it also    *! removes the data from the buffer. +  *! +  *! @seealso +  *! @[try_read()]    */    PIKEFUN string(0..255) read()    {
1836:    if( s )    push_string( s );    else -  push_undefined(); +  push_undefined(); /* FIXME: Is this ever reached? */    }    -  +  /*! @decl string(8bit) try_read(int len) +  *! +  *! Attempt to read some data from the buffer. +  *! +  *! @param len +  *! Read at most @[len] bytes from the buffer. +  *! +  *! @returns +  *! If the buffer contains less than @[len] bytes +  *! of data, the entire buffer contents are returned. +  *! Otherwise the first @[len] bytes are returned. +  *! +  *! @seealso +  *! @[read()] +  */ +  PIKEFUN string(0..255) try_read( int bytes ) +  { +  struct pike_string *s; +  Pike_sp--; +  +  if (bytes > io_len(THIS)) +  bytes = io_len(THIS); +  s = io_read_string(THIS, bytes ); +  if( s ) +  push_string( s ); +  else +  push_undefined(); /* FIXME: Is this ever reached? */ +  } +     /*! @decl int(8bit) read_int8()    */    PIKEFUN int(8bit) read_int8( )