#!/usr/bin/perl -w
#
# __copy1__
# __copy2__
#
my $CMD		= "csv2pipe";
my $VERSION	= "1.0";

my @tmp;
my $FSep	= ',';				# field separator
my $TSep	= '"';				# text fields separator
my $NSep	= '|';				# new separator

my $EscapedFSep	= 'XX-ESCAPED-F-SEP-XX';	# escaped field sep
my $EscapedTSep	= 'XX-ESCAPED-T-SEP-XX';	# escaped text sep
my $EscapedNSep	= ' ';				# escaped new field sep (NOTE: WIPED OUT)

# input in CSV format:
#
# - fields are separated by commas (',')
# - text fields are surrounded by '"' if contains commas
# - double "" is an escaped "
#
my $buf;
my $idx;

while (<>) {
	chomp();

	# -- fast escapes detection

	$buf	= $_;

	# 1. reduces empty fields
	#
	$buf	=~ s/,$TSep$TSep,/,,/g;
	$buf	=~ s/,$TSep$TSep,/,,/g;		# herm ... why I need to do this twice?
	$buf	=~ s/^$TSep$TSep,/,/;
	$buf	=~ s/,$TSep$TSep$/,/;

	# 2. escape escaped text separators and new field separators, if any
	#
	$buf	=~ s/$TSep$TSep/$EscapedTSep/g;
	$buf	=~ s/\|/$EscapedNSep/g;			# NOTE: can't use $NSep var, here

	# 3. split using text separators, even elements are escaped text fields
	#
	@tmp = split( /$TSep/, $buf );

	# 4. escape field separator in text fields (even elements)
	#
	for ($idx=1; $idx<scalar(@tmp); $idx+=2 ) {
		$tmp[$idx]	=~ s/$FSep/$EscapedFSep/g;
	}

	# 5. rebuild input buffer from temporary array; we shrink the array
	#    wiping out the original text fields separators
	#
	$buf	= join( '', @tmp );

	# -- after we have escaped all dangerous chars, we can split the buffer
	#    using the original field separator
	#
	@tmp	= split( /$FSep/, $buf );

	# unescape single fields
	#
	for ($idx=0; $idx<scalar(@tmp); $idx++) {
		$tmp[$idx]	=~ s/^ +//;
		$tmp[$idx]	=~ s/ +$//;
		$tmp[$idx]	=~ s/$EscapedFSep/$FSep/g;
		$tmp[$idx]	=~ s/$EscapedTSep/$TSep/g;
	}

	# finally, rejoin fields using new separator
	#
	$buf	= join( $NSep, @tmp );

	print( $buf, "\n" );
}

exit( 0 );
