Author: allison
Date: Fri Dec 26 21:06:38 2008
New Revision: 34402
Modified:
branches/pdd22io_part3/src/io/api.c
branches/pdd22io_part3/src/io/filehandle.c
branches/pdd22io_part3/src/pmc/filehandle.pmc
Log:
[pdd22io] Invert 'close', 'is_closed', and 'putps', so public interface just
calls a method on the FileHandle PMC, to allow clean polymorphism.
Modified: branches/pdd22io_part3/src/io/api.c
==============================================================================
--- branches/pdd22io_part3/src/io/api.c (original)
+++ branches/pdd22io_part3/src/io/api.c Fri Dec 26 21:06:38 2008
@@ -140,7 +140,7 @@
=item C<INTVAL Parrot_io_close>
-Flushes, closes, and destroys the C<ParrotIO> PMC C<*pmc>.
+Closes the filehandle object.
=cut
@@ -150,15 +150,14 @@
INTVAL
Parrot_io_close(PARROT_INTERP, ARGMOD(PMC *pmc))
{
- INTVAL res;
+ INTVAL result;
- if (Parrot_io_is_closed(interp, pmc))
+ if (PMC_IS_NULL(pmc))
return -1;
- Parrot_io_flush(interp, pmc);
- res = PIO_CLOSE(interp, pmc);
- Parrot_io_clear_buffer(interp, pmc);
- return res;
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "close"), "->I",
&result);
+
+ return result;
}
/*
@@ -175,7 +174,13 @@
INTVAL
Parrot_io_is_closed(PARROT_INTERP, ARGMOD(PMC *pmc))
{
- return PIO_IS_CLOSED(interp, pmc);
+ INTVAL result;
+
+ if (PMC_IS_NULL(pmc))
+ return 1;
+
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "is_closed"), "->I",
&result);
+ return result;
}
/*
@@ -436,33 +441,16 @@
INTVAL
Parrot_io_putps(PARROT_INTERP, ARGMOD(PMC *pmc), ARGMOD_NULLOK(STRING *s))
{
+ INTVAL result;
- if (PMC_IS_NULL(pmc)
- || !VTABLE_isa(interp, pmc, CONST_STRING(interp, "FileHandle")))
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
- "Cannot write to non-IO PMC");
-
- if (Parrot_io_is_closed(interp, pmc))
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
- "Cannot write to a closed filehandle");
-
- if (!(Parrot_io_get_flags(interp, pmc) & PIO_F_WRITE))
+ if (PMC_IS_NULL(pmc))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
- "Cannot write to a filehandle not opened for write");
+ "Cannot write to null PMC");
- if (STRING_IS_NULL(s))
- return 0;
-
-#if ! DISABLE_GC_DEBUG
- /* trigger GC for debug - but not during tests */
- if (0 && GC_DEBUG(interp))
- Parrot_do_dod_run(interp, GC_trace_stack_FLAG);
-#endif
-
- if (Parrot_io_is_encoding(interp, pmc, CONST_STRING(interp, "utf8")))
- return Parrot_io_write_utf8(interp, pmc, s);
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "puts"), "S->I",
+ s, &result);
+ return result;
- return Parrot_io_write_buffer(interp, pmc, s);
}
/*
Modified: branches/pdd22io_part3/src/io/filehandle.c
==============================================================================
--- branches/pdd22io_part3/src/io/filehandle.c (original)
+++ branches/pdd22io_part3/src/io/filehandle.c Fri Dec 26 21:06:38 2008
@@ -195,7 +195,7 @@
PARROT_EXPORT
void
-Parrot_io_set_os_handle(PARROT_INTERP, ARGIN(PMC *filehandle), PIOHANDLE
file_descriptor)
+Parrot_io_set_os_handle(SHIM_INTERP, ARGIN(PMC *filehandle), PIOHANDLE
file_descriptor)
{
PARROT_FILEHANDLE(filehandle)->os_handle = file_descriptor;
}
@@ -218,7 +218,7 @@
PARROT_EXPORT
PIOHANDLE
-Parrot_io_get_os_handle(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_os_handle(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->os_handle;
}
@@ -241,7 +241,7 @@
PARROT_EXPORT
void
-Parrot_io_set_flags(PARROT_INTERP, ARGIN(PMC *filehandle), INTVAL flags)
+Parrot_io_set_flags(SHIM_INTERP, ARGIN(PMC *filehandle), INTVAL flags)
{
Parrot_FileHandle_attributes *handle_struct =
PARROT_FILEHANDLE(filehandle);
handle_struct->flags = flags;
@@ -265,7 +265,7 @@
PARROT_EXPORT
INTVAL
-Parrot_io_get_flags(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_flags(SHIM_INTERP, ARGIN(PMC *filehandle))
{
Parrot_FileHandle_attributes *handle_struct =
PARROT_FILEHANDLE(filehandle);
INTVAL flags = handle_struct->flags;
@@ -290,7 +290,7 @@
PARROT_EXPORT
void
-Parrot_io_set_file_size(PARROT_INTERP, ARGIN(PMC *filehandle), PIOOFF_T
file_size)
+Parrot_io_set_file_size(SHIM_INTERP, ARGIN(PMC *filehandle), PIOOFF_T
file_size)
{
PARROT_FILEHANDLE(filehandle)->file_size = file_size;
}
@@ -315,7 +315,7 @@
PARROT_EXPORT
PIOOFF_T
-Parrot_io_get_file_size(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_file_size(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->file_size;
}
@@ -337,7 +337,7 @@
*/
void
-Parrot_io_set_buffer_start(PARROT_INTERP, ARGIN(PMC *filehandle),
+Parrot_io_set_buffer_start(SHIM_INTERP, ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_start))
{
PARROT_FILEHANDLE(filehandle)->buffer_start = new_start;
@@ -362,7 +362,7 @@
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
unsigned char *
-Parrot_io_get_buffer_start(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_buffer_start(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->buffer_start;
}
@@ -386,7 +386,7 @@
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
unsigned char *
-Parrot_io_get_buffer_next(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_buffer_next(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->buffer_next;
}
@@ -408,7 +408,7 @@
*/
void
-Parrot_io_set_buffer_next(PARROT_INTERP, ARGIN(PMC *filehandle),
+Parrot_io_set_buffer_next(SHIM_INTERP, ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_next))
{
PARROT_FILEHANDLE(filehandle)->buffer_next = new_next;
@@ -433,7 +433,7 @@
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
unsigned char *
-Parrot_io_get_buffer_end(PARROT_INTERP, ARGIN_NULLOK(PMC *filehandle))
+Parrot_io_get_buffer_end(SHIM_INTERP, ARGIN_NULLOK(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->buffer_end;
}
@@ -455,7 +455,7 @@
*/
void
-Parrot_io_set_buffer_end(PARROT_INTERP, ARGIN(PMC *filehandle),
+Parrot_io_set_buffer_end(SHIM_INTERP, ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_end))
{
PARROT_FILEHANDLE(filehandle)->buffer_end = new_end;
@@ -479,7 +479,7 @@
PARROT_CAN_RETURN_NULL
INTVAL
-Parrot_io_get_buffer_flags(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_buffer_flags(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->buffer_flags;
}
@@ -501,7 +501,7 @@
*/
void
-Parrot_io_set_buffer_flags(PARROT_INTERP, ARGIN(PMC *filehandle), INTVAL
new_flags)
+Parrot_io_set_buffer_flags(SHIM_INTERP, ARGIN(PMC *filehandle), INTVAL
new_flags)
{
PARROT_FILEHANDLE(filehandle)->buffer_flags = new_flags;
}
@@ -524,7 +524,7 @@
PARROT_CAN_RETURN_NULL
size_t
-Parrot_io_get_buffer_size(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_buffer_size(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->buffer_size;
}
@@ -546,7 +546,7 @@
*/
void
-Parrot_io_set_buffer_size(PARROT_INTERP, ARGIN(PMC *filehandle), size_t
new_size)
+Parrot_io_set_buffer_size(SHIM_INTERP, ARGIN(PMC *filehandle), size_t new_size)
{
PARROT_FILEHANDLE(filehandle)->buffer_size = new_size;
}
@@ -568,7 +568,7 @@
PARROT_CAN_RETURN_NULL
void
-Parrot_io_clear_buffer(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_clear_buffer(SHIM_INTERP, ARGIN(PMC *filehandle))
{
Parrot_FileHandle_attributes *io = PARROT_FILEHANDLE(filehandle);
if (io->buffer_start && (io->flags & PIO_BF_MALLOC)) {
@@ -595,7 +595,7 @@
PARROT_EXPORT
PIOOFF_T
-Parrot_io_get_file_position(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_file_position(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->file_pos;
}
@@ -618,7 +618,7 @@
PARROT_EXPORT
PIOOFF_T
-Parrot_io_get_last_file_position(PARROT_INTERP, ARGIN(PMC *filehandle))
+Parrot_io_get_last_file_position(SHIM_INTERP, ARGIN(PMC *filehandle))
{
return PARROT_FILEHANDLE(filehandle)->last_pos;
}
@@ -642,7 +642,7 @@
PARROT_EXPORT
void
-Parrot_io_set_file_position(PARROT_INTERP, ARGIN(PMC *filehandle), PIOOFF_T
file_pos)
+Parrot_io_set_file_position(SHIM_INTERP, ARGIN(PMC *filehandle), PIOOFF_T
file_pos)
{
Parrot_FileHandle_attributes *handle_struct =
PARROT_FILEHANDLE(filehandle);
handle_struct->last_pos = handle_struct->file_pos;
@@ -680,6 +680,52 @@
return 0;
}
+/*
+
+=item C<INTVAL Parrot_io_close_filehandle>
+
+Flushes and closes the C<FileHandle> PMC C<*pmc>, but leaves the object intact
+to be reused or collected.
+
+=cut
+
+*/
+
+PARROT_EXPORT
+INTVAL
+Parrot_io_close_filehandle(PARROT_INTERP, ARGMOD(PMC *pmc))
+{
+ INTVAL result;
+
+ if (Parrot_io_is_closed_filehandle(interp, pmc))
+ return -1;
+
+ Parrot_io_flush_buffer(interp, pmc);
+ PIO_FLUSH(interp, pmc);
+
+ result = PIO_CLOSE(interp, pmc);
+ Parrot_io_clear_buffer(interp, pmc);
+
+ return result;
+}
+
+/*
+
+=item C<INTVAL Parrot_io_is_closed_filehandle>
+
+Test whether a filehandle is closed.
+
+=cut
+
+*/
+
+PARROT_EXPORT
+INTVAL
+Parrot_io_is_closed_filehandle(PARROT_INTERP, ARGMOD(PMC *pmc))
+{
+ return PIO_IS_CLOSED(interp, pmc);
+}
+
/*
Modified: branches/pdd22io_part3/src/pmc/filehandle.pmc
==============================================================================
--- branches/pdd22io_part3/src/pmc/filehandle.pmc (original)
+++ branches/pdd22io_part3/src/pmc/filehandle.pmc Fri Dec 26 21:06:38 2008
@@ -144,11 +144,11 @@
if (PARROT_FILEHANDLE(SELF)) {
Parrot_FileHandle_attributes *data_struct =
PARROT_FILEHANDLE(SELF);
- if (!Parrot_io_is_closed(INTERP, SELF)) {
+ if (!Parrot_io_is_closed_filehandle(INTERP, SELF)) {
if (data_struct->flags & PIO_F_SHARED)
Parrot_io_flush(INTERP, SELF);
else
- Parrot_io_close(INTERP, SELF);
+ Parrot_io_close_filehandle(INTERP, SELF);
}
if (data_struct->buffer_start)
@@ -199,7 +199,7 @@
STRING *open_filename, *open_mode;
INTVAL flags = Parrot_io_parse_open_flags(interp, mode);
- if (!Parrot_io_is_closed(INTERP, SELF))
+ if (!Parrot_io_is_closed_filehandle(INTERP, SELF))
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Cannot reopen already open filehandle");
@@ -259,8 +259,25 @@
*/
METHOD close() {
+ INTVAL status;
+ status = Parrot_io_close_filehandle(interp, pmc);
+ RETURN(INTVAL status);
+ }
+
+/*
+
+=item C<METHOD is_closed()>
+
+Test if the filehandle is closed.
+
+=cut
+
+*/
- Parrot_io_close(INTERP, SELF);
+ METHOD is_closed() {
+ INTVAL status;
+ status = Parrot_io_is_closed_filehandle(interp, pmc);
+ RETURN(INTVAL status);
}
/*
@@ -383,7 +400,7 @@
STRING *encoding;
size_t size;
GET_ATTR_encoding(INTERP, SELF, encoding);
- if (!Parrot_io_is_closed(INTERP, SELF)) {
+ if (!Parrot_io_is_closed_filehandle(INTERP, SELF)) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Cannot readall on a new file from an already
open filehandle");
}
@@ -397,7 +414,7 @@
}
else {
/* slurp open file */
- if (Parrot_io_is_closed(INTERP, SELF)) {
+ if (Parrot_io_is_closed_filehandle(INTERP, SELF)) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
"Cannot readall without a file name or open
filehandle");
}
@@ -461,7 +478,31 @@
*/
METHOD puts(STRING *to_print) {
- Parrot_io_putps(interp, SELF, to_print);
+ INTVAL flags, status;
+ if (Parrot_io_is_closed_filehandle(interp, pmc))
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+ "Cannot write to a closed filehandle");
+
+ GET_ATTR_flags(INTERP, SELF, flags);
+ if (!(flags & PIO_F_WRITE))
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+ "Cannot write to a filehandle not opened for write");
+
+ if (STRING_IS_NULL(to_print))
+ RETURN(INTVAL 0);
+
+#if ! DISABLE_GC_DEBUG
+ /* trigger GC for debug - but not during tests */
+ if (0 && GC_DEBUG(interp))
+ Parrot_do_dod_run(interp, GC_trace_stack_FLAG);
+#endif
+
+ if (Parrot_io_is_encoding(interp, pmc, CONST_STRING(interp, "utf8")))
+ status = Parrot_io_write_utf8(interp, pmc, to_print);
+ else
+ status = Parrot_io_write_buffer(interp, pmc, to_print);
+
+ RETURN(INTVAL status);
}
/*