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. */
111 else if (!SvPVX(s->var)) {
112 /* ensure there's always a character buffer */
113 (void)SvGROW(s->var,1);
120 PerlIOScalar_tell(pTHX_ PerlIO * f)
122 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
127 PerlIOScalar_unread(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
129 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
130 char *dst = SvGROW(s->var, (STRLEN)s->posn + count);
132 Move(vbuf, dst + s->posn, count, char);
138 PerlIOScalar_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
140 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
142 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
145 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
146 dst = SvGROW(sv, SvCUR(sv) + count);
148 s->posn = offset + count;
151 if ((s->posn + count) > SvCUR(sv))
152 dst = SvGROW(sv, (STRLEN)s->posn + count);
154 dst = SvPV_nolen(sv);
158 Move(vbuf, dst + offset, count, char);
159 if ((STRLEN) s->posn > SvCUR(sv))
160 SvCUR_set(sv, (STRLEN)s->posn);
169 PerlIOScalar_fill(pTHX_ PerlIO * f)
175 PerlIOScalar_flush(pTHX_ PerlIO * f)
181 PerlIOScalar_get_base(pTHX_ PerlIO * f)
183 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
184 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
185 return (STDCHAR *) SvPV_nolen(s->var);
187 return (STDCHAR *) Nullch;
191 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
193 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
194 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
195 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
197 return (STDCHAR *) Nullch;
201 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
203 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
204 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
205 if (SvCUR(s->var) > (STRLEN) s->posn)
206 return SvCUR(s->var) - (STRLEN)s->posn;
214 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
216 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
217 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
218 return SvCUR(s->var);
224 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
226 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
227 s->posn = SvCUR(s->var) - cnt;
231 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
232 const char *mode, int fd, int imode, int perm,
233 PerlIO * f, int narg, SV ** args)
235 SV *arg = (narg > 0) ? *args : PerlIOArg;
236 if (SvROK(arg) || SvPOK(arg)) {
238 f = PerlIO_allocate(aTHX);
240 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
241 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
249 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
251 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
253 if (flags & PERLIO_DUP_CLONE)
254 var = PerlIO_sv_dup(aTHX_ var, param);
255 else if (flags & PERLIO_DUP_FD) {
256 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
260 var = SvREFCNT_inc(var);
262 return newRV_noinc(var);
266 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
269 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
270 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
271 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
272 /* var has been set by implicit push */
278 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
279 sizeof(PerlIO_funcs),
281 sizeof(PerlIOScalar),
282 PERLIO_K_BUFFERED | PERLIO_K_RAW,
301 PerlIOBase_setlinebuf,
302 PerlIOScalar_get_base,
304 PerlIOScalar_get_ptr,
305 PerlIOScalar_get_cnt,
306 PerlIOScalar_set_ptrcnt,
310 #endif /* Layers available */
312 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
319 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));