Skip to content

Commit

Permalink
Try to read whole file in FileHandle.readall. Closes #1749
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@48710 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
bacek committed Aug 28, 2010
1 parent 46653bc commit bbeb6c3
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/pmc/filehandle.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -438,46 +438,67 @@ filehandle when finished.
*/

METHOD readall(STRING *name :optional, INTVAL got_name :opt_flag) {
STRING *result;
PMC *filehandle;
STRING *result = STRINGNULL;
size_t size = 0;

if (got_name) {
/* called as class method - open, slurp, close file */
PMC *filehandle;
STRING *encoding;
size_t size;

GET_ATTR_encoding(INTERP, SELF, encoding);

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");

filehandle = Parrot_io_open(INTERP, PMCNULL, name, NULL);
GET_ATTR_encoding(INTERP, SELF, encoding);

filehandle = Parrot_io_open(INTERP, PMCNULL, name, NULL);
PARROT_ASSERT(filehandle->vtable->base_type == enum_class_FileHandle);

SET_ATTR_encoding(INTERP, filehandle, encoding);
size = (size_t)(Parrot_stat_info_intval(INTERP, name, STAT_FILESIZE));

result = Parrot_io_reads(INTERP, filehandle, size);
Parrot_io_close(INTERP, filehandle);
}
else {
/* slurp open file */
/* Just get size of already opened file */
STRING *filename;
Parrot_runloop jump_point;

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");

/* Do line buffering on the filehandle */
if (!(PARROT_FILEHANDLE(SELF)->flags & PIO_F_LINEBUF))
Parrot_io_setlinebuf(INTERP, SELF);
GET_ATTR_filename(INTERP, SELF, filename);

/* stat_info ca throw exception. Switch to chunked loading */
if (setjmp(jump_point.resume)) {
/* caught exception */
Parrot_cx_delete_handler_local(interp,
Parrot_str_new_constant(interp, "exception"));

result = STRINGNULL;
do {
STRING * const part = Parrot_io_reads(INTERP, SELF, 0);
result = STRING_IS_NULL(result) ? part :
/* Do line buffering on the filehandle */
if (!(PARROT_FILEHANDLE(SELF)->flags & PIO_F_LINEBUF))
Parrot_io_setlinebuf(INTERP, SELF);

do {
STRING * const part = Parrot_io_reads(INTERP, SELF, 0);
result = STRING_IS_NULL(result) ? part :
Parrot_str_concat(INTERP, result, part);
} while (!Parrot_io_eof(INTERP, SELF));
} while (!Parrot_io_eof(INTERP, SELF));
}
else {
/* run normally */
Parrot_ex_add_c_handler(interp, &jump_point);

size = (size_t)(Parrot_stat_info_intval(INTERP, filename, STAT_FILESIZE));
filehandle = SELF;
}
}

if (size)
result = Parrot_io_reads(INTERP, filehandle, size);

if (got_name) {
Parrot_io_close(INTERP, filehandle);
}

RETURN(STRING *result);
Expand Down

0 comments on commit bbeb6c3

Please sign in to comment.