...
 
Commits (4)
...@@ -455,14 +455,16 @@ PIKECLASS Buffer ...@@ -455,14 +455,16 @@ PIKECLASS Buffer
PMOD_EXPORT void io_actually_trigger_output( Buffer *io ) PMOD_EXPORT void io_actually_trigger_output( Buffer *io )
{ {
if (PIKE_TYPEOF(io->output) == PIKE_T_FUNCTION) { if (UNLIKELY(!io->output.u.object->prog)) {
if (UNLIKELY(!io->output.u.object->prog)) { /* Destructed object. */
/* Destructed object. */ free_svalue(&io->output);
free_svalue(&io->output); SET_SVAL(io->output, PIKE_T_INT, NUMBER_NUMBER, integer, 0);
return; return;
} }
if ( io->output.u.object ) {
move_svalue(Pike_sp++, &io->output); move_svalue(Pike_sp++, &io->output);
SET_SVAL(io->output, PIKE_T_INT, NUMBER_NUMBER, integer, 0);
apply_svalue(Pike_sp-1, 0); apply_svalue(Pike_sp-1, 0);
pop_n_elems(2); pop_n_elems(2);
} }
...@@ -887,15 +889,9 @@ PIKECLASS Buffer ...@@ -887,15 +889,9 @@ PIKECLASS Buffer
} }
} }
static Buffer* already_destructed() {
Pike_error("Stdio.Buffer already destructed.\n");
return 0;
}
#undef THIS #undef THIS
#define THIS (&(((struct Buffer_struct *)Pike_fp->current_storage)->b)) #define THIS (&(((struct Buffer_struct *)Pike_fp->current_storage)->b))
#define CHECK_DESTRUCTED(io) ((io)->this ? (io) : already_destructed())
/* pike functions */ /* pike functions */
...@@ -913,7 +909,7 @@ PIKECLASS Buffer ...@@ -913,7 +909,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(-1..) input_from( object f, int|void _nbytes, int|void _once ) PIKEFUN int(-1..) input_from( object f, int|void _nbytes, int|void _once )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
size_t sz = io_len( io ); size_t sz = io_len( io );
size_t bread = 0, nbytes = (size_t)-1; size_t bread = 0, nbytes = (size_t)-1;
struct my_file *fd; struct my_file *fd;
...@@ -989,13 +985,16 @@ PIKECLASS Buffer ...@@ -989,13 +985,16 @@ PIKECLASS Buffer
*/ */
PIKEFUN void __set_on_write( zero|function f ) PIKEFUN void __set_on_write( zero|function f )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if (f) if (f)
assign_svalue(&io->output, f); assign_svalue(&io->output, f);
else { else {
if (PIKE_TYPEOF(io->output) == PIKE_T_FUNCTION) if (io->output.u.object) {
free_svalue(&io->output); free_svalue(&io->output);
io->output.u.object = 0; // free_svalue() puts in -1
}
SET_SVAL(io->output, PIKE_T_INT, NUMBER_NUMBER, integer, 0);
} }
} }
...@@ -1037,7 +1036,7 @@ PIKECLASS Buffer ...@@ -1037,7 +1036,7 @@ PIKECLASS Buffer
PIKEFUN int(-1..) output_to( object|function(string(8bit):int) f, PIKEFUN int(-1..) output_to( object|function(string(8bit):int) f,
int|void nbytes ) int|void nbytes )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
ptrdiff_t written = 0; ptrdiff_t written = 0;
ptrdiff_t sz = io_len( io ); ptrdiff_t sz = io_len( io );
int write_fun_num = -1; int write_fun_num = -1;
...@@ -1125,7 +1124,7 @@ PIKECLASS Buffer ...@@ -1125,7 +1124,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int read_sint( int nbytes ) PIKEFUN int read_sint( int nbytes )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
struct pike_string *tmp; struct pike_string *tmp;
Pike_sp--; Pike_sp--;
if( !io_avail( io, nbytes ) ) if( !io_avail( io, nbytes ) )
...@@ -1158,7 +1157,7 @@ PIKECLASS Buffer ...@@ -1158,7 +1157,7 @@ PIKECLASS Buffer
PIKEFUN int(0..) _size_object( ) PIKEFUN int(0..) _size_object( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if (io->malloced) if (io->malloced)
{ {
push_ulongest(THIS->allocated); push_ulongest(THIS->allocated);
...@@ -1177,7 +1176,7 @@ PIKECLASS Buffer ...@@ -1177,7 +1176,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_padding( int(0..) nbytes, int|void _byte ) PIKEFUN Buffer add_padding( int(0..) nbytes, int|void _byte )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
int byte = 0; int byte = 0;
if( _byte ) byte = _byte->u.integer; if( _byte ) byte = _byte->u.integer;
...@@ -1224,8 +1223,8 @@ PIKECLASS Buffer ...@@ -1224,8 +1223,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add( object|string(8bit)|int|array(object|string(8bit)|int) ... argp) PIKEFUN Buffer add( object|string(8bit)|int|array(object|string(8bit)|int) ... argp)
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
int i; int i;
Buffer *io = THIS;
for(i=0; i<args; i++ ) for(i=0; i<args; i++ )
io_append_svalue( io, argp+i ); io_append_svalue( io, argp+i );
...@@ -1239,7 +1238,7 @@ PIKECLASS Buffer ...@@ -1239,7 +1238,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int8( int i ) PIKEFUN Buffer add_int8( int i )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
*io_add_space(io,1,0)=i; *io_add_space(io,1,0)=i;
io->len++; io->len++;
Pike_sp--; Pike_sp--;
...@@ -1252,8 +1251,8 @@ PIKECLASS Buffer ...@@ -1252,8 +1251,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int8( object mpz ) PIKEFUN Buffer add_int8( object mpz )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT64 i = 0; INT64 i = 0;
Buffer *io = THIS;
unsigned char *p = io_add_space(io,1,0); unsigned char *p = io_add_space(io,1,0);
if (!low_int64_from_bignum(&i, mpz)) { if (!low_int64_from_bignum(&i, mpz)) {
SIMPLE_ARG_TYPE_ERROR("add_int8", 1, "int|Gmp.mpz"); SIMPLE_ARG_TYPE_ERROR("add_int8", 1, "int|Gmp.mpz");
...@@ -1269,7 +1268,7 @@ PIKECLASS Buffer ...@@ -1269,7 +1268,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int16( int i ) PIKEFUN Buffer add_int16( int i )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
unsigned char *p = io_add_space(io,2,0); unsigned char *p = io_add_space(io,2,0);
p[0] = i>>8; p[0] = i>>8;
p[1] = i; p[1] = i;
...@@ -1284,8 +1283,8 @@ PIKECLASS Buffer ...@@ -1284,8 +1283,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int16( object mpz ) PIKEFUN Buffer add_int16( object mpz )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT64 i = 0; INT64 i = 0;
Buffer *io = THIS;
unsigned char *p = io_add_space(io,2,0); unsigned char *p = io_add_space(io,2,0);
if (!low_int64_from_bignum(&i, mpz)) { if (!low_int64_from_bignum(&i, mpz)) {
SIMPLE_ARG_TYPE_ERROR("add_int16", 1, "int|Gmp.mpz"); SIMPLE_ARG_TYPE_ERROR("add_int16", 1, "int|Gmp.mpz");
...@@ -1301,7 +1300,7 @@ PIKECLASS Buffer ...@@ -1301,7 +1300,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int32( int i ) PIKEFUN Buffer add_int32( int i )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
unsigned char *p = io_add_space(io,4,0); unsigned char *p = io_add_space(io,4,0);
p[0] = i>>24; p[0] = i>>24;
p[1] = i>>16; p[1] = i>>16;
...@@ -1317,8 +1316,8 @@ PIKECLASS Buffer ...@@ -1317,8 +1316,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int32( object mpz ) PIKEFUN Buffer add_int32( object mpz )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT64 i = 0; INT64 i = 0;
Buffer *io = THIS;
unsigned char *p = io_add_space(io,4,0); unsigned char *p = io_add_space(io,4,0);
if (!low_int64_from_bignum(&i, mpz)) { if (!low_int64_from_bignum(&i, mpz)) {
SIMPLE_ARG_TYPE_ERROR("add_int32", 1, "int|Gmp.mpz"); SIMPLE_ARG_TYPE_ERROR("add_int32", 1, "int|Gmp.mpz");
...@@ -1382,7 +1381,7 @@ PIKECLASS Buffer ...@@ -1382,7 +1381,7 @@ PIKECLASS Buffer
int|string(8bit)|object|array(int|string(8bit)|Buffer|array) str, int|string(8bit)|object|array(int|string(8bit)|Buffer|array) str,
int size_size, void|int offset ) int size_size, void|int offset )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
size_t len = io_svalue_len(io, str); size_t len = io_svalue_len(io, str);
if( offset ) if( offset )
...@@ -1408,17 +1407,16 @@ PIKECLASS Buffer ...@@ -1408,17 +1407,16 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_int( object|int i, int width ) PIKEFUN Buffer add_int( object|int i, int width )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
pop_stack(); /* width */ pop_stack(); /* width */
if( TYPEOF(*i) == PIKE_T_INT ) if( TYPEOF(*i) == PIKE_T_INT )
{ {
io_add_int( io, i->u.integer, width ); io_add_int( THIS, i->u.integer, width );
Pike_sp--; Pike_sp--;
} }
else else
{ {
convert_stack_top_to_bignum(); convert_stack_top_to_bignum();
io_add_bignum( io, i->u.object, width ); io_add_bignum( THIS, i->u.object, width );
pop_stack(); /* o. */ pop_stack(); /* o. */
} }
ref_push_object(Pike_fp->current_object); ref_push_object(Pike_fp->current_object);
...@@ -1435,21 +1433,20 @@ PIKECLASS Buffer ...@@ -1435,21 +1433,20 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_hint( object|int i, int len_width ) PIKEFUN Buffer add_hint( object|int i, int len_width )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
int width; int width;
pop_stack(); /* width */ pop_stack(); /* width */
if( TYPEOF(*i) == PIKE_T_INT ) if( TYPEOF(*i) == PIKE_T_INT )
{ {
INT_TYPE ui = i->u.integer; INT_TYPE ui = i->u.integer;
if (!ui) { if (!ui) {
io_add_int( io, 0, len_width ); io_add_int( THIS, 0, len_width );
} else { } else {
for( width=1; width<SIZEOF_INT_TYPE; width++ ) for( width=1; width<SIZEOF_INT_TYPE; width++ )
if( ui < (((INT_TYPE)1)<<(width*8)) && if( ui < (((INT_TYPE)1)<<(width*8)) &&
ui >= -(((INT_TYPE)1)<<(width*8-1)) ) ui >= -(((INT_TYPE)1)<<(width*8-1)) )
break; break;
io_add_int( io, width, len_width ); io_add_int( THIS, width, len_width );
io_add_int( io, i->u.integer, width ); io_add_int( THIS, i->u.integer, width );
} }
Pike_sp--; Pike_sp--;
} }
...@@ -1457,8 +1454,8 @@ PIKECLASS Buffer ...@@ -1457,8 +1454,8 @@ PIKECLASS Buffer
{ {
convert_stack_top_to_bignum(); convert_stack_top_to_bignum();
width = (mpz_sizeinbase( (void*)i->u.object->storage, 2)+7)/8; width = (mpz_sizeinbase( (void*)i->u.object->storage, 2)+7)/8;
io_add_int( io, width, len_width ); io_add_int( THIS, width, len_width );
io_add_bignum( io, i->u.object, width ); io_add_bignum( THIS, i->u.object, width );
pop_stack(); /* o. */ pop_stack(); /* o. */
} }
ref_push_object(Pike_fp->current_object); ref_push_object(Pike_fp->current_object);
...@@ -1478,12 +1475,12 @@ PIKECLASS Buffer ...@@ -1478,12 +1475,12 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer add_ints( array(int) a, int bpi ) PIKEFUN Buffer add_ints( array(int) a, int bpi )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
int i,l = a->size; int i,l = a->size;
struct svalue *it = a->item; struct svalue *it = a->item;
unsigned char *ptr; unsigned char *ptr;
ptrdiff_t n=0; ptrdiff_t n=0;
ONERROR e; ONERROR e;
Buffer *io = THIS;
io_unwrite_on_error(io, &e); io_unwrite_on_error(io, &e);
...@@ -1552,7 +1549,7 @@ PIKECLASS Buffer ...@@ -1552,7 +1549,7 @@ PIKECLASS Buffer
PIKEFUN int(8bit) `[]( int off ) PIKEFUN int(8bit) `[]( int off )
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if( off < 0 ) if( off < 0 )
off = io_len(io)-off; off = io_len(io)-off;
...@@ -1569,7 +1566,7 @@ PIKECLASS Buffer ...@@ -1569,7 +1566,7 @@ PIKECLASS Buffer
PIKEFUN void `[]=( int off, int val ) PIKEFUN void `[]=( int off, int val )
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
io_ensure_malloced( io, 0 ); io_ensure_malloced( io, 0 );
...@@ -1596,8 +1593,7 @@ PIKECLASS Buffer ...@@ -1596,8 +1593,7 @@ PIKECLASS Buffer
PIKEFUN int(0..) _sizeof() PIKEFUN int(0..) _sizeof()
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_ulongest(io_len(THIS));
push_ulongest(io_len(io));
} }
/*! @decl string(8bit) cast("string") /*! @decl string(8bit) cast("string")
...@@ -1610,16 +1606,15 @@ PIKECLASS Buffer ...@@ -1610,16 +1606,15 @@ PIKECLASS Buffer
PIKEFUN string(8bit) cast(string to) PIKEFUN string(8bit) cast(string to)
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
if( to != literal_string_string ) if( to != literal_string_string )
{ {
push_undefined(); push_undefined();
return; return;
} }
if( io_len(io) > 0x7fffffff ) if( io_len(THIS) > 0x7fffffff )
Pike_error("This buffer is too large to convert to a string.\n"); Pike_error("This buffer is too large to convert to a string.\n");
push_string(make_shared_binary_string((void*)io_read_pointer(io), push_string(make_shared_binary_string((void*)io_read_pointer(THIS),
(INT32)io_len(io))); (INT32)io_len(THIS)));
} }
...@@ -1675,11 +1670,10 @@ PIKECLASS Buffer ...@@ -1675,11 +1670,10 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer set_error_mode( int|program m ) PIKEFUN Buffer set_error_mode( int|program m )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
if( TYPEOF(*m) == PIKE_T_INT ) if( TYPEOF(*m) == PIKE_T_INT )
io_set_error_mode( io, m->u.integer ? buffer_error_program : 0 ); io_set_error_mode( THIS, m->u.integer ? buffer_error_program : 0 );
else else
io_set_error_mode( io, program_from_svalue(m)); io_set_error_mode( THIS, program_from_svalue(m));
pop_stack(); pop_stack();
ref_push_object(Pike_fp->current_object); ref_push_object(Pike_fp->current_object);
...@@ -1694,14 +1688,12 @@ PIKECLASS Buffer ...@@ -1694,14 +1688,12 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer lock() PIKEFUN Buffer lock()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_object( io_read_buffer( THIS, 0, 0 ) );
push_object( io_read_buffer( io, 0, 0 ) );
} }
PIKEFUN string(8bit) _sprintf(int o, mapping|void UNUSED) PIKEFUN string(8bit) _sprintf(int o, mapping|void UNUSED)
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
size_t bytes; size_t bytes;
pop_n_elems(args-1); pop_n_elems(args-1);
Pike_sp--; Pike_sp--;
...@@ -1712,15 +1704,15 @@ PIKECLASS Buffer ...@@ -1712,15 +1704,15 @@ PIKECLASS Buffer
push_static_text("%O(%d bytes, read=[..%d] data=[%d..%d] free=[%d..%d] %s%s)"); push_static_text("%O(%d bytes, read=[..%d] data=[%d..%d] free=[%d..%d] %s%s)");
ref_push_program(Pike_fp->current_object->prog); ref_push_program(Pike_fp->current_object->prog);
/* io_len [..offset] [offset..len] [..allocated] */ /* io_len [..offset] [offset..len] [..allocated] */
push_int(io_len(io)); push_int(io_len(THIS));
push_int(io->offset-1); push_int(THIS->offset-1);
push_int(io->offset); push_int(THIS->offset);
push_int(io->len-1); push_int(THIS->len-1);
push_int(io->len); push_int(THIS->len);
push_int(io->allocated); push_int(THIS->allocated);
push_static_text( (io->str ? "string" : io->malloced ? "allocated" : "subbuffer" ) ); push_static_text( (THIS->str ? "string" : THIS->malloced ? "allocated" : "subbuffer" ) );
if( io->locked ) if( THIS->locked )
push_static_text(" (read only)"); push_static_text(" (read only)");
else else
push_static_text(""); push_static_text("");
...@@ -1729,20 +1721,20 @@ PIKECLASS Buffer ...@@ -1729,20 +1721,20 @@ PIKECLASS Buffer
break; break;
case 's': case 's':
bytes = io_len(io); bytes = io_len(THIS);
io->locked_move++; THIS->locked_move++;
push_string( io_read_string(io, bytes) ); push_string( io_read_string(THIS, bytes) );
io_rewind(io, bytes); io_rewind(THIS, bytes);
io->locked_move--; THIS->locked_move--;
break; break;
case 'q': case 'q':
push_static_text("%q"); push_static_text("%q");
bytes = io_len(io); bytes = io_len(THIS);
io->locked_move++; THIS->locked_move++;
push_string( io_read_string(io, bytes) ); push_string( io_read_string(THIS, bytes) );
io_rewind(io, bytes); io_rewind(THIS, bytes);
io->locked_move--; THIS->locked_move--;
f_sprintf(2); f_sprintf(2);
break; break;
default: default:
...@@ -1768,8 +1760,8 @@ PIKECLASS Buffer ...@@ -1768,8 +1760,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN string(8bit) read_hstring( int bytes, void|int offset ) PIKEFUN string(8bit) read_hstring( int bytes, void|int offset )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT64 len; INT64 len;
Buffer *io = THIS;
struct pike_string *s; struct pike_string *s;
ONERROR e; ONERROR e;
...@@ -1826,7 +1818,7 @@ PIKECLASS Buffer ...@@ -1826,7 +1818,7 @@ PIKECLASS Buffer
PIKEFUN string(8bit) read_cstring(void|int(8bit) sentinel, PIKEFUN string(8bit) read_cstring(void|int(8bit) sentinel,
void|int(8bit) escape) void|int(8bit) escape)
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
int csentinel = sentinel ? sentinel->u.integer : 0; int csentinel = sentinel ? sentinel->u.integer : 0;
if (!escape || (escape->u.integer == csentinel)) { if (!escape || (escape->u.integer == csentinel)) {
...@@ -1914,7 +1906,7 @@ PIKECLASS Buffer ...@@ -1914,7 +1906,7 @@ PIKECLASS Buffer
rawtype tFunc(tInt8bit tOr(tInt, tVoid) tOr(tIntPos, tVoid), rawtype tFunc(tInt8bit tOr(tInt, tVoid) tOr(tIntPos, tVoid),
tOr(tInt_10, tIntPos)); tOr(tInt_10, tIntPos));
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
unsigned char *buf = io_read_pointer(io); unsigned char *buf = io_read_pointer(io);
unsigned char *buf_end = buf + io_len(io); unsigned char *buf_end = buf + io_len(io);
...@@ -1984,7 +1976,7 @@ PIKECLASS Buffer ...@@ -1984,7 +1976,7 @@ PIKECLASS Buffer
PIKEFUN int _search(string(8bit) substring, int|void start, int|void end) PIKEFUN int _search(string(8bit) substring, int|void start, int|void end)
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
unsigned char *buf = io_read_pointer(io); unsigned char *buf = io_read_pointer(io);
unsigned char *buf_end = buf + io_len(io); unsigned char *buf_end = buf + io_len(io);
SearchMojt mojt; SearchMojt mojt;
...@@ -2059,9 +2051,9 @@ PIKECLASS Buffer ...@@ -2059,9 +2051,9 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer read_hbuffer( int bytes, int|void copy ) PIKEFUN Buffer read_hbuffer( int bytes, int|void copy )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT64 len; INT64 len;
int do_copy = 0; int do_copy = 0;
Buffer *io = THIS;
ONERROR e; ONERROR e;
io_rewind_on_error( io, &e ); io_rewind_on_error( io, &e );
...@@ -2097,13 +2089,12 @@ PIKECLASS Buffer ...@@ -2097,13 +2089,12 @@ PIKECLASS Buffer
*/ */
PIKEFUN Buffer read_buffer( int bytes, int|void copy ) PIKEFUN Buffer read_buffer( int bytes, int|void copy )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
int do_copy = 0; int do_copy = 0;
struct object *o; struct object *o;
if( copy ) if( copy )
do_copy = copy->u.integer; do_copy = copy->u.integer;
Pike_sp-=args; Pike_sp-=args;
if( (o = io_read_buffer( io, bytes, do_copy )) ) if( (o = io_read_buffer( THIS, bytes, do_copy )) )
push_object(o); push_object(o);
else else
push_int(0); push_int(0);
...@@ -2120,7 +2111,6 @@ PIKECLASS Buffer ...@@ -2120,7 +2111,6 @@ PIKECLASS Buffer
rawtype tFuncV(tAttr("strict_sprintf_format", tOr(tStr, tObj)), rawtype tFuncV(tAttr("strict_sprintf_format", tOr(tStr, tObj)),
tAttr("sprintf_args", tMix), tObjIs_BUFFER); tAttr("sprintf_args", tMix), tObjIs_BUFFER);
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
ONERROR _e; ONERROR _e;
struct string_builder tmp; struct string_builder tmp;
init_string_builder(&tmp,0); init_string_builder(&tmp,0);
...@@ -2128,7 +2118,7 @@ PIKECLASS Buffer ...@@ -2128,7 +2118,7 @@ PIKECLASS Buffer
low_f_sprintf(args, &tmp); low_f_sprintf(args, &tmp);
if( tmp.s->size_shift ) if( tmp.s->size_shift )
Pike_error("Buffer only handles 8bit data.\n"); Pike_error("Buffer only handles 8bit data.\n");
io_append( io, tmp.s->str, tmp.s->len ); io_append( THIS, tmp.s->str, tmp.s->len );
pop_n_elems(args); pop_n_elems(args);
CALL_AND_UNSET_ONERROR(_e); CALL_AND_UNSET_ONERROR(_e);
ref_push_object(Pike_fp->current_object); ref_push_object(Pike_fp->current_object);
...@@ -2145,26 +2135,25 @@ PIKECLASS Buffer ...@@ -2145,26 +2135,25 @@ PIKECLASS Buffer
*/ */
PIKEFUN array sscanf( string(8bit) format ) PIKEFUN array sscanf( string(8bit) format )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT32 i; INT32 i;
ptrdiff_t num_used; ptrdiff_t num_used;
struct svalue *start = Pike_sp; struct svalue *start = Pike_sp;
retry: retry:
i = low_sscanf_pcharp( i = low_sscanf_pcharp(
MKPCHARP(io_read_pointer(io), 0), io_len(io), MKPCHARP(io_read_pointer(THIS), 0), io_len(THIS),
MKPCHARP(format->str,format->size_shift), format->len, MKPCHARP(format->str,format->size_shift), format->len,
&num_used); &num_used);
if( !num_used ) if( !num_used )
{ {
if( io_range_error(io,0) ) if( io_range_error(THIS,0) )
goto retry; goto retry;
pop_n_elems(Pike_sp-start); pop_n_elems(Pike_sp-start);
push_int(0); push_int(0);
} }
else else
{ {
io_consume( io, num_used ); io_consume( THIS, num_used );
f_aggregate(Pike_sp-start); f_aggregate(Pike_sp-start);
} }
} }
...@@ -2192,7 +2181,6 @@ PIKECLASS Buffer ...@@ -2192,7 +2181,6 @@ PIKECLASS Buffer
*/ */
PIKEFUN mixed read_json(int|void require_whitespace) PIKEFUN mixed read_json(int|void require_whitespace)
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT_TYPE whites = 0; INT_TYPE whites = 0;
ptrdiff_t stop; ptrdiff_t stop;
static ptrdiff_t(*parse_json_pcharp)(PCHARP,size_t,int,char**); static ptrdiff_t(*parse_json_pcharp)(PCHARP,size_t,int,char**);
...@@ -2204,14 +2192,14 @@ PIKECLASS Buffer ...@@ -2204,14 +2192,14 @@ PIKECLASS Buffer
if( !parse_json_pcharp ) if( !parse_json_pcharp )
parse_json_pcharp = PIKE_MODULE_IMPORT(Standards.JSON, parse_json_pcharp ); parse_json_pcharp = PIKE_MODULE_IMPORT(Standards.JSON, parse_json_pcharp );
retry: retry:
stop = parse_json_pcharp( MKPCHARP(io_read_pointer(io),0), stop = parse_json_pcharp( MKPCHARP(io_read_pointer(THIS),0),
io_len(io), 1|8, &err ); /* json_utf8 */ io_len(THIS), 1|8, &err ); /* json_utf8 */
if( stop < 0 ) if( stop < 0 )
{ {
if( -stop == (ptrdiff_t)io_len(io) || (err && !strncmp(err,"Unterminated",12))) if( -stop == (ptrdiff_t)io_len(THIS) || (err && !strncmp(err,"Unterminated",12)))
{ {
if( io_range_error(io,0) ) if( io_range_error(THIS,0) )
goto retry; goto retry;
push_undefined(); push_undefined();
} }
...@@ -2227,11 +2215,11 @@ PIKECLASS Buffer ...@@ -2227,11 +2215,11 @@ PIKECLASS Buffer
else else
{ {
if( whites && if( whites &&
(io_is_whitespace(io,stop)<=0 && io_is_whitespace(io,stop-1)<=0)) (io_is_whitespace(THIS,stop)<=0 && io_is_whitespace(THIS,stop-1)<=0))
{ {
if( stop == (ptrdiff_t)io_len(io) ) if( stop == (ptrdiff_t)io_len(THIS) )
{ {
if( io_range_error(io,0) ) if( io_range_error(THIS,0) )
goto retry; goto retry;
pop_stack(); pop_stack();
push_undefined(); push_undefined();
...@@ -2242,9 +2230,9 @@ PIKECLASS Buffer ...@@ -2242,9 +2230,9 @@ PIKECLASS Buffer
else else
{ {
if( whites ) if( whites )
while( io_is_whitespace( io, stop ) ) while( io_is_whitespace( THIS, stop ) )
stop++; stop++;
io_consume( io, stop ); io_consume( THIS, stop );
} }
} }
} }
...@@ -2268,26 +2256,25 @@ PIKECLASS Buffer ...@@ -2268,26 +2256,25 @@ PIKECLASS Buffer
*/ */
PIKEFUN string(8bit)|int|float|array match( string(8bit) format ) PIKEFUN string(8bit)|int|float|array match( string(8bit) format )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
INT32 i; INT32 i;
ptrdiff_t num_used; ptrdiff_t num_used;
struct svalue *start = Pike_sp; struct svalue *start = Pike_sp;
retry: retry:
i = low_sscanf_pcharp( i = low_sscanf_pcharp(
MKPCHARP(io_read_pointer(io), 0), io_len(io), MKPCHARP(io_read_pointer(THIS), 0), io_len(THIS),
MKPCHARP(format->str,format->size_shift), format->len, MKPCHARP(format->str,format->size_shift), format->len,
&num_used); &num_used);
if( !num_used ) if( !num_used )
{ {
if( io_range_error(io,0) ) if( io_range_error(THIS,0) )
goto retry; goto retry;
pop_n_elems(Pike_sp-start); pop_n_elems(Pike_sp-start);
push_int(0); push_int(0);
} }
else else
{ {
io_consume( io, num_used ); io_consume( THIS, num_used );
if( Pike_sp-start > 1 ) if( Pike_sp-start > 1 )
f_add(Pike_sp-start); f_add(Pike_sp-start);
} }
...@@ -2299,7 +2286,7 @@ PIKECLASS Buffer ...@@ -2299,7 +2286,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN void clear( ) PIKEFUN void clear( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
io->offset = io->len = 0; io->offset = io->len = 0;
} }
...@@ -2313,7 +2300,7 @@ PIKECLASS Buffer ...@@ -2313,7 +2300,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN void set_max_waste(float howmuch) PIKEFUN void set_max_waste(float howmuch)
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
io->max_waste = howmuch; io->max_waste = howmuch;
io_add_space( io, 0, 1 ); io_add_space( io, 0, 1 );
io_trim_waste( io ); io_trim_waste( io );
...@@ -2332,7 +2319,7 @@ PIKECLASS Buffer ...@@ -2332,7 +2319,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN void trim( ) PIKEFUN void trim( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
io_add_space( io, 0, 1 ); io_add_space( io, 0, 1 );
io_trim(io); io_trim(io);
...@@ -2344,7 +2331,7 @@ PIKECLASS Buffer ...@@ -2344,7 +2331,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN void allocate( int n ) PIKEFUN void allocate( int n )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
Pike_sp--; Pike_sp--;
if (!n) return; if (!n) return;
if (n < 0) if (n < 0)
...@@ -2360,12 +2347,11 @@ PIKECLASS Buffer ...@@ -2360,12 +2347,11 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(-1..) consume( int n ) PIKEFUN int(-1..) consume( int n )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
Pike_sp--; Pike_sp--;
if( !io_avail( io, n ) ) if( !io_avail( THIS, n ) )
push_int(-1); push_int(-1);
else else
push_int64( io_consume( io, n ) ); push_int64( io_consume( THIS, n ) );
} }
/*! @decl int(0..)|int(-1..-1) truncate( int(0..) n ) /*! @decl int(0..)|int(-1..-1) truncate( int(0..) n )
...@@ -2376,7 +2362,7 @@ PIKECLASS Buffer ...@@ -2376,7 +2362,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(-1..) truncate( int(0..) n ) PIKEFUN int(-1..) truncate( int(0..) n )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
ptrdiff_t diff = io_len(io) - n; ptrdiff_t diff = io_len(io) - n;
Pike_sp--; Pike_sp--;
...@@ -2406,9 +2392,8 @@ PIKECLASS Buffer ...@@ -2406,9 +2392,8 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(-1..) unread( int bytes ) PIKEFUN int(-1..) unread( int bytes )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
Pike_sp--; Pike_sp--;
push_int64( io_rewind( io, bytes ) ); push_int64( io_rewind( THIS, bytes ) );
} }
/*! @decl string(8bit) read( int n ) /*! @decl string(8bit) read( int n )
...@@ -2422,10 +2407,9 @@ PIKECLASS Buffer ...@@ -2422,10 +2407,9 @@ PIKECLASS Buffer
*/ */
PIKEFUN string(8bit) read( int bytes ) PIKEFUN string(8bit) read( int bytes )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS);
struct pike_string *s; struct pike_string *s;
Pike_sp--; Pike_sp--;
s = io_read_string(io, bytes ); s = io_read_string(THIS, bytes );
if( s ) if( s )
push_string( s ); push_string( s );
else else
...@@ -2446,8 +2430,7 @@ PIKECLASS Buffer ...@@ -2446,8 +2430,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN string(8bit) read() PIKEFUN string(8bit) read()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_string( io_read_string(THIS, io_len(THIS)) );
push_string( io_read_string(io, io_len(io)) );
} }
/*! @decl string(8bit) try_read(int len) /*! @decl string(8bit) try_read(int len)
...@@ -2467,20 +2450,20 @@ PIKECLASS Buffer ...@@ -2467,20 +2450,20 @@ PIKECLASS Buffer
*/ */
PIKEFUN string(8bit) try_read( int bytes ) PIKEFUN string(8bit) try_read( int bytes )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *this = THIS;
struct pike_string *s; struct pike_string *s;
Pike_sp--; Pike_sp--;
/* Hm. signed/unsigned comparisons abound. */ /* Hm. signed/unsigned comparisons abound. */
if( bytes > 0 && (size_t)bytes > io_len(io) ) if( bytes > 0 && (size_t)bytes > io_len(this) )
bytes = io_len(io); bytes = io_len(this);
push_string( io_read_string(io, bytes ) ); push_string( io_read_string(this, bytes ) );
} }
/*! @decl int(8bit) read_int8() /*! @decl int(8bit) read_int8()
*/ */
PIKEFUN int(8bit) read_int8( ) PIKEFUN int(8bit) read_int8( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if( LIKELY(io_avail( io, 1 )) ) if( LIKELY(io_avail( io, 1 )) )
push_int( io_read_byte_uc(io) ); push_int( io_read_byte_uc(io) );
else else
...@@ -2491,7 +2474,7 @@ PIKECLASS Buffer ...@@ -2491,7 +2474,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..65535) read_int16( ) PIKEFUN int(0..65535) read_int16( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if( LIKELY(io_avail( io, 2 )) ) if( LIKELY(io_avail( io, 2 )) )
push_int( io_read_number_uc(io,2) ); push_int( io_read_number_uc(io,2) );
else else
...@@ -2502,7 +2485,7 @@ PIKECLASS Buffer ...@@ -2502,7 +2485,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..16777215) read_int24( ) PIKEFUN int(0..16777215) read_int24( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if( LIKELY(io_avail( io, 3 )) ) if( LIKELY(io_avail( io, 3 )) )
push_int( io_read_number_uc(io,3) ); push_int( io_read_number_uc(io,3) );
else else
...@@ -2513,7 +2496,7 @@ PIKECLASS Buffer ...@@ -2513,7 +2496,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..4294967295) read_int32( ) PIKEFUN int(0..4294967295) read_int32( )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
if( LIKELY(io_avail( io, 4 )) ) if( LIKELY(io_avail( io, 4 )) )
{ {
push_int( io_read_number_uc(io,4) ); push_int( io_read_number_uc(io,4) );
...@@ -2543,7 +2526,7 @@ PIKECLASS Buffer ...@@ -2543,7 +2526,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..) read_int( int len ) PIKEFUN int(0..) read_int( int len )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
struct object *o; struct object *o;
Pike_sp--; Pike_sp--;
...@@ -2576,7 +2559,7 @@ PIKECLASS Buffer ...@@ -2576,7 +2559,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..) read_le_int( int len ) PIKEFUN int(0..) read_le_int( int len )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
struct object *o; struct object *o;
Pike_sp--; Pike_sp--;
...@@ -2607,7 +2590,7 @@ PIKECLASS Buffer ...@@ -2607,7 +2590,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN int(0..) read_hint( int size_len ) PIKEFUN int(0..) read_hint( int size_len )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
ONERROR e; ONERROR e;
INT_TYPE len; INT_TYPE len;
struct object *o; struct object *o;
...@@ -2648,7 +2631,7 @@ PIKECLASS Buffer ...@@ -2648,7 +2631,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN array(int(0..)) read_ints( int num, int len ) PIKEFUN array(int(0..)) read_ints( int num, int len )
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *io = THIS;
INT_TYPE i; INT_TYPE i;
struct object *o; struct object *o;
struct array *a; struct array *a;
...@@ -2685,21 +2668,20 @@ PIKECLASS Buffer ...@@ -2685,21 +2668,20 @@ PIKECLASS Buffer
*/ */
PIKEFUN string _encode() PIKEFUN string _encode()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_string(io_read_string(THIS, io_len(THIS)));
push_string(io_read_string(io, io_len(io)));
} }
PIKEFUN void _decode(string(8bit) x) PIKEFUN void _decode(string(8bit) x)
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *this = THIS;
if( io->buffer ) if( this->buffer )
Pike_error("Can not initialize twice.\n"); Pike_error("Can not initialize twice.\n");
if( x->size_shift ) if( x->size_shift )
Pike_error("Can not handle non-8bit data.\n"); Pike_error("Can not handle non-8bit data.\n");
io->buffer = (unsigned char*)x->str; this->buffer = (unsigned char*)x->str;
io->len = x->len; this->len = x->len;
io->malloced = 0; this->malloced = 0;
io->str = x; this->str = x;
add_ref(x); add_ref(x);
} }
...@@ -2711,8 +2693,7 @@ PIKECLASS Buffer ...@@ -2711,8 +2693,7 @@ PIKECLASS Buffer
*/ */
PIKEFUN void read_only() PIKEFUN void read_only()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); io_lock( THIS );
io_lock( io );
} }
...@@ -2778,14 +2759,12 @@ PIKECLASS Buffer ...@@ -2778,14 +2759,12 @@ PIKECLASS Buffer
*/ */
PIKEFUN object(Buffer.RewindKey) rewind_on_error() PIKEFUN object(Buffer.RewindKey) rewind_on_error()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_object( io_create_rewind_key( THIS, 1 ) );
push_object( io_create_rewind_key( io, 1 ) );
} }
PIKEFUN object(Buffer.RewindKey) rewind_key() PIKEFUN object(Buffer.RewindKey) rewind_key()
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); push_object( io_create_rewind_key( THIS, 0 ) );
push_object( io_create_rewind_key( io, 0 ) );
} }
/*! @decl void create( int|void len ) /*! @decl void create( int|void len )
...@@ -2819,47 +2798,46 @@ PIKECLASS Buffer ...@@ -2819,47 +2798,46 @@ PIKECLASS Buffer
PIKEFUN void create( int|void|string(8bit)|object|array x ) PIKEFUN void create( int|void|string(8bit)|object|array x )
flags ID_PROTECTED; flags ID_PROTECTED;
{ {
Buffer *io = CHECK_DESTRUCTED(THIS); Buffer *this = THIS;
if( io->buffer ) if( this->buffer )
Pike_error("Can not initialize twice.\n"); Pike_error("Can not initialize twice.\n");
if( !x ) if( !x )
{ {
io->buffer = xalloc(256-32); this->buffer = xalloc(256-32);
io->allocated = 256-32; this->allocated = 256-32;
io->malloced = 1; this->malloced = 1;
} }
else if( TYPEOF(*x) == PIKE_T_INT ) else if( TYPEOF(*x) == PIKE_T_INT )
{ {
INT_TYPE len = x->u.integer; INT_TYPE len = x->u.integer;
if( len <= 0 ) if( len <= 0 )
io->buffer = xalloc(1); this->buffer = xalloc(1);
else else
io->buffer = xalloc(len); this->buffer = xalloc(len);
io->allocated = MAXIMUM(len,1); this->allocated = MAXIMUM(len,1);
io->malloced = 1; this->malloced = 1;
pop_stack(); pop_stack();
} }
else else
{ {
io_append_svalue( io, x ); io_append_svalue( THIS, x );
pop_stack(); pop_stack();
} }
} }
INIT { INIT {
Buffer *io = THIS; Buffer *this = THIS;
io->max_waste = 0.615; this->max_waste = 0.615;
io->this = Pike_fp->current_object; this->this = Pike_fp->current_object;
} }
EXIT { EXIT {
Buffer *io = THIS; Buffer *this = THIS;
io->this = 0; io_unlink_external_storage( this );
io_unlink_external_storage( io ); if( this->error_mode )
if( io->error_mode ) free_program( this->error_mode );
free_program( io->error_mode ); if( this->malloced )
if( io->malloced ) free( this->buffer );
free( io->buffer );
} }
......