8b2e792003-01-14Per Hedbor /* || 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. */ #include "global.h" #include "bignum.h" #include "object.h" #include "interpret.h" #include "fdlib.h" #include "fd_control.h" #include <sys/types.h> #include <sys/stat.h> #include "shuffler.h" /* Source: blocking pike-stream * Argument: Stdio.File lookalike without read * callback support (read only needed) */ struct pf_source { struct source s; struct object *obj;
ff0a162004-07-09Stephen R. van den Berg  INT64 len, skip;
8b2e792003-01-14Per Hedbor };
feb6822009-08-13Henrik Grubbström (Grubba) static struct data get_data( struct source *src, off_t len )
8b2e792003-01-14Per Hedbor {
feb6822009-08-13Henrik Grubbström (Grubba)  struct pf_source *s = (struct pf_source *)src;
6559812007-11-10Martin Nilsson  struct data res = { 0, 0, 0, NULL };
8b2e792003-01-14Per Hedbor 
ff0a162004-07-09Stephen R. van den Berg  if( s->len>0 && len > s->len ) { len = s->len; s->s.eof = 1; } do {
8b2e792003-01-14Per Hedbor  struct pike_string *st; push_int( len ); apply( s->obj, "read", 1 );
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != PIKE_T_STRING
ff0a162004-07-09Stephen R. van den Berg  || !(st = Pike_sp[-1].u.string)->len) { pop_stack(); break;
8b2e792003-01-14Per Hedbor  } if( st->len < s->skip ) s->skip -= st->len;
ff0a162004-07-09Stephen R. van den Berg  else {
74a5892013-05-17Arne Goedeke  res.len = st->len - s->skip; res.data = xalloc(res.len); memcpy(res.data, st->str+s->skip, res.len);
8b2e792003-01-14Per Hedbor  res.do_free = 1; s->skip = 0; }
ff0a162004-07-09Stephen R. van den Berg  pop_stack();
8b2e792003-01-14Per Hedbor  }
ff0a162004-07-09Stephen R. van den Berg  while(s->skip || !res.len); if(res.len < len)
8b2e792003-01-14Per Hedbor  s->s.eof = 1;
ff0a162004-07-09Stephen R. van den Berg  if(s->len > 0) s->len -= res.len;
8b2e792003-01-14Per Hedbor  return res; }
feb6822009-08-13Henrik Grubbström (Grubba) static void free_source( struct source *src )
8b2e792003-01-14Per Hedbor {
feb6822009-08-13Henrik Grubbström (Grubba)  free_object(((struct pf_source *)src)->obj);
8b2e792003-01-14Per Hedbor } struct source *source_block_pikestream_make( struct svalue *s, INT64 start, INT64 len ) { struct pf_source *res;
017b572011-10-28Henrik Grubbström (Grubba)  if( (TYPEOF(*s) != PIKE_T_OBJECT) ||
8b2e792003-01-14Per Hedbor  (find_identifier("read",s->u.object->prog)==-1) ) return 0;
9c14f32014-04-27Martin Nilsson  res = calloc( 1, sizeof( struct pf_source ) ); if( !res ) return NULL;
8b2e792003-01-14Per Hedbor  res->len = len; res->skip = start; res->s.get_data = get_data; res->s.free_source = free_source; res->obj = s->u.object;
50ea682003-03-14Henrik Grubbström (Grubba)  add_ref(res->obj);
8b2e792003-01-14Per Hedbor  return (struct source *)res; } void source_block_pikestream_exit( ) { } void source_block_pikestream_init( ) { }