1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
/* 
|| 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" 
#include "modules/_Stdio/buffer.h" 
 
 
/* Source: Stdio.Buffer object 
 * Argument: Stdio.Buffer lookalike with read_buffer and consume 
 */ 
 
struct sb_source 
{ 
  struct source s; 
 
  struct object *obj; 
}; 
 
static struct data get_data( struct source *src, off_t len ) 
{ 
  struct sb_source *s = (struct sb_source *)src; 
  struct data res; 
  Buffer *io = io_buffer_from_object(s->obj); 
 
  res.data = (char*)io_read_pointer(io); 
  res.len = io_len(io); 
  s->s.eof = 1; 
  return res; 
} 
 
static void free_source( struct source *src ) 
{ 
  struct sb_source *s = (struct sb_source *)src; 
  free_object(s->obj); 
} 
 
struct source *source_stdio_buffer_make( struct svalue *s, 
                                             INT64 start, INT64 len ) 
{ 
  struct sb_source *res; 
  struct object *o; 
 
  if (TYPEOF(*s) != PIKE_T_OBJECT 
   || find_identifier("read_buffer", s->u.object->prog) < 0) 
    return 0; 
 
  if (!(res = calloc(1, sizeof(struct sb_source)))) 
    return 0; 
 
  o = s->u.object; 
 
  { 
    Buffer *io = io_buffer_from_object(o); 
    INT64 slen = io_len(io); 
 
    if (start) { 
      if (start > slen) 
        start = slen; 
      io_consume(io, start); 
      slen -= start; 
    } 
 
    if (len < 0) 
      len = slen; 
    if (len > slen) 
      len = slen; 
 
    push_int64(len); 
    apply(o, "read_buffer", 1); 
  } 
 
  if (TYPEOF(Pike_sp[-1]) != PIKE_T_OBJECT) 
    Pike_error("Some data went missing.\n"); 
 
  o = Pike_sp[-1].u.object; 
  Pike_sp--; 
 
  res->s.get_data = get_data; 
  res->s.free_source = free_source; 
  add_ref(res->obj = o); 
  return (struct source *)res; 
} 
 
void source_stdio_buffer_exit( ) 
{ 
} 
 
void source_stdio_buffer_init( ) 
{ 
}