| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # string::csv Brik | ||
| 5 | # | ||
| 6 | package Metabrik::String::Csv; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | first_line_is_header => [ qw(0|1) ], | ||
| 20 | separator => [ qw(character) ], | ||
| 21 | header => [ qw($column_header_list) ], | ||
| 22 | encoding => [ qw(utf8|ascii) ], | ||
| 23 | escape => [ qw(character) ], | ||
| 24 | quote => [ qw(character) ], | ||
| 25 | }, | ||
| 26 | attributes_default => { | ||
| 27 | first_line_is_header => 0, | ||
| 28 | header => [ ], | ||
| 29 | separator => ';', | ||
| 30 | encoding => 'utf8', | ||
| 31 | escape => '"', | ||
| 32 | quote => '"', | ||
| 33 | }, | ||
| 34 | commands => { | ||
| 35 | encode => [ qw($data) ], | ||
| 36 | decode => [ qw($data) ], | ||
| 37 | }, | ||
| 38 | require_modules => { | ||
| 39 | 'IO::Scalar' => [ ], | ||
| 40 | 'Text::CSV_XS' => [ ], | ||
| 41 | }, | ||
| 42 | }; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub encode { | ||
| 46 | my $self = shift; | ||
| 47 | my ($data) = @_; | ||
| 48 | |||
| 49 | $self->brik_help_run_undef_arg('encode', $data) or return; | ||
| 50 | # We only handle array of hashes format (aoh) for writing | ||
| 51 | $self->brik_help_run_invalid_arg('encode', $data, 'ARRAY') or return; | ||
| 52 | $self->brik_help_run_empty_array_arg('encode', $data, 'ARRAY') or return; | ||
| 53 | |||
| 54 | if (ref($data->[0]) ne 'HASH') { | ||
| 55 | return $self->log->error("encode: csv structure does not contain HASHes"); | ||
| 56 | } | ||
| 57 | |||
| 58 | my $output = ''; | ||
| 59 | my $fd = IO::Scalar->new(\$output); | ||
| 60 | |||
| 61 | my $header_written = 0; | ||
| 62 | my %order = (); | ||
| 63 | for my $this (@$data) { | ||
| 64 | if (! $header_written) { | ||
| 65 | my $idx = 0; | ||
| 66 | for my $k (sort { $a cmp $b } keys %$this) { | ||
| 67 | $order{$k} = $idx; | ||
| 68 | $idx++; | ||
| 69 | } | ||
| 70 | my @header = sort { $a cmp $b } keys %$this; | ||
| 71 | my $string = join($self->separator, @header)."\n"; | ||
| 72 | print $fd $string; | ||
| 73 | $header_written++; | ||
| 74 | } | ||
| 75 | |||
| 76 | my @fields = (); | ||
| 77 | for my $key (sort { $a cmp $b } keys %$this) { | ||
| 78 | $fields[$order{$key}] = $this->{$key}; | ||
| 79 | } | ||
| 80 | |||
| 81 | for (@fields) { | ||
| 82 | if (! defined($_)) { | ||
| 83 | $_ = ''; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | my $string = join($self->separator, @fields)."\n"; | ||
| 88 | print $fd $string; | ||
| 89 | } | ||
| 90 | |||
| 91 | $fd->close; | ||
| 92 | |||
| 93 | if (! length($output)) { | ||
| 94 | return $self->log->error("encode: nothing to encode"); | ||
| 95 | } | ||
| 96 | |||
| 97 | return $output; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub decode { | ||
| 101 | my $self = shift; | ||
| 102 | my ($data) = @_; | ||
| 103 | |||
| 104 | $self->brik_help_run_undef_arg('decode', $data) or return; | ||
| 105 | |||
| 106 | my $csv = Text::CSV_XS->new({ | ||
| 107 | binary => 1, | ||
| 108 | sep_char => $self->separator, | ||
| 109 | allow_loose_quotes => 1, | ||
| 110 | allow_loose_escapes => 1, | ||
| 111 | escape_char => $self->escape, | ||
| 112 | quote_char => $self->quote, | ||
| 113 | }) or return $self->log->error("decode: Text::CSV_XS new failed"); | ||
| 114 | |||
| 115 | my $fd = IO::Scalar->new(\$data); | ||
| 116 | |||
| 117 | my $sep = $self->separator; | ||
| 118 | my $headers; | ||
| 119 | my $count; | ||
| 120 | my $first_line = 1; | ||
| 121 | my @rows = (); | ||
| 122 | while (my $row = $csv->getline($fd)) { | ||
| 123 | if ($self->first_line_is_header) { | ||
| 124 | if ($first_line) { # This is first line | ||
| 125 | $headers = $row; | ||
| 126 | $count = scalar @$row - 1; | ||
| 127 | $first_line = 0; | ||
| 128 | $self->header($headers); | ||
| 129 | next; | ||
| 130 | } | ||
| 131 | |||
| 132 | my $h; | ||
| 133 | for (0..$count) { | ||
| 134 | $h->{$headers->[$_]} = $row->[$_]; | ||
| 135 | } | ||
| 136 | push @rows, $h; | ||
| 137 | } | ||
| 138 | else { | ||
| 139 | push @rows, $row; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | if (! $csv->eof) { | ||
| 144 | my $error_str = "".$csv->error_diag(); | ||
| 145 | $self->log->error("decode: incomplete: error [$error_str]"); | ||
| 146 | return \@rows; | ||
| 147 | } | ||
| 148 | |||
| 149 | return \@rows; | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | __END__ | ||