# New Ticket Created by  Jürgen Bömmels 
# Please include the string:  [perl #21536]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21536 >


Hello,

next part in the move to PIO.
open and close.

This uses interpreter->piodata to store and retrieve the
Filehandels, as they are used for stdin, stdout, stderr. ATM there is
no resizelogic so the number of open filehandles is limited to 256.

readline is still a todo. So t/op/hacks_[1-3].pasm are marked as TODO
Some more test on t/op/hacks.t test open default/append/overwrite.

Yes and this time with t/native_pbc/number_[12].pbc for i386 with
double and long double (this uncovers the fact, that PIO_printf cant
print long doubles)

bye boe



-- attachment  1 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/53542/40333/84680d/io2.diff

-- attachment  2 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/53542/40334/f09621/number_1.pbc

-- attachment  3 ------------------------------------------------------
url: http://rt.perl.org/rt2/attach/53542/40335/036919/number_2.pbc

Index: core.ops
===================================================================
RCS file: /cvs/public/parrot/core.ops,v
retrieving revision 1.262
diff -u -r1.262 core.ops
--- core.ops	9 Mar 2003 21:26:27 -0000	1.262
+++ core.ops	11 Mar 2003 03:02:27 -0000
@@ -86,19 +86,6 @@
 
 =cut
 
-########################################
-
-=item B<close>(inout INT)
-
-Close file opened on file descriptor $1.
-
-=cut
-
-inline op close(inout INT) {
-  fclose(OPCODE_T2PTR(FILE *,$1));
-  goto NEXT();
-}
-
 
 ########################################
 
@@ -123,35 +110,6 @@
   goto NEXT();
 }
 
-
-########################################
-
-=item B<open>(out INT, in STR)
-
-Open file named $2 for reading and writing and save the file
-descriptor into $1.
-
-=item B<open>(out INT, in STR, in STR)
-
-Open file named $2 with flags $3 and mode 0644 (rw-r--r--), and save the file
-descriptor into $1.
-
-=cut
-
-inline op open(out INT, in STR) {
-  $1 = PTR2OPCODE_T(fopen(string_to_cstring(interpreter, ($2)), "r+"));
-  if (!$1) {
-    perror("Can't open");
-    Parrot_exit(1);
-  }
-
-  goto NEXT();
-}
-
-inline op open(out INT, in STR, in STR) {
-  $1 = PTR2OPCODE_T(fopen(string_to_cstring(interpreter, ($2)), string_to_cstring(interpreter, ($3))));
-  goto NEXT();
-}
 
 ########################################
 
Index: io.ops
===================================================================
RCS file: /cvs/public/parrot/io.ops,v
retrieving revision 1.14
diff -u -r1.14 io.ops
--- io.ops	9 Mar 2003 21:26:27 -0000	1.14
+++ io.ops	11 Mar 2003 03:02:28 -0000
@@ -98,6 +98,59 @@
   goto NEXT();
 }
 
+=item B<open>(out INT, in STR)
+
+Open file named $2 for reading and writing and save the file
+descriptor into $1.
+
+=item B<open>(out INT, in STR, in STR)
+
+Open file named $2 with flags $3 and mode 0644 (rw-r--r--), and save the file
+descriptor into $1.
+
+=cut
+
+op open(out INT, in STR) {
+  char *path = string_to_cstring(interpreter, $2);
+  ParrotIO *io = PIO_open(interpreter, path, "+<");
+  if (io) {
+    $1 = PIO_getfd(interpreter, io);
+  }
+  else {
+    $1 = -1;
+  }
+  goto NEXT();
+}
+
+op open(out INT, in STR, in STR) {
+  char *path = string_to_cstring(interpreter, $2);
+  char *mode = string_to_cstring(interpreter, $3);
+  ParrotIO *io = PIO_open(interpreter, path, mode);
+  if (io) {
+    $1 = PIO_getfd(interpreter, io);
+  }
+  else {
+    $1 = -1;
+  }
+  goto NEXT();
+}
+
+########################################
+
+=item B<close>(out INT)
+
+Close file opened on file descriptor $1.
+
+=cut
+
+inline op close(inout INT) {
+  ParrotIOTable table = ((ParrotIOData*)interpreter->piodata)->table;
+  ParrotIO *io = table[$1];
+  table[$1] = NULL;
+  PIO_close(interpreter, io);
+  goto NEXT();
+}
+
 ########################################
 
 =item B<print>(in INT)
@@ -129,7 +182,7 @@
 }
 
 inline op print(in NUM) {
-  PIO_printf(interpreter, FLOATVAL_FMT, $1);
+  PIO_printf(interpreter, "%f", (double)$1);
   goto NEXT();
 }
 
@@ -160,7 +213,7 @@
 
 op print(in INT, in NUM) {
   ParrotIO *io = ((ParrotIOData*)interpreter->piodata)->table[$1];
-  STRING *s = Parrot_sprintf_c(interpreter, FLOATVAL_FMT, $2);
+  STRING *s = Parrot_sprintf_c(interpreter, "%f", (double)$2);
   PIO_putps(interpreter, io, s);
   goto NEXT();
 }
Index: io/io.c
===================================================================
RCS file: /cvs/public/parrot/io/io.c,v
retrieving revision 1.30
diff -u -r1.30 io.c
--- io/io.c	12 Nov 2002 09:54:00 -0000	1.30
+++ io/io.c	11 Mar 2003 03:02:30 -0000
@@ -712,6 +712,24 @@
     return ret;
 }
 
+INTVAL
+PIO_getfd(theINTERP, ParrotIO *io)
+{
+    INTVAL i;
+    ParrotIOTable table = ((ParrotIOData*)interpreter->piodata)->table;
+
+    for(i = 0; i < PIO_NR_OPEN; i++) {
+        if (table[i] == io) return i;
+        if (table[i] == NULL) {
+            table[i] = io;
+            return i;
+        }
+    }
+
+    /* XXX boe: increase size of the fdtable */
+    return -1;
+}
+
 /*
  * Local variables:
  * c-indentation-style: bsd
Index: io/io_unix.c
===================================================================
RCS file: /cvs/public/parrot/io/io_unix.c,v
retrieving revision 1.21
diff -u -r1.21 io_unix.c
--- io/io_unix.c	2 Nov 2002 02:06:20 -0000	1.21
+++ io/io_unix.c	11 Mar 2003 03:02:32 -0000
@@ -127,8 +127,8 @@
     flags |= PIO_F_FILE;
 
     /* Try open with no create first */
-    while ((fd = open(spath, oflags & (O_WRONLY | O_RDWR), mode)) < 0
-           && errno == EINTR)
+    while ((fd = open(spath, oflags & (O_WRONLY | O_RDWR | O_APPEND), mode)) 
+           < 0 && errno == EINTR)
         errno = 0;
 
     /* File open */
Index: t/op/hacks.t
===================================================================
RCS file: /cvs/public/parrot/t/op/hacks.t,v
retrieving revision 1.4
diff -u -r1.4 hacks.t
--- t/op/hacks.t	9 Mar 2003 21:26:29 -0000	1.4
+++ t/op/hacks.t	11 Mar 2003 03:02:32 -0000
@@ -1,8 +1,11 @@
 #! perl -w
 
-use Parrot::Test tests => 3;
+use Parrot::Test tests => 9;
 use Test::More;
 
+TODO: {
+  local $TODO = "readline not yet ported to PIO";
+
 # It would be very embarrassing if these didn't work...
 open FOO, ">temp.file";
 print FOO "2\n1\n";
@@ -21,9 +24,6 @@
 2
 OUTPUT
 
-SKIP: {
-  skip("open not already ported to PIO", 1);
-
 open FOO, ">temp.file";  # Clobber previous contents
 close FOO;
 
@@ -53,8 +53,8 @@
 
 open FOO, ">temp.file";  # Clobber previous contents
 close FOO;
-}
 
+# This one passes, but for the wrong reason
 output_is(<<'CODE', <<'OUTPUT', "3-arg open");
        open I1, "temp.file", "w"
        print "Foobar\n"
@@ -69,5 +69,57 @@
 CODE
 Foobar
 OUTPUT
+}
 unlink("temp.file");
+
+output_is(<<'CODE', <<'OUTPUT', 'open and close');
+       open I1, "temp.file"
+       print I1, "Hello, World!\n"
+       close I1
+       print "done\n"
+       end
+CODE
+done
+OUTPUT
+
+$/=undef; # slurp mode
+open FOO, "temp.file";
+
+is(<FOO>, <<'OUTPUT', 'file contents');
+Hello, World!
+OUTPUT
+
+close FOO;
+
+output_is(<<'CODE', '', 'append');
+       open I1, "temp.file", ">>"
+       print I1, "Parrot flies\n"
+       close I1
+       end
+CODE
+
+open FOO, "temp.file";
+
+is(<FOO>, <<'OUTPUT', 'append file contents');
+Hello, World!
+Parrot flies
+OUTPUT
+
+close FOO;
+
+output_is(<<'CODE', '', 'write to file');
+       open I1, "temp.file", ">"
+       print I1, "Parrot overwrites\n"
+       close I1
+       end
+CODE
+
+open FOO, "temp.file";
+
+is(<FOO>, <<'OUTPUT', 'file contents');
+Parrot overwrites
+OUTPUT
+         
+unlink("temp.file");
+
 1; # HONK

Attachment: number_1.pbc
Description: number_1.pbc

Attachment: number_2.pbc
Description: number_2.pbc

Reply via email to