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/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;
eb94452019-06-11Stephen R. van den Berg  struct pike_string *str;
8b2e792003-01-14Per Hedbor  struct object *obj;
ff0a162004-07-09Stephen R. van den Berg  INT64 len, skip;
8b2e792003-01-14Per Hedbor };
eb94452019-06-11Stephen R. van den Berg static void frees(struct pf_source*s) { if (s->str) { free_string(s->str); s->str = 0; } }
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;
eb94452019-06-11Stephen R. van den Berg  struct data res;
8b2e792003-01-14Per Hedbor 
f8caf02019-06-18Stephen R. van den Berg  if (s->len >= 0 && len > s->len)
ff0a162004-07-09Stephen R. van den Berg  len = s->len;
13670c2015-05-25Martin Nilsson 
5403882019-08-08Henrik Grubbström (Grubba)  res.data = NULL;
f8caf02019-06-18Stephen R. van den Berg  for (;;) {
8b2e792003-01-14Per Hedbor  struct pike_string *st;
f8caf02019-06-18Stephen R. van den Berg  push_int(len); apply(s->obj, "read", 1);
8b2e792003-01-14Per Hedbor 
f8caf02019-06-18Stephen R. van den Berg  if (TYPEOF(Pike_sp[-1]) != PIKE_T_STRING || (st = Pike_sp[-1].u.string)->size_shift || !st->len) { s->s.eof = 1;
ff0a162004-07-09Stephen R. van den Berg  pop_stack();
f8caf02019-06-18Stephen R. van den Berg  res.len = 0;
ff0a162004-07-09Stephen R. van den Berg  break;
f8caf02019-06-18Stephen R. van den Berg  } else if (st->len <= s->skip) {
8b2e792003-01-14Per Hedbor  s->skip -= st->len;
eb94452019-06-11Stephen R. van den Berg  pop_stack(); } else { frees(s);
74a5892013-05-17Arne Goedeke  res.len = st->len - s->skip;
eb94452019-06-11Stephen R. van den Berg  s->str = st; res.data = st->str + s->skip;
8b2e792003-01-14Per Hedbor  s->skip = 0;
f8caf02019-06-18Stephen R. van den Berg  Pike_sp--; break;
8b2e792003-01-14Per Hedbor  } }
f8caf02019-06-18Stephen R. van den Berg  if (s->len >= 0) {
ff0a162004-07-09Stephen R. van den Berg  s->len -= res.len;
f8caf02019-06-18Stephen R. van den Berg  if(!s->len) s->s.eof = 1; }
8b2e792003-01-14Per Hedbor  return res; }
feb6822009-08-13Henrik Grubbström (Grubba) static void free_source( struct source *src )
8b2e792003-01-14Per Hedbor {
eb94452019-06-11Stephen R. van den Berg  struct pf_source *s = (struct pf_source *)src; frees(s); free_object(s->obj);
8b2e792003-01-14Per Hedbor } struct source *source_block_pikestream_make( struct svalue *s, INT64 start, INT64 len ) { struct pf_source *res;
97611f2019-06-17Stephen R. van den Berg  if (TYPEOF(*s) != PIKE_T_OBJECT || find_identifier("read",s->u.object->prog) < 0)
8b2e792003-01-14Per Hedbor  return 0;
13670c2015-05-25Martin Nilsson 
97611f2019-06-17Stephen R. van den Berg  if (!(res = calloc(1, sizeof(struct pf_source)))) return 0;
8b2e792003-01-14Per Hedbor 
eb94452019-06-11Stephen R. van den Berg  res->str = 0;
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( ) { }