1 #define PERL_NO_GET_CONTEXT
10 struct _PerlIO base; /* Base "class" info */
16 PerlIOScalar_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
20 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
21 /* If called (normally) via open() then arg is ref to scalar we are
22 * using, otherwise arg (from binmode presumably) is either NULL
23 * or the _name_ of the scalar
27 s->var = SvREFCNT_inc(SvRV(arg));
31 SvREFCNT_inc(perl_get_sv
32 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
36 s->var = newSVpvn("", 0);
38 sv_upgrade(s->var, SVt_PV);
39 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
40 if ((PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
42 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
43 s->posn = SvCUR(s->var);
50 PerlIOScalar_popped(pTHX_ PerlIO * f)
52 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
61 PerlIOScalar_close(pTHX_ PerlIO * f)
63 IV code = PerlIOBase_close(aTHX_ f);
64 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
69 PerlIOScalar_fileno(pTHX_ PerlIO * f)
75 PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
77 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
83 s->posn = offset + s->posn;
86 s->posn = offset + SvCUR(s->var);
89 if ((STRLEN) s->posn > SvCUR(s->var)) {
90 (void) SvGROW(s->var, (STRLEN) s->posn);
96 PerlIOScalar_tell(pTHX_ PerlIO * f)
98 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
103 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
105 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
106 char *dst = SvGROW(s->var, s->posn + count);
107 Move(vbuf, dst + s->posn, count, char);
109 SvCUR_set(s->var, s->posn);
115 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
117 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
119 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
122 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
123 dst = SvGROW(sv, SvCUR(sv) + count);
125 s->posn = offset + count;
128 if ((s->posn + count) > SvCUR(sv))
129 dst = SvGROW(sv, s->posn + count);
131 dst = SvPV_nolen(sv);
135 Move(vbuf, dst + offset, count, char);
136 if ((STRLEN) s->posn > SvCUR(sv))
137 SvCUR_set(sv, s->posn);
146 PerlIOScalar_fill(pTHX_ PerlIO * f)
152 PerlIOScalar_flush(pTHX_ PerlIO * f)
158 PerlIOScalar_get_base(pTHX_ PerlIO * f)
160 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
161 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
162 return (STDCHAR *) SvPV_nolen(s->var);
164 return (STDCHAR *) Nullch;
168 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
170 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
171 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
172 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
174 return (STDCHAR *) Nullch;
178 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
180 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
181 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
182 if (SvCUR(s->var) > (STRLEN) s->posn)
183 return SvCUR(s->var) - s->posn;
191 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
193 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
194 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
195 return SvCUR(s->var);
201 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
203 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
204 s->posn = SvCUR(s->var) - cnt;
208 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
209 const char *mode, int fd, int imode, int perm,
210 PerlIO * f, int narg, SV ** args)
212 SV *arg = (narg > 0) ? *args : PerlIOArg;
213 if (SvROK(arg) || SvPOK(arg)) {
215 f = PerlIO_allocate(aTHX);
217 if (f = PerlIO_push(aTHX_ f, self, mode, arg)) {
218 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
226 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
228 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
230 if (flags & PERLIO_DUP_CLONE)
231 var = PerlIO_sv_dup(aTHX_ var, param);
232 else if (flags & PERLIO_DUP_FD) {
233 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
237 var = SvREFCNT_inc(var);
239 return newRV_noinc(var);
243 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
246 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
247 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
248 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
249 /* var has been set by implicit push */
255 PerlIO_funcs PerlIO_scalar = {
256 sizeof(PerlIO_funcs),
258 sizeof(PerlIOScalar),
259 PERLIO_K_BUFFERED | PERLIO_K_RAW,
278 PerlIOBase_setlinebuf,
279 PerlIOScalar_get_base,
281 PerlIOScalar_get_ptr,
282 PerlIOScalar_get_cnt,
283 PerlIOScalar_set_ptrcnt,
287 #endif /* Layers available */
289 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
296 PerlIO_define_layer(aTHX_ &PerlIO_scalar);