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_write(pTHX_ PerlIO * f, const void *vbuf, Size_t count)
130 if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) {
132 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
135 if ((PerlIOBase(f)->flags) & PERLIO_F_APPEND) {
136 dst = SvGROW(sv, SvCUR(sv) + count);
138 s->posn = offset + count;
141 if ((s->posn + count) > SvCUR(sv))
142 dst = SvGROW(sv, (STRLEN)s->posn + count);
144 dst = SvPV_nolen(sv);
148 Move(vbuf, dst + offset, count, char);
149 if ((STRLEN) s->posn > SvCUR(sv))
150 SvCUR_set(sv, (STRLEN)s->posn);
159 PerlIOScalar_fill(pTHX_ PerlIO * f)
165 PerlIOScalar_flush(pTHX_ PerlIO * f)
171 PerlIOScalar_get_base(pTHX_ PerlIO * f)
173 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
174 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
175 return (STDCHAR *) SvPV_nolen(s->var);
177 return (STDCHAR *) NULL;
181 PerlIOScalar_get_ptr(pTHX_ PerlIO * f)
183 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
184 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
185 return PerlIOScalar_get_base(aTHX_ f) + s->posn;
187 return (STDCHAR *) NULL;
191 PerlIOScalar_get_cnt(pTHX_ PerlIO * f)
193 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
194 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
195 if (SvCUR(s->var) > (STRLEN) s->posn)
196 return SvCUR(s->var) - (STRLEN)s->posn;
204 PerlIOScalar_bufsiz(pTHX_ PerlIO * f)
206 if (PerlIOBase(f)->flags & PERLIO_F_CANREAD) {
207 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
208 return SvCUR(s->var);
214 PerlIOScalar_set_ptrcnt(pTHX_ PerlIO * f, STDCHAR * ptr, SSize_t cnt)
216 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
217 s->posn = SvCUR(s->var) - cnt;
221 PerlIOScalar_open(pTHX_ PerlIO_funcs * self, PerlIO_list_t * layers, IV n,
222 const char *mode, int fd, int imode, int perm,
223 PerlIO * f, int narg, SV ** args)
225 SV *arg = (narg > 0) ? *args : PerlIOArg;
226 if (SvROK(arg) || SvPOK(arg)) {
228 f = PerlIO_allocate(aTHX);
230 if ( (f = PerlIO_push(aTHX_ f, self, mode, arg)) ) {
231 PerlIOBase(f)->flags |= PERLIO_F_OPEN;
239 PerlIOScalar_arg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags)
241 PerlIOScalar *s = PerlIOSelf(f, PerlIOScalar);
243 if (flags & PERLIO_DUP_CLONE)
244 var = PerlIO_sv_dup(aTHX_ var, param);
245 else if (flags & PERLIO_DUP_FD) {
246 /* Equivalent (guesses NI-S) of dup() is to create a new scalar */
250 var = SvREFCNT_inc(var);
252 return newRV_noinc(var);
256 PerlIOScalar_dup(pTHX_ PerlIO * f, PerlIO * o, CLONE_PARAMS * param,
259 if ((f = PerlIOBase_dup(aTHX_ f, o, param, flags))) {
260 PerlIOScalar *fs = PerlIOSelf(f, PerlIOScalar);
261 PerlIOScalar *os = PerlIOSelf(o, PerlIOScalar);
262 /* var has been set by implicit push */
268 PERLIO_FUNCS_DECL(PerlIO_scalar) = {
269 sizeof(PerlIO_funcs),
271 sizeof(PerlIOScalar),
272 PERLIO_K_BUFFERED | PERLIO_K_RAW,
291 PerlIOBase_setlinebuf,
292 PerlIOScalar_get_base,
294 PerlIOScalar_get_ptr,
295 PerlIOScalar_get_cnt,
296 PerlIOScalar_set_ptrcnt,
300 #endif /* Layers available */
302 MODULE = PerlIO::scalar PACKAGE = PerlIO::scalar
309 PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_scalar));