fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / dynoplibs / io.ops
blob1cc443584bfb1421552c02549a207cc6702351b5
1 /*
2  * $Id$
3  * Copyright (C) 2010, Parrot Foundation.
4 ** io.ops
5 */
7 =head1 NAME
9 io.ops - Extended I/O Dynops
11 =head1 DESCRIPTION
13 A richer I/O API than that available in core Parrot.
15 =over 4
17 =cut
19 BEGIN_OPS_PREAMBLE
20 #include "../io/io_private.h"
21 END_OPS_PREAMBLE
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
30 items currently are:
32  EXISTS     0
33  FILESIZE   1
34  ISDIR      2
35  ISDEV      3
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)
41  UID        9
42  GID        10
45 =cut
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.
65 =cut
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.
80 =cut
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.
98 =cut
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 {
115     PMC * const p = $1;
116     STRING * const s = (VTABLE_get_string(interp, p));
117     if (s)
118         Parrot_io_putps(interp, _PIO_STDERR(interp), s);
121 ##########################################
123 =item B<seek>(invar PMC, in INT, in INT)
125 seek:
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:
129  Value      Meaning
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
135 at this point ]
137 =item B<seek>(invar PMC, in INT, in INT, in INT)
139 64bit seek:
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
142 INTVALS.
144 =cut
146 op seek(invar PMC, in INT, in INT) :base_io {
147     if ($1) {
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(),
150                 EXCEPTION_PIO_ERROR,
151                 "seek failed (32bit)");
152             goto ADDRESS(handler);
153         }
154     }
157 op seek(invar PMC, in INT, in INT, in INT) :base_io {
158     if ($1) {
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(),
161                 EXCEPTION_PIO_ERROR,
162                 "seek failed (64bit)");
163             goto ADDRESS(handler);
164         }
165     }
168 =item B<tell>(out INT, invar PMC)
170 tell:
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)
177 64bit tell:
178 Get the current file positon of stream $3 in two parts of 32-bit each
179 ($1 = pos >> 32, $2 = pos & 0xffffffff).
181 =cut
183 op tell(out INT, invar PMC) :base_io {
184     if ($2)
185         $1 = (INTVAL)Parrot_io_tell(interp, $2);
188 op tell(out INT, out INT, invar PMC) :base_io {
189     if ($3) {
190         PIOOFF_T pos;
191         pos = Parrot_io_tell(interp, $3);
192         $1 = (INTVAL)(pos >> 31);
193         $2 = (INTVAL)(pos & 0xffffffff);
194     }
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.
209 =cut
211 op peek(out STR) :base_io {
212     STRING ** const s = &$1;
214     *s = NULL;
215     if (Parrot_io_peek(interp, _PIO_STDIN(interp), s) < 0) {
216         $1 = STRINGNULL;
217     }
220 op peek(out STR, invar PMC) :base_io {
221     STRING ** const s = &$1;
223     *s = NULL;
224     if (Parrot_io_peek(interp, $2, s) < 0) {
225         $1 = STRINGNULL;
226     }
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:
238  r : read
239  w : write
240  a : append (Note: you must specify "wa", not just "a")
241  p : pipe
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.
248 =cut
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);
255     }
256     else {
257         $1 = Parrot_io_open(interp, PMCNULL, $2, $3);
258         PARROT_ASSERT(! PMC_IS_NULL($1));
259     }
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);
267     }
268     else {
269         $1 = Parrot_io_open(interp, PMCNULL, $2, NULL);
270         PARROT_ASSERT(! PMC_IS_NULL($1));
271     }
274 ########################################
276 =item B<close>(invar PMC)
278 Close IO object $1
280 =cut
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.
292 =cut
294 inline op fdopen(out PMC, in INT, in STR) :filesys_open {
295     $1 = Parrot_io_fdopen(interp, PMCNULL, (PIOHANDLE)$2, $3);
296     if (!$1)
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.
317 =cut
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 ########################################
333 =back
335 =cut
338  * Local variables:
339  *   c-file-style: "parrot"
340  * End:
341  * vim: expandtab shiftwidth=4:
342  */