X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/dcd6ce29ba0d7318c16b08c237ec0247b59a571d..c6a27f90a8e644ed00910aeddaff38c74b513032:/SL/Helper/Csv.pm?ds=inline diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index d4a0c8883..5b629ee63 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -99,17 +99,28 @@ sub _open_file { sub _check_header { my ($self, %params) = @_; - return $self->header if $self->header; + my $header = $self->header; - my $header = $self->_csv->getline($self->_io); + if (! $header) { + $header = $self->_csv->getline($self->_io); - $self->_push_error([ - $self->_csv->error_input, - $self->_csv->error_diag, - 0, - ]) unless $header; + $self->_push_error([ + $self->_csv->error_input, + $self->_csv->error_diag, + 0, + ]) unless $header; + } + + # Special case: utf8 BOM. + # certain software (namely MS Office and notepad.exe insist on prefixing + # data with a discouraged but valid byte order mark + # if not removed, the first header field will not be recognized + if ($header && $header->[0] && $self->encoding =~ /utf-?8/i) { + $header->[0] =~ s/^\x{FEFF}//; + } - $self->header($header); + return unless $header; + return $self->header([ map { lc } @$header ]); } sub _parse_data { @@ -120,18 +131,19 @@ sub _parse_data { while (1) { my $row = $self->_csv->getline($self->_io); - last if $self->_csv->eof; if ($row) { my %hr; @hr{@{ $self->header }} = @$row; push @data, \%hr; } else { + last if $self->_csv->eof; push @errors, [ $self->_csv->error_input, $self->_csv->error_diag, $self->_io->input_line_number, ]; } + last if $self->_csv->eof; } $self->_data(\@data);