X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FMisc.pm;h=5231350fa3507027b24559726c8526785060def1;hb=1b8a2cc3b3697f3921e26a31691acfabacc1efd6;hp=936f94a6e30dee4ab7f63025d190da36d1123702;hpb=7153190ee1bfeb6d3ad9e6da270a41a949333a7e;p=freeside.git diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index 936f94a6e..5231350fa 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; @@ -44,6 +46,8 @@ but are collected here to elimiate code duplication. Options: +=over 4 + =item from Sender address, required @@ -745,8 +749,7 @@ sub _pslatex { for ( 1, 2 ) { local($SIG{CHLD}) = sub {}; - #run( \@cmd, '>'=>'/dev/null', '2>'=>'/dev/null', timeout($timeout) ) - run( \@cmd, timeout($timeout) ) + run( \@cmd, '>'=>'/dev/null', '2>'=>'/dev/null', timeout($timeout) ) or die "pslatex $file.tex failed; see $file.log for details?\n"; } @@ -773,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 => "FILE.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