File Coverage

lib/App/MtAws/Glacier/Inventory/CSV.pm
Criterion Covered Total %
statement 46 47 97.8
branch 7 8 87.5
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 61 64 95.3


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::Glacier::Inventory::CSV;
22              
23             our $VERSION = '1.114_2';
24              
25 3     3   20374 use strict;
  3         5  
  3         70  
26 3     3   9 use warnings;
  3         4  
  3         55  
27 3     3   9 use utf8;
  3         4  
  3         14  
28              
29 3     3   44 use Carp;
  3         5  
  3         111  
30              
31 3     3   430 use App::MtAws::Glacier::Inventory ();
  3         5  
  3         56  
32 3     3   11 use base q{App::MtAws::Glacier::Inventory};
  3         3  
  3         1276  
33              
34             sub new
35             {
36 805     805 0 1260044 my $class = shift;
37 805         1759 my $self = { rawdata => \$_[0] };
38 805         1217 bless $self, $class;
39 805         2566 $self;
40             }
41              
42             sub _parse
43             {
44 805     805   750 my ($self) = @_;
45              
46             # Text::CSV with below options does not seem to work for our case
47             # ( { binary => 1 , allow_whitespace => 1, quote_char => '"', allow_loose_quotes => 1, escape_char => "\\", auto_diag=>1} )
48             # because Amazon CSV is buggy https://forums.aws.amazon.com/thread.jspa?threadID=141807&tstart=0
49              
50 805         719 my $re = undef;
51 805         637 my @fields;
52             my @records;
53 805         751 while (${$self->{rawdata}} =~ /^(.*?)\r?$/gsm) {
  2433         12770  
54 1629         3095 my $line = $1;
55 1629 100       2901 if(!defined $re) {
56 805         1965 @fields = split /,/, $line;
57 805         1560 for (@fields) {
58 1937         1546 s/^\"//;
59 1937         2259 s/\"$//;
60             }
61             my $re_s .= join(',', map {
62 805         1194 qr{
  1937         5088  
63             (
64             ([^\\\"\,]*?)|
65             (?:\"(
66             (?:\\\"|\\|.)*)
67             \")
68             )
69             }x;
70              
71             } 1..@fields);
72 805         10063 $re = qr/^$re_s$/;
73              
74             } else {
75 824 100       15928 my @x = $line =~ /$re/xm or confess "Bad CSV line [$line]";;
76 823         1099 my %data;
77             @data{@fields} = map {
78 823 100       1482 if (defined $x[$_*3+2]) {
  2030 50       3191  
79 1436         1443 my $s = $x[$_*3+2];
80 1436         2615 $s =~ s/\\"/"/g;
81 1436         3861 $s;
82             } elsif (defined $x[$_*3+1]) {
83 594         939 $x[$_*3+1];
84             } else {
85 0         0 confess;
86             }
87             } (0..$#fields);
88 823         2423 push @records, \%data;
89              
90             }
91             }
92 804         3492 $self->{data} = { ArchiveList => \@records };
93             }
94              
95             1;