pike.git / src / modules / _Stdio / buffer.cmod

version» Context lines:

pike.git/src/modules/_Stdio/buffer.cmod:305:    *! @endint    *!    *! @returns    *!    *! @[true] if the operation should be retried, @[false] otherwise.    *!    *! Do not return true unless you have added data to the buffer,    *! doing so could result in an infinite loop (since no data is    *! added, the range_error will be called again immediately).    */ -  PIKEFUN int(0..1) range_error( int howmuch ) +  PIKEFUN int(0..1) range_error( int range )    flags ID_PROTECTED;    { -  /* Default: throw error if so desired, otherwise return 0. */ -  pop_n_elems(args); -  push_int(0); +  Pike_sp[-1].u.integer = 0;    }       static void io_range_error_throw( IOBuffer *io, int howmuch )    ATTRIBUTE((noclone,noinline));       static void io_range_error_throw( IOBuffer *io, int howmuch )    {    if( io->error_mode )    {    if( howmuch > 0 )
pike.git/src/modules/_Stdio/buffer.cmod:982:    io_rewind(io, rd-wr );    break;    }    }    }    RETURN written;    }       PIKEFUN int(0..) _size_object( )    { -  RETURN THIS->malloced ? THIS->allocated : 0; +  RETURN THIS->malloced ? THIS->allocated : THIS->stash.len;    }       /*! @decl IOBuffer add( AddArgument ... data )    *! @code    *! private typedef @[System.Memory]|@[Stdio.IOBuffer]|@[String.Buffer] BufferObject;    *! private typedef BufferObject|string(8bit)|int(8bit)|array(AddArgument) AddArgument;    *! @endcode    *!    *! Add the items in data to the end of the buffer.    *!
pike.git/src/modules/_Stdio/buffer.cmod:1250:    }       /*! @decl protected int `[](int off)    *!    *! Return the character at the specified offset.    */    PIKEFUN int(0..255) `[]( int off )    flags ID_PROTECTED;    {    IOBuffer *io = THIS; -  pop_stack(); +     if( off < 0 )    off = io_len(io)-off;       if( io_avail( io, off ) ) -  push_int( io_read_pointer(io)[off] ); +  Pike_sp[-1].u.integer = io_read_pointer(io)[off];    else -  push_int(-1); +  Pike_sp[-1].u.integer = -1;    }       /*! @decl protected int `[]=(int off, int char)    *!    *! Set the character at the specified offset to @[char].    */    PIKEFUN int(0..255) `[]=( int off, int val )    flags ID_PROTECTED;    {    IOBuffer *io = THIS; -  +     io_ensure_malloced( io, 0 );       if( off < 0 ) off = io_len(io)-off; -  +  again:    if( io_avail( io, off ) )    {    io_read_pointer(io)[off]=(val&0xff);    }    else    { -  /* hm, well. We could extend the buffer? */ -  push_int(-1); +  /* hm, well. We could extend the buffer. Should we? */ +  if( io_range_error(io, off ) ) +  goto again; +  Pike_error("Writing outside buffer\n");    }    }       /*! @decl int _sizeof()    *!    *! Returns the buffer size, in bytes.    *! This is how much you can read from the buffer until it runs out of data.    */    PIKEFUN int(0..) _sizeof()    flags ID_PROTECTED;
pike.git/src/modules/_Stdio/buffer.cmod:1370:    *! ...    *! }    *! }    *! @endcode    */    PIKEFUN IOBuffer set_error_mode( int|program m )    {    if( TYPEOF(*m) == PIKE_T_INT )    io_set_error_mode( THIS, m->u.integer ? buffer_error_program : 0 );    else -  { +     io_set_error_mode( THIS, program_from_svalue(m)); -  } +     pop_stack();    ref_push_object(Pike_fp->current_object);    }       /*! @decl object lock()    *!    *! Makes this buffer read only until the returned object is released.    *!    *! @note    *! This currently simply returns a 0-length subbuffer.    */    PIKEFUN IOBuffer lock()    {    push_object( io_read_buffer( THIS, 0, 0 ) );    }    -  PIKEFUN string(0..255) _sprintf(int o, mapping ignore) +  PIKEFUN string(0..255) _sprintf(int o, mapping UNUSED)    flags ID_PROTECTED;    { -  pop_n_elems(args); -  if( o == 'O' ) +  pop_n_elems(args-1); +  Pike_sp--; +  switch( o )    { -  +  case 'O': +  {    push_text("%O(%d bytes, read=[..%d] data=[%d..%d] free=[%d..%d] %s%s)");    ref_push_program(Pike_fp->current_object->prog);    /* io_len [..offset] [offset..len] [..allocated] */    push_int(io_len(THIS));    push_int(THIS->offset-1);    push_int(THIS->offset);       push_int(THIS->len-1);    push_int(THIS->len);    push_int(THIS->allocated);    push_text( (THIS->str ? "string" : THIS->malloced ? "allocated" : "subbuffer" ) );    if( THIS->locked )    push_text(" (read only)");    else    push_text("");    f_sprintf(10);    } -  else +  break; +  +  case 's': +  push_string( io_read_string( THIS, io_len(THIS)) ); +  break; +  +  case 'q': +  push_text("%q"); +  push_string( io_read_string( THIS, io_len(THIS)) ); +  f_sprintf(2); +  break; +  default:    push_undefined();    } -  +  }       /*! @decl string(8bit) read_hstring( int(0..) n )    *!    *! Identical in functionality to @[read](@[read_number](@[n])) but    *! faster.    *!    *! Read a network byte order number of size n*8 bits, then return the    *! indicated number of bytes as a string.    *!    *! If there is not enough data available return 0.
pike.git/src/modules/_Stdio/buffer.cmod:1441:    IOBuffer *io = THIS;    struct pike_string *s;    ONERROR e;       io_rewind_on_error( io, &e );    len = io_read_number( io, bytes );       if (len < 0) {    /* io_avail() in io_read_number() failed. */    CALL_AND_UNSET_ONERROR(e); -  push_int(0); +  Pike_sp[-1].u.integer = 0;    return;    }       /* NB: We assume that io_avail() in io_read_string() doesn't throw. */    s = io_read_string( io, len );       if( s ) {    io_unset_rewind_on_error( io, &e ); -  +  Pike_sp--;    push_string(s);    } else {    CALL_AND_UNSET_ONERROR(e); -  push_int(0); +  Pike_sp[-1].u.integer = 0;    }    }       /*! @decl IOBuffer read_hbuffer( int n )    *! @decl IOBuffer read_hbuffer( int n, bool copy )    *!    *! Same as @[read_hstring], but returns the result as an IOBuffer.    *!    *! No data is copied unless @[copy] is specified and true, the new    *! buffer points into the old one.
pike.git/src/modules/_Stdio/buffer.cmod:1614:    *! for most other cases    */    PIKEFUN mixed read_json(int|void require_whitespace)    {    int stop, whites = 0;    static ptrdiff_t(*parse_json_pcharp)(PCHARP,size_t,int,char**);    char *err = NULL;    if( require_whitespace )    whites = require_whitespace->u.integer;    +  Pike_sp-=args;    if( !parse_json_pcharp )    parse_json_pcharp = PIKE_MODULE_IMPORT(Standards.JSON, parse_json_pcharp );    retry:    stop = parse_json_pcharp( MKPCHARP(io_read_pointer(THIS),0),    io_len(THIS), 1|8, &err ); /* json_utf8 */       if( stop < 0 )    {    if( -stop == (ptrdiff_t)io_len(THIS) || (err && !strncmp(err,"Unterminated",12)))    {
pike.git/src/modules/_Stdio/buffer.cmod:1676:    *!    *! The non-matching data will be left in the buffer.    *!    *! This function is very similar to @[sscanf], but the    *! result is the sum of the matches. Most useful to match    *! a single value.    *!    *! @example    *! @code    *! // get the next whitespace separated word from the buffer. -  *! buffer->match("%*[ \t\r\n]%*[^ \t\r\n]"); +  *! buffer->match("%*[ \t\r\n]%[^ \t\r\n]");    *! @endcode    */    PIKEFUN string(0..255)|int|float|array match( string format )    {    INT32 i;    ptrdiff_t num_used;    struct svalue *start = Pike_sp;    retry:    i = low_sscanf_pcharp(    MKPCHARP(io_read_pointer(THIS), 0), io_len(THIS),
pike.git/src/modules/_Stdio/buffer.cmod:1747:    }       /*! @decl int(0..)|int(-1..-1) consume( int(0..) n )    *!    *! Discard the first @[n] bytes from the buffer    *!    *! Returns -1 on error and the amount of space still left otherwise.    */    PIKEFUN int(-1..) consume( int n )    { +  Pike_sp--;    if( !io_avail( THIS, n ) )    push_int(-1);    else    push_int64( io_consume( THIS, n ) );    }          /*! @decl int(0..)|int(-1..-1) unread( int(0..) n )    *!    *! Rewind the buffer @[n] bytes.
pike.git/src/modules/_Stdio/buffer.cmod:1772:    *!    *! @note    *!    *! Unless you add new data to the buffer using any of the add    *! functions you can always rewind.    *!    *! You can call @[unread(0)] to see how much.    */    PIKEFUN int(-1..) unread( int bytes )    { +  Pike_sp--;    push_int64( io_rewind( THIS, bytes ) );    }       /*! @decl string(8bit) read( int n )    *!    *! Read @[bytes] bytes of data from the buffer.    *!    *! If there is not enough data available this returns 0.    */    PIKEFUN string(0..255) read( int bytes )    {    struct pike_string *s; -  +  Pike_sp--;    s = io_read_string(THIS, bytes );    if( s ) -  RETURN s; +  push_string( s ); +  else    push_undefined();    }       /*! @decl string(8bit) read( )    *!    *! Read all data from the buffer.    *!    *! If there is not enough data available this returns 0.    *!    *! This is basically equivalent to (string)buffer, but it also    *! removes the data from the buffer.    */    PIKEFUN string(0..255) read()    {    struct pike_string * s = io_read_string(THIS, io_len(THIS));    if( s ) -  RETURN s; +  push_string( s ); +  else    push_undefined();    }       /*! @decl int(8bit) read_int8()    */    PIKEFUN int(8bit) read_int8( )    {    IOBuffer *io = THIS;    if( LIKELY(io_avail( io, 1 )) )    push_int( io_read_byte_uc(io) );
pike.git/src/modules/_Stdio/buffer.cmod:1868:    *! return it.    *!    *! Will return -1 if there is not enough buffer space available    *! unless error mode is set to throw errors.    */    PIKEFUN int(0..) read_int( int len )    {    IOBuffer *io = THIS;    struct object *o;    -  pop_stack(); +  Pike_sp--;       if( len < SIZEOF_INT_TYPE-1 ) /* will for sure fit. */    {    push_int( io_read_number( io, len ) );    return;    }       if( (o = io_read_bignum(io, len )) )    {    push_object(o);
pike.git/src/modules/_Stdio/buffer.cmod:1900:    *! Will return 0 if there is not enough buffer space available    *! unless error mode is set to throw errors.    */    PIKEFUN array(int(0..)) read_ints( int num, int len )    {    IOBuffer *io = THIS;    INT_TYPE i;    struct object *o;    struct array *a;    -  pop_n_elems(2); +  Pike_sp-=2;       if( !io_avail_mul( io, num, len ) )    {    push_int(0);    return;    }       if( len < SIZEOF_INT_TYPE-1 ) /* will for sure fit. */    {    push_array(a = allocate_array(num));