Created
June 24, 2013 21:53
-
-
Save tony-o/5853957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
class Text::CSV { | |
has $.binary = 1; | |
has $.file_handle = Nil; | |
has $.contains_header_row = 0; | |
has $.field_separator = ','; | |
has $.line_separator = "\n"; | |
has $.field_operator = '"'; | |
has $.escape_operator = '\\'; | |
has $.chunk_size = 1024; | |
has $!fpos = 0; | |
has $!bpos = 0; | |
has $!bopn = 0; | |
method get_line () { | |
my $buffer = ''; | |
$!bpos = 0; | |
$!bopn = 0; | |
while my $line = $.file_handle.get { | |
$buffer = $buffer ~ $line; | |
$buffer = $buffer ~ "\n"; | |
#say '!last' if $.detect_end_line( $buffer ) != 1; | |
#say 'debug: ' ~ $!bpos ~ ' :: ' ~ $buffer; | |
last if $.detect_end_line( $buffer ) == 1; | |
} | |
$.file_handle.seek($!bpos, 0) if not $.file_handle.eof; | |
$buffer = $buffer.substr(0, $!bpos); | |
return $buffer; | |
# return $.parse( $line ); | |
}; | |
method parse ( $line ) { | |
return 'whatevs'; | |
}; | |
method detect_end_line ( $buffer ) { | |
my $localbuff = ''; | |
while $!bpos < $buffer.chars { | |
if ( $buffer.substr($!bpos, $.field_operator.chars) eq $.field_operator && | |
$localbuff ne $.escape_operator ) { | |
$!bopn = $!bopn == 1 ?? 0 !! 1; | |
} | |
#detect line separator | |
if ( $buffer.substr($!bpos, $.line_separator.chars) eq $.line_separator && | |
$localbuff ne $.escape_operator && | |
$!bopn == 0 ) { | |
$!bpos++; | |
return 1; | |
} | |
$localbuff = ($localbuff.chars >= $.escape_operator.chars ?? $localbuff.substr(1) !! $localbuff) ~ $buffer.substr($!bpos, 1); | |
$!bpos++; | |
} | |
return 0; | |
}; | |
}; | |
my $fh = open 'in1.csv', :r; | |
my $fdom = Text::CSV.new( file_handle => $fh ); | |
print '1: ' ~ $fdom.get_line(); | |
print '2: ' ~ $fdom.get_line(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment