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));
28 if (!SvPOK(s->var) && SvTYPE(SvRV(arg)) > SVt_NULL)
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 SvUPGRADE(s->var, SVt_PV);
41 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
42 if (!SvOK(s->var) || (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, (STRLEN)s->posn + count);
110 Move(vbuf, dst + s->posn, count, char);
116 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
118 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
120 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
123 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
124 dst = SvGROW(sv, SvCUR(sv) + count);
126 s->posn = offset + count;
129 if ((s->posn + count) > SvCUR(sv))
130 dst = SvGROW(sv, (STRLEN)s->posn + count);
132 dst = SvPV_nolen(sv);
136 Move(vbuf, dst + offset, count, char);
137 if ((STRLEN) s->posn > SvCUR(sv))
138 SvCUR_set(sv, (STRLEN)s->posn);
147 PerlIOScalar_fill(pTHX_ PerlIO * f)
153 PerlIOScalar_flush(pTHX_ PerlIO * f)
159 PerlIOScalar_get_base(pTHX_ PerlIO * f)
161 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
162 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
163 return (STDCHAR *) SvPV_nolen(s->var);
165 return (STDCHAR *) Nullch;
169 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
171 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
172 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
173 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
175 return (STDCHAR *) Nullch;
179 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
181 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
182 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
183 if (SvCUR(s->var) > (STRLEN) s->posn)
184 return SvCUR(s->var) - (STRLEN)s->posn;
192 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
194 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
195 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
196 return SvCUR(s->var);
202 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
204 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
205 s->posn = SvCUR(s->var) - cnt;
209 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
210 const char *mode, int fd, int imode, int perm,
211 PerlIO * f, int narg, SV ** args)
213 SV *arg = (narg > 0) ? *args : PerlIOArg;
214 if (SvROK(arg) || SvPOK(arg)) {
216 f = PerlIO_allocate(aTHX);
218 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
219 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
227 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
229 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
231 if (flags & PERLIO_DUP_CLONE)
232 var = PerlIO_sv_dup(aTHX_ var, param);
233 else if (flags & PERLIO_DUP_FD) {
234 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
238 var = SvREFCNT_inc(var);
240 return newRV_noinc(var);
244 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
247 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
248 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
249 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
250 /* var has been set by implicit push */
256 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
257 sizeof(PerlIO_funcs),
259 sizeof(PerlIOScalar),
260 PERLIO_K_BUFFERED | PERLIO_K_RAW,
279 PerlIOBase_setlinebuf,
280 PerlIOScalar_get_base,
282 PerlIOScalar_get_ptr,
283 PerlIOScalar_get_cnt,
284 PerlIOScalar_set_ptrcnt,
288 #endif /* Layers available */
290 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
297 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));