develooper Front page | perl.cvs.parrot | Postings from December 2008

[svn:parrot] r34361 - in branches/pdd22io_part3/src: io pmc

From:
allison
Date:
December 25, 2008 16:26
Subject:
[svn:parrot] r34361 - in branches/pdd22io_part3/src: io pmc
Message ID:
20081226002611.76106CBA12@x12.develooper.com
Author: allison
Date: Thu Dec 25 16:26:10 2008
New Revision: 34361

Modified:
   branches/pdd22io_part3/src/io/api.c
   branches/pdd22io_part3/src/pmc/filehandle.pmc

Log:
[pdd22io] Invert the invocation path for 'open' to allow subclassing and
polymorphism: the public API function calls the method, instead of the method
calling the public API.


Modified: branches/pdd22io_part3/src/io/api.c
==============================================================================
--- branches/pdd22io_part3/src/io/api.c	(original)
+++ branches/pdd22io_part3/src/io/api.c	Thu Dec 25 16:26:10 2008
@@ -66,7 +66,9 @@
 
 =item C<PMC * Parrot_io_open>
 
-Creates and returns a C<FileHandle> PMC for a given string path and flags.
+Return an open filehandle for a given string path and flags. Defaults to
+creating a new FileHandle PMC. If a PMC object is passed in, it uses that
+object instead of creating a new FileHandle.
 
 =cut
 
@@ -77,19 +79,18 @@
 PARROT_CANNOT_RETURN_NULL
 PMC *
 Parrot_io_open(PARROT_INTERP, ARGIN_NULLOK(PMC *pmc),
-        ARGIN(STRING *path), ARGIN_NULLOK(STRING *mode_str))
+        ARGIN(STRING *path), ARGIN_NULLOK(STRING *mode))
 {
     PMC *new_filehandle;
-    const INTVAL flags = Parrot_io_parse_open_flags(interp, mode_str);
 
+    if (PMC_IS_NULL(pmc))
+        new_filehandle = pmc_new(interp, enum_class_FileHandle);
+    else
+        new_filehandle = pmc;
 
-    new_filehandle = PIO_OPEN(interp, pmc, path, flags);
-
-    if (PMC_IS_NULL(new_filehandle))
-        Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
-            "Unable to open filehandle");
+    Parrot_PCCINVOKE(interp, new_filehandle, CONST_STRING(interp, "open"), "SS->P",
+            path, mode, &new_filehandle);
 
-    Parrot_io_setbuf(interp, new_filehandle, PIO_UNBOUND);
     return new_filehandle;
 }
 

Modified: branches/pdd22io_part3/src/pmc/filehandle.pmc
==============================================================================
--- branches/pdd22io_part3/src/pmc/filehandle.pmc	(original)
+++ branches/pdd22io_part3/src/pmc/filehandle.pmc	Thu Dec 25 16:26:10 2008
@@ -196,33 +196,43 @@
     METHOD open(STRING *filename :optional, INTVAL got_filename :opt_flag,
                 STRING *mode :optional, INTVAL got_mode :opt_flag) {
         PMC *filehandle;
+        STRING *open_filename, *open_mode;
+        INTVAL flags = Parrot_io_parse_open_flags(interp, mode);
 
         if (!Parrot_io_is_closed(INTERP, SELF))
             Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
                                 "Cannot reopen already open filehandle");
 
-        if (got_filename && got_mode) {
-            SET_ATTR_filename(INTERP, SELF, string_copy(INTERP, filename));
+        if (got_mode && !STRING_IS_NULL(mode))
             SET_ATTR_mode(INTERP, SELF, string_copy(INTERP, mode));
-            filehandle = Parrot_io_open(INTERP, SELF, filename, mode);
-        }
-        else if (got_filename) {
+
+        if (got_filename && !STRING_IS_NULL(filename))
             SET_ATTR_filename(INTERP, SELF, string_copy(INTERP, filename));
-            SET_ATTR_mode(INTERP, SELF, CONST_STRING(INTERP, "r"));
-            filehandle = Parrot_io_open(INTERP, SELF, filename, NULL);
-        }
-        else {
-            /* When no options are passed, reopen the same file as before */
-            STRING *reopen_filename, *reopen_mode;
-            GET_ATTR_filename(INTERP, SELF, reopen_filename);
-            GET_ATTR_mode(INTERP, SELF, reopen_mode);
 
-            if (STRING_IS_NULL(reopen_filename))
-                Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
-                                "Cannot reopen filehandle, no filename stored");
-            filehandle = Parrot_io_open(INTERP, SELF, reopen_filename, reopen_mode);
+        /* Open the file. When no options are passed, reopen the same file as
+         * before */
+        GET_ATTR_filename(INTERP, SELF, open_filename);
+        GET_ATTR_mode(INTERP, SELF, open_mode);
+
+
+        if (STRING_IS_NULL(open_filename))
+            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+                            "Cannot open filehandle, no filename");
+
+        if (STRING_IS_NULL(open_mode)) {
+            open_mode = CONST_STRING(INTERP, "r");
+            SET_ATTR_mode(INTERP, SELF, open_mode);
         }
 
+        flags = Parrot_io_parse_open_flags(interp, open_mode);
+        filehandle = PIO_OPEN(INTERP, SELF, open_filename, flags);
+
+        if (PMC_IS_NULL(filehandle))
+            Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+                "Unable to open filehandle");
+
+        Parrot_io_setbuf(interp, filehandle, PIO_UNBOUND);
+
         RETURN(PMC *filehandle);
     }
 



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About