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);
30 SETERRNO(EINVAL, SS_IVCHAN);
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);
86 STRLEN oldcur = SvCUR(s->var);
93 s->posn = offset + s->posn;
96 s->posn = offset + SvCUR(s->var);
100 if (ckWARN(WARN_LAYER))
101 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Offset outside string");
102 SETERRNO(EINVAL, SS_IVCHAN);
105 newlen = (STRLEN) s->posn;
106 if (newlen > oldcur) {
107 (void) SvGROW(s->var, newlen);
108 Zero(SvPVX(s->var) + oldcur, newlen - oldcur, char);
109 /* No SvCUR_set(), though. This is just a seek, not a write. */
116 PerlIOScalar_tell(pTHX_ PerlIO * f)
118 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
123 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
125 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
126 char *dst = SvGROW(s->var, (STRLEN)s->posn + count);
128 Move(vbuf, dst + s->posn, count, char);
134 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
136 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
138 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
141 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
142 dst = SvGROW(sv, SvCUR(sv) + count);
144 s->posn = offset + count;
147 if ((s->posn + count) > SvCUR(sv))
148 dst = SvGROW(sv, (STRLEN)s->posn + count);
150 dst = SvPV_nolen(sv);
154 Move(vbuf, dst + offset, count, char);
155 if ((STRLEN) s->posn > SvCUR(sv))
156 SvCUR_set(sv, (STRLEN)s->posn);
165 PerlIOScalar_fill(pTHX_ PerlIO * f)
171 PerlIOScalar_flush(pTHX_ PerlIO * f)
177 PerlIOScalar_get_base(pTHX_ PerlIO * f)
179 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
180 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
181 return (STDCHAR *) SvPV_nolen(s->var);
183 return (STDCHAR *) Nullch;
187 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
189 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
190 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
191 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
193 return (STDCHAR *) Nullch;
197 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
199 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
200 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
201 if (SvCUR(s->var) > (STRLEN) s->posn)
202 return SvCUR(s->var) - (STRLEN)s->posn;
210 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
212 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
213 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
214 return SvCUR(s->var);
220 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
222 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
223 s->posn = SvCUR(s->var) - cnt;
227 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
228 const char *mode, int fd, int imode, int perm,
229 PerlIO * f, int narg, SV ** args)
231 SV *arg = (narg > 0) ? *args : PerlIOArg;
232 if (SvROK(arg) || SvPOK(arg)) {
234 f = PerlIO_allocate(aTHX);
236 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
237 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
245 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
247 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
249 if (flags & PERLIO_DUP_CLONE)
250 var = PerlIO_sv_dup(aTHX_ var, param);
251 else if (flags & PERLIO_DUP_FD) {
252 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
256 var = SvREFCNT_inc(var);
258 return newRV_noinc(var);
262 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
265 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
266 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
267 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
268 /* var has been set by implicit push */
274 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
275 sizeof(PerlIO_funcs),
277 sizeof(PerlIOScalar),
278 PERLIO_K_BUFFERED | PERLIO_K_RAW,
297 PerlIOBase_setlinebuf,
298 PerlIOScalar_get_base,
300 PerlIOScalar_get_ptr,
301 PerlIOScalar_get_cnt,
302 PerlIOScalar_set_ptrcnt,
306 #endif /* Layers available */
308 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
315 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));