don't try to decode utf-8 strings
[webpac2] / lib / WebPAC / Input / CSV.pm
1 package WebPAC::Input::CSV;
2
3 use warnings;
4 use strict;
5
6 use WebPAC::Input;
7 use base qw/WebPAC::Common/;
8
9 use Text::CSV;
10 use Encode;
11 use Data::Dump qw/dump/;
12
13 =head1 NAME
14
15 WebPAC::Input::CSV - support for CSV Export Format
16
17 =cut
18
19 our $VERSION = '0.02';
20
21 =head1 FUNCTIONS
22
23 =head2 new
24
25 Returns new low-level input API object
26
27   my $input = new WebPAC::Input::CSV(
28         path => '/path/to/records.csv',
29   );
30
31 Options:
32
33 =over 4
34
35 =item path
36
37 path to CSV file
38
39 =back
40
41 Default encoding of input file is C<utf-8>
42
43 =cut
44
45 sub new {
46         my $class = shift;
47         my $self = {@_};
48         bless($self, $class);
49
50         my $arg = {@_};
51
52         my $log = $self->_get_logger();
53
54         open( my $fh, '<:raw', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");
55
56         my $csv = Text::CSV->new({ binary => 1 });
57
58         $self->{size} = 0;
59
60         while ( 1 ) {
61                 my $line = $csv->getline( $fh );
62                 last if $csv->eof;
63
64                 $log->logdie( "can't parse CSV file ", $csv->error_diag ) unless $line;
65
66                 my $rec;
67                 $rec->{'000'} = [ ++$self->{size} ];
68
69                 my $col = 'A';
70                 foreach my $cell ( @$line ) {
71                         my $str = eval { Encode::decode_utf8( $cell ) };
72                         if ( $@ ) {
73                                 if ( $@ =~ m/Cannot decode string with wide characters/ ) {
74                                         $str = $cell;
75                                 } else {
76                                         die "ERROR: $@ in line ",dump( $line );
77                                 }
78                         }
79                         $rec->{ $col++ } = $str;
80                 }
81
82                 push @{ $self->{_rec} }, $rec;
83
84         };
85
86         $log->debug("loaded ", $self->size, " records");
87
88         $self ? return $self : return undef;
89 }
90
91 =head2 fetch_rec
92
93 Return record with ID C<$mfn> from database
94
95   my $rec = $input->fetch_rec( $mfn, $filter_coderef );
96
97 =cut
98
99 sub fetch_rec {
100         my ( $self, $mfn, $filter_coderef ) = @_;
101
102         return $self->{_rec}->[$mfn-1];
103 }
104
105
106 =head2 size
107
108 Return number of records in database
109
110   my $size = $input->size;
111
112 =cut
113
114 sub size {
115         my $self = shift;
116         return $self->{size};
117 }
118
119 =head1 AUTHOR
120
121 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
122
123 =head1 COPYRIGHT & LICENSE
124
125 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
126
127 This program is free software; you can redistribute it and/or modify it
128 under the same terms as Perl itself.
129
130 =cut
131
132 1; # End of WebPAC::Input::CSV