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 if (SvREADONLY(SvRV(arg)) && mode && *mode != 'r') {
28 if (ckWARN(WARN_LAYER))
29 Perl_warner(aTHX_ packWARN(WARN_LAYER), PL_no_modify);
33 s->var = SvREFCNT_inc(SvRV(arg));
34 if (!SvPOK(s->var) && SvTYPE(SvRV(arg)) > SVt_NULL)
35 (void)SvPV_nolen(s->var);
39 SvREFCNT_inc(perl_get_sv
40 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
44 s->var = newSVpvn("", 0);
46 SvUPGRADE(s->var, SVt_PV);
47 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
48 if (!SvOK(s->var) || (PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
50 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
51 s->posn = SvCUR(s->var);
58 PerlIOScalar_popped(pTHX_ PerlIO * f)
60 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
69 PerlIOScalar_close(pTHX_ PerlIO * f)
71 IV code = PerlIOBase_close(aTHX_ f);
72 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
77 PerlIOScalar_fileno(pTHX_ PerlIO * f)
83 PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
85 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
91 s->posn = offset + s->posn;
94 s->posn = offset + SvCUR(s->var);
97 if ((STRLEN) s->posn > SvCUR(s->var)) {
98 (void) SvGROW(s->var, (STRLEN) s->posn);
104 PerlIOScalar_tell(pTHX_ PerlIO * f)
106 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
111 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
113 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
114 char *dst = SvGROW(s->var, (STRLEN)s->posn + count);
116 Move(vbuf, dst + s->posn, count, char);
122 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
124 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
126 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
129 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
130 dst = SvGROW(sv, SvCUR(sv) + count);
132 s->posn = offset + count;
135 if ((s->posn + count) > SvCUR(sv))
136 dst = SvGROW(sv, (STRLEN)s->posn + count);
138 dst = SvPV_nolen(sv);
142 Move(vbuf, dst + offset, count, char);
143 if ((STRLEN) s->posn > SvCUR(sv))
144 SvCUR_set(sv, (STRLEN)s->posn);
153 PerlIOScalar_fill(pTHX_ PerlIO * f)
159 PerlIOScalar_flush(pTHX_ PerlIO * f)
165 PerlIOScalar_get_base(pTHX_ PerlIO * f)
167 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
168 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
169 return (STDCHAR *) SvPV_nolen(s->var);
171 return (STDCHAR *) Nullch;
175 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
177 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
178 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
179 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
181 return (STDCHAR *) Nullch;
185 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
187 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
188 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
189 if (SvCUR(s->var) > (STRLEN) s->posn)
190 return SvCUR(s->var) - (STRLEN)s->posn;
198 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
200 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
201 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
202 return SvCUR(s->var);
208 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
210 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
211 s->posn = SvCUR(s->var) - cnt;
215 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
216 const char *mode, int fd, int imode, int perm,
217 PerlIO * f, int narg, SV ** args)
219 SV *arg = (narg > 0) ? *args : PerlIOArg;
220 if (SvROK(arg) || SvPOK(arg)) {
222 f = PerlIO_allocate(aTHX);
224 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
225 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
233 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
235 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
237 if (flags & PERLIO_DUP_CLONE)
238 var = PerlIO_sv_dup(aTHX_ var, param);
239 else if (flags & PERLIO_DUP_FD) {
240 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
244 var = SvREFCNT_inc(var);
246 return newRV_noinc(var);
250 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
253 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
254 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
255 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
256 /* var has been set by implicit push */
262 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
263 sizeof(PerlIO_funcs),
265 sizeof(PerlIOScalar),
266 PERLIO_K_BUFFERED | PERLIO_K_RAW,
285 PerlIOBase_setlinebuf,
286 PerlIOScalar_get_base,
288 PerlIOScalar_get_ptr,
289 PerlIOScalar_get_cnt,
290 PerlIOScalar_set_ptrcnt,
294 #endif /* Layers available */
296 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
303 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));