Skip to content

Commit

Permalink
Merge branch 'gci_read_socket'
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Dec 1, 2010
2 parents 6e99560 + 4a2f0c7 commit 157ac28
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 69 deletions.
9 changes: 8 additions & 1 deletion src/io/api.c
Expand Up @@ -381,7 +381,7 @@ Parrot_io_flush(PARROT_INTERP, ARGMOD_NULLOK(PMC *pmc))
=item C<STRING * Parrot_io_reads(PARROT_INTERP, PMC *pmc, size_t length)>
Return a new C<STRING*> holding up to C<len> bytes read from the filehandle
Return a new C<STRING*> holding up to C<len> bytes read from the handle
PMC. Calls the C<read> method on the filehandle PMC.
=cut
Expand Down Expand Up @@ -452,6 +452,9 @@ Parrot_io_reads(PARROT_INTERP, ARGMOD(PMC *pmc), size_t length)
SETATTR_StringHandle_read_offset(interp, pmc, offset + read_length);
}
}
else if (pmc->vtable->base_type == enum_class_Socket) {
INTVAL read = Parrot_io_recv(interp, pmc, &result);
}
else
Parrot_pcc_invoke_method_from_c_args(interp, pmc, CONST_STRING(interp, "read"), "I->S", length, &result);
return result;
Expand Down Expand Up @@ -509,6 +512,10 @@ Parrot_io_readline(PARROT_INTERP, ARGMOD(PMC *pmc))
result = STRING_substr(interp, result, offset, read_length);
SETATTR_StringHandle_read_offset(interp, pmc, newline_pos + 1);
}
else if (pmc->vtable->base_type == enum_class_Socket) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Parrot_io_readline on Socket not implemented");
}
else
Parrot_pcc_invoke_method_from_c_args(interp, pmc, CONST_STRING(interp, "readline"), "->S", &result);
return result;
Expand Down
33 changes: 0 additions & 33 deletions src/pmc/filehandle.pmc
Expand Up @@ -308,39 +308,6 @@ Test if the filehandle is closed.
}


/*

=item C<METHOD read(INTVAL bytes)>

Read the given number of bytes from the filehandle and return them in a string.

=cut

*/

METHOD read(INTVAL length) {
STRING * const string_result = Parrot_io_reads(INTERP, SELF, length);

RETURN(STRING *string_result);
}


/*

=item C<METHOD readline()>

Read a line from the filehandle and return it in a string.

=cut

*/

METHOD readline() {
STRING * const string_result = Parrot_io_readline(INTERP, SELF);
RETURN(STRING *string_result);
}


/*

=item C<METHOD readline_interactive(STRING *prompt)>
Expand Down
32 changes: 32 additions & 0 deletions src/pmc/handle.pmc
Expand Up @@ -91,6 +91,38 @@ platforms that use integer file descriptors).
}


/*

=item C<METHOD read(INTVAL bytes)>

Read the given number of bytes from the handle and return them in a string.

=cut

*/

METHOD read(INTVAL length) {
STRING * const string_result = Parrot_io_reads(INTERP, SELF, length);
RETURN(STRING *string_result);
}


/*

=item C<METHOD readline()>

Read a line from the handle and return it in a string.

=cut

*/

METHOD readline() {
STRING * const string_result = Parrot_io_readline(INTERP, SELF);
RETURN(STRING *string_result);
}


/*

=item C<METHOD close()>
Expand Down
2 changes: 1 addition & 1 deletion src/pmc/socket.pmc
Expand Up @@ -214,7 +214,7 @@ string containing the received message.

METHOD recv() {
STRING * result;
INTVAL read = Parrot_io_recv(INTERP, SELF, &result);
result = Parrot_io_reads(INTERP, SELF, 0);
RETURN(STRING * result);
}

Expand Down
31 changes: 0 additions & 31 deletions src/pmc/stringhandle.pmc
Expand Up @@ -232,37 +232,6 @@ Check if the StringHandle is open.
RETURN(INTVAL status);
}

/*

=item C<METHOD read(INTVAL bytes)>

Read the entire contents of the stringhandle and return it in a string. The
C<bytes> argument is currently ignored.

=cut

*/

METHOD read(INTVAL length) {
STRING * const string_result = Parrot_io_reads(INTERP, SELF, length);
RETURN(STRING *string_result);
}

/*

=item C<METHOD readline()>

Read a line from the stringhandle and return it in a string. (Currently only
responds to "\n" newlines.)

=cut

*/

METHOD readline() {
STRING * const string_result = Parrot_io_readline(INTERP, SELF);
RETURN(STRING *string_result);
}

/*

Expand Down
27 changes: 24 additions & 3 deletions t/pmc/socket.t
Expand Up @@ -19,9 +19,12 @@ Tests the Socket PMC.
.sub main :main
.include 'test_more.pir'

plan(16)
plan(18)

test_init()
test_get_fd()
test_read()
test_readline()
test_clone()
test_bool()
test_close()
Expand All @@ -40,11 +43,29 @@ Tests the Socket PMC.
new $P0, ['Socket']
ok(1, 'Instantiated a Socket PMC')

$S0 = typeof $P0
is($S0, 'Socket', 'PMC has correct type')
.end

.sub test_get_fd
new $P0, ['Socket']
$N0 = $P0.'get_fd'()
isnt($N0, -1, 'Socket get_fd did not return -1')
.end

$S0 = typeof $P0
is($S0, 'Socket', 'PMC has correct type')
.sub test_read
new $P0, ['Socket']
$N0 = $P0.'read'(5)
is($N0, 0, 'Socket read returns 0 when not connected')
.end

.sub test_readline
throws_substring(<<'CODE', 'not implemented', 'Socket readline is not implemented')
.sub main
new $P0, ['Socket']
$P0.'readline'()
.end
CODE
.end

.sub test_bool
Expand Down

0 comments on commit 157ac28

Please sign in to comment.