Author: allison
Date: Tue Dec 30 00:09:18 2008
New Revision: 34624
Added:
trunk/src/io/filehandle.c
- copied unchanged from r34623,
/branches/pdd22io_part3/src/io/filehandle.c
trunk/src/pmc/stringhandle.pmc
- copied unchanged from r34623,
/branches/pdd22io_part3/src/pmc/stringhandle.pmc
trunk/t/pmc/stringhandle.t
- copied unchanged from r34623,
/branches/pdd22io_part3/t/pmc/stringhandle.t
Removed:
trunk/src/io.c
Modified:
trunk/MANIFEST
trunk/config/gen/makefiles/root.in
trunk/include/parrot/io.h
trunk/src/io/api.c
trunk/src/ops/io.ops
trunk/src/pmc/filehandle.pmc
trunk/t/pmc/filehandle.t
Log:
[pdd22io] Merging the pdd22io_part3 branch into trunk for r34353 to r34623.
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST (original)
+++ trunk/MANIFEST Tue Dec 30 00:09:18 2008
@@ -1,7 +1,7 @@
# ex: set ro:
# $Id$
#
-# generated by tools/dev/mk_manifest_and_skip.pl Mon Dec 29 08:34:09 2008 UT
+# generated by tools/dev/mk_manifest_and_skip.pl Tue Dec 30 06:32:01 2008 UT
#
# See tools/dev/install_files.pl for documentation on the
# format of this file.
@@ -2967,10 +2967,10 @@
src/interp_guts.h []
src/interpreter.c []
src/intlist.c []
-src/io.c []
src/io/api.c []
src/io/buffer.c []
src/io/core.c []
+src/io/filehandle.c []
src/io/io_private.h []
src/io/io_string.c []
src/io/portable.c []
@@ -3149,6 +3149,7 @@
src/pmc/stmref.pmc []
src/pmc/stmvar.pmc []
src/pmc/string.pmc []
+src/pmc/stringhandle.pmc []
src/pmc/sub.pmc []
src/pmc/task.pmc []
src/pmc/timer.pmc []
@@ -3579,6 +3580,7 @@
t/pmc/stmref.t []
t/pmc/stmvar.t []
t/pmc/string.t []
+t/pmc/stringhandle.t []
t/pmc/sub.t []
t/pmc/sys.t []
t/pmc/task.t []
Modified: trunk/config/gen/makefiles/root.in
==============================================================================
--- trunk/config/gen/makefiles/root.in (original)
+++ trunk/config/gen/makefiles/root.in Tue Dec 30 00:09:18 2008
@@ -360,7 +360,7 @@
$(IO_DIR)/unix$(O) \
$(IO_DIR)/win32$(O) \
$(IO_DIR)/portable$(O) \
- $(SRC_DIR)/io$(O)
+ $(IO_DIR)/filehandle$(O)
INTERP_O_FILES = \
$(SRC_DIR)/string$(O) \
@@ -1197,7 +1197,7 @@
$(SRC_DIR)/pbc_merge$(O) : $(SRC_DIR)/pbc_merge.c $(GENERAL_H_FILES)
-$(SRC_DIR)/io$(O) : $(SRC_DIR)/pmc/pmc_filehandle.h $(SRC_DIR)/io/io_private.h
+$(IO_DIR)/filehandle$(O) : $(SRC_DIR)/pmc/pmc_filehandle.h
$(SRC_DIR)/io/io_private.h
$(OPS_DIR)/core_ops$(O) : $(GENERAL_H_FILES) $(OPS_DIR)/core_ops.c
Modified: trunk/include/parrot/io.h
==============================================================================
--- trunk/include/parrot/io.h (original)
+++ trunk/include/parrot/io.h Tue Dec 30 00:09:18 2008
@@ -217,7 +217,7 @@
PMC * Parrot_io_open(PARROT_INTERP,
ARGIN_NULLOK(PMC *pmc),
ARGIN(STRING *path),
- ARGIN_NULLOK(STRING *mode_str))
+ ARGIN_NULLOK(STRING *mode))
__attribute__nonnull__(1)
__attribute__nonnull__(3);
@@ -253,19 +253,16 @@
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
-INTVAL Parrot_io_read(PARROT_INTERP,
- ARGMOD(PMC *pmc),
- ARGIN(char *buffer),
- size_t len)
+PARROT_CANNOT_RETURN_NULL
+STRING * Parrot_io_readline(PARROT_INTERP, ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
- __attribute__nonnull__(3)
FUNC_MODIFIES(*pmc);
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
-STRING * Parrot_io_reads(PARROT_INTERP, ARGMOD(PMC *pmc), size_t len)
+STRING * Parrot_io_reads(PARROT_INTERP, ARGMOD(PMC *pmc), size_t length)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*pmc);
@@ -310,7 +307,7 @@
INTVAL Parrot_io_write(PARROT_INTERP,
ARGMOD(PMC *pmc),
ARGIN(const void *buffer),
- size_t len)
+ size_t length)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
@@ -424,57 +421,67 @@
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will
be lost. */
/* HEADERIZER END: src/io/utf8.c */
-/* io.c - utility functions shared between all platforms */
-/* HEADERIZER BEGIN: src/io.c */
+/* io/filehandle.c - utility functions for FileHandle PMC */
+/* HEADERIZER BEGIN: src/io/filehandle.c */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will
be lost. */
PARROT_EXPORT
+INTVAL Parrot_io_close_filehandle(PARROT_INTERP, ARGMOD(PMC *pmc))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ FUNC_MODIFIES(*pmc);
+
+PARROT_EXPORT
+void Parrot_io_flush_filehandle(PARROT_INTERP, ARGMOD(PMC *pmc))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ FUNC_MODIFIES(*pmc);
+
+PARROT_EXPORT
PARROT_CAN_RETURN_NULL
-unsigned char * Parrot_io_get_buffer_end(PARROT_INTERP,
- ARGIN_NULLOK(PMC *filehandle))
- __attribute__nonnull__(1);
+unsigned char * Parrot_io_get_buffer_end(SHIM_INTERP,
+ ARGIN_NULLOK(PMC *filehandle));
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
-unsigned char * Parrot_io_get_buffer_next(PARROT_INTERP,
+unsigned char * Parrot_io_get_buffer_next(SHIM_INTERP,
ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
-unsigned char * Parrot_io_get_buffer_start(PARROT_INTERP,
+unsigned char * Parrot_io_get_buffer_start(SHIM_INTERP,
ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_EXPORT
-PIOOFF_T Parrot_io_get_file_position(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+PIOOFF_T Parrot_io_get_file_position(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_EXPORT
-PIOOFF_T Parrot_io_get_file_size(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+PIOOFF_T Parrot_io_get_file_size(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_EXPORT
-INTVAL Parrot_io_get_flags(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+INTVAL Parrot_io_get_flags(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_EXPORT
-PIOOFF_T Parrot_io_get_last_file_position(PARROT_INTERP,
+PIOOFF_T Parrot_io_get_last_file_position(SHIM_INTERP,
ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_EXPORT
-PIOHANDLE Parrot_io_get_os_handle(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+PIOHANDLE Parrot_io_get_os_handle(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_EXPORT
+INTVAL Parrot_io_is_closed_filehandle(PARROT_INTERP, ARGMOD(PMC *pmc))
+ __attribute__nonnull__(1)
+ __attribute__nonnull__(2)
+ FUNC_MODIFIES(*pmc);
+
+PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL Parrot_io_is_encoding(PARROT_INTERP,
ARGIN(PMC *filehandle),
@@ -490,46 +497,37 @@
__attribute__nonnull__(1);
PARROT_EXPORT
-void Parrot_io_set_file_position(PARROT_INTERP,
+void Parrot_io_set_file_position(SHIM_INTERP,
ARGIN(PMC *filehandle),
PIOOFF_T file_pos)
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_EXPORT
-void Parrot_io_set_file_size(PARROT_INTERP,
+void Parrot_io_set_file_size(SHIM_INTERP,
ARGIN(PMC *filehandle),
PIOOFF_T file_size)
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_EXPORT
-void Parrot_io_set_flags(PARROT_INTERP,
- ARGIN(PMC *filehandle),
- INTVAL flags)
- __attribute__nonnull__(1)
+void Parrot_io_set_flags(SHIM_INTERP, ARGIN(PMC *filehandle), INTVAL flags)
__attribute__nonnull__(2);
PARROT_EXPORT
-void Parrot_io_set_os_handle(PARROT_INTERP,
+void Parrot_io_set_os_handle(SHIM_INTERP,
ARGIN(PMC *filehandle),
PIOHANDLE file_descriptor)
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_CAN_RETURN_NULL
-void Parrot_io_clear_buffer(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+void Parrot_io_clear_buffer(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_CAN_RETURN_NULL
-INTVAL Parrot_io_get_buffer_flags(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+INTVAL Parrot_io_get_buffer_flags(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_CAN_RETURN_NULL
-size_t Parrot_io_get_buffer_size(PARROT_INTERP, ARGIN(PMC *filehandle))
- __attribute__nonnull__(1)
+size_t Parrot_io_get_buffer_size(SHIM_INTERP, ARGIN(PMC *filehandle))
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
@@ -541,38 +539,33 @@
__attribute__nonnull__(2)
FUNC_MODIFIES(*buf);
-void Parrot_io_set_buffer_end(PARROT_INTERP,
+void Parrot_io_set_buffer_end(SHIM_INTERP,
ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_end))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
-void Parrot_io_set_buffer_flags(PARROT_INTERP,
+void Parrot_io_set_buffer_flags(SHIM_INTERP,
ARGIN(PMC *filehandle),
INTVAL new_flags)
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
-void Parrot_io_set_buffer_next(PARROT_INTERP,
+void Parrot_io_set_buffer_next(SHIM_INTERP,
ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_next))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
-void Parrot_io_set_buffer_size(PARROT_INTERP,
+void Parrot_io_set_buffer_size(SHIM_INTERP,
ARGIN(PMC *filehandle),
size_t new_size)
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
-void Parrot_io_set_buffer_start(PARROT_INTERP,
+void Parrot_io_set_buffer_start(SHIM_INTERP,
ARGIN(PMC *filehandle),
ARGIN_NULLOK(unsigned char *new_start))
- __attribute__nonnull__(1)
__attribute__nonnull__(2);
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will
be lost. */
-/* HEADERIZER END: src/io.c */
+/* HEADERIZER END: src/io/filehandle.c */
/* Put platform specific macros here if you must */
#ifdef PIO_OS_WIN32
Modified: trunk/src/io/api.c
==============================================================================
--- trunk/src/io/api.c (original)
+++ trunk/src/io/api.c Tue Dec 30 00:09:18 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;
}
@@ -139,7 +140,7 @@
=item C<INTVAL Parrot_io_close>
-Flushes, closes, and destroys the C<ParrotIO> PMC C<*pmc>.
+Closes the filehandle object.
=cut
@@ -149,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;
}
/*
@@ -174,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;
}
/*
@@ -191,14 +197,10 @@
void
Parrot_io_flush(PARROT_INTERP, ARGMOD(PMC *pmc))
{
- INTVAL ignored;
-
- if (Parrot_io_is_closed(interp, pmc))
+ if (PMC_IS_NULL(pmc))
return;
- Parrot_io_flush_buffer(interp, pmc);
- ignored = PIO_FLUSH(interp, pmc);
- UNUSED(ignored);
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "flush"), "->");
}
/*
@@ -216,63 +218,35 @@
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING *
-Parrot_io_reads(PARROT_INTERP, ARGMOD(PMC *pmc), size_t len)
+Parrot_io_reads(PARROT_INTERP, ARGMOD(PMC *pmc), size_t length)
{
- STRING *res;
- INTVAL ignored;
-
- if (Parrot_io_is_closed(interp, pmc))
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
- "Cannot read from a closed filehandle");
-
- if (Parrot_io_get_buffer_flags(interp, pmc) & PIO_BF_MMAP) {
- res = new_string_header(interp, 0);
- res->charset = Parrot_iso_8859_1_charset_ptr; /* XXX binary */
- res->encoding = Parrot_fixed_8_encoding_ptr;
- }
- else {
- res = NULL;
- res = Parrot_io_make_string(interp, &res, len);
- }
-
- res->bufused = len;
-
- if (Parrot_io_is_encoding(interp, pmc, CONST_STRING(interp, "utf8")))
- ignored = Parrot_io_read_utf8(interp, pmc, &res);
- else
- ignored = Parrot_io_read_buffer(interp, pmc, &res);
- UNUSED(ignored);
-
- return res;
+ STRING *result;
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "read"), "I->S",
+ length, &result);
+ return result;
}
/*
-=item C<INTVAL Parrot_io_read>
+=item C<STRING * Parrot_io_readline>
-Reads up to C<len> bytes from C<*pmc> and copies them into C<*buffer>.
+Return a new C<STRING*> holding the next line read from the file.
=cut
*/
+
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
-INTVAL
-Parrot_io_read(PARROT_INTERP, ARGMOD(PMC *pmc), ARGIN(char *buffer), size_t
len)
+PARROT_CANNOT_RETURN_NULL
+STRING *
+Parrot_io_readline(PARROT_INTERP, ARGMOD(PMC *pmc))
{
- STRING *res = new_string_header(interp, 0);
-
- if (Parrot_io_is_closed(interp, pmc))
- return -1;
-
- res->strstart = buffer;
- res->bufused = len;
-
- if (Parrot_io_is_encoding(interp, pmc, CONST_STRING(interp, "utf8")))
- return Parrot_io_read_utf8(interp, pmc, &res);
-
- return Parrot_io_read_buffer(interp, pmc, &res);
+ STRING *result;
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "readline"), "->S",
+ &result);
+ return result;
}
/*
@@ -288,28 +262,22 @@
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
INTVAL
-Parrot_io_write(PARROT_INTERP, ARGMOD(PMC *pmc), ARGIN(const void *buffer),
size_t len)
+Parrot_io_write(PARROT_INTERP, ARGMOD(PMC *pmc), ARGIN(const void *buffer),
size_t length)
{
DECL_CONST_CAST;
+ INTVAL result;
+ STRING fake;
- if (Parrot_io_is_closed(interp, pmc))
+ if (PMC_IS_NULL(pmc))
return -1;
- if (Parrot_io_get_flags(interp, pmc) & PIO_F_WRITE) {
- STRING fake;
- /* TODO skip utf8 translation layers if any */
- fake.strstart = (char *) PARROT_const_cast(void *, buffer);
- fake.strlen = fake.bufused = len;
- fake.charset = Parrot_default_charset_ptr;
- fake.encoding = Parrot_default_encoding_ptr;
+ fake.strstart = (char *) PARROT_const_cast(void *, buffer);
+ fake.strlen = fake.bufused = length;
+ fake.charset = Parrot_default_charset_ptr;
+ fake.encoding = Parrot_default_encoding_ptr;
- if (Parrot_io_is_encoding(interp, pmc, CONST_STRING(interp, "utf8")))
- return Parrot_io_write_utf8(interp, pmc, &fake);
-
- return Parrot_io_write_buffer(interp, pmc, &fake);
- }
- else
- return 0;
+ result = Parrot_io_putps(interp, pmc, &fake);
+ return result;
}
/*
@@ -394,13 +362,18 @@
INTVAL
Parrot_io_eof(PARROT_INTERP, ARGMOD(PMC *pmc))
{
+ INTVAL result;
+
/* io could be null here, but rather than return a negative error
* we just fake EOF since eof test is usually in a boolean context.
*/
- if (PMC_IS_NULL(pmc) || Parrot_io_is_closed(interp, pmc))
+ if (PMC_IS_NULL(pmc))
return 1;
- return (Parrot_io_get_flags(interp, pmc) & (PIO_F_EOF)) ? 1 : 0;
+ Parrot_PCCINVOKE(interp, pmc, CONST_STRING(interp, "eof"), "->I",
+ &result);
+
+ return result;
}
@@ -435,33 +408,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))
+ if (PMC_IS_NULL(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))
- Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
- "Cannot write to a filehandle not opened for write");
-
- 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
+ "Cannot write to null PMC");
- 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: trunk/src/ops/io.ops
==============================================================================
--- trunk/src/ops/io.ops (original)
+++ trunk/src/ops/io.ops Tue Dec 30 00:09:18 2008
@@ -319,30 +319,7 @@
=cut
inline op readline(out STR, invar PMC) :base_io {
- PMC * const pio = $2;
- /* this ugly error handling will go away, when all the
- * io stuff are methods
- */
- opcode_t *dest = expr NEXT();
- if (pio->vtable->base_type != enum_class_FileHandle) {
- opcode_t *handler = Parrot_ex_throw_from_op_args(interp, dest,
- EXCEPTION_PIO_ERROR,
- "Cannot read line from empty filehandle");
- goto ADDRESS(handler);
- }
- else {
- INTVAL flags = Parrot_io_get_flags(interp, pio);
- if (Parrot_io_is_closed(interp, pio)) {
- opcode_t *handler = Parrot_ex_throw_from_op_args(interp, dest,
- EXCEPTION_PIO_ERROR,
- "Cannot read line from empty filehandle");
- goto ADDRESS(handler);
- }
-
- if (!(flags & PIO_F_LINEBUF))
- Parrot_io_setlinebuf(interp, pio);
- $1 = Parrot_io_reads(interp, pio, 0);
- }
+ $1 = Parrot_io_readline(interp, $2);
}
##########################################
Modified: trunk/src/pmc/filehandle.pmc
==============================================================================
--- trunk/src/pmc/filehandle.pmc (original)
+++ trunk/src/pmc/filehandle.pmc Tue Dec 30 00:09:18 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);
+ Parrot_io_flush_filehandle(INTERP, SELF);
else
- Parrot_io_close(INTERP, SELF);
+ Parrot_io_close_filehandle(INTERP, SELF);
}
if (data_struct->buffer_start)
@@ -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;
- 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");
- 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);
}
@@ -249,8 +259,25 @@
*/
METHOD close() {
+ INTVAL status;
+ status = Parrot_io_close_filehandle(interp, SELF);
+ 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, SELF);
+ RETURN(INTVAL status);
}
/*
@@ -263,8 +290,22 @@
*/
- METHOD read(INTVAL bytes) {
- STRING *string_result = Parrot_io_reads(INTERP, SELF, bytes);
+ METHOD read(INTVAL length) {
+ STRING *string_result = NULL;
+ INTVAL ignored;
+
+ if (Parrot_io_is_closed_filehandle(interp, SELF))
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+ "Cannot read from a closed filehandle");
+
+ string_result = Parrot_io_make_string(interp, &string_result, length);
+ string_result->bufused = length;
+
+ if (Parrot_io_is_encoding(interp, SELF, CONST_STRING(interp, "utf8")))
+ ignored = Parrot_io_read_utf8(interp, SELF, &string_result);
+ else
+ ignored = Parrot_io_read_buffer(interp, SELF, &string_result);
+
RETURN(STRING *string_result);
}
@@ -373,7 +414,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");
}
@@ -387,7 +428,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");
}
@@ -420,7 +461,7 @@
*/
METHOD flush() {
- Parrot_io_flush(interp, SELF);
+ Parrot_io_flush_filehandle(interp, SELF);
}
/*
@@ -451,7 +492,31 @@
*/
METHOD puts(STRING *to_print) {
- Parrot_io_putps(interp, SELF, to_print);
+ INTVAL flags, status;
+ if (Parrot_io_is_closed_filehandle(interp, SELF))
+ 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, SELF, CONST_STRING(interp, "utf8")))
+ status = Parrot_io_write_utf8(interp, SELF, to_print);
+ else
+ status = Parrot_io_write_buffer(interp, SELF, to_print);
+
+ RETURN(INTVAL status);
}
/*
@@ -589,7 +654,12 @@
*/
METHOD eof() {
- if (Parrot_io_eof(INTERP, SELF))
+ INTVAL flags;
+ if (Parrot_io_is_closed_filehandle(interp, SELF))
+ RETURN(INTVAL 1);
+
+ GET_ATTR_flags(INTERP, SELF, flags);
+ if (flags & PIO_F_EOF)
RETURN(INTVAL 1);
RETURN(INTVAL 0);
Modified: trunk/t/pmc/filehandle.t
==============================================================================
--- trunk/t/pmc/filehandle.t (original)
+++ trunk/t/pmc/filehandle.t Tue Dec 30 00:09:18 2008
@@ -281,6 +281,9 @@
# goto read_loop
end_read_loop:
+ if counter > 1 goto read_something
+ print "not "
+ read_something:
say 'ok 1 - read 10,000 lines'
filehandle.'close'()
.end