Skip to content

Commit

Permalink
Four more methods from IO moved to the setting, including lines (whic…
Browse files Browse the repository at this point in the history
…h is now imported from the setting, by other recent changes, thus why it is safe to mvoe without breaking any tests).
  • Loading branch information
jnthn committed Mar 22, 2009
1 parent f6e7fcb commit 34d36f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 102 deletions.
79 changes: 0 additions & 79 deletions src/classes/IO.pir
Expand Up @@ -28,70 +28,6 @@ This file implements the IO file handle class.

.namespace ['IO']

=item eof

Tests if we have reached the end of the file.

=cut

.namespace ['IO']
.sub 'eof' :method
.local pmc PIO
PIO = getattribute self, "$!PIO"
if PIO goto not_eof
$P0 = get_hll_global [ 'Bool' ], 'True'
.return ($P0)
not_eof:
$P0 = get_hll_global [ 'Bool' ], 'False'
.return ($P0)
.end


=item lines

our List multi method lines (IO $handle:) is export;

Returns all the lines of a file as a (lazy) List regardless of context.
See also slurp.

=cut

.namespace ['IO']
.sub 'lines' :method :multi('IO')
.local pmc pio, res, chomper
pio = getattribute self, "$!PIO"
pio = '!DEREF'(pio)
res = new 'List'
chomper = get_hll_global 'chomp'

loop:
$S0 = pio.'readline'()
unless $S0 goto done
$S0 = chomper($S0)
res.'push'($S0)
goto loop

done:
.return (res)
.end


=item printf

Parses a format string and prints formatted output according to it.

=cut

.sub 'printf' :method
.param pmc args :slurpy
.local pmc pio
pio = getattribute self, "$!PIO"
$S0 = 'sprintf'(args :flat)
print pio, $S0
.return (1)
.end


=item readline

Reads a line from the file handle.
Expand All @@ -105,21 +41,6 @@ Reads a line from the file handle.
.end


=item slurp

Slurp a file into a string.

=cut

.sub 'slurp' :method
.local pmc pio
pio = getattribute self, "$!PIO"
pio = '!DEREF'(pio)
$S0 = pio.'readall'()
.return($S0)
.end


=back

=head2 Functions
Expand Down
22 changes: 11 additions & 11 deletions src/setting/Any-str.pm
Expand Up @@ -97,6 +97,17 @@ sub split($delimiter, $target) {
$target.split($delimiter);
}
# TODO: '$filename as Str' once support for that is in place
multi sub lines(Str $filename,
:$bin = False,
:$enc = 'Unicode',
:$nl = "\n",
:$chomp = True) {

my $filehandle = open($filename, :r);
return lines($filehandle, :$bin, :$enc, :$nl, :$chomp);
}

sub unpack($template, $target) {
$template.trans(/\s+/ => '') ~~ / ((<[Ax]>)(\d+))* /
or return (); # unknown syntax
Expand All @@ -111,15 +122,4 @@ sub unpack($template, $target) {
}
}

# TODO: '$filename as Str' once support for that is in place
multi sub lines(Str $filename,
:$bin = False,
:$enc = 'Unicode',
:$nl = "\n",
:$chomp = True) {

my $filehandle = open($filename, :r);
return lines($filehandle, :$bin, :$enc, :$nl, :$chomp);
}

# vim: ft=perl6
38 changes: 26 additions & 12 deletions src/setting/IO.pm
Expand Up @@ -7,24 +7,38 @@ class IO is also {
$! ?? fail($!) !! Bool::True
}

multi method eof() is export {
return ?$!PIO.eof();
}

multi method lines() is export {
my @result = ();
while !$.eof {
push @result, $!PIO.readline().chomp()
}
return @result;
}

multi method print(*@items) is export {
try {
$!PIO.print($_) for @items;
for @items -> $item {
$!PIO.print($item);
}
}
$! ?? fail($!) !! Bool::True
return $! ?? fail($!) !! Bool::True;
}

multi method printf($format, *@args) {
return self.print(sprintf($format, |@args));
}

multi method say(*@items) is export {
my $print_res = self.print(|@items);
if $print_res {
try {
$!PIO.print("\n");
}
return $! ?? fail($!) !! Bool::True
}
else {
return $print_res;
}
@items.push("\n");
return self.print(|@items);
}

multi method slurp() is export {
return $!PIO.readall();
}

}
Expand Down

0 comments on commit 34d36f0

Please sign in to comment.