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));
29 (void)SvPV_nolen(s->var);
33 SvREFCNT_inc(perl_get_sv
34 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
38 s->var = newSVpvn("", 0);
40 sv_upgrade(s->var, SVt_PV);
41 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
42 if ((PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
44 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
45 s->posn = SvCUR(s->var);
52 PerlIOScalar_popped(pTHX_ PerlIO * f)
54 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
63 PerlIOScalar_close(pTHX_ PerlIO * f)
65 IV code = PerlIOBase_close(aTHX_ f);
66 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
71 PerlIOScalar_fileno(pTHX_ PerlIO * f)
77 PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
79 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
85 s->posn = offset + s->posn;
88 s->posn = offset + SvCUR(s->var);
91 if ((STRLEN) s->posn > SvCUR(s->var)) {
92 (void) SvGROW(s->var, (STRLEN) s->posn);
98 PerlIOScalar_tell(pTHX_ PerlIO * f)
100 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
105 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
107 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
108 char *dst = SvGROW(s->var, s->posn + count);
109 Move(vbuf, dst + s->posn, count, char);
111 SvCUR_set(s->var, s->posn);
117 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
119 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
121 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
124 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
125 dst = SvGROW(sv, SvCUR(sv) + count);
127 s->posn = offset + count;
130 if ((s->posn + count) > SvCUR(sv))
131 dst = SvGROW(sv, s->posn + count);
133 dst = SvPV_nolen(sv);
137 Move(vbuf, dst + offset, count, char);
138 if ((STRLEN) s->posn > SvCUR(sv))
139 SvCUR_set(sv, s->posn);
148 PerlIOScalar_fill(pTHX_ PerlIO * f)
154 PerlIOScalar_flush(pTHX_ PerlIO * f)
160 PerlIOScalar_get_base(pTHX_ PerlIO * f)
162 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
163 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
164 return (STDCHAR *) SvPV_nolen(s->var);
166 return (STDCHAR *) Nullch;
170 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
172 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
173 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
174 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
176 return (STDCHAR *) Nullch;
180 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
182 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
183 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
184 if (SvCUR(s->var) > (STRLEN) s->posn)
185 return SvCUR(s->var) - s->posn;
193 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
195 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
196 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
197 return SvCUR(s->var);
203 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
205 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
206 s->posn = SvCUR(s->var) - cnt;
210 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
211 const char *mode, int fd, int imode, int perm,
212 PerlIO * f, int narg, SV ** args)
214 SV *arg = (narg > 0) ? *args : PerlIOArg;
215 if (SvROK(arg) || SvPOK(arg)) {
217 f = PerlIO_allocate(aTHX);
219 if (f = PerlIO_push(aTHX_ f, self, mode, arg)) {
220 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
228 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
230 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
232 if (flags & PERLIO_DUP_CLONE)
233 var = PerlIO_sv_dup(aTHX_ var, param);
234 else if (flags & PERLIO_DUP_FD) {
235 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
239 var = SvREFCNT_inc(var);
241 return newRV_noinc(var);
245 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
248 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
249 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
250 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
251 /* var has been set by implicit push */
257 PerlIO_funcs PerlIO_scalar = {
258 sizeof(PerlIO_funcs),
260 sizeof(PerlIOScalar),
261 PERLIO_K_BUFFERED | PERLIO_K_RAW,
280 PerlIOBase_setlinebuf,
281 PerlIOScalar_get_base,
283 PerlIOScalar_get_ptr,
284 PerlIOScalar_get_cnt,
285 PerlIOScalar_set_ptrcnt,
289 #endif /* Layers available */
291 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
298 PerlIO_define_layer(aTHX_ &PerlIO_scalar);