#!/usr/home/elb/ruby-1.6.6/ruby #!/usr/bin/env ruby #!/usr/local/bin/ruby # Copyright 2002 Blossom Associates West # All rights reserved. # Converts from comma separated values to tab separated values. # Leaves quoted commas undisturbed. # BUG: Gets out of step with errors in input like quote marks where they shouldn't be. # BUG: Preserves spaces after commas. # Eric Blossom class ParserState def initialize( &action ) super() @action = action end def charArrived( c ) @action.call( c ); end end # Define empty states so that we can refer to them. ourState = nil inQuoteState = nil escapeState = nil startState = ParserState.new() { |c| case c.chr when ',' STDOUT.putc( "\t" ) when '"' ourState = inQuoteState else STDOUT.putc( c ) end } inQuoteState = ParserState.new() { |c| case c.chr when '"' ourState = startState when "\n" STDOUT.putc( c ) ourState = startState # Recover from unfinished quote. when "\\" ourState = escapeState else STDOUT.putc( c ) end } escapeState = ParserState.new() { |c| case c.chr when "\\" when "\"" else STDOUT.putc( "\\" ) end STDOUT.putc( c ) ourState = inQuoteState } ourState = startState while ( c = STDIN.getc ) ourState.charArrived( c ) end