diff options
author | jeff <jeff> | 2008-11-19 14:56:00 +0000 |
---|---|---|
committer | jeff <jeff> | 2008-11-19 14:56:00 +0000 |
commit | f32fab28c5cea2b5619c9b1d5cc6e3fe7beef126 (patch) | |
tree | 0e069e7e490d9d102a5ee0379788d824326e3b83 /FS/FS/Misc.pm | |
parent | 7c9c69a6e85094310f437d01877b79b3d5c845db (diff) |
support for cch fixed format
Diffstat (limited to 'FS/FS/Misc.pm')
-rw-r--r-- | FS/FS/Misc.pm | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index e254b51fc..d79f86e0c 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -7,6 +7,7 @@ use Carp; use Data::Dumper; use IPC::Run qw( run timeout ); # for _pslatex use IPC::Run3; # for do_print... should just use IPC::Run i guess +use File::Temp; #do NOT depend on any FS:: modules here, causes weird (sometimes unreproducable #until on client machine) dependancy loops. put them in FS::Misc::Something #instead @@ -16,6 +17,7 @@ use IPC::Run3; # for do_print... should just use IPC::Run i guess states_hash counties state_label card_types generate_ps generate_pdf do_print + csv_from_fixed ); $DEBUG = 0; @@ -774,6 +776,65 @@ sub do_print { } +=item csv_from_fixed, FILEREF COUNTREF, [ LENGTH_LISTREF, [ CALLBACKS_LISTREF ] ] + +Converts the filehandle referenced by FILEREF from fixed length record +lines to a CSV file according to the lengths specified in LENGTH_LISTREF. +The CALLBACKS_LISTREF refers to a correpsonding list of coderefs. Each +should return the value to be substituted in place of its single argument. + +Returns false on success or an error if one occurs. + +=cut + +sub csv_from_fixed { + my( $fhref, $countref, $lengths, $callbacks) = @_; + + eval { require Text::CSV_XS; }; + return $@ if $@; + + my $ofh = $$fhref; + my $unpacker = new Text::CSV_XS; + my $total = 0; + my $template = join('', map {$total += $_; "A$_"} @$lengths) if $lengths; + + my $dir = "%%%FREESIDE_CACHE%%%/cache.$FS::UID::datasrc"; + my $fh = new File::Temp( TEMPLATE => "CODE.csv.XXXXXXXX", + DIR => $dir, + UNLINK => 0, + ) or return "can't open temp file: $!\n" + if $template; + + while ( defined(my $line=<$ofh>) ) { + $$countref++; + if ( $template ) { + my $column = 0; + + chomp $line; + return "unexpected input at line $$countref: $line". + " -- expected $total but received ". length($line) + unless length($line) == $total; + + $unpacker->combine( map { my $i = $column++; + defined( $callbacks->[$i] ) + ? &{ $callbacks->[$i] }( $_ ) + : $_ + } unpack( $template, $line ) + ) + or return "invalid data for CSV: ". $unpacker->error_input; + + print $fh $unpacker->string(), "\n" + or return "can't write temp file: $!\n"; + } + } + + if ( $template ) { close $$fhref; $$fhref = $fh } + + seek $$fhref, 0, 0; + ''; +} + + =back =head1 BUGS |