#
# This assembles multiline CSV files into records and then parses using
# Christopher Rath's (http://rath.ca/) CSV parser.
#
# This code assumes that every field is represented in every record;
# that is, it assumes that trailing blank fields are not truncated.
# MS Outlook performs such truncation; however, MS Excel does not.
#
# This code was written by Dave Stafford.
# No copyright retained.
#

use CSV;

my $prev_line=<DATA>;

while (<DATA>) {
  $i++;
  
  ## read the line, and check the number of fields the CSVsplit
  ## function returns if it is less than the expected number, the line
  ## is split, so append it and try again, keep going till it matches
  
  my $line=$_;
  my @c_vals=CSVsplit($line);
  my $this=scalar(@c_vals);                 
 
  my @vals=CSVsplit($prev_line);
  my $f=scalar @vals;
 
  if ($f==$num) {
    ## this is a full line
    process_prev(@vals);
    $prev_line=$line;
 
  } elsif ( ($f+$this) > $num ) {
    ## this means the total number fields by combining the lines is
    ## greater than the headers This should not happen, but outlook
    ## for example does not bother putting in additional delimiters if
    ## the last values in the line are null. Either crap out, or
    ## process the previous line
 
    process_prev(@vals);
    $prev_line=$line;
    # print "WARNING, $f fields found instead of $num, malformed CSV, is this Outlook?\n";
 
  } else {
    $prev_line.="\n" . $line;
  }
}
my @vals=CSVsplit($prev_line);
process_prev(@vals);
