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), "%s", PL_no_modify);
30 SETERRNO(EINVAL, SS_IVCHAN);
33 s->var = SvREFCNT_inc(SvRV(arg));
35 if (!SvPOK(s->var) && SvOK(s->var))
36 (void)SvPV_nomg_const_nolen(s->var);
40 SvREFCNT_inc(perl_get_sv
41 (SvPV_nolen(arg), GV_ADD | GV_ADDMULTI));
45 s->var = newSVpvn("", 0);
47 SvUPGRADE(s->var, SVt_PV);
48 code = PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab);
49 if (!SvOK(s->var) || (PerlIOBase(f)->flags) & PERLIO_F_TRUNCATE)
51 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND)
52 s->posn = SvCUR(s->var);
59 PerlIOScalar_popped(pTHX_ PerlIO * f)
61 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
70 PerlIOScalar_close(pTHX_ PerlIO * f)
72 IV code = PerlIOBase_close(aTHX_ f);
73 PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF);
78 PerlIOScalar_fileno(pTHX_ PerlIO * f)
84 PerlIOScalar_seek(pTHX_ PerlIO * f, Off_t offset, int whence)
86 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
87 STRLEN oldcur = SvCUR(s->var);
94 s->posn = offset + s->posn;
97 s->posn = offset + SvCUR(s->var);
101 if (ckWARN(WARN_LAYER))
102 Perl_warner(aTHX_ packWARN(WARN_LAYER), "Offset outside string");
103 SETERRNO(EINVAL, SS_IVCHAN);
106 newlen = (STRLEN) s->posn;
107 if (newlen > oldcur) {
108 (void) SvGROW(s->var, newlen);
109 Zero(SvPVX(s->var) + oldcur, newlen - oldcur, char);
110 /* No SvCUR_set(), though. This is just a seek, not a write. */
112 else if (!SvPVX(s->var)) {
113 /* ensure there's always a character buffer */
114 (void)SvGROW(s->var,1);
121 PerlIOScalar_tell(pTHX_ PerlIO * f)
123 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
128 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
130 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
131 char *dst = SvGROW(s->var, (STRLEN)s->posn + count);
133 Move(vbuf, dst + s->posn, count, char);
139 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
141 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
143 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
146 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
147 dst = SvGROW(sv, SvCUR(sv) + count);
149 s->posn = offset + count;
152 if ((s->posn + count) > SvCUR(sv))
153 dst = SvGROW(sv, (STRLEN)s->posn + count);
155 dst = SvPV_nolen(sv);
159 Move(vbuf, dst + offset, count, char);
160 if ((STRLEN) s->posn > SvCUR(sv))
161 SvCUR_set(sv, (STRLEN)s->posn);
170 PerlIOScalar_fill(pTHX_ PerlIO * f)
176 PerlIOScalar_flush(pTHX_ PerlIO * f)
182 PerlIOScalar_get_base(pTHX_ PerlIO * f)
184 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
185 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
186 return (STDCHAR *) SvPV_nolen(s->var);
188 return (STDCHAR *) NULL;
192 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
194 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
195 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
196 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
198 return (STDCHAR *) NULL;
202 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
204 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
205 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
206 if (SvCUR(s->var) > (STRLEN) s->posn)
207 return SvCUR(s->var) - (STRLEN)s->posn;
215 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
217 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
218 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
219 return SvCUR(s->var);
225 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
227 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
228 s->posn = SvCUR(s->var) - cnt;
232 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
233 const char *mode, int fd, int imode, int perm,
234 PerlIO * f, int narg, SV ** args)
236 SV *arg = (narg > 0) ? *args : PerlIOArg;
237 if (SvROK(arg) || SvPOK(arg)) {
239 f = PerlIO_allocate(aTHX);
241 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
242 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
250 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
252 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
254 if (flags & PERLIO_DUP_CLONE)
255 var = PerlIO_sv_dup(aTHX_ var, param);
256 else if (flags & PERLIO_DUP_FD) {
257 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
261 var = SvREFCNT_inc(var);
263 return newRV_noinc(var);
267 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
270 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
271 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
272 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
273 /* var has been set by implicit push */
279 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
280 sizeof(PerlIO_funcs),
282 sizeof(PerlIOScalar),
283 PERLIO_K_BUFFERED | PERLIO_K_RAW,
302 PerlIOBase_setlinebuf,
303 PerlIOScalar_get_base,
305 PerlIOScalar_get_ptr,
306 PerlIOScalar_get_cnt,
307 PerlIOScalar_set_ptrcnt,
311 #endif /* Layers available */
313 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
320 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));