e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
387cbc2001-06-21Per Hedbor #include "global.h" #include "stralloc.h" #include "pike_macros.h" #include "interpret.h" #include "program.h" #include "program_id.h" #include "object.h" #include "operators.h" #include "module_support.h" #include "config.h" #include "buffer.h" static void buffer_mkspace( struct buffer *d, int n ) { while( d->size+n > d->allocated_size ) { d->allocated_size += 512; d->data = realloc( d->data, d->allocated_size*4 ); } }
cf19532005-12-30Martin Nilsson void uc_buffer_write( struct buffer *d, p_wchar2 data )
387cbc2001-06-21Per Hedbor { buffer_mkspace( d, 1 ); d->data[d->size++] = data; } INT32 uc_buffer_read( struct buffer *d ) { if( d->rpos < d->size ) return d->data[d->rpos++]; return 0; } struct buffer *uc_buffer_new_size( int s ) { struct buffer *res = malloc( sizeof( struct buffer ) ); res->allocated_size = s; res->size = 0; res->data = malloc(s * 4); return res; } struct buffer *uc_buffer_new( ) { return uc_buffer_new_size( 32 ); } struct buffer *uc_buffer_from_pikestring( struct pike_string *s ) { struct buffer *res = uc_buffer_new_size( s->len ); uc_buffer_write_pikestring( res, s ); return res; }
4bed502001-07-04Per Hedbor struct buffer *uc_buffer_write_pikestring( struct buffer *d, struct pike_string *s )
387cbc2001-06-21Per Hedbor { switch( s->size_shift ) { case 0: {
cf19532005-12-30Martin Nilsson  p_wchar0 *p = STR0(s);
387cbc2001-06-21Per Hedbor  int i; for( i = 0; i<s->len; i++ ) uc_buffer_write( d, p[i] ); } break; case 1: {
cf19532005-12-30Martin Nilsson  p_wchar1 *p = STR1(s);
387cbc2001-06-21Per Hedbor  int i; for( i = 0; i<s->len; i++ ) uc_buffer_write( d, p[i] ); } break; case 2: {
cf19532005-12-30Martin Nilsson  p_wchar2 *p = STR2(s);
387cbc2001-06-21Per Hedbor  int i; for( i = 0; i<s->len; i++ ) uc_buffer_write( d, p[i] ); } break; }
4bed502001-07-04Per Hedbor  return d;
387cbc2001-06-21Per Hedbor } void uc_buffer_free( struct buffer *d) { free( d->data ); free( d ); } struct pike_string *uc_buffer_to_pikestring( struct buffer *d ) { struct pike_string * s = make_shared_binary_string2( d->data, d->size ); uc_buffer_free( d ); return s; }
cf19532005-12-30Martin Nilsson void uc_buffer_insert( struct buffer *b, unsigned int pos, p_wchar2 c )
387cbc2001-06-21Per Hedbor { unsigned int i; if( pos == b->size ) uc_buffer_write( b, c ); else { uc_buffer_write( b, 0 );
9f85792002-04-19Henrik Grubbström (Grubba)  for( i = b->size-1; i>pos; i-- )
387cbc2001-06-21Per Hedbor  b->data[i] = b->data[i-1]; b->data[pos] = c; } }