DS/Importer/TabStream.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 77 CC 5 SCAR 22.3
CC
5
Cov
0%
_fetch
20.0
new
6.0
getline
2.0
CRAP 9.3
1 #!perl
2
3 # ########################################################################## #
4 # Title: Tabular stream to datastream importer
5 # Creation date: 2007-03-05
6 # Author: Michael Zedeler
7 # Description: Produces a datastream from a tab separated stream (IO::Handle)
8 # Data Stream class
9 # Data importer
10 # File: $Source: /data/cvs/lib/DSlib/lib/DS/Importer/TabStream.pm,v $
11 # Repository: kronhjorten
12 # State: $State: Exp $
13 # Documentation: inline
14 # Recepient: -
15 # TODO Must store field order internally.
16 # ########################################################################## #
17
18 package DS::Importer::TabStream;
19
20 use base qw{ DS::Importer };
21
22 use strict;
23 use Carp::Assert;
24
25 our ($VERSION) = $DS::VERSION;
26 our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/;
27
28 require DS::TypeSpec;
29
30 # Important: calling this method with no typespec will make
31 # the class read a header from the stream and use it
32 # to create a typespec.
33 sub new {
34 my( $class, $fh, $target, $typespec, $field_order, $row ) = @_;
35
36 assert($fh->isa('IO::Handle'));
37
38 my @fields;
39 if( not defined( $typespec ) ) {
40 # Get header from stream to create type spec from
41
42 assert( (not defined $field_order),
43 "Can't handle specifying field order if fields derived from stream header." );
44
45 # Get first non-comment line in stream
46 my $line = getline($fh);
47 $line =~ s/[\n\r]+$//;
48 $line =~ s/(\s+|$)/\n/g;
49
50 my @fields = split /\s+/, $line;
51
52 $typespec = new DS::TypeSpec( [ @fields ] );
53
54 $field_order = [@fields];
55 }
56
57 my $self = $class->SUPER::new( $typespec, $target, $row );
58
59 $self->{field_order} = $field_order;
60
61 $self->{fh} = $fh;
62
63 return $self;
64 }
65
66 sub _fetch {
67 my($self) = @_;
68
69 my $result = undef;
70
71 unless($self->{fh}->eof()) {
72
73 my ($line) = getline($self->{fh}) =~ /^([^\n\r]+)/;
74 my (@line) = split /\t/, $line;
75
76 # Replace values that are not defined (because the field was empty or not there)
77 # with empty strings.
78 # This is a design decision: we could check whether each line has exactly the right
79 # number of fields. The problem is that Microsoft Excel truncates trailing empty fields
80 # on each line, which would trigger spurious errors.
81 for(my $i = 0; $i <= $#{$self->{field_order}}; $i++) {
82 $line[$i] = '' unless defined( $line[$i] );
83 }
84
85 @{$self->{row}}{@{$self->{field_order}}} = @line;
86
87 $result = 1;
88 }
89
90 return $result ? $self->{row} : undef;
91 }
92
93 # Functions (not methods!)
94
95 sub getline {
96 my( $fh ) = @_;
97
98 my $line;
99 do {
100 $line = $fh->getline();
101 } while( $line =~ /^#/ and not $fh->eof());
102
103 return $line;
104 }
105
106 1;