3 * Copyright (C) 2010, Parrot Foundation.
9 io.ops - Extended I/O Dynops
13 A richer I/O API than that available in core Parrot.
20 #include "../io/io_private.h"
23 ##########################################
25 =item B<stat>(out INT, in STR, in INT)
27 =item B<stat>(out INT, in INT, in INT)
29 Stat the file. Return stat element $3 for file $2 into $1. The queryable
36 CREATETIME 4 (Time file was created)
37 ACCESSTIME 5 (Time file was last accessed)
38 MODIFYTIME 6 (Time file data was changed)
39 CHANGETIME 7 (Time file metadata was changed)
40 BACKUPTIME 8 (Time of last backup)
47 op stat(out INT, in STR, in INT) {
48 $1 = Parrot_stat_info_intval(interp, $2, $3);
51 op stat(out INT, in INT, in INT) {
52 $1 = Parrot_fstat_info_intval(interp, $2, $3);
55 ##########################################
57 =item B<read>(out STR, in INT)
59 Read up to N bytes from standard input stream
61 =item B<read>(out STR, invar PMC, in INT)
63 Read up to N bytes from IO PMC stream.
67 op read(out STR, in INT) :base_io {
68 $1 = Parrot_io_reads(interp, _PIO_STDIN(interp), (size_t)$2);
71 op read(out STR, invar PMC, in INT) :base_io {
72 $1 = Parrot_io_reads(interp, $2, (size_t)$3);
75 =item B<readline>(out STR, invar PMC)
77 Read a line up to EOL from filehandle $2.
78 This switches the filehandle to linebuffer-mode.
82 inline op readline(out STR, invar PMC) :base_io {
83 $1 = Parrot_io_readline(interp, $2);
86 ##########################################
88 =item B<printerr>(in INT)
90 =item B<printerr>(in NUM)
92 =item B<printerr>(in STR)
94 =item B<printerr>(invar PMC)
96 Print $1 to standard error.
100 op printerr(in INT) :base_io {
101 Parrot_io_eprintf(interp, INTVAL_FMT, $1);
104 op printerr(in NUM) :base_io {
105 Parrot_io_eprintf(interp, FLOATVAL_FMT, $1);
108 op printerr(in STR) :base_io {
109 STRING * const s = $1;
110 if (s && Parrot_str_byte_length(interp, s))
111 Parrot_io_putps(interp, _PIO_STDERR(interp), s);
114 op printerr(invar PMC) :base_io {
116 STRING * const s = (VTABLE_get_string(interp, p));
118 Parrot_io_putps(interp, _PIO_STDERR(interp), s);
121 ##########################################
123 =item B<seek>(invar PMC, in INT, in INT)
126 Set file position to offset $2 on IO stream $1. 'whence' is
127 indicated by the value in $3. The valid values for 'whence' are:
130 0 Seek from the beginning of the file
131 1 Seek from the current position
132 2 Seek from the end of the file
134 [ Note: the above values for 'whence' is just an educated guess
137 =item B<seek>(invar PMC, in INT, in INT, in INT)
140 Set file position to offset ($2 << 32 | $3) on IO stream $1. 'whence' is
141 indicated by the value in $4. This allows 64-bit seeks with only 32-bit
146 op seek(invar PMC, in INT, in INT) :base_io {
148 if (Parrot_io_seek(interp, $1, Parrot_io_make_offset($2), $3) < 0) {
149 opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
151 "seek failed (32bit)");
152 goto ADDRESS(handler);
157 op seek(invar PMC, in INT, in INT, in INT) :base_io {
159 if (Parrot_io_seek(interp, $1, Parrot_io_make_offset32($2, $3), $4) < 0) {
160 opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
162 "seek failed (64bit)");
163 goto ADDRESS(handler);
168 =item B<tell>(out INT, invar PMC)
171 Get the current file position of stream $2 and store it in $1.
172 On systems where INTVAL is 32bit the result will be truncated if the
173 position is beyond 2 GiB
175 =item B<tell>(out INT, out INT, invar PMC)
178 Get the current file positon of stream $3 in two parts of 32-bit each
179 ($1 = pos >> 32, $2 = pos & 0xffffffff).
183 op tell(out INT, invar PMC) :base_io {
185 $1 = (INTVAL)Parrot_io_tell(interp, $2);
188 op tell(out INT, out INT, invar PMC) :base_io {
191 pos = Parrot_io_tell(interp, $3);
192 $1 = (INTVAL)(pos >> 31);
193 $2 = (INTVAL)(pos & 0xffffffff);
197 ##########################################
199 =item B<peek>(out STR)
201 Returns the next byte from standard input, but does not
202 remove it from the stream.
204 =item B<peek>(out STR, invar PMC)
206 Reads the next byte from an IO PMC, but does not
207 remove it from the stream.
211 op peek(out STR) :base_io {
212 STRING ** const s = &$1;
215 if (Parrot_io_peek(interp, _PIO_STDIN(interp), s) < 0) {
220 op peek(out STR, invar PMC) :base_io {
221 STRING ** const s = &$1;
224 if (Parrot_io_peek(interp, $2, s) < 0) {
229 #########################################
231 =item B<open>(out PMC, in STR, in STR)
233 Open URL (file, address, database, in core image) named $2 with
234 a mode string in $3 and create an IO object in $1.
236 The mode consists of a string of characters specified in any order:
240 a : append (Note: you must specify "wa", not just "a")
243 =item B<open>(out PMC, in STR)
245 Open URL (file, address, database, in core image) named $2 with
246 read mode and create an IO object in $1.
250 inline op open(out PMC, in STR, in STR) :filesys_open {
251 if (STRING_IS_NULL($2) || STRING_IS_NULL($3)) {
252 opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
253 EXCEPTION_UNEXPECTED_NULL, "Invalid open");
254 goto ADDRESS(handler);
257 $1 = Parrot_io_open(interp, PMCNULL, $2, $3);
258 PARROT_ASSERT(! PMC_IS_NULL($1));
262 inline op open(out PMC, in STR) :filesys_open {
263 if (STRING_IS_NULL($2)) {
264 opcode_t *handler = Parrot_ex_throw_from_op_args(interp, expr NEXT(),
265 EXCEPTION_UNEXPECTED_NULL, "Invalid open");
266 goto ADDRESS(handler);
269 $1 = Parrot_io_open(interp, PMCNULL, $2, NULL);
270 PARROT_ASSERT(! PMC_IS_NULL($1));
274 ########################################
276 =item B<close>(invar PMC)
282 inline op close(invar PMC) :base_io {
283 Parrot_io_close(interp, $1);
286 ########################################
288 =item B<fdopen>(out PMC, in INT, in STR)
290 Create ParrotIO object in $1 as a copy of file descriptor $2.
294 inline op fdopen(out PMC, in INT, in STR) :filesys_open {
295 $1 = Parrot_io_fdopen(interp, PMCNULL, (PIOHANDLE)$2, $3);
297 $1 = Parrot_pmc_new(interp, enum_class_Undef);
300 #########################################
302 =item B<setstdin>(invar PMC)
304 Sets the standard input for a bare C<read> op to go to the supplied PMC.
305 Call C<getstdin> first if you care about retaining the previous PMC.
307 =item B<setstdout>(invar PMC)
309 Sets the standard output for a bare C<print> op to go to the supplied PMC.
310 Call C<getstdout> first if you care about retaining the previous PMC.
312 =item B<setstderr>(invar PMC)
314 Sets the standard error for a bare C<printerr> op to go to the supplied PMC.
315 Call C<getstderr> first if you care about retaining the previous PMC.
319 inline op setstdin(invar PMC) :base_io {
320 _PIO_STDIN(interp) = $1;
323 inline op setstdout(invar PMC) :base_io {
324 _PIO_STDOUT(interp) = $1;
327 inline op setstderr(invar PMC) :base_io {
328 _PIO_STDERR(interp) = $1;
331 ########################################
339 * c-file-style: "parrot"
341 * vim: expandtab shiftwidth=4: