« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/JSON.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Session/State/Cookie.pm »
| 1 | package Dwarf::Plugin::Text::CSV_XS; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Util qw/add_method write_file/; | ||
| 4 | use Encode qw/encode/; | ||
| 5 | use Text::CSV_XS; | ||
| 6 | use Unicode::Normalize; | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my ($class, $c, $conf) = @_; | ||
| 10 | $conf ||= {}; | ||
| 11 | $conf->{eol} //= "\r\n"; | ||
| 12 | $conf->{encode_charset} //= "utf-8"; | ||
| 13 | |||
| 14 | # CSV ファイルを Perl オブジェクトに読み込み | ||
| 15 | add_method($c, read_csv => sub { | ||
| 16 | my ($self, $filepath) = @_; | ||
| 17 | my $csv = Text::CSV_XS->new ({ binary => 1, eol => $conf->{eol} }); | ||
| 18 | |||
| 19 | open my $fh, "<:encoding(" . $conf->{encode_charset} . ")", $filepath or die "Couldn't open $filepath: $!"; | ||
| 20 | |||
| 21 | my @rows; | ||
| 22 | while (my $row = $csv->getline($fh)) { | ||
| 23 | push @rows, $row; | ||
| 24 | } | ||
| 25 | $csv->eof or $csv->error_diag(); | ||
| 26 | close $fh; | ||
| 27 | |||
| 28 | $csv->eol ("\r\n"); | ||
| 29 | |||
| 30 | return wantarray ? @rows : \@rows; | ||
| 31 | }); | ||
| 32 | |||
| 33 | # Perl オブジェクトを CSV ファイルに書き込み | ||
| 34 | add_method($c, write_csv => sub { | ||
| 35 | my ($self, $filepath, @rows) = @_; | ||
| 36 | my $content = $self->encode_csv(@rows); | ||
| 37 | write_file($filepath, $content); | ||
| 38 | }); | ||
| 39 | |||
| 40 | # CSV 文字列 ---> Perl オブジェクト | ||
| 41 | add_method($c, decode_csv => sub { | ||
| 42 | my ($self, $str) = @_; | ||
| 43 | my $csv = Text::CSV_XS->new ({ binary => 1 }); | ||
| 44 | |||
| 45 | my @rows = split(/(?:\r\n|\r|\n)/, $str); | ||
| 46 | for my $row (@rows) { | ||
| 47 | if ($csv->parse($row)) { | ||
| 48 | $row = [ $csv->fields ]; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return wantarray ? @rows : \@rows; | ||
| 53 | }); | ||
| 54 | |||
| 55 | # Perl オブジェクト ---> CSV 文字列 | ||
| 56 | add_method($c, encode_csv => sub { | ||
| 57 | my ($self, @rows) = @_; | ||
| 58 | my $csv = Text::CSV_XS->new ({ binary => 1, eol => $conf->{eol}, always_quote => 1 }); | ||
| 59 | |||
| 60 | my $content = ''; | ||
| 61 | for my $row (@rows) { | ||
| 62 | if ($csv->combine(@$row)) { | ||
| 63 | $content .= $csv->string; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | $content = NFKC($content); | ||
| 68 | $content = encode($conf->{encode_charset}, $content); | ||
| 69 | return $content; | ||
| 70 | }); | ||
| 71 | |||
| 72 | $c->add_trigger(AFTER_DISPATCH => sub { | ||
| 73 | my ($self, $res) = @_; | ||
| 74 | return unless ref $res->body eq 'ARRAY'; | ||
| 75 | |||
| 76 | if ($res->content_type =~ /text\/csv/) { | ||
| 77 | $self->call_trigger(BEFORE_RENDER => $self->handler, $self, $res->body); | ||
| 78 | my $encoded = $self->encode_csv(@{ $res->body }); | ||
| 79 | $self->call_trigger(AFTER_RENDER => $self->handler, $self, \$encoded); | ||
| 80 | $res->body(encode_utf8($encoded)); | ||
| 81 | } | ||
| 82 | }); | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |